def test_create_quote_request(self): shipper = { "postal_code": "H3N1S4", "country_code": "CA", "account_number": "2349857", } recipient = {"city": "Lome", "country_code": "TG"} shipment = { "currency": "USD", "payment_account_number": "2349857", "items": [{ "id": "1", "height": 3, "length": 10, "width": 3, "weight": 4.0 }], } payload = Quote.create(shipper=shipper, recipient=recipient, shipment=shipment) RateRequest_ = proxy.mapper.create_quote_request(payload) # Remove timeStamp for testing RateRequest_.RequestedShipment.ShipTimestamp = None self.assertEqual(export(RateRequest_), export(self.RateRequest))
def test_create_quote_request(self): shipper = {"postal_code": "H3N1S4", "country_code": "CA"} recipient = {"city": "Lome", "country_code": "TG"} shipment = { "services": "EXPRESS_WORLDWIDE_DOC", "currency": "CAD", "insured_amount": 75, "declared_value": 100, "items": [{ "id": "1", "height": 3, "length": 10, "width": 3, "weight": 4.0 }], "is_document": False, } payload = Quote.create(shipper=shipper, recipient=recipient, shipment=shipment) DCTRequest_ = proxy.mapper.create_quote_request(payload) # remove MessageTime, Date and ReadyTime for testing purpose DCTRequest_.GetQuote.Request.ServiceHeader.MessageTime = None DCTRequest_.GetQuote.BkgDetails.Date = None DCTRequest_.GetQuote.BkgDetails.ReadyTime = None self.assertEqual(export(DCTRequest_), export(self.DCTRequest))
def test_create_quote_request(self): payload = Quote.create(**rate_req_data) RateRequest_ = proxy.mapper.create_quote_request(payload) self.assertEqual( export(RateRequest_), export(self.RateRequest).replace("common:Code", "rate:Code"), )
def test_create_freight_quote_request(self): shipper = { "account_number": "56GJE", "postal_code": "H3N1S4", "country_code": "CA", "city": "Montreal", "address_lines": ["Rue Fake"], } recipient = { "postal_code": "89109", "city": "Las Vegas", "country_code": "US" } shipment = { "items": [{ "id": "1", "height": 3, "length": 170, "width": 3, "weight": 4.0, "packaging_type": "Bag", "description": "TV", }] } payload = Quote.create(shipper=shipper, recipient=recipient, shipment=shipment) FreightRateRequest_ = proxy.mapper.create_freight_quote_request( payload) self.assertEqual(export(FreightRateRequest_), export(self.FreightRateRequest))
def test_create_quote_request(self): shipper = { "postal_code": "H8Z2Z3", "country_code": "CA", "account_number": "1234567", } recipient = {"postal_code": "H8Z2V4", "country_code": "CA"} shipment = { "items": [{ "height": 3, "length": 10, "width": 3, "weight": 4.0 }], "services": ["Expedited_Parcel"], "dimension_unit": "CM", "weight_unit": "KG", "extra": { "options": [] }, } payload = Quote.create(shipper=shipper, recipient=recipient, shipment=shipment) mailing_scenario_ = proxy.mapper.create_quote_request(payload) self.assertEqual(export(mailing_scenario_), export(self.mailing_scenario))
def test_create_shipment_request(self): payload = Shipment.create(**shipment_data) ShipmentRequest_ = proxy.mapper.create_shipment_request(payload) # remove MessageTime, Date for testing purpose ShipmentRequest_.Request.ServiceHeader.MessageTime = None ShipmentRequest_.ShipmentDetails.Date = None self.assertEqual(export(ShipmentRequest_), export(self.ShipmentRequest))
def test_create_tracking_request(self): payload = Tracking.create(tracking_numbers=["8346088391"]) KnownTrackingRequest_ = proxy.mapper.create_tracking_request(payload) # remove MessageTime for testing purpose KnownTrackingRequest_.Request.ServiceHeader.MessageTime = None self.assertEqual(export(KnownTrackingRequest_), export(self.KnownTrackingRequest))
def test_create_pickup_cancellation_request(self): payload = Pickup.cancellation(**cancellation_data) CancelPURequest_ = proxy.mapper.create_pickup_cancellation_request(payload) # remove MessageTime for testing purpose CancelPURequest_.Request.ServiceHeader.MessageTime = None CancelPURequest_.CancelTime = None self.assertEqual( export(CancelPURequest_).replace("dhlPickup:", ""), export(self.CancelPURequest).replace("dhlPickup:", ""), )
def test_create_modify_pickup_request(self): payload = Pickup.request(**modification_data) ModifyPURequest_ = proxy.mapper.modify_pickup_request(payload) # remove MessageTime for testing purpose ModifyPURequest_.Request.ServiceHeader.MessageTime = None ModifyPURequest_.Place.LocationType = None ModifyPURequest_.Place.Address1 = None ModifyPURequest_.Place.PackageLocation = None ModifyPURequest_.Place.StateCode = None self.assertEqual( export(ModifyPURequest_).replace("dhlPickup:", ""), export(self.ModifyPURequest).replace("dhlPickup:", ""), )
def get_tracking(self, tracking_request: TrackFieldRequest) -> etree.ElementBase: query = urllib.parse.urlencode({'API': 'TrackV2', 'XML': export(tracking_request)}) response = http( url=f"{self.client.server_url}?{query}", method="GET", ) return to_xml(response)
def _get_tracking(self, TrackRequest_: TrackRequest): envelopeStr = export( create_envelope(header_content=self.mapper.Security, body_content=TrackRequest_), namespacedef_=""" xmlns:tns="http://schemas.xmlsoap.org/soap/envelope/" xmlns:upss="http://www.ups.com/XMLSchema/XOLTWS/UPSS/v1.0" xmlns:trk="http://www.ups.com/XMLSchema/XOLTWS/Track/v2.0" xmlns:common="http://www.ups.com/XMLSchema/XOLTWS/Common/v1.0" """.replace(" ", "").replace("\n", " "), ) xmlStr = clean_namespaces( envelopeStr, envelope_prefix="tns:", header_child_prefix="upss:", body_child_prefix="trk:", header_child_name="UPSSecurity", body_child_name="TrackRequest", ) result = http( url="%s/Track" % self.client.server_url, data=bytearray(xmlStr, "utf-8"), headers={"Content-Type": "application/xml"}, method="POST", ) return result
def cancel_pickup( self, FreightCancelPickupRequest_: FreightCancelPickupRequest ) -> etree.ElementBase: envelopeStr = export( create_envelope( header_content=self.mapper.Security, body_content=FreightCancelPickupRequest_, ), namespacedef_=""" xmlns:auth="http://www.ups.com/schema/xpci/1.0/auth" xmlns:upss="http://www.ups.com/XMLSchema/XOLTWS/UPSS/v1.0" xmlns:tns="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:upsa="http://www.ups.com/XMLSchema/XOLTWS/upssa/v1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:common="http://www.ups.com/XMLSchema/XOLTWS/Common/v1.0" xmlns:wsf="http://www.ups.com/schema/wsf" xmlns="http://www.ups.com/XMLSchema/XOLTWS/FreightPickup/v1.0" """.replace(" ", "").replace("\n", " "), ) xmlStr = clean_namespaces( envelopeStr, envelope_prefix="tns:", header_child_prefix="upss:", body_child_prefix="fpu:", header_child_name="UPSSecurity", body_child_name="FreightCancelPickupRequest", ).replace("fpu:", "") result = http( url="%s/FreightRate" % self.client.server_url, data=bytearray(xmlStr, "utf-8"), headers={"Content-Type": "application/xml"}, method="POST", ) return to_xml(result)
def get_quotes( self, rate_request: Union[RateV4Request, IntlRateV2Request] ) -> etree.ElementBase: api = "RateV4" if isinstance(rate_request, RateV4Request) else "IntlRateV2" query = urllib.parse.urlencode({'API': api, 'XML': export(rate_request)}) response = http( url=f'{self.client.server_url}?{query}', method="GET", ) return to_xml(response)
def create_shipment( self, ShipRequest_: Union[FreightShipRequest, ShipmentRequest] ) -> etree.ElementBase: is_freight = isinstance(ShipRequest_, FreightShipRequest) if is_freight: url_ = f"{self.client.server_url}/FreightShip" body_child_name_ = "FreightShipRequest" body_child_prefix_ = "fsp:" namespace_ = """ xmlns:tns="http://schemas.xmlsoap.org/soap/envelope/" xmlns:common="http://www.ups.com/XMLSchema/XOLTWS/Common/v1.0" xmlns:upss="http://www.ups.com/XMLSchema/XOLTWS/UPSS/v1.0" xmlns:wsf="http://www.ups.com/schema/wsf" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:fsp="http://www.ups.com/XMLSchema/XOLTWS/FreightShip/v1.0" xmlns:IF="http://www.ups.com/XMLSchema/XOLTWS/IF/v1.0" """.replace(" ", "").replace("\n", " ") else: url_ = f"{self.client.server_url}/Ship" body_child_name_ = "Shipment" body_child_prefix_ = "ship:" namespace_ = """ xmlns:tns="http://schemas.xmlsoap.org/soap/envelope/" xmlns:common="http://www.ups.com/XMLSchema/XOLTWS/Common/v1.0" xmlns:upss="http://www.ups.com/XMLSchema/XOLTWS/UPSS/v1.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ship="http://www.ups.com/XMLSchema/XOLTWS/Ship/v1.0" xmlns:ifs="http://www.ups.com/XMLSchema/XOLTWS/IF/v1.0" xsi:schemaLocation="http://www.ups.com/XMLSchema/XOLTWS/Ship/v1.0" """.replace(" ", "").replace("\n", " ") envelopeStr = export( create_envelope(header_content=self.mapper.Security, body_content=ShipRequest_), namespacedef_=namespace_, ) xmlStr = clean_namespaces( envelopeStr, envelope_prefix="tns:", header_child_prefix="upss:", body_child_prefix=body_child_prefix_, header_child_name="UPSSecurity", body_child_name=body_child_name_, ) result = http( url=url_, data=bytearray(xmlStr, "utf-8"), headers={"Content-Type": "application/xml"}, method="POST", ) return to_xml(result)
def parse(entity: ISerializable): try: return export(entity) except: pass try: return etree.tostring(entity) except: pass try: return to_dict(entity) except: return None
def get_quotes(self, DCTRequest_: DCTRequest) -> etree.ElementBase: xmlElt = export( DCTRequest_, name_="p:DCTRequest", namespacedef_='xmlns:p="http://www.dhl.com" xmlns:p1="http://www.dhl.com/datatypes" xmlns:p2="http://www.dhl.com/DCTRequestdatatypes" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.dhl.com DCT-req.xsd "', ).replace('schemaVersion="1."', 'schemaVersion="1.0"') result = http( url=self.client.server_url, data=bytearray(xmlElt, "utf-8"), headers={"Content-Type": "application/xml"}, method="POST", ) return to_xml(result)
def get_quotes( self, RateRequest_: Union[RateRequest, FreightRateRequest]) -> etree.ElementBase: is_freight = isinstance(RateRequest_, FreightRateRequest) if is_freight: url_ = "%s/FreightRate" % self.client.server_url body_child_name_ = "FreightRateRequest" body_child_prefix_ = "frt:" namespace_ = """ xmlns:tns="http://schemas.xmlsoap.org/soap/envelope/" xmlns:common="http://www.ups.com/XMLSchema/XOLTWS/Common/v1.0" xmlns:upss="http://www.ups.com/XMLSchema/XOLTWS/UPSS/v1.0" xmlns:wsf="http://www.ups.com/schema/wsf" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:frt="http://www.ups.com/XMLSchema/XOLTWS/FreightRate/v1.0" """.replace(" ", "").replace("\n", " ") else: url_ = "%s/Rate" % self.client.server_url body_child_name_ = "RateRequest" body_child_prefix_ = "rate:" namespace_ = """ xmlns:tns="http://schemas.xmlsoap.org/soap/envelope/" xmlns:common="http://www.ups.com/XMLSchema/XOLTWS/Common/v1.0" xmlns:upss="http://www.ups.com/XMLSchema/XOLTWS/UPSS/v1.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:rate="http://www.ups.com/XMLSchema/XOLTWS/Rate/v1.1" """.replace(" ", "").replace("\n", " ") envelopeStr = export( create_envelope(header_content=self.mapper.Security, body_content=RateRequest_), namespacedef_=namespace_, ) xmlStr = clean_namespaces( envelopeStr, envelope_prefix="tns:", header_child_prefix="upss:", header_child_name="UPSSecurity", body_child_name=body_child_name_, body_child_prefix=body_child_prefix_, ).replace("common:Code", "rate:Code") result = http( url=url_, data=bytearray(xmlStr, "utf-8"), headers={"Content-Type": "application/xml"}, method="POST", ) return to_xml(result)
def cancel_pickup(self, CancelPURequest_: CancelPURequest) -> etree.ElementBase: xmlElt = export( CancelPURequest_, name_="req:CancelPURequest", namespacedef_='xmlns:req="http://www.dhl.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.dhl.com cancel-pickup-global-req.xsd" schemaVersion="2.0"', ) result = http( url=self.client.server_url, data=bytearray(xmlElt, "utf-8"), headers={"Content-Type": "application/xml"}, method="POST", ) return to_xml(result)
def create_shipment( self, ShipmentRequest_: ShipmentRequest) -> etree.ElementBase: xmlElt = (export( ShipmentRequest_, name_="req:ShipmentRequest", namespacedef_= 'xmlns:req="http://www.dhl.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.dhl.com ship-val-global-req.xsd" schemaVersion="6.1"', ).replace("<Image>b'", "<Image>").replace("'</Image>", "</Image>")) result = http( url=self.client.server_url, data=bytearray(xmlElt, "utf-8"), headers={"Content-Type": "application/xml"}, method="POST", ) return to_xml(result)
def get_tracking( self, KnownTrackingRequest_: KnownTrackingRequest ) -> etree.ElementBase: xmlElt = export( KnownTrackingRequest_, name_="req:KnownTrackingRequest", namespacedef_='xmlns:req="http://www.dhl.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.dhl.com TrackingRequestKnown.xsd"', ) result = http( url=self.client.server_url, data=bytearray(xmlElt, "utf-8"), headers={"Content-Type": "application/xml"}, method="POST", ) return to_xml(result)
def create_shipment( self, shipment: Union[NonContractShipmentType, ShipmentType] ) -> etree.ElementBase: is_non_contract = isinstance(shipment, NonContractShipmentType) if is_non_contract: req_type = "application/vnd.cpc.ncshipment-v4+xml" namespace = 'xmlns="http://www.canadapost.ca/ws/ncshipment-v4"' name = "non-contract-shipment" url_ = ( f"{self.client.server_url}/rs/{self.client.customer_number}/ncshipment" ) else: req_type = "application/vnd.cpc.shipment-v8+xml" namespace = 'xmlns="http://www.canadapost.ca/ws/shipment-v8"' name = "shipment" url_ = f"{self.client.server_url}/rs/{self.client.customer_number}/{shipment.customer_request_id}/shipment" xmlStr = export(shipment, name_=name, namespacedef_=namespace) result = http( url=url_, data=bytearray(xmlStr, "utf-8"), headers={ "Content-Type": req_type, "Accept": req_type, "Authorization": "Basic %s" % self.authorization, "Accept-language": "en-CA", }, method="POST", ) response = to_xml(result) links = response.xpath(".//*[local-name() = $name]", name="link") if len(links) > 0: results = exec_parrallel( self._get_info, [ link for link in links if link.get("rel") in ["price", "receipt"] ], ) return to_xml(bundle_xml(xml_strings=[result] + results)) return response
def get_quotes( self, mailing_scenario: Rate.mailing_scenario) -> etree.ElementBase: xmlStr = export( mailing_scenario, namespacedef_='xmlns="http://www.canadapost.ca/ws/ship/rate-v3"', ) result = http( url="%s/rs/ship/price" % self.client.server_url, data=bytearray(xmlStr, "utf-8"), headers={ "Content-Type": "application/vnd.cpc.ship.rate-v3+xml", "Accept": "application/vnd.cpc.ship.rate-v3+xml", "Authorization": "Basic %s" % self.authorization, "Accept-language": "en-CA", }, method="POST", ) return to_xml(result)
def modify_pickup(self, ModifyPURequest_: ModifyPURequest) -> etree.ElementBase: xmlElt = (export( ModifyPURequest_, name_="req:ModifyPURequest", namespacedef_= 'xmlns:req="http://www.dhl.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.dhl.com modify-pickup-Global-req.xsd"', ).replace("dhlPickup:", "").replace('schemaVersion="1."', 'schemaVersion="1.0"')) xmlElt = _reformat_time("CloseTime", _reformat_time("ReadyByTime", xmlElt)) result = http( url=self.client.server_url, data=bytearray(xmlElt, "utf-8"), headers={"Content-Type": "application/xml"}, method="POST", ) return to_xml(result)
def get_quotes(self, RateRequest_: Rate.RateRequest) -> etree.ElementBase: envelopeStr = export( create_envelope(body_content=RateRequest_), namespacedef_= 'xmlns:tns="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:ns="http://fedex.com/ws/rate/v22"', ) xmlStr = clean_namespaces( envelopeStr, envelope_prefix="tns:", body_child_prefix="ns:", body_child_name="RateRequest", ) result = http( url=self.client.server_url, data=bytearray(xmlStr, "utf-8"), headers={"Content-Type": "application/xml"}, method="POST", ) return to_xml(result)
def get_tracking(self, TrackRequest_: Track.TrackRequest) -> etree.ElementBase: envelopeStr = export( create_envelope(body_content=TrackRequest_), namespacedef_= 'xmlns:tns="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns="http://fedex.com/ws/track/v14"', ) xmlStr = clean_namespaces( envelopeStr, envelope_prefix="tns:", body_child_prefix="ns:", body_child_name="TrackRequest", ) result = http( url=self.client.server_url, data=bytearray(xmlStr, "utf-8"), headers={"Content-Type": "application/xml"}, method="POST", ) return to_xml(result)
def request_pickup( self, pickup_request_details: Pick.PickupRequestDetailsType, method: str = "POST", ) -> etree.ElementBase: xmlStr = export( pickup_request_details, name_="pickup-request-details", namespacedef_='xmlns="http://www.canadapost.ca/ws/pickuprequest"', ) result = http( url="%s/enab/%s/pickuprequest" % (self.client.server_url, self.client.customer_number), data=bytearray(xmlStr, "utf-8"), headers={ "Content-Type": "application/vnd.cpc.pickuprequest+xml", "Accept": "application/vnd.cpc.pickuprequest+xml", "Authorization": "Basic %s" % self.authorization, "Accept-language": "en-CA", }, method="POST", ) return to_xml(result)
def test_create_tracking_request(self): payload = Tracking.create(tracking_numbers=["794887075005"]) TrackRequest_ = proxy.mapper.create_tracking_request(payload) self.assertEqual(export(TrackRequest_), export(self.TrackRequest))
def test_create_shipment_request(self): payload = Shipment.create(**shipment_data) Shipment_ = proxy.mapper.create_shipment_request(payload) self.assertEqual(export(Shipment_), export(self.Shipment))
def test_create_tracking_request(self): payload = Tracking.create(tracking_numbers=["1Z12345E6205277936"]) TrackRequests_ = proxy.mapper.create_tracking_request(payload) self.assertEqual(export(TrackRequests_[0]), export(self.TrackRequest))
def test_create_ncshipment_request(self): payload = Shipment.create(**ncshipment_data) NCShipment_ = proxy.mapper.create_shipment_request(payload) # removed for testing purpose NCShipment_.delivery_spec.parcel_characteristics.document = None self.assertEqual(export(NCShipment_), export(self.NCShipment))