Skip to content

Commit

Permalink
fix: eshipper carrier data parsing for both rating and shipping respo…
Browse files Browse the repository at this point in the history
…nse data
  • Loading branch information
danh91 committed Aug 22, 2024
1 parent 3ef15ca commit 58f37b1
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 125 deletions.
6 changes: 3 additions & 3 deletions modules/connectors/eshipper/karrio/providers/eshipper/rate.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def _extract_details(
rate = lib.to_object(rating.QuoteType, data)
service = provider_units.ShippingService.map(str(rate.serviceId))
carrierId = provider_units.ShippingService.carrier_id(service.value_or_key)
rate_provider = provider_units.ShippingService.carrier(service.value_or_key)
rate_provider = provider_units.ShippingService.carrier(service.value_or_key).lower()
charges = [
("baseCharge", rate.baseCharge),
("fuelSurcharge", rate.fuelSurcharge),
Expand Down Expand Up @@ -56,11 +56,11 @@ def _extract_details(
if amount
],
meta=dict(
rate_provider=rate_provider,
service_name=rate.serviceName or service.name,
carrierId=carrierId,
serviceName=rate.serviceName,
carrierName=rate.carrierName,
carrierId=carrierId,
rate_provider=rate_provider,
),
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,28 @@ def _extract_details(
shipment = lib.to_object(shipping.ShippingResponseType, data)
label_type = next((_.type for _ in shipment.labelData.label), "PDF").upper()
label = lib.bundle_base64([_.data for _ in shipment.labelData.label], label_type)
invoice = lib.failsafe(lambda: shipment.customsInvoice.data)
trackingNumbers = [_.trackingNumber for _ in shipment.packages]
rate_provider = provider_units.ShippingService.carrier(shipment.quote.serviceId)

return models.ShipmentDetails(
carrier_id=settings.carrier_id,
carrier_name=settings.carrier_name,
tracking_number=shipment.trackingNumber,
shipment_identifier=shipment.order.orderId,
shipment_identifier=shipment.order.id,
label_type=label_type,
docs=models.Documents(label=label),
meta=dict(
carrier_tracking_link=shipment.trackingUrl,
service_name=shipment.carrier.serviceName,
tracking_numbers=trackingNumbers,
trackingId=shipment.order.trackingId,
orderId=shipment.order.orderId,
carrierName=shipment.carrier.carrierName,
transactionId=shipment.transactionId,
billingReference=shipment.billingReference,
docs=models.Documents(label=label, invoice=invoice),
meta=lib.to_dict(
dict(
rate_provider=rate_provider,
carrier_tracking_link=shipment.trackingUrl,
service_name=shipment.carrier.serviceName,
tracking_numbers=trackingNumbers,
orderId=shipment.order.id,
carrierName=shipment.carrier.carrierName,
transactionId=shipment.transactionId,
billingReference=shipment.billingReference,
)
),
)

Expand Down
22 changes: 11 additions & 11 deletions modules/connectors/eshipper/karrio/providers/eshipper/units.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,13 +168,13 @@ class ShippingService(lib.StrEnum):
eshipper_project44_xpo_logistics = "5000110"
eshipper_project44_xpress_global_systems = "5000109"
eshipper_project44_yrc = "5000053"
eshipper_purolator_purolator_express = "5000001"
eshipper_purolator_purolator_express_1030 = "5000003"
eshipper_purolator_purolator_express_9am = "5000002"
eshipper_purolator_purolator_expresscheque = "5000011"
eshipper_purolator_purolator_ground = "5000010"
eshipper_purolator_purolator_ground_1030 = "5000013"
eshipper_purolator_purolator_ground_9am = "5000012"
eshipper_purolator_express = "5000001"
eshipper_purolator_express_1030 = "5000003"
eshipper_purolator_express_9am = "5000002"
eshipper_purolator_expresscheque = "5000011"
eshipper_purolator_ground = "5000010"
eshipper_purolator_ground_1030 = "5000013"
eshipper_purolator_ground_9am = "5000012"
eshipper_purolator_puroletter = "5000004"
eshipper_purolator_puroletter_1030 = "5000006"
eshipper_purolator_puroletter_9am = "5000005"
Expand Down Expand Up @@ -233,15 +233,15 @@ class ShippingService(lib.StrEnum):
# fmt: on

@staticmethod
def carrier_id(service: str) -> str:
def carrier_id(service_id: str) -> str:
return next(
(_ for _, __ in CARRIER_SERVICES.items() if service in __),
(_ for _, __ in CARRIER_SERVICES.items() if str(service_id) in __),
"5000011",
)

@staticmethod
def carrier(service: str) -> str:
return CARRIER_IDS.get(ShippingService.carrier_id(service))
def carrier(service_id: str) -> str:
return CARRIER_IDS.get(ShippingService.carrier_id(service_id))


class ShippingOption(lib.Enum):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class LabelDataType:
@s(auto_attribs=True)
class OrderType:
trackingId: Optional[str] = None
orderId: Optional[str] = None
id: Optional[str] = None
message: Optional[str] = None


Expand Down
2 changes: 1 addition & 1 deletion modules/connectors/eshipper/schemas/shipping_response.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"order": {
"trackingId": "string",
"orderId": "string",
"id": "string",
"message": "string"
},
"carrier": {
Expand Down
8 changes: 4 additions & 4 deletions modules/connectors/eshipper/tests/eshipper/test_rate.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def test_parse_rate_response(self):
"serviceName": "Purolator Ground",
"service_name": "Purolator Ground",
},
"service": "eshipper_purolator_purolator_ground",
"service": "eshipper_purolator_ground",
"total_charge": 37.75,
"transit_days": 1,
},
Expand All @@ -123,7 +123,7 @@ def test_parse_rate_response(self):
"serviceName": "Purolator Express",
"service_name": "Purolator Express",
},
"service": "eshipper_purolator_purolator_express",
"service": "eshipper_purolator_express",
"total_charge": 38.44,
"transit_days": 1,
},
Expand Down Expand Up @@ -207,7 +207,7 @@ def test_parse_rate_response(self):
"serviceName": "Purolator Express 1030",
"service_name": "Purolator Express 1030",
},
"service": "eshipper_purolator_purolator_express_1030",
"service": "eshipper_purolator_express_1030",
"total_charge": 59.45,
"transit_days": 1,
},
Expand All @@ -228,7 +228,7 @@ def test_parse_rate_response(self):
"serviceName": "Purolator Express 9AM",
"service_name": "Purolator Express 9AM",
},
"service": "eshipper_purolator_purolator_express_9am",
"service": "eshipper_purolator_express_9am",
"total_charge": 91.52,
"transit_days": 1,
},
Expand Down
128 changes: 34 additions & 94 deletions modules/connectors/eshipper/tests/eshipper/test_shipment.py

Large diffs are not rendered by default.

0 comments on commit 58f37b1

Please sign in to comment.