Skip to content

Commit

Permalink
feat: Consolidate Easyship integration with live tests
Browse files Browse the repository at this point in the history
  • Loading branch information
danh91 committed Sep 30, 2024
1 parent 388405d commit d745fbd
Show file tree
Hide file tree
Showing 16 changed files with 5,342 additions and 13,945 deletions.
48 changes: 40 additions & 8 deletions modules/connectors/easyship/karrio/mappers/easyship/proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ def get_rates(self, request: lib.Serializable) -> lib.Deserializable[str]:
data=lib.to_json(request.serialize()),
trace=self.trace_as("json"),
method="POST",
headers={"Authorization": f"Bearer {self.settings.access_token}"},
headers={
"Authorization": f"Bearer {self.settings.access_token}",
"Content-Type": "application/json",
"user-agent": "app/1.0",
},
)

return lib.Deserializable(response, lib.to_dict)
Expand All @@ -26,7 +30,11 @@ def create_shipment(self, request: lib.Serializable) -> lib.Deserializable[str]:
data=lib.to_json(request.serialize()),
trace=self.trace_as("json"),
method="POST",
headers={"Authorization": f"Bearer {self.settings.access_token}"},
headers={
"Authorization": f"Bearer {self.settings.access_token}",
"Content-Type": "application/json",
"user-agent": "app/1.0",
},
)

return lib.Deserializable(response, lib.to_dict, request.ctx)
Expand All @@ -37,7 +45,11 @@ def cancel_shipment(self, request: lib.Serializable) -> lib.Deserializable[str]:
url=f"{self.settings.server_url}/2023-01/shipments/{easyship_shipment_id}/cancel",
trace=self.trace_as("json"),
method="POST",
headers={"Authorization": f"Bearer {self.settings.access_token}"},
headers={
"Authorization": f"Bearer {self.settings.access_token}",
"Content-Type": "application/json",
"user-agent": "app/1.0",
},
)

return lib.Deserializable(response, lib.to_dict)
Expand All @@ -50,7 +62,11 @@ def get_tracking(self, request: lib.Serializable) -> lib.Deserializable[str]:
url=f"{self.settings.server_url}/2023-01/shipments/{easyship_shipment_id}",
trace=self.trace_as("json"),
method="GET",
headers={"Authorization": f"Bearer {self.settings.access_token}"},
headers={
"Authorization": f"Bearer {self.settings.access_token}",
"Content-Type": "application/json",
"user-agent": "app/1.0",
},
),
),
request.serialize(),
Expand All @@ -67,7 +83,11 @@ def schedule_pickup(self, request: lib.Serializable) -> lib.Deserializable[str]:
data=lib.to_json(request.serialize()),
trace=self.trace_as("json"),
method="POST",
headers={"Authorization": f"Bearer {self.settings.access_token}"},
headers={
"Authorization": f"Bearer {self.settings.access_token}",
"Content-Type": "application/json",
"user-agent": "app/1.0",
},
)

return lib.Deserializable(response, lib.to_dict)
Expand All @@ -86,7 +106,11 @@ def cancel_pickup(self, request: lib.Serializable) -> lib.Deserializable[str]:
url=f"{self.settings.server_url}/2023-01/pickups/{easyship_pickup_id}/cancel",
trace=self.trace_as("json"),
method="POST",
headers={"Authorization": f"Bearer {self.settings.access_token}"},
headers={
"Authorization": f"Bearer {self.settings.access_token}",
"Content-Type": "application/json",
"user-agent": "app/1.0",
},
)

return lib.Deserializable(response, lib.to_dict)
Expand All @@ -99,7 +123,11 @@ def create_manifest(self, request: lib.Serializable) -> lib.Deserializable[str]:
data=lib.to_json(request.serialize()),
trace=self.trace_as("json"),
method="POST",
headers={"Authorization": f"Bearer {self.settings.access_token}"},
headers={
"Authorization": f"Bearer {self.settings.access_token}",
"Content-Type": "application/json",
"user-agent": "app/1.0",
},
)
)

