All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.easypost.app.BatchManifestExample Maven / Gradle / Ivy

The newest version!
package com.easypost.app;

import com.easypost.EasyPost;
import com.easypost.exception.EasyPostException;
import com.easypost.model.Batch;
import com.easypost.model.Shipment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BatchManifestExample {

    public static void main(final String[] args) throws InterruptedException {
        EasyPost.apiKey = System.getenv("EASYPOST_API_KEY");

        try {
            Map addressMap = new HashMap();
            addressMap.put("name", "Simpler Postage Inc");
            addressMap.put("street1", "164 Townsend St");
            addressMap.put("street2", "Unit 1");
            addressMap.put("city", "San Francisco");
            addressMap.put("state", "CA");
            addressMap.put("zip", "94107");
            addressMap.put("phone", "415-456-7890");

            Map parcelMap = new HashMap();
            parcelMap.put("weight", 22.9);
            parcelMap.put("height", 12.1);
            parcelMap.put("width", 8);
            parcelMap.put("length", 19.8);

            // create two shipments
            Map shipmentMap = new HashMap();
            shipmentMap.put("to_address", addressMap);
            shipmentMap.put("from_address", addressMap);
            shipmentMap.put("parcel", parcelMap);
            Shipment shipment1 = Shipment.create(shipmentMap);
            Shipment shipment2 = Shipment.create(shipmentMap);

            // create batch
            Batch batch = Batch.create();

            // wait until batch is created asynchronously
            while (true) {
                batch = batch.refresh();

                if (batch.getState().equals("created")) {
                    break;
                }

                Thread.sleep(5000);
            }

            // add shipments to batch
            List shipments = new ArrayList();
            shipments.add(shipment1);
            shipments.add(shipment2);
            batch.addShipments(shipments);

            // create manifest
            batch.createScanForm();

            // batch label creation is asyncronou
            while (true) {
                batch = batch.refresh();

                if (batch.getScanForm() != null) {
                    break;
                }

                Thread.sleep(5000);
            }

            System.out.println(batch.prettyPrint());

        } catch (EasyPostException e) {
            e.printStackTrace();
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy