Esempio n. 1
0
def _create_shipment(payload: ShipmentRequest, settings: Settings) -> Job:
    no_contract = settings.contract_id is None or settings.contract_id == ""
    create_shipment = (non_contract_shipment_request
                       if no_contract else contract_shipment_request)
    return Job(
        id="non_contract_shipment" if no_contract else "contract_shipment",
        data=create_shipment(payload, settings))
Esempio n. 2
0
def _validate_shipment(payload: ShipmentRequest, settings: Settings) -> Job:
    return Job(
        id="validate",
        data=create_shipping_request(payload=payload,
                                     settings=settings,
                                     validate=True).serialize(),
    )
Esempio n. 3
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="",
    )
Esempio n. 4
0
def _get_shipment_label(shipement_response: str) -> Job:
    links = to_xml(shipement_response).xpath(".//*[local-name() = $name]",
                                             name="link")
    label_url = next(
        (link.get("href") for link in links if link.get("rel") == "label"),
        None,
    )
    return Job(id="shipment_label", data=label_url, fallback="")
Esempio n. 5
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),
    )
Esempio n. 6
0
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="")
Esempio n. 7
0
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="")