GithubHelp home page GithubHelp logo

yuderekyu / go-shippo Goto Github PK

View Code? Open in Web Editor NEW

This project forked from coldbrewcloud/go-shippo

0.0 1.0 0.0 58 KB

Shippo API - Golang wrapper

Home Page: https://goshippo.com/

Makefile 0.61% Go 99.39%

go-shippo's Introduction

Shippo API Golang Wrapper

Documentation: https://godoc.org/github.com/coldbrewcloud/go-shippo/client

Examples

package main

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

	"github.com/coldbrewcloud/go-shippo"
	"github.com/coldbrewcloud/go-shippo/client"
	"github.com/coldbrewcloud/go-shippo/models"
)

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 shipment
	shipment := createShipment(c)

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

func createShipment(c *client.Client) *models.Shipment {
	// create a sending address
	addressFromInput := &models.AddressInput{
		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 {
		panic(err)
	}

	// create a receiving address
	addressToInput := &models.AddressInput{
		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 {
		panic(err)
	}

	// create a parcel
	parcelInput := &models.ParcelInput{
		Length:       "5",
		Width:        "5",
		Height:       "5",
		DistanceUnit: models.DistanceUnitInch,
		Weight:       "2",
		MassUnit:     models.MassUnitPound,
	}
	parcel, err := c.CreateParcel(parcelInput)
	if err != nil {
		panic(err)
	}

	// create a shipment
	shipmentInput := &models.ShipmentInput{
		AddressFrom: addressFrom.ObjectID,
		AddressTo:   addressTo.ObjectID,
		Parcels:     []string{parcel.ObjectID},
		Async:       false,
	}
	shipment, err := c.CreateShipment(shipmentInput)
	if err != nil {
		panic(err)
	}

	fmt.Printf("Shipment:\n%s\n", dump(shipment))

	return shipment
}

func purchaseShippingLabel(c *client.Client, shipment *models.Shipment) {
	transactionInput := &models.TransactionInput{
		Rate:          shipment.Rates[len(shipment.Rates)-1].ObjectID,
		LabelFileType: models.LabelFileTypePDF,
		Async:         false,
	}
	transaction, err := c.PurchaseShippingLabel(transactionInput)
	if err != nil {
		panic(err)
	}

	fmt.Printf("Transaction:\n%s\n", dump(transaction))
}

func dump(v interface{}) string {
	data, err := json.MarshalIndent(v, "", "  ")
	if err != nil {
		panic(err)
	}

	return string(data)
}

See more examples.

go-shippo's People

Contributors

d5 avatar yuderekyu avatar mdrollette avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.