From c3a974a495cc1ae0ebb4a7d90cb08f8e6bb86221 Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Tue, 2 Jul 2019 15:34:23 +0200 Subject: [PATCH] Refactor _util.check_request --- fbchat/_util.py | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/fbchat/_util.py b/fbchat/_util.py index b672082..823e313 100644 --- a/fbchat/_util.py +++ b/fbchat/_util.py @@ -140,25 +140,31 @@ def check_json(j): 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( - "Error when sending request: Got {} response".format(r.status_code), - request_status_code=r.status_code, + "Error when sending request: Got {} response".format(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: raise FBchatFacebookError("Error when sending request: Got empty response") - if as_json: - content = strip_json_cruft(content) - j = parse_json(content) - check_json(j) - log.debug(j) - return j - else: - return content + +def to_json(content): + content = strip_json_cruft(content) + j = parse_json(content) + check_json(j) + log.debug(j) + return j def get_jsmods_require(j, index):