Skip to content

How To Use the Final Mile API - get_pdf_images() #3

@josiahcarlson

Description

@josiahcarlson

def get_pdf_images(
    pro: str,
    which: str,
    scac_or_carrier_id: Union[str, int] = "LN",
    test_output: bool = False,
):
    full_url = (
        url.format(
            method=which,
            api_key=get_api_key(scac_or_carrier_id),
            pro=pro,
            scac=scac_or_carrier_id,
        )  # providing dl=1 flag ensures we get content-disposition response header
        + "&dl=1"
    )

    with urllib.request.urlopen(full_url) as resp:
        rr = resp.read()
        filename_header = resp.headers["content-disposition"]

    if not filename_header:
        return json.loads(rr.decode())

    filename = filename_header.partition("=")[-1].strip('"')
    if test_output:
        assert filename.endswith(".pdf"), (
            "File should be a pdf, not a: " + filename.rpartition(".")[-1]
        )
        assert b"PDF" in rr[:4], "File does not seem to be a pdf"

    # should be <pro>_lading.pdf or <pro>_proof.pdf
    with open(filename, "wb") as out:
        out.write(rr)

    return filename

Metadata

Metadata

Assignees

No one assigned

    Labels

    documentationImprovements or additions to documentationinformationCode examples as-used in the docs

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions