Exemplo n.º 1
0
def _process_quotes(
        args: Tuple[RateRequest,
                    Proxy]) -> Tuple[List[QuoteDetails], List[Error]]:
    payload, proxy = args
    try:
        custom_payload = RateRequest(**to_dict(payload))
        if custom_payload.shipper.account_number is None:
            custom_payload.shipper.account_number = DEFAULTS.get(
                proxy.client.carrier_name, {}).get("account_number", "")

        request = proxy.mapper.create_quote_request(custom_payload)
        response = proxy.get_quotes(request)
        logger.debug(
            (proxy.client.carrier_name, parse(request), parse(response)))
        return proxy.mapper.parse_quote_response(response)

    except OriginNotServicedError as e:
        logger.error(('error', proxy.client.carrier_name, f"{e}"))
        return [[],
                [
                    Error(carrier=proxy.client.carrier_name,
                          code="400",
                          message=f"{e}")
                ]]
    except Exception as e:
        logger.error(('error', e.args, proxy.client.carrier_name))
        return [[],
                [
                    Error(carrier=proxy.client.carrier_name,
                          code="500",
                          message="An error occured while processing quotes")
                ]]
Exemplo n.º 2
0
def _process_pickup(payload: PickupRequest,
                    proxy: Proxy,
                    update: bool = False) -> Tuple[PickupDetails, List[Error]]:
    logger.debug(
        ("start processing pickup request", proxy.client.carrier_name))
    try:
        if payload.account_number is None:
            payload.account_number = DEFAULTS.get(proxy.client.carrier_name,
                                                  {}).get(
                                                      "account_number", "")

        request = proxy.mapper.modify_pickup_request(
            payload) if update else proxy.mapper.create_pickup_request(payload)
        response = proxy.modify_pickup(
            request) if update else proxy.request_pickup(request)
        logger.debug((proxy.client.carrier_name, parse(request), response))
        return proxy.mapper.parse_shipment_response(response)
    except Exception as e:
        logger.error(
            ('error processing pickup request', e.args, payload, proxy))
        return [
            [],
            [
                Error(
                    carrier=proxy.client.carrier_name,
                    code="500",
                    message=
                    f"An error occured while processing pickup request: {e.args}"
                )
            ]
        ]
Exemplo n.º 3
0
 def parse_error_response(self, response: dict) -> List[Error]:
     if 'errors' in response:
         return [
             Error(
                 message=error.get('message'),
                 carrier=self.client.carrier_name,
                 code=error.get('code') or error.get('error_code')
             ) for error in response.get('errors', [])
         ]
     return []
Exemplo n.º 4
0
 def parse_error_response(self, response: List[dict]) -> List[Error]:
     errors: List[ValidationError] = [
         ValidationError(**e) for e in response if 'error' in e
     ]
     return [
         Error(code=error.error,
               carrier=self.client.carrier_name,
               message=error.error_description,
               details=error.messages) for error in errors
     ]
Exemplo n.º 5
0
 def parse_error_response(self, response: etree.ElementBase) -> List[Error]:
     error_nodes: List[USPSError] = [
         (lambda error: (error, error.build(node)))(USPSError())[0]
         for node in ([response] if response.tag == "Error" else response.
                      xpath(".//*[local-name() = $name]", name="Error"))
     ]
     return [
         Error(carrier=self.client.carrier_name,
               code=str(error.Number),
               message=error.Description) for error in error_nodes
     ]
Exemplo n.º 6
0
 def parse_error_response(self, response: dict) -> List[Error]:
     if "errors" not in response and "error" not in response:
         return []
     error_response: ErrorResponse = ErrorResponse(**response)
     errors: List[
         Union[APIError,
               PostageError]] = error_response.error + error_response.errors
     return [
         Error(carrier=self.client.carrier_name,
               code=error.get('code'),
               message=error.get('message') or error.get('errorMessage'))
         for error in to_dict(errors)
     ]
Exemplo n.º 7
0
def _process_tracking(
    args: Tuple[TrackingRequest, Proxy]
) -> Tuple[List[TrackingDetails], List[Error]]:
    payload, proxy = args
    try:
        request = proxy.mapper.create_tracking_request(payload)
        response = proxy.get_tracking(request)
        logger.debug((proxy.client.carrier_name, payload, parse(request),
                      parse(response)))
        return proxy.mapper.parse_tracking_response(response)
    except Exception as e:
        logger.error((e.args, payload, proxy))
        return [[],
                [
                    Error(carrier=proxy.client.carrier_name,
                          code="500",
                          message="An error occured while processing tracking")
                ]]
Exemplo n.º 8
0
def _process_shipment(payload: ShipmentRequest,
                      proxy: Proxy) -> Tuple[ShipmentDetails, List[Error]]:
    try:
        custom_payload = RateRequest(**to_dict(payload))
        if custom_payload.shipper.account_number is None:
            custom_payload.shipper.account_number = DEFAULTS.get(
                proxy.client.carrier_name, {}).get("account_number", "")

        request = proxy.mapper.create_shipment_request(custom_payload)
        response = proxy.create_shipment(request)
        logger.debug((proxy.client.carrier_name, parse(request), response))
        return proxy.mapper.parse_shipment_response(response)
    except Exception as e:
        logger.error((e.args, payload, proxy))
        return [
            [],
            [
                Error(carrier=proxy.client.carrier_name,
                      code="500",
                      message=
                      f"An error occured while processing shipping: {e.args}")
            ]
        ]