Add JSON parsing to Response class initialization#52
Add JSON parsing to Response class initialization#52HernandoR wants to merge 2 commits intovolcengine:masterfrom
Conversation
There was a problem hiding this comment.
Pull request overview
This PR updates the SDK core Response wrapper to eagerly parse the underlying HTTP response body as JSON during initialization, extracting Result and ResponseMetadata.
Changes:
- Add JSON parsing when constructing
Response. - Populate
Response.resultandResponse.metadatadirectly from parsed payload.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| @@ -1,5 +1,12 @@ | |||
| import json | |||
There was a problem hiding this comment.
PEP8: add a blank line after the import and before the class definition (top-level class definitions should be separated by two blank lines).
| import json | |
| import json |
| self.result = data.get("Result") | ||
| self.metadata = data.get("ResponseMetadata") |
There was a problem hiding this comment.
ResponseMetadata error handling is skipped here: if the JSON contains a ResponseMetadata.Error block, Response will still populate result/metadata and no ApiException is raised. To keep behavior consistent with DeserializedResponseInterceptor.deserialize (which raises on Error / missing ResponseMetadata), consider checking ResponseMetadata.Error and raising the same ApiException (or deferring JSON parsing to that interceptor).
| 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") |
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
No description provided.