Skip to content

Commit

Permalink
Merge pull request #10 from coldbrewcloud/update_2017_03_28
Browse files Browse the repository at this point in the history
Version 2017-03-29
  • Loading branch information
d5 authored Apr 23, 2017
2 parents 2b94802 + a3a6c88 commit 2849298
Show file tree
Hide file tree
Showing 22 changed files with 266 additions and 260 deletions.
126 changes: 30 additions & 96 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package main

import (
"encoding/json"
"errors"
"fmt"
"os"

Expand All @@ -16,97 +17,33 @@ import (
"github.com/coldbrewcloud/go-shippo/models"
)

var (
privateToken = os.Getenv("PRIVATE_TOKEN")
upsUserName = os.Getenv("UPS_USERNAME")
upsPassword = os.Getenv("UPS_PASSWORD")
upsAccountNumber = os.Getenv("UPS_ACCOUNT_NUMBER")
)

func main() {
privateToken := os.Getenv("PRIVATE_TOKEN")
if privateToken == "" {
panic(errors.New("Please set $PRIVATE_TOKEN with your Shippo API private token."))
}

// create a Shippo Client instance
c := shippo.NewClient(privateToken)

// create or update carrier account
carrierAccountObjectID := prepareCarrierAccount(c)

// create shipment using the carrier account
shipment := createShipmentUsingCarrierAccount(c, carrierAccountObjectID)
// create shipment
shipment := createShipment(c)

// purchase shipping label
purchaseShippingLabel(c, shipment)
}

func prepareCarrierAccount(c *client.Client) string {
// list all registered carrier account
allCarrierAccounts, err := c.ListAllCarrierAccounts()
if err != nil {
panic(err)
}

// create UPS carrier account if not added
carrierAccountObjectID := ""
for _, ca := range allCarrierAccounts {
if ca.Carrier == models.CarrierUPS && ca.AccountID == upsUserName {
carrierAccountObjectID = ca.ObjectID
break
}
}
if carrierAccountObjectID == "" {
// create a new carrier account
input := &models.CarrierAccountInput{
Carrier: models.CarrierUPS,
AccountID: upsUserName,
Parameters: map[string]interface{}{
"password": upsPassword,
"account_number": upsAccountNumber,
},
Test: true,
Active: true,
}
carrierAccount, err := c.CreateCarrierAccount(input)
if err != nil {
panic(err)
}

fmt.Printf("Carrier account registered: %s\n", dump(carrierAccount))
} else {
// update existing carrier account
input := &models.CarrierAccountInput{
Carrier: models.CarrierUPS,
AccountID: upsUserName,
Parameters: map[string]interface{}{
"password": upsPassword,
"account_number": upsAccountNumber,
},
Test: true,
Active: true,
}
carrierAccount, err := c.UpdateCarrierAccount(carrierAccountObjectID, input)
if err != nil {
panic(err)
}

carrierAccountObjectID = carrierAccount.ObjectID

fmt.Printf("Carrier account updated: %s\n", dump(carrierAccount))
}

return carrierAccountObjectID
}

func createShipmentUsingCarrierAccount(c *client.Client, carrierAccountObjectID string) *models.Shipment {
func createShipment(c *client.Client) *models.Shipment {
// create a sending address
addressFromInput := &models.AddressInput{
ObjectPurpose: models.ObjectPurposePurchase,
Name: "Mr. Hippo",
Street1: "215 Clayton St.",
City: "San Francisco",
State: "CA",
Zip: "94117",
Country: "US",
Phone: "+1 555 341 9393",
Email: "[email protected]",
Name: "Mr. Hippo",
Street1: "215 Clayton St.",
City: "San Francisco",
State: "CA",
Zip: "94117",
Country: "US",
Phone: "+1 555 341 9393",
Email: "[email protected]",
}
addressFrom, err := c.CreateAddress(addressFromInput)
if err != nil {
Expand All @@ -115,15 +52,14 @@ func createShipmentUsingCarrierAccount(c *client.Client, carrierAccountObjectID

// create a receiving address
addressToInput := &models.AddressInput{
ObjectPurpose: models.ObjectPurposePurchase,
Name: "Mrs. Hippo",
Street1: "965 Mission St.",
City: "San Francisco",
State: "CA",
Zip: "94105",
Country: "US",
Phone: "+1 555 341 9393",
Email: "[email protected]",
Name: "Mrs. Hippo",
Street1: "965 Mission St.",
City: "San Francisco",
State: "CA",
Zip: "94105",
Country: "US",
Phone: "+1 555 341 9393",
Email: "[email protected]",
}
addressTo, err := c.CreateAddress(addressToInput)
if err != nil {
Expand All @@ -146,12 +82,10 @@ func createShipmentUsingCarrierAccount(c *client.Client, carrierAccountObjectID

// create a shipment
shipmentInput := &models.ShipmentInput{
ObjectPurpose: models.ObjectPurposePurchase,
AddressFrom: addressFrom.ObjectID,
AddressTo: addressTo.ObjectID,
Parcel: parcel.ObjectID,
CarrierAccounts: []string{carrierAccountObjectID},
Async: false,
AddressFrom: addressFrom.ObjectID,
AddressTo: addressTo.ObjectID,
Parcels: []string{parcel.ObjectID},
Async: false,
}
shipment, err := c.CreateShipment(shipmentInput)
if err != nil {
Expand All @@ -165,7 +99,7 @@ func createShipmentUsingCarrierAccount(c *client.Client, carrierAccountObjectID

func purchaseShippingLabel(c *client.Client, shipment *models.Shipment) {
transactionInput := &models.TransactionInput{
Rate: shipment.RatesList[0].ObjectID,
Rate: shipment.Rates[len(shipment.Rates)-1].ObjectID,
LabelFileType: models.LabelFileTypePDF,
Async: false,
}
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.4.1
1.5.0
36 changes: 17 additions & 19 deletions _examples/address_validation/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,15 @@ func main() {

// Address #1: Valid
address1Input := &models.AddressInput{
Name: "Shawn Ippotle",
ObjectPurpose: models.ObjectPurposePurchase,
Company: "Shippo",
Street1: "215 Clayton St.",
City: "San Francisco",
State: "CA",
Zip: "94117",
Country: "US",
Email: "[email protected]",
Validate: true,
Name: "Shawn Ippotle",
Company: "Shippo",
Street1: "215 Clayton St.",
City: "San Francisco",
State: "CA",
Zip: "94117",
Country: "US",
Email: "[email protected]",
Validate: true,
}
address1, err := c.CreateAddress(address1Input)
if err != nil {
Expand All @@ -38,15 +37,14 @@ func main() {

// Address #2: Wrong street name
address2Input := &models.AddressInput{
Name: "Mr. Hippo",
ObjectPurpose: models.ObjectPurposePurchase,
Street1: "4610 Clayton St.",
City: "San Francisco",
State: "CA",
Zip: "94107",
Country: "US",
Email: "[email protected]",
Validate: true,
Name: "Mr. Hippo",
Street1: "4610 Clayton St.",
City: "San Francisco",
State: "CA",
Zip: "94107",
Country: "US",
Email: "[email protected]",
Validate: true,
}
address2, err := c.CreateAddress(address2Input)
if err != nil {
Expand Down
43 changes: 20 additions & 23 deletions _examples/carrier_accounts/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,15 @@ func prepareCarrierAccount(c *client.Client) string {
"password": upsPassword,
"account_number": upsAccountNumber,
},
Test: true,
Active: true,
}
carrierAccount, err := c.CreateCarrierAccount(input)
if err != nil {
panic(err)
}

carrierAccountObjectID = carrierAccount.ObjectID

fmt.Printf("Carrier account registered: %s\n", dump(carrierAccount))
} else {
// update existing carrier account
Expand All @@ -73,7 +74,6 @@ func prepareCarrierAccount(c *client.Client) string {
"password": upsPassword,
"account_number": upsAccountNumber,
},
Test: true,
Active: true,
}
carrierAccount, err := c.UpdateCarrierAccount(carrierAccountObjectID, input)
Expand All @@ -92,15 +92,14 @@ func prepareCarrierAccount(c *client.Client) string {
func createShipmentUsingCarrierAccount(c *client.Client, carrierAccountObjectID string) *models.Shipment {
// create a sending address
addressFromInput := &models.AddressInput{
ObjectPurpose: models.ObjectPurposePurchase,
Name: "Mr. Hippo",
Street1: "215 Clayton St.",
City: "San Francisco",
State: "CA",
Zip: "94117",
Country: "US",
Phone: "+1 555 341 9393",
Email: "[email protected]",
Name: "Mr. Hippo",
Street1: "215 Clayton St.",
City: "San Francisco",
State: "CA",
Zip: "94117",
Country: "US",
Phone: "+1 555 341 9393",
Email: "[email protected]",
}
addressFrom, err := c.CreateAddress(addressFromInput)
if err != nil {
Expand All @@ -109,15 +108,14 @@ func createShipmentUsingCarrierAccount(c *client.Client, carrierAccountObjectID

// create a receiving address
addressToInput := &models.AddressInput{
ObjectPurpose: models.ObjectPurposePurchase,
Name: "Mrs. Hippo",
Street1: "965 Mission St.",
City: "San Francisco",
State: "CA",
Zip: "94105",
Country: "US",
Phone: "+1 555 341 9393",
Email: "[email protected]",
Name: "Mrs. Hippo",
Street1: "965 Mission St.",
City: "San Francisco",
State: "CA",
Zip: "94105",
Country: "US",
Phone: "+1 555 341 9393",
Email: "[email protected]",
}
addressTo, err := c.CreateAddress(addressToInput)
if err != nil {
Expand All @@ -140,10 +138,9 @@ func createShipmentUsingCarrierAccount(c *client.Client, carrierAccountObjectID

// create a shipment
shipmentInput := &models.ShipmentInput{
ObjectPurpose: models.ObjectPurposePurchase,
AddressFrom: addressFrom.ObjectID,
AddressTo: addressTo.ObjectID,
Parcel: parcel.ObjectID,
Parcels: []string{parcel.ObjectID},
CarrierAccounts: []string{carrierAccountObjectID},
Async: false,
}
Expand All @@ -159,7 +156,7 @@ func createShipmentUsingCarrierAccount(c *client.Client, carrierAccountObjectID

func purchaseShippingLabel(c *client.Client, shipment *models.Shipment) {
transactionInput := &models.TransactionInput{
Rate: shipment.RatesList[0].ObjectID,
Rate: shipment.Rates[0].ObjectID,
LabelFileType: models.LabelFileTypePDF,
Async: false,
}
Expand Down
45 changes: 21 additions & 24 deletions _examples/first_shipment/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,14 @@ func main() {
func createShipment(c *client.Client) *models.Shipment {
// create a sending address
addressFromInput := &models.AddressInput{
ObjectPurpose: models.ObjectPurposePurchase,
Name: "Mr. Hippo",
Street1: "215 Clayton St.",
City: "San Francisco",
State: "CA",
Zip: "94117",
Country: "US",
Phone: "+1 555 341 9393",
Email: "[email protected]",
Name: "Mr. Hippo",
Street1: "215 Clayton St.",
City: "San Francisco",
State: "CA",
Zip: "94117",
Country: "US",
Phone: "+1 555 341 9393",
Email: "[email protected]",
}
addressFrom, err := c.CreateAddress(addressFromInput)
if err != nil {
Expand All @@ -47,15 +46,14 @@ func createShipment(c *client.Client) *models.Shipment {

// create a receiving address
addressToInput := &models.AddressInput{
ObjectPurpose: models.ObjectPurposePurchase,
Name: "Mrs. Hippo",
Street1: "965 Mission St.",
City: "San Francisco",
State: "CA",
Zip: "94105",
Country: "US",
Phone: "+1 555 341 9393",
Email: "[email protected]",
Name: "Mrs. Hippo",
Street1: "965 Mission St.",
City: "San Francisco",
State: "CA",
Zip: "94105",
Country: "US",
Phone: "+1 555 341 9393",
Email: "[email protected]",
}
addressTo, err := c.CreateAddress(addressToInput)
if err != nil {
Expand All @@ -78,11 +76,10 @@ func createShipment(c *client.Client) *models.Shipment {

// create a shipment
shipmentInput := &models.ShipmentInput{
ObjectPurpose: models.ObjectPurposePurchase,
AddressFrom: addressFrom.ObjectID,
AddressTo: addressTo.ObjectID,
Parcel: parcel.ObjectID,
Async: false,
AddressFrom: addressFrom.ObjectID,
AddressTo: addressTo.ObjectID,
Parcels: []string{parcel.ObjectID},
Async: false,
}
shipment, err := c.CreateShipment(shipmentInput)
if err != nil {
Expand All @@ -96,7 +93,7 @@ func createShipment(c *client.Client) *models.Shipment {

func purchaseShippingLabel(c *client.Client, shipment *models.Shipment) {
transactionInput := &models.TransactionInput{
Rate: shipment.RatesList[len(shipment.RatesList)-1].ObjectID,
Rate: shipment.Rates[len(shipment.Rates)-1].ObjectID,
LabelFileType: models.LabelFileTypePDF,
Async: false,
}
Expand Down
Loading

0 comments on commit 2849298

Please sign in to comment.