Refactor _util.check_request
This commit is contained in:
@@ -140,25 +140,31 @@ def check_json(j):
|
|||||||
|
|
||||||
|
|
||||||
def check_request(r, as_json=True):
|
def check_request(r, as_json=True):
|
||||||
if not r.ok:
|
check_http_code(r.status_code)
|
||||||
|
content = get_decoded_r(r)
|
||||||
|
check_content(content)
|
||||||
|
return to_json(content) if as_json else content
|
||||||
|
|
||||||
|
|
||||||
|
def check_http_code(code):
|
||||||
|
if 400 <= code < 600:
|
||||||
raise FBchatFacebookError(
|
raise FBchatFacebookError(
|
||||||
"Error when sending request: Got {} response".format(r.status_code),
|
"Error when sending request: Got {} response".format(code),
|
||||||
request_status_code=r.status_code,
|
request_status_code=code,
|
||||||
)
|
)
|
||||||
|
|
||||||
content = get_decoded_r(r)
|
|
||||||
|
|
||||||
|
def check_content(content, as_json=True):
|
||||||
if content is None or len(content) == 0:
|
if content is None or len(content) == 0:
|
||||||
raise FBchatFacebookError("Error when sending request: Got empty response")
|
raise FBchatFacebookError("Error when sending request: Got empty response")
|
||||||
|
|
||||||
if as_json:
|
|
||||||
content = strip_json_cruft(content)
|
def to_json(content):
|
||||||
j = parse_json(content)
|
content = strip_json_cruft(content)
|
||||||
check_json(j)
|
j = parse_json(content)
|
||||||
log.debug(j)
|
check_json(j)
|
||||||
return j
|
log.debug(j)
|
||||||
else:
|
return j
|
||||||
return content
|
|
||||||
|
|
||||||
|
|
||||||
def get_jsmods_require(j, index):
|
def get_jsmods_require(j, index):
|
||||||
|
Reference in New Issue
Block a user