-
-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from PurplShip/ReviewGeneralTypes
Review general types
- Loading branch information
Showing
35 changed files
with
1,327 additions
and
1,052 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
from .caps_client import CanadaPostClient | ||
from .caps_mapper import CanadaPostMapper | ||
from .caps_proxy import CanadaPostProxy | ||
from purplship.mappers.caps.caps_client import CanadaPostClient | ||
from purplship.mappers.caps.caps_mapper import CanadaPostMapper | ||
from purplship.mappers.caps.caps_proxy import CanadaPostProxy |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
from typing import Tuple, List, Union | ||
from purplship.domain.mapper import Mapper | ||
from purplship.domain import entities as E | ||
from pycaps.rating import mailing_scenario | ||
from pycaps.shipment import ShipmentType | ||
from pycaps.ncshipment import NonContractShipmentType | ||
from .partials import ( | ||
CanadaPostRateMapperPartial, | ||
CanadaPostTrackMapperPartial, | ||
CanadaPostShipmentMapperPartial | ||
) | ||
|
||
|
||
class CanadaPostMapper( | ||
Mapper, | ||
CanadaPostRateMapperPartial, | ||
CanadaPostTrackMapperPartial, | ||
CanadaPostShipmentMapperPartial | ||
): | ||
|
||
def create_quote_request(self, payload: E.shipment_request) -> mailing_scenario: | ||
return self.create_mailing_scenario(payload) | ||
|
||
def create_tracking_request(self, payload: E.tracking_request) -> List[str]: | ||
return self.create_tracking_pins(payload) | ||
|
||
def create_shipment_request(self, payload: E.shipment_request) -> Union[ShipmentType, NonContractShipmentType]: | ||
return self.create_shipment(payload) | ||
|
||
|
||
|
||
def parse_quote_response(self, response: 'XMLElement') -> Tuple[List[E.QuoteDetails], List[E.Error]]: | ||
return self.parse_price_quotes(response) | ||
|
||
def parse_tracking_response(self, response: 'XMLElement') -> Tuple[List[E.TrackingDetails], List[E.Error]]: | ||
return self.parse_tracking_summary(response) | ||
|
||
def parse_shipment_response(self, response: 'XMLElement') -> Tuple[E.ShipmentDetails, List[E.Error]]: | ||
return self.parse_shipment_info(response) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from .rate import CanadaPostMapperPartial as CanadaPostRateMapperPartial | ||
from .track import CanadaPostMapperPartial as CanadaPostTrackMapperPartial | ||
from .shipment import CanadaPostMapperPartial as CanadaPostShipmentMapperPartial |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
from typing import Tuple, List, Union | ||
from functools import reduce | ||
from purplship.mappers.caps import CanadaPostClient | ||
from purplship.domain import entities as E | ||
from pycaps.rating import mailing_scenario | ||
from pycaps.shipment import ShipmentType | ||
from pycaps.ncshipment import NonContractShipmentType | ||
from pycaps.messages import messageType | ||
|
||
|
||
class CanadaPostCapabilities: | ||
""" | ||
CanadaPost native service request types | ||
""" | ||
|
||
""" Requests """ | ||
|
||
def create_mailing_scenario(self, payload: E.shipment_request) -> mailing_scenario: | ||
pass | ||
|
||
def create_tracking_pins(self, payload: E.tracking_request) -> List[str]: | ||
pass | ||
|
||
def create_shipment(self, payload: E.shipment_request) -> Union[ShipmentType, NonContractShipmentType]: | ||
pass | ||
|
||
|
||
""" Replys """ | ||
|
||
def parse_price_quotes(self, response: 'XMLElement') -> Tuple[List[E.QuoteDetails], List[E.Error]]: | ||
pass | ||
|
||
def parse_tracking_summary(self, response: 'XMLElement') -> Tuple[List[E.TrackingDetails], List[E.Error]]: | ||
pass | ||
|
||
def parse_shipment_info(self, response: 'XMLElement') -> Tuple[E.ShipmentDetails, List[E.Error]]: | ||
pass | ||
|
||
|
||
class CanadaPostMapperBase(CanadaPostCapabilities): | ||
""" | ||
CanadaPost mapper base class | ||
""" | ||
def __init__(self, client: CanadaPostClient): | ||
self.client = client | ||
|
||
def parse_error_response(self, response: 'XMLElement') -> List[E.Error]: | ||
messages = response.xpath('.//*[local-name() = $name]', name="message") | ||
return reduce(self._extract_error, messages, []) | ||
|
||
def _extract_error(self, errors: List[E.Error], messageNode: 'XMLElement') -> List[E.Error]: | ||
message = messageType() | ||
message.build(messageNode) | ||
return errors + [ | ||
E.Error(code=message.code, | ||
message=message.description, carrier=self.client.carrier_name) | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
from pycaps.rating import * | ||
from datetime import datetime | ||
from .interface import reduce, Tuple, List, E, CanadaPostMapperBase | ||
|
||
|
||
class CanadaPostMapperPartial(CanadaPostMapperBase): | ||
|
||
def parse_price_quotes(self, response: 'XMLElement') -> Tuple[List[E.QuoteDetails], List[E.Error]]: | ||
price_quotes = response.xpath('.//*[local-name() = $name]', name="price-quote") | ||
quotes = reduce(self._extract_quote, price_quotes, []) | ||
return (quotes, self.parse_error_response(response)) | ||
|
||
def _extract_quote(self, quotes: List[E.QuoteDetails], price_quoteNode: 'XMLElement') -> List[E.QuoteDetails]: | ||
price_quote = price_quoteType() | ||
price_quote.build(price_quoteNode) | ||
discounts = [E.ChargeDetails(name=d.adjustment_name, currency="CAD", amount=float(d.adjustment_cost or 0)) for d in price_quote.price_details.adjustments.adjustment] | ||
return quotes + [ | ||
E.QuoteDetails( | ||
carrier=self.client.carrier_name, | ||
currency="CAD", | ||
delivery_date=str(price_quote.service_standard.expected_delivery_date), | ||
service_name=price_quote.service_name, | ||
service_type=price_quote.service_code, | ||
base_charge=float(price_quote.price_details.base or 0), | ||
total_charge=float(price_quote.price_details.due or 0), | ||
discount=reduce(lambda sum, d: sum + d.amount, discounts, 0), | ||
duties_and_taxes=float(price_quote.price_details.taxes.gst.valueOf_ or 0) + | ||
float(price_quote.price_details.taxes.pst.valueOf_ or 0) + | ||
float(price_quote.price_details.taxes.hst.valueOf_ or 0), | ||
extra_charges=list(map(lambda a: E.ChargeDetails( | ||
name=a.adjustment_name, currency="CAD", amount=float(a.adjustment_cost or 0)), price_quote.price_details.adjustments.adjustment) | ||
) | ||
) | ||
] | ||
|
||
|
||
def create_mailing_scenario(self, payload: E.shipment_request) -> mailing_scenario: | ||
package = payload.shipment.packages[0] | ||
|
||
if len(payload.shipment.services) > 0: | ||
services = servicesType() | ||
for code in payload.shipment.services: | ||
services.add_service_code(code) | ||
|
||
if 'options' in payload.shipment.extra: | ||
options = optionsType() | ||
for option in payload.shipment.extra.get('options'): | ||
options.add_option(optionType( | ||
option_amount=option.get('option-amount'), | ||
option_code=option.get('option-code') | ||
)) | ||
|
||
return mailing_scenario( | ||
customer_number=payload.shipper.account_number or payload.shipment.payment_account_number or self.client.customer_number, | ||
contract_id=payload.shipment.extra.get('contract-id'), | ||
promo_code=payload.shipment.extra.get('promo-code'), | ||
quote_type=payload.shipment.extra.get('quote-type'), | ||
expected_mailing_date=payload.shipment.extra.get('expected-mailing-date'), | ||
options=options if ('options' in payload.shipment.extra) else None, | ||
parcel_characteristics=parcel_characteristicsType( | ||
weight=payload.shipment.total_weight or package.weight, | ||
dimensions=dimensionsType( | ||
length=package.length, | ||
width=package.width, | ||
height=package.height | ||
), | ||
unpackaged=payload.shipment.extra.get('unpackaged'), | ||
mailing_tube=payload.shipment.extra.get('mailing-tube'), | ||
oversized=payload.shipment.extra.get('oversized') | ||
), | ||
services=services if (len(payload.shipment.services) > 0) else None, | ||
origin_postal_code=payload.shipper.postal_code, | ||
destination=destinationType( | ||
domestic=domesticType( | ||
postal_code=payload.recipient.postal_code | ||
) if (payload.recipient.country_code == 'CA') else None, | ||
united_states=united_statesType( | ||
zip_code=payload.recipient.postal_code | ||
) if (payload.recipient.country_code == 'US') else None, | ||
international=internationalType( | ||
country_code=payload.shipment.country_code | ||
) if (payload.recipient.country_code not in ['US', 'CA']) else None | ||
) | ||
) |
Oops, something went wrong.