Example #1
0
 def get_rates(self, request: Serializable) -> Deserializable[str]:
     response = http(
         url=self.settings.server_url,
         data=bytearray(request.serialize(), "utf-8"),
         headers={"Content-Type": "application/xml"},
         method="POST",
     )
     return Deserializable(response, XP.to_xml)
Example #2
0
    def cancel_shipment(self, request: Serializable) -> Deserializable:
        def _request(method: str, shipment_id: str, path: str = '', **kwargs):
            return http(
                url=
                f"{self.settings.server_url}/rs/{self.settings.customer_number}/{self.settings.customer_number}/shipment/{shipment_id}{path}",
                headers={
                    "Content-Type": "application/vnd.cpc.shipment-v8+xml",
                    "Accept": "application/vnd.cpc.shipment-v8+xml",
                    "Authorization": f"Basic {self.settings.authorization}",
                    "Accept-language": f"{self.settings.language}-CA",
                },
                method=method,
                **kwargs)

        def process(job: Job):
            if job.data is None:
                return job.fallback

            subprocess = {
                "info":
                lambda _: _request('GET', job.data.serialize()),
                "refund":
                lambda _: _request(
                    'POST',
                    job.data['id'],
                    '/refund',
                    data=bytearray(job.data['payload'].serialize(), "utf-8")),
                "cancel":
                lambda _: _request('DELETE', job.data.serialize()),
            }
            if job.id not in subprocess:
                raise PurplShipError(
                    f"Unknown shipment cancel request job id: {job.id}")

            return subprocess[job.id](job)

        pipeline: Pipeline = request.serialize()
        response = pipeline.apply(process)
        return Deserializable(XP.bundle_xml(response), XP.to_xml)