This repository has been archived by the owner on Aug 30, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor response validation and improve rate limit detection
- Loading branch information
1 parent
3976bca
commit ec91f4a
Showing
3 changed files
with
52 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,46 @@ | ||
import json | ||
from ..exceptions import AuthFailed, AuthUserError | ||
from ..exceptions import RequestFailed, RateLimitedError | ||
|
||
|
||
def check_auth(response): | ||
def validate_response(session, response, auth=False): | ||
try: | ||
try: | ||
js_response = json.loads(response.text) | ||
except: | ||
response.raise_for_status() | ||
if auth: | ||
raise AuthFailed('Authentication failed') | ||
else: | ||
if auth: | ||
_check_auth(js_response) | ||
_check_status(js_response) | ||
response.raise_for_status() | ||
return js_response | ||
except: | ||
if response: | ||
session.logger.error(response.text) | ||
raise | ||
|
||
|
||
def _check_auth(response): | ||
if not response.get('user', False): | ||
raise AuthUserError('Please check your username') | ||
if not response.get('authenticated', False): | ||
raise AuthFailed('Authentication failed') | ||
|
||
|
||
def _check_status(response): | ||
message = response.get('message', '').lower() | ||
status = response.get('status', '').lower() | ||
|
||
msg = '' | ||
if status: | ||
msg = status | ||
if message: | ||
msg += f': {message}' | ||
|
||
if 'rate limit' in message: | ||
raise RateLimitedError(msg) | ||
if 'fail' in status: | ||
raise RequestFailed(msg) |