Skip to content

Commit

Permalink
fix: sapient shipment cancellation response parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
danh91 committed Aug 27, 2024
1 parent 7fb2a4a commit 660e397
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 1 deletion.
1 change: 1 addition & 0 deletions modules/connectors/sapient/karrio/mappers/sapient/proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def cancel_shipment(self, request: lib.Serializable) -> lib.Deserializable[str]:
"Authorization": f"Bearer {self.settings.access_token}",
"user-agent": "Karrio/1.0",
},
on_ok=lambda _: '{"ok": true}',
on_error=provider_utils.parse_error_response,
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def shipment_cancel_request(
# fmt: off
{
"reason": lib.OptionEnum("Reason"),
"shipment_ids": lib.OptionEnum("ShipmentIds"),
"shipment_ids": lib.OptionEnum("ShipmentIds", list),
},
# fmt: on
),
Expand All @@ -48,6 +48,7 @@ def shipment_cancel_request(
# map data to convert karrio model to sapient specific type
request = dict(
Status="Cancel",
Reason=options.reason.state or "Order Cancelled",
ShipmentIds=lib.identity(
options.shipment_ids.state or [payload.shipment_identifier]
),
Expand Down
5 changes: 5 additions & 0 deletions modules/connectors/sapient/karrio/providers/sapient/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,11 @@ class ConnectionConfig(lib.Enum):

def parse_error_response(response):
"""Parse the error response from the SAPIENT API."""
content = lib.failsafe(lambda: lib.decode(response.read()))

if any(content or ""):
return content

return lib.to_json(
dict(Errors=[dict(ErrorCode=str(response.code), Message=response.reason)])
)
1 change: 1 addition & 0 deletions modules/connectors/sapient/tests/sapient/test_shipment.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@ def test_parse_cancel_shipment_response(self):
ShipmentCancelRequest = {
"ShipmentIds": ["fa3bb603-2687-4b38-ba18-3264208446c6"],
"Status": "Cancel",
"Reason": "Order Cancelled",
}


Expand Down

0 comments on commit 660e397

Please sign in to comment.