Git Product home page Git Product logo

aftership-sdk-python's Introduction

About aftership-python

aftership-python is Python SDK (module) for AfterShip API. Module provides clean and elegant way to access API endpoints.

Installation

Via pip

Run the following:

$ pip install aftership

Via source code

Download the code archive, unzip it, create/activate virtualenv, go to the source root directory, then run:

$ python setup.py install

Usage

Quick Start

The following code gets list of supported couriers

import aftership
api = aftership.APIv4('AFTERSHIP_API_KEY')
couriers = api.couriers.all.get()

Get API object

Import aftership module and obtain APIv4 object. Valid API key must be provided.

import aftership
api = aftership.APIv4('AFTERSHIP_API_KEY')

APIv4 object

APIv4 object is used to make API calls. Object constructor:

class aftership.APIv4(key, max_calls_per_sec=10, datetime_convert=True)
  1. key is AfterShip API key
  2. max_calls_per_sec represents maximum number of calls provided for each second (10 is a limit for single client, but you might want to set less if you have multiple parallel API objects)
  3. datetime_convert provides timestamp strings conversion to datetime in API output

Make API calls

The module provides direct bindings to API calls: https://www.aftership.com/docs/api/4

Each call consists of three parts:

  1. Positional arguments (goes after api.aftership.com/v4/, separated by slash "/")
  2. Named arguments (listed in Request โ€”> Parameters section)
  3. HTTP Method (GET, POST, PUT or DELETE)

The following convention is used to construct a call:

<APIv4 object>.positional.arguments.<HTTP method>(*more_positional_arguments, **named_arguments)

The code above makes a call to /positional/arguments/... endpoint with named arguments passed in request body as JSON or HTTP query (depending on HTTP method).

API calls examples

The following code creates, modifies and deletes tracking:

>>> import aftership
>>> api = aftership.APIv4(API_KEY)
>>> slug = 'russian-post'
>>> number = '65600077151512'

# create tracking
# https://www.aftership.com/docs/api/4/trackings/post-trackings
>>> api.trackings.post(tracking=dict(slug=slug, tracking_number=number, title="Title"))
{u'tracking': { ... }}

# get tracking by slug and number, return 'title' and 'created_at' field
# https://www.aftership.com/docs/api/4/trackings/get-trackings-slug-tracking_number
>>> api.trackings.get(slug, number, fields=['title', 'created_at'])
{u'tracking': { ... }}

# change tracking title
# https://www.aftership.com/docs/api/4/trackings/put-trackings-slug-tracking_number
>>> api.trackings.put(slug, number, tracking=dict(title="Title (changed)"))
{u'tracking': { ... }}

# delete tracking
# https://www.aftership.com/docs/api/4/trackings/delete-trackings
>>> api.trackings.delete(slug, number)
{u'tracking': { ... }}

Positional arguments

Positional arguments passed in the following forms:

  1. APIv4 object attributes
  2. APIv4 object keys
  3. HTTP Method arguments

APIv4 object attributes used to represent constant parts of the endpoint, while HTTP Method arguments are used for variable parts, e.g.:

api.trackings.get('russian-post', '65600077151512')

Positional arguments passed as keys are useful if they are stored in variables and followed by constant value, e.g.:

api.trackings['russian-post']['65600077151512'].retrack.post()

Named arguments

Named arguments passed as keyword arguments of HTTP Methods calls. Comma-separated values strings could be passed as [lists], timestamp strings could be passed as regular datetime objects, e.g.:

import datetime
...
api.trackings.get(created_at_min=datetime.datetime(2014, 9, 1), fields=['title', 'order_id'])

HTTP Method arguments

The following HTTP methods are supported:

  1. get()
  2. post()
  3. put()
  4. delete()

Each method return either JSON of 'data' field or throws an aftership.APIv4RequestException. aftership-python relies on Requests library and ones should expect Requests exceptions.

APIv4RequestException

An exception is throwed on errors. The following methods are provided to get error details:

  1. code()
  2. type()
  3. message()

Each functions returns appropriate value from 'meta' field. Check errors documentation for more details. Code example:

try:
    api = aftership.APIv4('FAKE_API_KEY')
    api.couriers.get()
except aftership.APIv4RequestException as error:
    # FAKE_API_KEY will result in Unauthorized (401) error
    print 'Error:', error.code(), error.type(), error.message()

aftership-sdk-python's People

Contributors

fedor avatar russelldavies avatar teddychan 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.