Esempio n. 1
0
    def setUp(self):
        self.PURequest = BookPURequest()
        self.PURequest.build(to_xml(PickupRequestXML))

        self.ModifyPURequest = ModifyPURequest()
        self.ModifyPURequest.build(to_xml(ModifyPURequestXML))

        self.CancelPURequest = CancelPURequest()
        self.CancelPURequest.build(to_xml(CancelPURequestXML))
Esempio n. 2
0
 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)
Esempio n. 3
0
    def get_trackings(self, tracking_pins: List[str]) -> etree.ElementBase:
        """
        get_trackings make parrallel request for each pin
        """
        results = exec_parrallel(self._get_tracking, tracking_pins)

        return to_xml(bundle_xml(xml_strings=results))
Esempio n. 4
0
    def get_tracking(self, TrackRequests_: TrackRequest) -> etree.ElementBase:
        """
        get_tracking make parrallel request for each TrackRequest
        """
        results = exec_parrallel(self._get_tracking, TrackRequests_)

        return to_xml(bundle_xml(xml_strings=results))
 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)
Esempio n. 6
0
    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, 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)
Esempio n. 8
0
    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)
Esempio n. 10
0
    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)
Esempio n. 11
0
    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)
Esempio n. 12
0
 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)
Esempio n. 13
0
    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)
Esempio n. 14
0
    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)
Esempio n. 15
0
    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)
Esempio n. 16
0
 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)
Esempio n. 17
0
    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)
Esempio n. 18
0
    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)
Esempio n. 19
0
 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)
Esempio n. 20
0
 def setUp(self):
     self.mailing_scenario = mailing_scenario()
     self.mailing_scenario.build(to_xml(QuoteRequestXml))
Esempio n. 21
0
 def test_parse_package_quote_response(self):
     parsed_response = proxy.mapper.parse_quote_response(
         to_xml(RateResponseXML))
     self.assertEqual(jsonify(parsed_response), jsonify(ParsedRateResponse))
Esempio n. 22
0
 def test_parse_freight_quote_response(self):
     parsed_response = proxy.mapper.parse_quote_response(
         to_xml(FreightRateResponseXML))
     self.assertEqual(jsonify(parsed_response),
                      jsonify(ParsedFreightRateResponse))
Esempio n. 23
0
 def test_tracking_unknown_response_parsing(self):
     parsed_response = proxy.mapper.parse_tracking_response(
         to_xml(UnknownTrackingNumberResponse))
     self.assertEqual(jsonify(parsed_response),
                      jsonify(ParsedUnknownTrackingNumberResponse))
Esempio n. 24
0
 def test_parse_tracking_response(self):
     parsed_response = proxy.mapper.parse_tracking_response(
         to_xml(TrackingResponseXml))
     self.assertEqual(jsonify(parsed_response),
                      jsonify(ParsedTrackingResponse))
Esempio n. 25
0
 def test_tracking_auth_error_parsing(self):
     parsed_response = proxy.mapper.parse_error_response(to_xml(AuthError))
     self.assertEqual(jsonify(parsed_response), jsonify(ParsedAuthError))
Esempio n. 26
0
 def test_parse_quote_response(self):
     parsed_response = proxy.mapper.parse_quote_response(
         to_xml(QuoteResponseXml))
     self.assertEqual(jsonify(parsed_response),
                      jsonify(ParsedQuoteResponse))
Esempio n. 27
0
 def test_parse_ncshipment_response(self):
     parsed_response = proxy.mapper.parse_shipment_response(
         to_xml(NCShipmentResponseXML))
     self.assertEqual(jsonify(parsed_response),
                      jsonify(NCParsedShipmentResponse))
Esempio n. 28
0
    def test_get_info(self, http_mock):
        proxy._get_info(to_xml(ShipmentPriceLinkXML))

        args = http_mock.call_args[1]
        self.assertEqual(jsonify(args), GetInfoRequestArgs)
Esempio n. 29
0
 def test_parse_quote_missing_args_error(self):
     parsed_response = proxy.mapper.parse_quote_response(
         to_xml(QuoteMissingArgsError))
     self.assertEqual(jsonify(parsed_response),
                      jsonify(ParsedQuoteMissingArgsError))
Esempio n. 30
0
    def setUp(self):
        self.Shipment = ShipmentType()
        self.Shipment.build(to_xml(ShipmentRequestXML))

        self.NCShipment = NonContractShipmentType()
        self.NCShipment.build(to_xml(NCShipmentRequestXML))