def _schedule_pickup(validation_response: str, payload: PickupRequest,
                     settings: Settings):
    errors = parse_error_response(XP.to_xml(validation_response), settings)
    data = _schedule_pickup_request(payload,
                                    settings) if len(errors) == 0 else None

    return Job(id="schedule", data=data, fallback="")
Exemple #2
0
def _get_shipment_label(create_response: str, payload: ShipmentRequest, settings: Settings) -> Job:
    errors = parse_error_response(to_xml(create_response), settings)
    valid = len(errors) == 0
    shipment_pin = None

    if valid:
        node = next(
            iter(
                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).serialize() if valid else None
        ),
        fallback="",
    )
Exemple #3
0
def parse_address_validation_response(
        response: Element,
        settings: Settings) -> Tuple[AddressValidationDetails, List[Message]]:
    errors = parse_error_response(response, settings)
    reply = XP.to_object(
        ValidateCityPostalCodeZipResponse,
        next(
            iter(
                response.xpath(".//*[local-name() = $name]",
                               name="ValidateCityPostalCodeZipResponse")),
            None))
    address: ShortAddress = next(
        (result.Address
         for result in reply.SuggestedAddresses.SuggestedAddress), None)
    success = len(errors) == 0
    validation_details = AddressValidationDetails(
        carrier_id=settings.carrier_id,
        carrier_name=settings.carrier_name,
        success=success,
        complete_address=Address(city=address.City,
                                 state_code=address.Province,
                                 country_code=address.Country,
                                 postal_code=address.PostalCode)
        if address is not None else None) if success else None

    return validation_details, errors
Exemple #4
0
def parse_full_estimate_response(
    response: Element, settings: Settings
) -> Tuple[List[RateDetails], List[Message]]:
    estimates = response.xpath(".//*[local-name() = $name]", name="ShipmentEstimate")
    return (
        [_extract_rate(node, settings) for node in estimates],
        parse_error_response(response, settings),
    )
Exemple #5
0
def parse_rate_response(
        response: Element,
        settings: Settings) -> Tuple[List[RateDetails], List[Message]]:
    estimates = XP.find("ShipmentEstimate", response)
    return (
        [_extract_rate(node, settings) for node in estimates],
        parse_error_response(response, settings),
    )
Exemple #6
0
def parse_shipment_response(
        response: Element,
        settings: Settings) -> Tuple[ShipmentDetails, List[Message]]:
    pin = XP.find("ShipmentPIN", response, PIN, first=True)
    shipment = (_extract_shipment(response, settings) if
                (getattr(pin, 'Value', None) is not None) else None)

    return shipment, parse_error_response(response, settings)
Exemple #7
0
def _create_shipment(validate_response: str, payload: ShipmentRequest, settings: Settings) -> Job:
    errors = parse_error_response(to_xml(validate_response), settings)
    valid = len(errors) == 0
    return Job(
        id="create",
        data=create_shipping_request(payload, settings).serialize() if valid else None,
        fallback=(validate_response if not valid else None),
    )
Exemple #8
0
def parse_track_package_response(
        response: Element,
        settings: Settings) -> Tuple[List[TrackingDetails], List[Message]]:
    track_infos = response.xpath(".//*[local-name() = $name]",
                                 name="TrackingInformation")
    return (
        [_extract_tracking(node, settings) for node in track_infos],
        parse_error_response(response, settings),
    )
Exemple #9
0
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 parse_pickup_response(
        response: Element,
        settings: Settings) -> Tuple[PickupDetails, List[Message]]:
    reply = XP.find("SchedulePickUpResponse",
                    response,
                    SchedulePickUpResponse,
                    first=True)
    pickup = (_extract_pickup_details(reply, settings) if reply is not None
              and reply.PickUpConfirmationNumber is not None else None)

    return pickup, parse_error_response(response, settings)
Exemple #11
0
def parse_shipment_creation_response(
    response: Element, settings: Settings
) -> Tuple[ShipmentDetails, List[Message]]:
    details = next(
        iter(
            response.xpath(".//*[local-name() = $name]", name="CreateShipmentResponse")
        ),
        None,
    )
    shipment = _extract_shipment(response, settings) if details is not None else None
    return shipment, parse_error_response(response, settings)
Exemple #12
0
def parse_pickup_cancel_response(
        response: Element,
        settings: Settings) -> Tuple[ConfirmationDetails, List[Message]]:
    errors = parse_error_response(response, settings)
    cancellation = (ConfirmationDetails(
        carrier_id=settings.carrier_id,
        carrier_name=settings.carrier_name,
        success=True,
        operation="Cancel Pickup",
    ) if not any(errors) else None)

    return cancellation, errors
Exemple #13
0
def parse_shipment_cancel_response(
        response: Element,
        settings: Settings) -> Tuple[ConfirmationDetails, List[Message]]:
    void_response = XP.find("VoidShipmentResponse",
                            response,
                            VoidShipmentResponse,
                            first=True)
    voided = void_response is not None and void_response.ShipmentVoided
    cancellation = ConfirmationDetails(
        carrier_id=settings.carrier_id,
        carrier_name=settings.carrier_name,
        success=True,
        operation="Cancel Shipment",
    ) if voided else None

    return cancellation, parse_error_response(response, settings)