How to use the easypost.Batch.create_and_buy function in easypost

To help you get started, we’ve selected a few easypost examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github EasyPost / easypost-python / tests / test_batch.py View on Github external
}]

    shipments = []

    for order in order_list:
        shipments.append({
            'to_address': order['address'],
            'from_address': from_address,
            'parcel': parcel,
            'reference': order['order_number'],
            'carrier': 'USPS',
            'service': 'Priority'
        })

    # create batch of shipments
    batch = easypost.Batch.create_and_buy(shipment=shipments)
    assert batch.num_shipments == 1

    # Poll while waiting for the batch to purchase the shipments
    while batch.state in ('creating', 'queued_for_purchase', 'purchasing'):
        sleep(1)
        batch.refresh()

    # Insure the shipments after purchase
    if batch.state == 'purchased':
        for shipment in batch.shipments:
            shipment.insure(amount=100)

    assert len(batch.shipments) == 1
    assert batch.shipments[0].batch_status == 'postage_purchased'
    assert batch.shipments[0].buyer_address.city == 'San Francisco'
    assert batch.shipments[0].buyer_address.country == 'US'
github EasyPost / easypost-python / tests / test_pickup.py View on Github external
'company': 'EasyPost',
                'street1': '164 Townsend St',
                'city': 'San Francisco',
                'state': 'CA',
                'zip': '94107',
                'phone': '415-456-7890'
            },
            'parcel': {
                'weight': 10.2
            },
            'carrier': 'USPS',
            'service': 'Priority'
        }
    ]

    batch = easypost.Batch.create_and_buy(shipments=shipments)
    while batch.state in ('creating', 'queued_for_purchase', 'purchasing'):
        time.sleep(0.1)
        batch.refresh()

    # Insure the shipments after purchase
    if batch.state == 'purchased':
        for shipment in batch.shipments:
            shipment.insure(amount=100)

    pickup = easypost.Pickup.create(
        address=pickup_address,
        batch=batch,
        reference='internal_id_1234',
        min_datetime=noon_on_next_monday.isoformat(),
        max_datetime=(noon_on_next_monday + ONE_DAY).isoformat(),
        is_account_address=True,
github EasyPost / easypost-python / examples / pickup_batch.py View on Github external
'company': "EasyPost",
            'street1': "164 Townsend St",
            'city': "San Francisco",
            'state': "CA",
            'zip': "94107",
            'phone': "415-456-7890"
        },
        'parcel': {
            'weight': 21.2
        },
        'carrier': "UPS",
        'service': "NextDayAir"
    }
]

batch = easypost.Batch.create_and_buy(shipments=shipments)
while batch.state in ("creating", "queued_for_purchase", "purchasing"):
    print(batch.state)
    time.sleep(5)
    batch.refresh()

# Insure the shipments after purchase
if batch.state == "purchased":
    for shipment in batch.shipments:
        shipment.insure(amount=100)

pickup = easypost.Pickup.create(
    address={
        'name': "Sawyer Bateman",
        'company': "EasyPost",
        'street1': "164 Townsend St",
        'city': "San Francisco",
github EasyPost / easypost-python / examples / batch.py View on Github external
}]

shipments = []

for order in order_list:
    shipments.append({
        'to_address': order['address'],
        'from_address': from_address,
        'parcel': parcel,
        'reference': order['order_number'],
        'carrier': 'USPS',
        'service': 'Priority'
    })

# create batch of shipments
batch = easypost.Batch.create_and_buy(shipment=shipments)

# Poll while waiting for the batch to purchase the shipments
while batch.state in ("creating", "queued_for_purchase", "purchasing"):
    sleep(5)
    batch.refresh()
    print(batch.state)

# Insure the shipments after purchase
if batch.state == "purchased":
    for shipment in batch.shipments:
        shipment.insure(amount=100)

###### below here should probably be in a seperate script so that there's no risk of re-running create_and_buy

# # run this to check on the batch status after it's been created
# # postage is purchased asyncronously, so you'll have to poll until it's done