blob: a7bdf6096aa49cf6187c29789a228855e0d1f673 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
from .cookies import cookiejar_from_dict, RequestsCookieJar
from .structures import CaseInsensitiveDict
from http.cookiejar import CookieJar
from typing import Union
import json
class Response:
"""object, which contains the response to an HTTP request."""
def __init__(self):
# Reference of URL the response is coming from (especially useful with redirects)
self.url = None
# Integer Code of responded HTTP Status, e.g. 404 or 200.
self.status_code = None
# String of responded HTTP Body.
self.text = None
# Case-insensitive Dictionary of Response Headers.
self.headers = CaseInsensitiveDict()
# A CookieJar of Cookies the server sent back.
self.cookies = cookiejar_from_dict({})
self._content = False
def __enter__(self):
return self
def __repr__(self):
return f"<Response [{self.status_code}]>"
def json(self, **kwargs):
"""parse response body to json (dict/list)"""
return json.loads(self.text, **kwargs)
@property
def content(self):
"""Content of the response, in bytes."""
if self._content is False:
if self._content_consumed:
raise RuntimeError("The content for this response was already consumed")
if self.status_code == 0:
self._content = None
else:
self._content = b"".join(self.iter_content(10 * 1024)) or b""
self._content_consumed = True
return self._content
def build_response(res: Union[dict, list], res_cookies: RequestsCookieJar) -> Response:
"""Builds a Response object """
response = Response()
# Add target / url
response.url = res["target"]
# Add status code
response.status_code = res["status"]
# Add headers
response_headers = {}
if res["headers"] is not None:
for header_key, header_value in res["headers"].items():
if len(header_value) == 1:
response_headers[header_key] = header_value[0]
else:
response_headers[header_key] = header_value
response.headers = response_headers
# Add cookies
response.cookies = res_cookies
# Add response body
response.text = res["body"]
# Add response content (bytes)
response._content = res["body"].encode()
return response
|