Skip to content
master
Go to file
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
Pod
 
 
 
 
 
 
 
 

README.md

EasyPostApi

Version License Platform

Requirements

  • iOS 9.0+ / Mac OS X 10.11+
  • Xcode 10.2+
  • Swift 5.0+

Installation

EasyPostApi is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'EasyPostApi',  '~> 1.2'

Usage

This library uses the EasyPost JSON api. Details about interacting with the EasyPost api can be found at http://www.easypost.com/docs

Set Credentials

Call the set credentials method on a shared instance of the API manager before any susequent calls. You only need to call this method once a shared instance as it maintains the credentials across calls.

let apiToken = "YOUR-TOKEN"
EasyPostApi.sharedInstance.setCredentials(apiToken, baseUrl: "https://api.easypost.com/v2/")

Post Address

Save an address record and get back an id

let address = EasyPostAddress()

address.name = "Johnny Appleseed"
address.company = "Apple"
address.street1 = "1 Infinite Loop"
address.street2 = "Suite 1"
address.city = "Cupertino"
address.state = "CA"

EasyPostApi.sharedInstance.postAddress(address) { (result) -> () in
    dispatch_async(dispatch_get_main_queue(), { () -> Void in
        switch(result) {
        case .Success(let value):
            
            print("Successfully posted address.")
            
        case .Failure(let error):
            print("Error posting address: \((error as NSError).localizedDescription)")
        }
    })
}

Verify Address

After posting an address to the API, use the verify call to verify the address with the address id that has been passed back from the API.

let addressId = "address-id-from-posting-address"

EasyPostApi.sharedInstance.verifyAddress(addressId, completion: { (verifyResult) -> () in
    dispatch_async(dispatch_get_main_queue(), { () -> Void in
        switch(verifyResult) {
        case .Success(let easyPostAddress):
            print("Successfully verified address.")
        case .Failure(let error):
            print("Error verifying address: \((error as NSError).localizedDescription)")
        
        }
    })
})

Post Shipment

Save a shipment record with a to address, from address, and parcel

let toAddress = EasyPostAddress()
toAddress.name = "Johnny Appleseed"
toAddress.company = "Apple"
toAddress.street1 = "1 Infinite Loop"
toAddress.street2 = "Suite 1"
toAddress.city = "Cupertino"
toAddress.state = "CA"

let fromAddress = EasyPostAddress()
fromAddress.name = "Johnny Appleseed"
fromAddress.company = "Apple"
fromAddress.street1 = "1 Infinite Loop"
fromAddress.street2 = "Suite 1"
fromAddress.city = "Cupertino"
fromAddress.state = "CA"

let parcel = EasyPostParcel()
parcel.length = NSNumber(value:10.0)	//inches
parcel.width = NSNumber(value:10.0)		//inches
parcel.height = NSNumber(value:10.0)	//inches
parcel.weight = NSNumber(value:10.0)	//ounces

EasyPostApi.sharedInstance.postShipment(toAddress, fromAddress: fromAddress, parcel: parcel) { (result) -> () in
    DispatchQueue.main.async {
        switch(result) {
        case .success(let shipment):
            
            print("Successfully posted shipment.")
            
            if let id = shipment.id {
                print("Shipment id: \(id)")
            }
            
        case .failure(let error):
            print("Error posting shipment: \((error as NSError).localizedDescription)")
        }
    }
}

Buy Shipment

Once you've created a shipment record, the rates records are returned inside the shipment object. You can buy the shipping and get a url to a label with a call to buy the shipment.

EasyPostApi.sharedInstance.buyShipment("shipment-id", rateId: "rate-id", completion: { (result) -> () in
	//Handle results
	DispatchQueue.main.async {
	    if(result.isSuccess) {
	        print("Successfully bought shipment.")
	        if let buyResponse = result.value {
	            if let postageLabel = buyResponse.postageLabel {
	                if let labelUrl = postageLabel.labelUrl {
	                    print("Label url: \(labelUrl)")
	                }
	            }
	        }
	    } else {
	        print("Error buying shipment: \((result.error! as NSError).localizedDescription)")
	    }
	}
})

Author

William Welbes

License

EasyPostApi is available under the MIT license. See the LICENSE file for more info.

The CocoaPods Master Repo

This repository contains the public CocoaPods specifications.

About

A simple client library for the EasyPost API built in Swift

Resources

License

Packages

No packages published
You can’t perform that action at this time.