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_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 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_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 get_quotes(self, parcel_quote_request: ParcelQuoteRequest) -> dict: query_string = urllib.parse.urlencode(to_dict(parcel_quote_request)) response = http( url=f"{self.client.server_url}/quote?{query_string}", headers={ "Content-Type": "application/json", "Authorization": f"Basic {self.authorization}", }, method="GET", ) return to_dict(response)
def _get_tracking(self, tracking_pin: str) -> etree.ElementBase: result = http( url="%s/vis/track/pin/%s/summary" % (self.client.server_url, tracking_pin), headers={ "Accept": "application/vnd.cpc.track+xml", "Authorization": "Basic %s" % self.authorization, "Accept-language": "en-CA", }, method="GET", ) return result
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 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 track(tracking_id): return { "ref": tracking_id, "response": to_dict( http( url=f"{self.client.server_url}/tracking/{tracking_id}", headers={ "Content-Type": "application/json", "Authorization": f"Basic {self.authorization}", }, method="GET", ) ) }
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 _get_shipping_price( self, shipping_price_request: ShippingPriceRequest) -> str: data = jsonify(to_dict(shipping_price_request)) return http( url=f"{self.client.server_url}/shipping/v1/prices/shipments", data=bytearray(data, "utf-8"), headers={ "Content-Type": "application/json", "Accept": "application/json", "Account-Number": self.client.account_number, "Authorization": f"Basic {self.authorization}", }, method="POST", ).replace('from', 'from_')
def get_tracking(self, tracking_ids: List[str]) -> dict: ids = ','.join(tracking_ids) result = http( url= f"{self.client.server_url}/shipping/v1/track?tracking_ids={ids}", headers={ "Content-Type": "application/json", "Accept": "application/json", "Account-Number": self.client.account_number, "Authorization": f"Basic {self.authorization}", }, method="GET", ) return to_dict(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, rel: str) -> etree.ElementBase: """ Invoke the link returned from a prior call to Get Pickup Requests where rel= “self” <link rel="self" .../> """ result = http( url=rel, headers={ "Accept": "application/vnd.cpc.pickuprequest+xml", "Authorization": "Basic %s" % self.authorization, "Accept-language": "en-CA", }, method="DELETE", ) 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 _get_postage_service(self, postage_request: PostageRequest) -> str: route: str = { DomesticParcelServiceRequest: "/parcel/domestic/service.json", DomesticLetterServiceRequest: "/letter/domestic/service.json", IntlParcelServiceRequest: "/parcel/international/service.json", IntlLetterServiceRequest: "/letter/international/service.json", }[type(postage_request)] query_string = urllib.parse.urlencode(to_dict(postage_request)) return http( url=f"{self.client.server_url}/postage{route}?{query_string}", headers={ "Content-Type": "application/json", "AUTH-KEY": self.client.api_key, }, method="GET", )
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 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_info(self, link: etree.ElementBase) -> str: href = link.get("href") is_ncdetails = all([(key in href) for key in ["details", "ncshipment"]]) args = dict([ ("url", href), ( "headers", dict([ ("Accept", link.get("media-type")), ("Authorization", "Basic %s" % self.authorization), ("Accept-language", "en-CA"), ] + ([("Content-Type", link.get("media-type"))] if is_ncdetails else [])), ), ("method", "POST" if is_ncdetails else "GET"), ]) return http(**args)
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_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 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 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)