def _extract_shipment(response: Element, settings: Settings) -> ShipmentDetails: shipment = CreateShipmentResponse() document = DocumentDetail() shipment_nodes = response.xpath(".//*[local-name() = $name]", name="CreateShipmentResponse") document_nodes = response.xpath(".//*[local-name() = $name]", name="DocumentDetail") next((shipment.build(node) for node in shipment_nodes), None) next((document.build(node) for node in document_nodes), None) label = next( (content for content in [document.Data, document.URL] if content is not None), "No label returned", ) pin = cast(PIN, shipment.ShipmentPIN).Value return ShipmentDetails( carrier_name=settings.carrier_name, carrier_id=settings.carrier_id, tracking_number=pin, shipment_identifier=pin, label=label, )
def _extract_details(response: Element, settings: Settings) -> ShipmentDetails: shipment = XP.build(eVSResponse, response) charges: List[ExtraServiceType] = shipment.ExtraServices.ExtraService total_charge = sum([ NF.decimal(shipment.Postage), *[NF.decimal(c.Price) for c in charges] ], 0.0) return ShipmentDetails(carrier_name=settings.carrier_name, carrier_id=settings.carrier_id, label=shipment.LabelImage, tracking_number=shipment.BarcodeNumber, shipment_identifier=shipment.BarcodeNumber, selected_rate=RateDetails( carrier_name=settings.carrier_name, carrier_id=settings.carrier_id, service=ServiceClassID(shipment).name, currency=Currency.USD.value, base_charge=NF.decimal(shipment.Postage), total_charge=total_charge, extra_charges=[ ChargeDetails(name=charge.ServiceName, amount=NF.decimal( charge.Price), currency=Currency.USD.value) for charge in charges ]))
def _extract_details(response: Tuple[str, str], settings: Settings) -> ShipmentDetails: package_id, label = response return ShipmentDetails( carrier_id=settings.carrier_id, carrier_name=settings.carrier_name, label=label, tracking_number=package_id, shipment_identifier=package_id, )
def _extract_details(response: Element, settings: Settings) -> ShipmentDetails: shipment = XP.build(eVSExpressMailIntlResponse, response) return ShipmentDetails( carrier_name=settings.carrier_name, carrier_id=settings.carrier_id, label=shipment.LabelImage, tracking_number=shipment.BarcodeNumber, shipment_identifier=shipment.BarcodeNumber, )
def _extract_shipment(node: Element, settings: Settings) -> ShipmentDetails: shipment = XP.build(ShipmentResultsType, node) return ShipmentDetails( carrier_name=settings.carrier_name, carrier_id=settings.carrier_id, tracking_number=shipment.ShipmentIdentificationNumber, shipment_identifier=shipment.ShipmentIdentificationNumber, label=None, )
def _extract_shipment(node: Element, settings: Settings) -> ShipmentDetails: shipment = ShipmentResultsType() shipment.build(node) package: PackageResultsType = next(iter(shipment.PackageResults), None) return ShipmentDetails( carrier_name=settings.carrier_name, carrier_id=settings.carrier_id, tracking_number=shipment.ShipmentIdentificationNumber, label=cast(LabelType, package.ShippingLabel).GraphicImage, )
def _extract_details(response: dict, settings: Settings) -> ShipmentDetails: label: str = response['label'] shipment = DP.to_object(ShipmentResponse, response['shipment']) return ShipmentDetails( carrier_name=settings.carrier_name, carrier_id=settings.carrier_id, label=label, tracking_number=shipment.trackingNumber, shipment_identifier=shipment.ID, )
def _extract_shipment(response: Element, settings: Settings) -> ShipmentDetails: info = XP.find("shipment-info", response, ShipmentInfoType, first=True) label = XP.find("label", response, first=True) return ShipmentDetails( carrier_name=settings.carrier_name, carrier_id=settings.carrier_id, tracking_number=info.tracking_pin, shipment_identifier=info.tracking_pin, label=getattr(label, "text", None), )
def _extract_shipment(shipment_node, settings: Settings) -> Optional[ShipmentDetails]: shipment = ShipmentResponse() shipment.build(shipment_node) label_image = next(iter(shipment.LabelImage), None) label = encodebytes(cast(LabelImage, label_image).OutputImage).decode("utf-8") return ShipmentDetails( carrier_name=settings.carrier_name, carrier_id=settings.carrier_id, tracking_number=shipment.AirwayBillNumber, label=label, )
def _extract_details(response: Tuple[str, dict], settings: Settings) -> ShipmentDetails: label, details = response shipment = DP.to_object(PackageLabel, details) return ShipmentDetails( carrier_name=settings.carrier_name, carrier_id=settings.carrier_id, label=label, tracking_number=shipment.trackingNumber, shipment_identifier=shipment.packageId, )
def _extract_shipment(shipment_node, settings: Settings) -> Optional[ShipmentDetails]: tracking_number = shipment_node.xpath(".//*[local-name() = $name]", name="AirwayBillNumber")[0].text label_node = shipment_node.xpath(".//*[local-name() = $name]", name="LabelImage")[0] label = encodebytes(XP.build(LabelImage, label_node).OutputImage).decode("utf-8") return ShipmentDetails( carrier_name=settings.carrier_name, carrier_id=settings.carrier_id, tracking_number=tracking_number, shipment_identifier=tracking_number, label=label, )
def _extract_shipment(node: Element, settings: Settings) -> ShipmentDetails: shipping = XP.build(ShippingReplyType, node) quote: QuoteType = shipping.Quote tracking_number = getattr(next(iter(shipping.Package), None), 'trackingNumber', None) rate_provider, service, service_name = Service.info( quote.serviceId, quote.carrierId, quote.serviceName, quote.carrierName ) surcharges = [ ChargeDetails( name=charge.name, currency=quote.currency, amount=NF.decimal(charge.amount), ) for charge in cast(List[SurchargeType], quote.Surcharge) ] fuel_surcharge = ( ChargeDetails( name="Fuel surcharge", currency=quote.currency, amount=NF.decimal(quote.fuelSurcharge), ) if quote.fuelSurcharge is not None else None ) return ShipmentDetails( carrier_name=settings.carrier_name, carrier_id=settings.carrier_id, tracking_number=tracking_number, shipment_identifier=shipping.Order.id, label=shipping.Labels, selected_rate=( RateDetails( carrier_name=settings.carrier_name, carrier_id=settings.carrier_id, service=service, currency=quote.currency, base_charge=NF.decimal(quote.baseCharge), total_charge=NF.decimal(quote.totalCharge), transit_days=quote.transitDays, extra_charges=([fuel_surcharge] + surcharges), meta=dict( rate_provider=rate_provider, service_name=service_name ) ) if quote is not None else None ), meta=dict( rate_provider=rate_provider, service_name=service_name, tracking_url=shipping.TrackingURL ) )
def _extract_details(response: Element, settings: Settings) -> ShipmentDetails: shipment = XP.build(eVSGXGGetLabelResponse, response) tracking_number = (shipment.USPSBarcodeNumber or shipment.FedExBarcodeNumber) return ShipmentDetails( carrier_name=settings.carrier_name, carrier_id=settings.carrier_id, label=shipment.LabelImage, tracking_number=tracking_number, shipment_identifier=tracking_number, )
def _extract_shipment(response: Element, settings: Settings) -> ShipmentDetails: pin: PIN = XP.find("ShipmentPIN", response, PIN, first=True) document = XP.find("DocumentDetail", response, DocumentDetail, first=True) or DocumentDetail() return ShipmentDetails( carrier_name=settings.carrier_name, carrier_id=settings.carrier_id, tracking_number=pin.Value, shipment_identifier=pin.Value, label=document.Data, )
def _extract_details(response: Element, settings: Settings) -> ShipmentDetails: shipment_node = next(iter(response.xpath(".//*[local-name() = $name]", name="shipment")), None) label = next(iter(response.xpath(".//*[local-name() = $name]", name="labels")), None) shipment = XP.to_object(Shipment, shipment_node) tracking_number = next(iter(shipment.packages), Package()).barcode return ShipmentDetails( carrier_id=settings.carrier_id, carrier_name=settings.carrier_name, label=str(label.text), tracking_number=tracking_number, shipment_identifier=str(shipment.id), selected_rate=_extract_rate_details(shipment_node, settings), )
def _extract_shipment(response: Element, settings: Settings) -> ShipmentDetails: info_node = next( iter(response.xpath(".//*[local-name() = $name]", name="shipment-info")) ) label = next(iter(response.xpath(".//*[local-name() = $name]", name="label"))) errors = parse_error_response(label, settings) info: ShipmentInfoType = ShipmentInfoType() info.build(info_node) return ShipmentDetails( carrier_name=settings.carrier_name, carrier_id=settings.carrier_id, tracking_number=info.tracking_pin, label=label.text if len(errors) == 0 else None, )
def _extract_shipment(shipment_node: Element, settings: Settings) -> ShipmentDetails: shipmentResponse = FreightShipResponse() shipmentResponse.build(shipment_node) shipment: ShipmentResultsType = shipmentResponse.ShipmentResults document = cast(DocumentType, shipment.Documents) label = (cast(ImageFormsType, document.Image).GraphicImage if document is not None else None) return ShipmentDetails( carrier_name=settings.carrier_name, carrier_id=settings.carrier_id, tracking_number=shipment.ShipmentNumber, label=label, )
def _extract_shipment(node: Element, settings: Settings) -> ShipmentDetails: shipment = XP.to_object(ShipmentResultsType, node) package: PackageResultsType = next(iter(shipment.PackageResults), None) shipping_label = cast(LabelType, package.ShippingLabel) label = (gif_to_pdf(shipping_label.GraphicImage) if cast( ImageFormatType, shipping_label.ImageFormat).Code == "GIF" else shipping_label.GraphicImage) return ShipmentDetails( carrier_name=settings.carrier_name, carrier_id=settings.carrier_id, tracking_number=shipment.ShipmentIdentificationNumber, shipment_identifier=shipment.ShipmentIdentificationNumber, label=label, )
def _extract_detail(response: Element, settings: Settings) -> Optional[ShipmentDetails]: activity: document = XP.find("document", response, element_type=document, first=True) if activity is None or activity.CREATE.SUCCESS != 'Y': return None label = parse_label_response(response) return ShipmentDetails( carrier_name=settings.carrier_name, carrier_id=settings.carrier_id, label=label, tracking_number=activity.CREATE.CONNUMBER, shipment_identifier=activity.CREATE.CONREF, )
def _extract_shipment(shipment_detail_node: Element, settings: Settings) -> ShipmentDetails: detail = CompletedShipmentDetail() detail.build(shipment_detail_node) tracking_number = cast(TrackingId, detail.MasterTrackingId).TrackingNumber package: CompletedPackageDetail = next( iter(detail.CompletedPackageDetails), None) part: ShippingDocumentPart = next(iter( package.Label.Parts)) if package is not None else None label = (encodebytes(cast(ShippingDocumentPart, part).Image).decode("utf-8") if part is not None else None) return ShipmentDetails( carrier_name=settings.carrier_name, carrier_id=settings.carrier_id, tracking_number=tracking_number, label=label, )
def _extract_shipment(shipment_node, settings: Settings) -> Optional[ShipmentDetails]: tracking_number = XP.find("AirwayBillNumber", shipment_node, first=True).text label_image = XP.find("LabelImage", shipment_node, LabelImage, first=True) multilabels: List[MultiLabelType] = XP.find("MultiLabel", shipment_node, MultiLabelType) invoice = next( (item for item in multilabels if item.DocName == "CustomInvoiceImage"), None) label = encodebytes(label_image.OutputImage).decode("utf-8") meta = (dict( custom_invoice=encodebytes(invoice.DocImageVal).decode("utf-8")) if invoice is not None else None) return ShipmentDetails(carrier_name=settings.carrier_name, carrier_id=settings.carrier_id, tracking_number=tracking_number, shipment_identifier=tracking_number, label=label, meta=meta)
"payment": { "currency": "CAD", "paid_by": "sender" } } SHIPPING_CANCEL_DATA = { "shipment_identifier": "123456789012" } RETURNED_VALUE = ( ShipmentDetails( carrier_name="canadapost", carrier_id="canadapost", label="==apodifjoefr", tracking_number="123456789012", shipment_identifier="123456789012" ), [], ) RETURNED_SUCCESS_CANCEL_VALUE = ( ConfirmationDetails( carrier_name="canadapost", carrier_id="canadapost", success=True, operation="Cancel Shipment" ), [], )
}], "service": "canadapost_priority", "total_charge": 106.71, "transit_days": 2, "meta": None, "test_mode": True } CREATED_SHIPMENT_RESPONSE = (ShipmentDetails( carrier_id="canadapost", carrier_name="canadapost", label="==apodifjoefr", tracking_number="123456789012", shipment_identifier="123456789012"), []) RETURNED_CANCEL_VALUE = ( ConfirmationDetails(carrier_name="canadapost", carrier_id="canadapost", success=True, operation="Cancel Shipment"), [], ) PURCHASED_SHIPMENT = { "id": ANY, "status":