aboutsummaryrefslogtreecommitdiff
path: root/src/utils/http.py
diff options
context:
space:
mode:
authorGravatar jesopo2018-12-11 22:26:38 +0000
committerGravatar jesopo2018-12-11 22:26:38 +0000
commit793d234a0b924c0330e88f07f68b13559b42e4ac (patch)
tree90ecd3746a77e17da915f90ec057cb16ead82c05 /src/utils/http.py
parentActually use `action_desc` in github.py.issues, so that we can see what things (diff)
signature
'utils.http.get_url' -> 'utils.http.request', return a Response object from
utils.http.request
Diffstat (limited to 'src/utils/http.py')
-rw-r--r--src/utils/http.py26
1 files changed, 16 insertions, 10 deletions
diff --git a/src/utils/http.py b/src/utils/http.py
index 8856cd43..5062422d 100644
--- a/src/utils/http.py
+++ b/src/utils/http.py
@@ -18,10 +18,18 @@ class HTTPParsingException(HTTPException):
def throw_timeout():
raise HTTPTimeoutException()
-def get_url(url: str, method: str="GET", get_params: dict={},
+class Response(object):
+ def __init__(self, code: int, data: typing.Any,
+ headers: typing.Dict[str, str]):
+ self.code = code
+ self.data = data
+ self.headers = headers
+
+def request(url: str, method: str="GET", get_params: dict={},
post_data: typing.Any=None, headers: dict={},
json_data: typing.Any=None, code: bool=False, json: bool=False,
- soup: bool=False, parser: str="lxml", fallback_encoding: str="utf8"):
+ soup: bool=False, parser: str="lxml", fallback_encoding: str="utf8",
+ ) -> Response:
if not urllib.parse.urlparse(url).scheme:
url = "http://%s" % url
@@ -49,23 +57,21 @@ def get_url(url: str, method: str="GET", get_params: dict={},
finally:
signal.signal(signal.SIGALRM, signal.SIG_IGN)
+ response_headers = utils.CaseInsensitiveDict(response.headers)
+
if soup:
soup = bs4.BeautifulSoup(response_content, parser)
- if code:
- return response.status_code, soup
- return soup
+ return Response(response.status_code, soup, response_heders)
data = response_content.decode(response.encoding or fallback_encoding)
if json and data:
try:
- data = _json.loads(data)
+ return Response(response.status_code, _json.loads(data),
+ response_headers)
except _json.decoder.JSONDecodeError as e:
raise HTTPParsingException(str(e))
- if code:
- return response.status_code, data
- else:
- return data
+ return Response(response.status_code, data, response_headers)
def strip_html(s: str) -> str:
return bs4.BeautifulSoup(s, "lxml").get_text()