Skip to content

Commit

Permalink
Result object adjustments for unusual API responses
Browse files Browse the repository at this point in the history
  • Loading branch information
jshcodes committed Nov 23, 2023
1 parent 302a692 commit 267250b
Showing 1 changed file with 16 additions and 25 deletions.
41 changes: 16 additions & 25 deletions src/falconpy/_result/_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
from ._meta import Meta
from ._errors import Errors
from ._headers import Headers
from ._resources import Resources, BinaryFile, RawBody
from ._resources import Resources, BinaryFile, RawBody, ResponseComponent


class BaseResult:
Expand All @@ -66,6 +66,9 @@ def __init__(self,
self.resources = Resources([])
self.errors = Errors()
self.raw = RawBody()
# RTR Batch session init and batch responses only
self.batch_id = None
self.batch_get_cmd_req_id = None

if status_code and headers and body:
self.status_code = status_code
Expand Down Expand Up @@ -103,25 +106,28 @@ def __init__(self,
self.errors = Errors(body.get("errors", []))
# RTR Batch responses
if body.get("batch_id", {}):
# Batch session init
# Batch session init returns as a dictionary
self.raw = RawBody(body)
self.resources = Resources(body.get("resources", []))
self.batch_id = body.get("batch_id")
self.resources = ResponseComponent(body.get("resources"))
elif body.get("combined", {}):
# Batch session results have to be handled as RawBody
# due to the combined response format.
# Batch session results return as a dictionary.
self.batch_get_cmd_req_id = body.get("batch_get_cmd_req_id", None)
self.raw = RawBody(body)
self.resources = ResponseComponent(body.get("combined"))
elif body.get("data", {}): # pragma: no cover
# GraphQL uses a custom response payload, we will
# use RawBody for this return for now. Due to
# GraphQL uses a custom response payload. Due to
# environment constraints, this is manually tested.
self.raw = RawBody(body)
self.resources = ResponseComponent(body)
elif body.get("resources", None) is None:
# No resources, this must be a raw dictionary
# Probably came from the container API
self.raw = RawBody(body)
elif isinstance(body.get("resources", []), dict):
# Catch unusual response payloads not explicitly handled
self.raw = RawBody(body)
self.resources = ResponseComponent(body.get("resources"))
else:
# Standard API responses
self.resources = Resources(body.get("resources", []))
Expand Down Expand Up @@ -300,10 +306,12 @@ def powered_by(self) -> Optional[str]:

@property
def trace_id(self) -> Optional[str]:
"""Return the trace ID from the underlying Meta object."""
"""Return the trace ID from the underlying Meta or Headers object."""
_returned: Optional[str] = None
if self.meta:
_returned = self.meta.trace_id
elif self.headers:
_returned = self.headers.trace_id
return _returned

@property
Expand Down Expand Up @@ -420,23 +428,6 @@ def full_return(self) -> dict:
if "meta" in _body:
_body = dict(_body)

# try:
# # Content is malformed JSON
# # No content returned, but a valid response
# _body = dict(_body)
# except ValueError:
# _body = _body
# _body = {
# "meta": {},
# "resources": [],
# "errors": [
# {
# "message": "Invalid JSON response received",
# "code": 500
# }
# ]
# }

_returned = {
"status_code": int(self.status_code),
"headers": _headers,
Expand Down

0 comments on commit 267250b

Please sign in to comment.