Skip to content

Commit

Permalink
Merge pull request #24 from PurplShip/rc5hotFixes
Browse files Browse the repository at this point in the history
Rc5hot fixes
  • Loading branch information
danh91 authored Nov 8, 2018
2 parents 16e3cc3 + afdedc6 commit d2a62ef
Show file tree
Hide file tree
Showing 42 changed files with 524 additions and 523 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ PurplSHip prevents you from reinventing the wheel and is easy to use:

```shell
>>> from purplship.mappers.dhl import DHLClient, DHLProxy
>>> from purplship.domain.entities import Tracking
>>> from purplship.domain.Types import Tracking
>>> client = DHLClient(
"https://xmlpi-ea.dhl.com/XMLShippingServlet",
"YOUR_DHL_SITE_ID",
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -50,19 +50,20 @@ class customs_type(NamedTuple):
commercial_invoice: bool = False
extra: Dict = {}


class label_type(NamedTuple):
format: str = None
type: str = None
extra: Dict = {}

class invoice_type(NamedTuple):
date: str
identifier: str = None
type: str = None
copies: int = None
extra: Dict = {}

class doc_image(NamedTuple):
type: str = None
format: str = None
image: str = None
extra: Dict = {}


class shipment_options(NamedTuple):

""" packages or commodities """
Expand All @@ -89,11 +90,12 @@ class shipment_options(NamedTuple):
ship_date: str = None
customs: customs_type = None
invoice: invoice_type = None
doc_images: List[doc_image] = []

references: List[str] = []
extra_services: List[str] = []

label: label_type = None
label: doc_image = None
extra: Dict = {}

class shipment_request(NamedTuple):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from collections import namedtuple
from typing import List, Dict
from purplship.domain.entities.datatypes import item_type, label_type, party, invoice_type
from purplship.domain.Types.datatypes import item_type, party, invoice_type, doc_image

''' customs Type definition '''
class customs_details_type(namedtuple("customs_details_type", "no_eei aes description terms_of_trade items commercial_invoice extra")):
Expand All @@ -18,8 +18,8 @@ def __new__(cls, no_eei: str = None, aes: str = None, description: str = None, t


''' shipment options Type definition '''
class shipment_options_type(namedtuple("shipment_options_type", "items insured_amount total_items packaging_type is_document currency date total_weight weight_unit dimension_unit paid_by duty_paid_by payment_type payment_country_code duty_payment_account declared_value service_type payment_account_number billing_account_number extra_services customs invoice references label, extra")):
def __new__(cls, items: List, insured_amount: float = None, total_items: int = None, packaging_type: str = None, is_document: bool = False, currency: str = None, date: str = None, total_weight: float = None, weight_unit: str = "LB", dimension_unit: str = "IN", paid_by: str = None, duty_paid_by: str = None, payment_type: str = None, payment_country_code: str = None, duty_payment_account: str = None, declared_value: float = None, service_type: str = None, payment_account_number: str = None, billing_account_number: str = None, extra_services: List[str] = [], customs: Dict = None, invoice: dict = None, references: List[str] = [], label: Dict = None, extra: Dict = {}):
class shipment_options_type(namedtuple("shipment_options_type", "items insured_amount total_items packaging_type is_document currency date total_weight weight_unit dimension_unit paid_by duty_paid_by payment_type payment_country_code duty_payment_account declared_value service_type payment_account_number billing_account_number extra_services customs invoice doc_images references label, extra")):
def __new__(cls, items: List, insured_amount: float = None, total_items: int = None, packaging_type: str = None, is_document: bool = False, currency: str = None, date: str = None, total_weight: float = None, weight_unit: str = "LB", dimension_unit: str = "IN", paid_by: str = None, duty_paid_by: str = None, payment_type: str = None, payment_country_code: str = None, duty_payment_account: str = None, declared_value: float = None, service_type: str = None, payment_account_number: str = None, billing_account_number: str = None, extra_services: List[str] = [], customs: Dict = None, invoice: dict = None, doc_images: List[dict] = [], references: List[str] = [], label: Dict = None, extra: Dict = {}):
return super(cls, shipment_options_type).__new__(
cls,
[item_type(**p) for p in items],
Expand All @@ -44,8 +44,9 @@ def __new__(cls, items: List, insured_amount: float = None, total_items: int = N
extra_services,
customs_details_type(**customs) if customs else None,
invoice_type(**invoice) if invoice else None,
[doc_image(**doc) for doc in doc_images],
references,
label_type(**label) if label else None,
doc_image(**label) if label else None,
extra
)

Expand Down
2 changes: 1 addition & 1 deletion purplship/domain/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .client import Client
from .mapper import Mapper
from .proxy import Proxy
from .entities import Quote, Tracking
from .Types import Quote, Tracking
24 changes: 12 additions & 12 deletions purplship/domain/mapper.py
Original file line number Diff line number Diff line change
@@ -1,51 +1,51 @@
from typing import List, Tuple
from .client import Client
from ..domain import entities as E
from ..domain import Types as T

class Mapper:
""" Unitied API to carrier API Mapper (Interface) """
client: Client

def create_quote_request(self, payload: E.shipment_request):
def create_quote_request(self, payload: T.shipment_request):
""" Create carrier specific quote request xml data from payload """
raise Exception("Not Supported")

def parse_quote_response(self, response) -> Tuple[List[E.QuoteDetails], List[E.Error]]:
def parse_quote_response(self, response) -> Tuple[List[T.QuoteDetails], List[T.Error]]:
""" Create united API quote result list from carrier xml response """
raise Exception("Not Supported")

def create_tracking_request(self, payload: E.tracking_request):
def create_tracking_request(self, payload: T.tracking_request):
""" Create carrier specific tracking request xml data from payload """
raise Exception("Not Supported")

def parse_tracking_response(self, response) -> Tuple[List[E.TrackingDetails], List[E.Error]]:
def parse_tracking_response(self, response) -> Tuple[List[T.TrackingDetails], List[T.Error]]:
""" Create united API tracking result list from carrier xml response """
raise Exception("Not Supported")

def create_shipment_request(self, payload: E.shipment_request):
def create_shipment_request(self, payload: T.shipment_request):
""" Create carrier specific shipment creation request xml data from payload """
raise Exception("Not Supported")

def parse_shipment_response(self, response) -> Tuple[E.ShipmentDetails, List[E.Error]]:
def parse_shipment_response(self, response) -> Tuple[T.ShipmentDetails, List[T.Error]]:
""" Create united API shipment creation result from carrier xml response """
raise Exception("Not Supported")

def create_pickup_request(self, payload: E.pickup_request):
def create_pickup_request(self, payload: T.pickup_request):
""" Create carrier specific pickup request xml data from payload """
raise Exception("Not Supported")

def modify_pickup_request(self, payload: E.pickup_request):
def modify_pickup_request(self, payload: T.pickup_request):
""" Create carrier specific pickup modification request xml data from payload """
raise Exception("Not Supported")

def parse_pickup_response(self, response) -> Tuple[E.PickupDetails, List[E.Error]]:
def parse_pickup_response(self, response) -> Tuple[T.PickupDetails, List[T.Error]]:
""" Create united API pickup result from carrier xml response """
raise Exception("Not Supported")

def create_pickup_cancellation_request(self, payload: E.pickup_cancellation_request):
def create_pickup_cancellation_request(self, payload: T.pickup_cancellation_request):
""" Create carrier specific pickup cancellation request xml data from payload """
raise Exception("Not Supported")

def parse_pickup_cancellation_response(self, response) -> Tuple[dict, List[E.Error]]:
def parse_pickup_cancellation_response(self, response) -> Tuple[dict, List[T.Error]]:
""" Create united API pickup cancellation result from carrier xml response """
raise Exception("Not Supported")
14 changes: 7 additions & 7 deletions purplship/mappers/caps/caps_mapper/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import Tuple, List, Union
from purplship.domain.mapper import Mapper
from purplship.domain import entities as E
from purplship.domain import Types as T
from pycaps.rating import mailing_scenario
from pycaps.shipment import ShipmentType
from pycaps.ncshipment import NonContractShipmentType
Expand All @@ -18,22 +18,22 @@ class CanadaPostMapper(
CanadaPostShipmentMapperPartial
):

def create_quote_request(self, payload: E.shipment_request) -> mailing_scenario:
def create_quote_request(self, payload: T.shipment_request) -> mailing_scenario:
return self.create_mailing_scenario(payload)

def create_tracking_request(self, payload: E.tracking_request) -> List[str]:
def create_tracking_request(self, payload: T.tracking_request) -> List[str]:
return self.create_tracking_pins(payload)

def create_shipment_request(self, payload: E.shipment_request) -> Union[ShipmentType, NonContractShipmentType]:
def create_shipment_request(self, payload: T.shipment_request) -> Union[ShipmentType, NonContractShipmentType]:
return self.create_shipment(payload)



def parse_quote_response(self, response: 'XMLElement') -> Tuple[List[E.QuoteDetails], List[E.Error]]:
def parse_quote_response(self, response: 'XMLElement') -> Tuple[List[T.QuoteDetails], List[T.Error]]:
return self.parse_price_quotes(response)

def parse_tracking_response(self, response: 'XMLElement') -> Tuple[List[E.TrackingDetails], List[E.Error]]:
def parse_tracking_response(self, response: 'XMLElement') -> Tuple[List[T.TrackingDetails], List[T.Error]]:
return self.parse_tracking_summary(response)

def parse_shipment_response(self, response: 'XMLElement') -> Tuple[E.ShipmentDetails, List[E.Error]]:
def parse_shipment_response(self, response: 'XMLElement') -> Tuple[T.ShipmentDetails, List[T.Error]]:
return self.parse_shipment_info(response)
20 changes: 10 additions & 10 deletions purplship/mappers/caps/caps_mapper/partials/interface.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing import Tuple, List, Union
from functools import reduce
from purplship.mappers.caps import CanadaPostClient
from purplship.domain import entities as E
from purplship.domain import Types as T
from pycaps.rating import mailing_scenario
from pycaps.shipment import ShipmentType
from pycaps.ncshipment import NonContractShipmentType
Expand All @@ -15,25 +15,25 @@ class CanadaPostCapabilities:

""" Requests """

def create_mailing_scenario(self, payload: E.shipment_request) -> mailing_scenario:
def create_mailing_scenario(self, payload: T.shipment_request) -> mailing_scenario:
pass

def create_tracking_pins(self, payload: E.tracking_request) -> List[str]:
def create_tracking_pins(self, payload: T.tracking_request) -> List[str]:
pass

def create_shipment(self, payload: E.shipment_request) -> Union[ShipmentType, NonContractShipmentType]:
def create_shipment(self, payload: T.shipment_request) -> Union[ShipmentType, NonContractShipmentType]:
pass


""" Replys """

def parse_price_quotes(self, response: 'XMLElement') -> Tuple[List[E.QuoteDetails], List[E.Error]]:
def parse_price_quotes(self, response: 'XMLElement') -> Tuple[List[T.QuoteDetails], List[T.Error]]:
pass

def parse_tracking_summary(self, response: 'XMLElement') -> Tuple[List[E.TrackingDetails], List[E.Error]]:
def parse_tracking_summary(self, response: 'XMLElement') -> Tuple[List[T.TrackingDetails], List[T.Error]]:
pass

def parse_shipment_info(self, response: 'XMLElement') -> Tuple[E.ShipmentDetails, List[E.Error]]:
def parse_shipment_info(self, response: 'XMLElement') -> Tuple[T.ShipmentDetails, List[T.Error]]:
pass


Expand All @@ -44,14 +44,14 @@ class CanadaPostMapperBase(CanadaPostCapabilities):
def __init__(self, client: CanadaPostClient):
self.client = client

def parse_error_response(self, response: 'XMLElement') -> List[E.Error]:
def parse_error_response(self, response: 'XMLElement') -> List[T.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]:
def _extract_error(self, errors: List[T.Error], messageNode: 'XMLElement') -> List[T.Error]:
message = messageType()
message.build(messageNode)
return errors + [
E.Error(code=message.code,
T.Error(code=message.code,
message=message.description, carrier=self.client.carrier_name)
]
14 changes: 7 additions & 7 deletions purplship/mappers/caps/caps_mapper/partials/rate.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
from pycaps.rating import *
from datetime import datetime
from .interface import reduce, Tuple, List, E, CanadaPostMapperBase
from .interface import reduce, Tuple, List, T, CanadaPostMapperBase


class CanadaPostMapperPartial(CanadaPostMapperBase):

def parse_price_quotes(self, response: 'XMLElement') -> Tuple[List[E.QuoteDetails], List[E.Error]]:
def parse_price_quotes(self, response: 'XMLElement') -> Tuple[List[T.QuoteDetails], List[T.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]:
def _extract_quote(self, quotes: List[T.QuoteDetails], price_quoteNode: 'XMLElement') -> List[T.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]
discounts = [T.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(
T.QuoteDetails(
carrier=self.client.carrier_name,
currency="CAD",
delivery_date=str(price_quote.service_standard.expected_delivery_date),
Expand All @@ -27,14 +27,14 @@ def _extract_quote(self, quotes: List[E.QuoteDetails], price_quoteNode: 'XMLElem
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(
extra_charges=list(map(lambda a: T.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:
def create_mailing_scenario(self, payload: T.shipment_request) -> mailing_scenario:
package = payload.shipment.items[0]
requested_services = payload.shipment.extra_services + [payload.shipment.service_type]

Expand Down
Loading

0 comments on commit d2a62ef

Please sign in to comment.