-
Notifications
You must be signed in to change notification settings - Fork 28
Add JSON parsing to Response class initialization #52
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,5 +1,12 @@ | ||||||||||||||||||||||
| import json | ||||||||||||||||||||||
| class Response: | ||||||||||||||||||||||
| def __init__(self, http_response): | ||||||||||||||||||||||
| self.http_response = http_response | ||||||||||||||||||||||
| self.result = None | ||||||||||||||||||||||
| self.metadata = None | ||||||||||||||||||||||
| self.__post_init__() | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| def __post_init__(self): | ||||||||||||||||||||||
| data = json.loads(self.http_response.data) | ||||||||||||||||||||||
| self.result = data.get("Result") | ||||||||||||||||||||||
| self.metadata = data.get("ResponseMetadata") | ||||||||||||||||||||||
HernandoR marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||||||||||||||
| self.result = data.get("Result") | |
| self.metadata = data.get("ResponseMetadata") | |
| metadata = data.get("ResponseMetadata") | |
| if metadata is None: | |
| raise ValueError("Missing ResponseMetadata in HTTP response.") | |
| error = metadata.get("Error") if isinstance(metadata, dict) else None | |
| if error: | |
| raise ValueError(f"API returned error in ResponseMetadata: {error}") | |
| self.metadata = metadata | |
| self.result = data.get("Result") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PEP8: add a blank line after the import and before the class definition (top-level class definitions should be separated by two blank lines).