def _modify_pickup( validation_response: str, payload: PickupUpdateRequest, settings: Settings ): errors = parse_error_response(XP.to_xml(validation_response), settings) data = _modify_pickup_request(payload, settings) if len(errors) == 0 else None return Job(id="modify", data=data, fallback="")
def _create_pickup(availability_response: str, payload: PickupRequest, settings: Settings): availability = XP.build(PickupAvailabilityReply, XP.to_xml(availability_response)) data = _pickup_request(payload, settings) if availability else None return Job(id="create_pickup", data=data, fallback="")
def _create_label_request(shipment_response: str) -> Job: activity = XP.build(document, XP.to_xml(shipment_response)) fallback = shipment_response if activity is None else None data = (create_label_request(activity, payload, settings) if activity is None else None) return Job(id='create', data=data, fallback=fallback)
def _create_shipment(validate_response: str, payload: ShipmentRequest, settings: Settings) -> Job: errors = parse_error_response(XP.to_xml(validate_response), settings) valid = len(errors) == 0 return Job( id="create", data=_shipment_request(payload, settings) if valid else None, fallback=(validate_response if not valid else None), )
def _create_pickup(cancel_response: str, payload: PickupUpdateRequest, settings: Settings) -> Job: errors = parse_error_response(XP.to_xml(cancel_response), settings) canceled = len(errors) == 0 data: Optional[PickupRequest] = (pickup_request(payload, settings) if canceled else None) fallback: Optional[str] = None if canceled else '' return Job(id='schedule', data=data, fallback=fallback)
def _get_shipment_label(create_response: str, payload: ShipmentRequest, settings: Settings) -> Job: response = XP.to_xml(create_response) valid = len(parse_error_response(response, settings)) == 0 shipment_pin = (getattr(XP.find("ShipmentPIN", response, PIN, first=True), 'Value', None) if valid else None) data = (get_shipping_documents_request(shipment_pin, payload, settings) if valid else None) return Job(id="document", data=data, fallback="")
def _create_pickup(availability_response: str, payload: PickupRequest, settings: Settings): availability = XP.build(pickup_availability, XP.to_xml(availability_response)) data = _create_pickup_request( payload, settings) if availability.on_demand_tour else None return Job(id="create_pickup", data=data, fallback="" if data is None else "")
def _get_pickup(update_response: str, payload: PickupUpdateRequest, settings: Settings) -> Job: errors = parse_error_response(XP.to_xml(XP.bundle_xml([update_response])), settings) data = None if any( errors ) else f"/enab/{settings.customer_number}/pickuprequest/{payload.confirmation_number}/details" return Job(id="get_pickup", data=Serializable(data), fallback="" if data is None else "")
def _get_shipment_label(shipement_response: str) -> Job: links = XP.to_xml(shipement_response).xpath(".//*[local-name() = $name]", name="link") href, media = next( ((link.get("href"), link.get("media-type")) for link in links if link.get("rel") == "label"), (None, None), ) return Job(id="shipment_label", data=dict(href=href, media=media), fallback="")
def _get_shipment_label(shipement_response: str) -> Job: response = XP.to_xml(shipement_response) has_errors = len( response.xpath(".//*[local-name() = $name]", name="message")) > 0 links = response.xpath(".//*[local-name() = $name]", name="link") href, media = next( ((link.get("href"), link.get("media-type")) for link in links if link.get("rel") == "label"), (None, None), ) data = None if has_errors else dict(href=href, media=media) return Job(id="shipment_label", data=data, fallback="")
def _get_label(shipment_response: str, settings: Settings) -> Job: response = XP.to_xml(shipment_response) shipment = XP.to_object( Shipment, next(iter(response.xpath(".//*[local-name() = $name]", name="shipment")), None) ) success = (shipment is not None and shipment.id is not None) data = ( get_label_request(LabelRequest(shipment_id=shipment.id), settings) if success else None ) return Job(id="get_label", data=data, fallback=("" if not success else None))
def _get_shipment_label(create_response: str, payload: ShipmentRequest, settings: Settings) -> Job: errors = parse_error_response(XP.to_xml(create_response), settings) valid = len(errors) == 0 shipment_pin = None if valid: node = next( iter( XP.to_xml(create_response).xpath(".//*[local-name() = $name]", name="ShipmentPIN")), None, ) pin = PIN() pin.build(node) shipment_pin = pin.Value return Job( id="document", data=(get_shipping_documents_request(shipment_pin, payload, settings) if valid else None), fallback="", )
def _cancel_pickup_request(response: str, payload: PickupUpdateRequest, settings: Settings): reply = next( iter( XP.to_xml(response).xpath(".//*[local-name() = $name]", name="PickupCreationResponse")), None, ) new_pickup = XP.to_object(PickupCreationResponse, reply) data = (pickup_cancel_request( PickupCancelRequest(confirmation_number=payload.confirmation_number), settings, ) if new_pickup is not None and new_pickup.PRN is not None else None) return Job(id="cancel_pickup", data=data, fallback="")
def _refund_if_submitted(shipment_details: str): shipment = XP.build(ShipmentInfoType, XP.to_xml(shipment_details)) transmitted = shipment.shipment_status == 'transmitted' data = dict( id=payload.shipment_identifier, payload=Serializable( ShipmentRefundRequestType( email=payload.options.get('email')), lambda request: XP. export(request, name_='shipment-refund-request', namespacedef_= 'xmlns="http://www.canadapost.ca/ws/shipment-v8"')) ) if transmitted else None return Job(id="refund", data=data, fallback=shipment_details)
def _cancel_pickup_request(response: str, payload: PickupUpdateRequest, settings: Settings): reply = next( iter( XP.to_xml(response).xpath(".//*[local-name() = $name]", name="CreatePickupReply")), None, ) new_pickup = XP.build(CreatePickupReply, reply) data = (pickup_cancel_request( PickupCancelRequest(confirmation_number=payload.confirmation_number), settings, ) if new_pickup is not None and new_pickup.HighestSeverity == NotificationSeverityType.SUCCESS.value else None) return Job(id="cancel_pickup", data=data, fallback="")
def create_shipment( self, request: Serializable[Envelope]) -> Deserializable[str]: response = self._send_request( soapaction="http://www.icscourier.ca/CreateShipment", request=request, ) labelURL = XP.find(XP.to_xml(response), "PackageIDAndLink", first=True) if labelURL is not None: label = http( labelURL, decoder=lambda b: base64.encodebytes(b).decode("utf-8")) responses = [f'<label>{label}</label>', response] return Deserializable(XP.bundle_xml(responses), XP.to_xml) return Deserializable(response, XP.to_xml)
def _cancel_pickup_request( response: str, payload: PickupUpdateRequest, settings: Settings ): new_pickup = XP.find( "PickupCreationResponse", XP.to_xml(response), PickupCreationResponse, first=True, ) data = ( pickup_cancel_request( PickupCancelRequest(confirmation_number=payload.confirmation_number), settings, ) if new_pickup is not None and new_pickup.PRN is not None else None ) return Job(id="cancel_pickup", data=data, fallback="")
def _create_pickup(rate_response: str, payload: PickupRequest, settings: Settings): rate_result = XP.to_object(RateResultType, XP.to_xml(rate_response)) data = _create_pickup_request(payload, settings) if rate_result else None return Job(id="create_pickup", data=data, fallback="")
def _cancel_other_wise(previous_job_response: str): response: Element = XP.to_xml(previous_job_response) refunded = (response.tag == 'shipment-refund-request-info') data = identifier if not refunded else None return Job(id="cancel", data=data)