Skip to content

Commit

Permalink
Merge pull request #65 from goshippo/master
Browse files Browse the repository at this point in the history
v3.1.0 - Add pickup and order (#64)
  • Loading branch information
sam-allen-shippo authored Feb 1, 2022
2 parents 04b5db7 + 5b315c7 commit e4799fb
Show file tree
Hide file tree
Showing 16 changed files with 1,273 additions and 36 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
=== 3.1.0
- Added pickups
- Added orders

=== 1.3.0

- Added support for tracks/ and batches/ endpoint
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ Below is a brief code example:
Testing
=======

You must have Maven installed. To run the tests, simply run `mvn test`. You can run particular tests by passing `-D test=Class#method` -- for example, `-D test=ShippoTest#testAddressCreate`.
You must have Maven installed. To run the tests, simply run `mvn test`. You can run particular tests by passing `-D test=Class#method` -- for example, `-D test=ShippoTest#testAddressCreate` or for an entire file `-D test=AddressTest`.

You can also run the tests in eclipse.

Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.0.5
3.1.0
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.goshippo</groupId>
<artifactId>shippo-java-client</artifactId>
<version>3.0.5</version>
<version>3.1.0</version>
<packaging>jar</packaging>
<name>Shippo Java Client</name>
<description>Shippo Java Bindings</description>
Expand Down Expand Up @@ -31,7 +31,7 @@
<connection>scm:git:[email protected]:goshippo/shippo-java-clienta.git</connection>
<developerConnection>scm:git:[email protected]:goshippo/shippo-java-client.git</developerConnection>
<url>[email protected]:goshippo/shippo-java-client.git</url>
<tag>v3.0.4</tag>
<tag>v3.1.0</tag>
</scm>
<dependencies>
<dependency>
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/shippo/Example.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public static void main(String[] args) throws ShippoException {
// replace with your Shippo Token
// don't have one? get more info here (https://goshippo.com/docs/#overview)
Shippo.setApiKey("<API-KEY>");
Shippo.setApiVersion("2018-02-08");

// Optional defaults to false
//Shippo.setDEBUG(true);
Expand Down
217 changes: 217 additions & 0 deletions src/main/java/com/shippo/ExampleBatchShipment.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
package com.shippo;

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

import com.shippo.exception.ShippoException;
import com.shippo.model.Address;
import com.shippo.model.Batch;
import com.shippo.model.Batch.BatchShipmentCollection;
import com.shippo.model.Batch.BatchStatus;
import com.shippo.model.BatchShipment;
import com.shippo.model.LabelFileType;
import com.shippo.model.Parcel;
import com.shippo.model.Shipment;

public class ExampleBatchShipment {

public static void main(String[] args) throws ShippoException {

// replace with your Shippo Token
// don't have one? get more info here (https://goshippo.com/docs/#overview)
Shippo.setApiKey("<API-KEY>");
Shippo.setApiVersion("2018-02-08");
// default carrier account
String defaultCarrierAccount = "<YOUR CARRIER ACCOUNT PRIVATE KEY>";

// Optional defaults to false
// Shippo.setDEBUG(true);

// to address
Map<String, Object> toAddressMap = new HashMap<String, Object>();
toAddressMap.put("name", "Mr Hippo");
toAddressMap.put("company", "Shippo");
toAddressMap.put("street1", "215 Clayton St.");
toAddressMap.put("city", "San Francisco");
toAddressMap.put("state", "CA");
toAddressMap.put("zip", "94117");
toAddressMap.put("country", "US");
toAddressMap.put("phone", "+1 555 341 9393");
toAddressMap.put("email", "[email protected]");
Address toAddress = Address.create(toAddressMap);

// from address
Map<String, Object> fromAddressMap = new HashMap<String, Object>();
fromAddressMap.put("name", "Ms Hippo");
fromAddressMap.put("company", "San Diego Zoo");
fromAddressMap.put("street1", "2920 Zoo Drive");
fromAddressMap.put("city", "San Diego");
fromAddressMap.put("state", "CA");
fromAddressMap.put("zip", "92101");
fromAddressMap.put("country", "US");
fromAddressMap.put("email", "[email protected]");
fromAddressMap.put("phone", "+1 619 231 1515");
fromAddressMap.put("metadata", "Customer ID 123456");
Address fromAddress = Address.create(fromAddressMap);

// parcel
Map<String, Object> parcelMap = new HashMap<String, Object>();
parcelMap.put("length", "5");
parcelMap.put("width", "5");
parcelMap.put("height", "5");
parcelMap.put("distance_unit", "in");
parcelMap.put("weight", "2");
parcelMap.put("mass_unit", "lb");
Parcel parcel = Parcel.create(parcelMap);
List<Parcel> parcels = new ArrayList<Parcel>();
parcels.add(parcel);

// shipment
Shipment firstShipment = Shipment.createForBatch(fromAddress, toAddress, parcels);
BatchShipment[] batchShipments = { BatchShipment.createForShipment(firstShipment, null, null) };

// batch shipment
String serviceLevelToken = "usps_priority";
System.out.println("Creating first shipment..");
Batch batch = Batch.create(defaultCarrierAccount, serviceLevelToken, LabelFileType.PDF, null, batchShipments);
if (batch.getStatus().equals(BatchStatus.VALID) || batch.getStatus().equals(BatchStatus.VALIDATING)) {
System.out.println(String.format("Batch created with id: %s", batch.getId()));
} else {
System.out.println(String.format("An Error has occured while creating the first batch shipment."));
System.exit(0);
}

// 2nd to address
Map<String, Object> toAddress2Map = new HashMap<String, Object>();
toAddress2Map.put("name", "Mrs Hippo");
toAddress2Map.put("company", "");
toAddress2Map.put("street1", "Broadway 1");
toAddress2Map.put("city", "New York");
toAddress2Map.put("state", "NY");
toAddress2Map.put("zip", "10007");
toAddress2Map.put("country", "US");
toAddress2Map.put("phone", "4151234567");
toAddress2Map.put("email", "[email protected]");

// 2nd from address
Map<String, Object> fromAddress2Map = new HashMap<String, Object>();
fromAddress2Map.put("name", "Mr Hippo");
fromAddress2Map.put("company", "");
fromAddress2Map.put("street1", "965 Mission St");
fromAddress2Map.put("street2", "Ste 201");
fromAddress2Map.put("city", "San Francisco");
fromAddress2Map.put("state", "CA");
fromAddress2Map.put("zip", "94103");
fromAddress2Map.put("country", "US");
fromAddress2Map.put("email", "[email protected]");
fromAddress2Map.put("phone", "4151234567");

// 2nd parcel
Map<String, Object> parcel2Map = new HashMap<String, Object>();
parcel2Map.put("length", "5");
parcel2Map.put("width", "5");
parcel2Map.put("height", "5");
parcel2Map.put("distance_unit", "in");
parcel2Map.put("weight", "15");
parcel2Map.put("mass_unit", "oz");
List<Map<String, Object>> parcels2 = new ArrayList<Map<String, Object>>();
parcels2.add(parcel2Map);

// 2nd shipment
Map<String, Object> secondShipmentMap = new HashMap<String, Object>();
secondShipmentMap.put("address_to", toAddressMap);
secondShipmentMap.put("address_from", fromAddressMap);
secondShipmentMap.put("parcels", parcels2);
Shipment shipment2 = Shipment.create(secondShipmentMap);

if (shipment2.getStatus().equals("SUCCESS")) {
System.out.println(String.format("Shipment created with id: %s", shipment2.getObjectId()));
} else {
System.out.println(String.format("An Error has occured while creating the 2nd shipment. Messages : %s",
shipment2.getMessages()));
System.exit(0);
}

// Example of retrieving a batch object to check its validation status
// For complete reference to the retrieve endpoint:
// https://goshippo.com/docs/reference#batches-retrieve
// This method of polling the batch validation status is for demo purposes only
// In practice it is advised to use the batch-create webhook in the user api
// dashboard: https://app.goshippo.com/api/
int maxTimeout = 10;
int counter = 0;
while (counter < maxTimeout) {
Batch retrievedBatch = Batch.get(batch.getId(), 0, null);
if (retrievedBatch.getStatus().equals(BatchStatus.VALID)) {
break;
} else {
counter++;
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
Batch retrievedBatch2 = Batch.get(batch.getId(), 0, null);
if (retrievedBatch2.getStatus().equals(BatchStatus.VALID)) {
System.out.println(String.format("Batch object %s has status %s ", retrievedBatch2.getId(),
retrievedBatch2.getStatus()));
} else {
System.out.println(String.format("An Error has occured validating the batch."));
System.exit(0);
}

// example adding a shipment to batch
String[] shipmentsToAdd = { shipment2.getObjectId() };
Batch addedShipment = Batch.addShipments(batch.getId(), shipmentsToAdd);
if (addedShipment.getStatus().equals(BatchStatus.VALID)) {
System.out.println(
String.format("Batch now contains shipments : %s ", addedShipment.getBatchShipments().toString()));
} else {
System.out.println(String.format("An Error has occured adding a shipment."));
System.exit(0);
}

// example removing a shipment from batch
BatchShipmentCollection batchCollection = addedShipment.getBatchShipments();
BatchShipment[] batchShipment = batchCollection.getShipments();
String[] shipmentsToRemove = { batchShipment[0].getId().toString() };
Batch removedShipment = Batch.removeShipments(batch.getId(), shipmentsToRemove);
if (removedShipment.getStatus().equals(BatchStatus.VALID)) {
System.out.println(String.format("Batch now contains shipments : %s ",
removedShipment.getBatchShipments().toString()));
} else {
System.out.println(String.format("An Error has occured removing a shipment."));
System.exit(0);
}

// purchase batch shipment
Batch.purchase(batch.getId());
while (counter < maxTimeout) {
Batch retrievedBatch3 = Batch.get(batch.getId(), 0, null);
if (retrievedBatch3.getStatus().equals(BatchStatus.PURCHASED)) {
break;
} else {
counter++;
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

Batch retrievedBatch4 = Batch.get(batch.getId(), 0, null);
if (retrievedBatch4.getStatus().equals(BatchStatus.PURCHASED)) {
System.out.println(String.format("Batch object %s has status %s ", retrievedBatch4.getId(),
retrievedBatch4.getStatus()));
} else {
System.out.println(String.format("An Error has occured purchasing the batch shipment."));
System.exit(0);
}
}
}
86 changes: 86 additions & 0 deletions src/main/java/com/shippo/ExampleOrder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package com.shippo;

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

import com.shippo.exception.ShippoException;
import com.shippo.model.Order;

public class ExampleOrder {

public static void main(String[] args) throws ShippoException {

// replace with your Shippo Token
// don't have one? get more info here (https://goshippo.com/docs/#overview)
Shippo.setApiKey("<API-KEY>");
Shippo.setApiVersion("2018-02-08");

// Optional defaults to false
// Shippo.setDEBUG(true);

// to address
Map<String, Object> toAddressMap = new HashMap<String, Object>();
toAddressMap.put("name", "Mr Hippo");
toAddressMap.put("company", "Regents Park");
toAddressMap.put("street1", "Outer Cir");
toAddressMap.put("city", "London");
toAddressMap.put("state", null);
toAddressMap.put("zip", "NW1 4RY");
toAddressMap.put("country", "GB");
toAddressMap.put("phone", "+1 555 341 9393");
toAddressMap.put("email", "[email protected]");
toAddressMap.put("is_residential", false);
toAddressMap.put("metadata", "For Order Number 123");

// from address
Map<String, Object> fromAddressMap = new HashMap<String, Object>();
fromAddressMap.put("name", "Ms Hippo");
fromAddressMap.put("company", "San Diego Zoo");
fromAddressMap.put("street1", "2920 Zoo Drive");
fromAddressMap.put("city", "San Diego");
fromAddressMap.put("state", "CA");
fromAddressMap.put("zip", "92101");
fromAddressMap.put("country", "US");
fromAddressMap.put("email", "[email protected]");
fromAddressMap.put("phone", "+1 619 231 1515");
fromAddressMap.put("metadata", "Customer ID 123456");

// parcel
Map<String, Object> lineItem = new HashMap<String, Object>();
lineItem.put("title", "Demo Line Item Object");
lineItem.put("sku", "demo_1234");
lineItem.put("quantity", 2);
lineItem.put("total_price", 2.34);
lineItem.put("currency", "USD");
lineItem.put("weight", 25.45);
lineItem.put("weight_unit", "lb");
lineItem.put("manufacture_country", "US");
List<Map<String, Object>> lineItems = new ArrayList<Map<String, Object>>();
lineItems.add(lineItem);

LocalDateTime now = LocalDateTime.now();
Map<String, Object> orderParams = new HashMap<String, Object>();
orderParams.put("name", "Ms Hippo");
orderParams.put("order_number", now.toString());
orderParams.put("order_status", "PAID");
orderParams.put("to_address", toAddressMap);
orderParams.put("from_address", fromAddressMap);
orderParams.put("line_items", lineItems);
orderParams.put("placed_at", String.join("", now.toString(), "Z"));
orderParams.put("weight", 10.0);
orderParams.put("weight_unit", "lb");
orderParams.put("shipping_method", "ground");
orderParams.put("shipping_cost", 1.23);
orderParams.put("shipping_cost_currency", "USD");
orderParams.put("subtotal_price", 2.34);
orderParams.put("total_price", 6.14);
orderParams.put("total_tax", 2.57);
orderParams.put("currency", "USD");

Order order = Order.create(orderParams);
System.out.println(String.format("An order has been created: %s", order.getObjectId()));
}
}
Loading

0 comments on commit e4799fb

Please sign in to comment.