Expand All @@ -114,7 +142,11 @@ def create_manifest(self, request: lib.Serializable) -> lib.Deserializable[str]:
else lib.request(
url=manifest_url,
method="GET",
headers={"Authorization": f"Bearer {self.settings.access_token}"},
headers={
"Authorization": f"Bearer {self.settings.access_token}",
"origin": "http://localhost:5002",
"user-agent": "app/1.0",
},
decoder=lib.encode_base64,
)
)
Expand Down
36 changes: 27 additions & 9 deletions modules/connectors/easyship/karrio/providers/easyship/rate.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ def _extract_details(
settings: provider_utils.Settings,
) -> models.RateDetails:
details = lib.to_object(rating.RateType, data)
service = provider_units.ShippingService.map(details.courier_id)
courier = provider_units.ShippingCourierID.map(details.courier_id)
service = provider_units.ShippingServiceID.map(details.courier_id)
courier = provider_units.ShippingCourierID.find(service.value_or_key)

charges = [
("Shipment Charge", details.shipment_charge),
("Insurance", details.insurance_fee),
Expand All @@ -51,7 +52,7 @@ def _extract_details(
return models.RateDetails(
carrier_id=settings.carrier_id,
carrier_name=settings.carrier_name,
service=service.name_or_key,
service=service.value_or_key,
total_charge=lib.to_money(details.total_charge),
currency=details.currency,
transit_days=details.max_delivery_time,
Expand All @@ -65,10 +66,10 @@ def _extract_details(
if amount is not None
],
meta=dict(
rate_provider=courier.name,
rate_provider=courier.name_or_key,
easyship_incoterms=details.incoterms,
easyship_courier_id=details.courier_id,
service_name=service.name or details.courier_name,
service_name=service.value or details.courier_name,
available_handover_options=details.available_handover_options,
value_for_money_rank=details.value_for_money_rank,
tracking_rating=details.tracking_rating,
Expand Down Expand Up @@ -167,17 +168,34 @@ def rate_request(
"contains_battery_pi967"
),
contains_liquids=item.metadata.get("contains_liquids"),
declared_currency=item.value_currency or options.currency.state,
declared_currency=lib.identity(
item.value_currency or options.currency.state or "USD"
),
dimensions=None,
origin_country_alpha2=item.origin_country,
quantity=item.quantity,
actual_weight=item.weight,
category=item.category,
declared_customs_value=item.value_amount,
description=item.description or item.title,
sku=item.sku,
description=item.description or item.title or "N/A",
sku=item.sku or "N/A",
hs_code=item.hs_code or "N/A",
)
for item in lib.identity(
package.items
if any(package.items)
else [
models.Commodity(
title=lib.text(package.description, max=35),
description=package.description,
quantity=1,
hs_code="N/A",
value_amount=1.0,
value_currency=options.currency.state or "USD",
category="bags_luggages",
)
]
)
for item in package.items
],
total_actual_weight=package.weight.value,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ def _extract_details(
docs=models.Documents(label=label),
meta=dict(
tracking_numbers=tracking_numbers,
rate_provider=ctx["rate_provider"],
easyship_courier_id=ctx["courier_id"],
easyship_shipment_id=details.easyship_shipment_id,
easyship_courier_account_id=details.courier.id,
),
Expand All @@ -67,21 +69,24 @@ def shipment_request(
return_address = lib.to_address(payload.return_address or payload.shipper)
packages = lib.to_packages(payload.parcels, options=payload.options)
weight_unit, dimension_unit = packages.compatible_units
service = provider_units.ShippingService.map(payload.service).name_or_key
courrier_id = provider_units.ShippingServiceID.map(service).value_or_key
options = lib.to_shipping_options(
payload.options,
package_options=packages.options,
initializer=provider_units.shipping_options_initializer,
)
service = lib.identity(
options.easyship_courier_id.state
or provider_units.ShippingServiceID.map(payload.service).name_or_key
)
courier = provider_units.ShippingCourierID.find(service)
customs = lib.to_customs_info(
payload.customs,
shipper=payload.shipper,
recipient=payload.recipient,
weight_unit=weight_unit.name,
)
incoterms = lib.identity(
options.easyship_incoterms.state or customs.options.incoterm
options.easyship_incoterms.state or customs.options.incoterm.state or "DDU"
)
label_type = provider_units.LabelFormat.map(payload.label_type)

Expand Down Expand Up @@ -116,14 +121,18 @@ def shipment_request(
if options.easyship_list_unavailable_couriers.state is not None
else False
),
selected_courier_id=courrier_id,
selected_courier_id=service,
),
destination_address=easyship.AddressType(
city=recipient.city,
company_name=recipient.company_name,
contact_email=recipient.email,
company_name=recipient.company_name or "N/A",
contact_email=lib.identity(
recipient.email
or options.email_notification_to.state
or "user@mail.com"
),
contact_name=recipient.person_name,
contact_phone=recipient.phone_number,
contact_phone=recipient.phone_number or "N/A",
country_alpha2=recipient.country_code,
line_1=recipient.address_line1,
line_2=recipient.address_line2,
Expand All @@ -140,10 +149,14 @@ def shipment_request(
order_data=None,
origin_address=easyship.AddressType(
city=return_address.city,
company_name=return_address.company_name,
contact_email=return_address.email,
company_name=return_address.company_name or "N/A",
contact_email=lib.identity(
return_address.email
or options.email_notification_to.state
or "user@mail.com"
),
contact_name=return_address.person_name,
contact_phone=return_address.phone_number,
contact_phone=return_address.phone_number or "N/A",
country_alpha2=return_address.country_code,
line_1=return_address.address_line1,
line_2=return_address.address_line2,
Expand All @@ -168,10 +181,14 @@ def shipment_request(
shipment_request_return=options.is_return.state,
return_address=easyship.AddressType(
city=return_address.city,
company_name=return_address.company_name,
contact_email=return_address.email,
company_name=return_address.company_name or "N/A",
contact_email=lib.identity(
return_address.email
or options.email_notification_to.state
or "user@mail.com"
),
contact_name=return_address.person_name,
contact_phone=return_address.phone_number,
contact_phone=return_address.phone_number or "N/A",
country_alpha2=return_address.country_code,
line_1=return_address.address_line1,
line_2=return_address.address_line2,
Expand All @@ -181,10 +198,12 @@ def shipment_request(
return_address_id=options.easyship_return_address_id.state,
sender_address=easyship.AddressType(
city=shipper.city,
company_name=shipper.company_name,
contact_email=shipper.email,
company_name=shipper.company_name or "N/A",
contact_email=lib.identity(
shipper.email or options.email_notification_to.state or "user@mail.com"
),
contact_name=shipper.person_name,
contact_phone=shipper.phone_number,
contact_phone=shipper.phone_number or "N/A",
country_alpha2=shipper.country_code,
line_1=shipper.address_line1,
line_2=shipper.address_line2,
Expand Down Expand Up @@ -233,15 +252,17 @@ def shipment_request(
items=[
easyship.ItemType(
dimensions=None,
declared_currency=item.value_currency or options.currency.state,
declared_currency=lib.identity(
item.value_currency or options.currency.state or "USD"
),
origin_country_alpha2=item.origin_country,
quantity=item.quantity,
actual_weight=item.weight,
category=item.category,
category=item.category or "bags_luggages",
declared_customs_value=item.value_amount,
description=item.description or item.title,
description=item.description or item.title or "Item",
sku=item.sku,
hs_code=item.hs_code,
hs_code=item.hs_code or "N/A",
contains_liquids=item.metadata.get("contains_liquids"),
contains_battery_pi966=item.metadata.get(
"contains_battery_pi966"
Expand All @@ -250,7 +271,17 @@ def shipment_request(
"contains_battery_pi967"
),
)
for item in package.items
for item in lib.identity(
(package.items if any(package.items) else customs.commodities)
if any(package.items) or any(payload.customs or "")
else [
models.Commodity(
title=lib.text(package.description, max=35),
quantity=1,
value_amount=1.0,
)
]
)
],
total_actual_weight=package.weight.value,
)
Expand All @@ -264,7 +295,8 @@ def shipment_request(
lib.to_json(_).replace("shipment_request_return", "return")
),
ctx=dict(
courier_id=courrier_id,
courier_id=service,
rate_provider=courier.name,
label_type=label_type.name or "PDF",
),
)
Loading

0 comments on commit d745fbd

Please sign in to comment.