Skip to content

Repository files navigation

Introduction

Python SDK for Postmen API. For problems and suggestions please open GitHub issue

Table of Contents

Installation

Manual

Download or clone this repo, then run

python setup.py install

PyPI

Run pip install postmen

Quick Start

In order to get API key and choose a region refer to the documentation.

importpprintpp=pprint.PrettyPrinter(indent=4)
frompostmenimportPostmen, PostmenExceptionapi_key='YOUR_API_KEY'region='sandbox'# create Postmen API handler objectapi=Postmen(api_key, region)
try:
# as an example we request all the labelsresult=api.get('labels')
print("RESULT:")
pp.pprint(result)
exceptPostmenExceptionase:
# if error occurs we can access all# the details in following wayprint("ERROR")
print(e.code()) # error codeprint(e.message()) # error messagepp.pprint(e.details()) # details

class Postmen

Postmen(api_key, region, **kwargs)

Initiate Postmen SDK object. In order to get API key and choose a region refer to the documentation.

ArgumentRequiredTypeDefaultDescription
api_keyYESStringN / AAPI key
regionNO if endpoint is setStringN / AAPI region (sandbox, production)
endpointStringN / ACustom URL API endpoint
retryBooleanTrueAutomatic retry on retryable errors
rateBooleanTrueWait before API call if rate limit exceeded or retry on 429 error
safeBooleanFalseSuppress exceptions on errors, None would be returned instead, check Error Handling
rawBooleanFalseTo return API response as a raw string
proxyDictionary{}Proxy credentials, handled as in requests library
timeBooleanFalseConvert ISO time strings into datetime objects

create(self, resource, payload, **kwargs)

Creates API resource object, returns new object payload as Dictionary.

ArgumentRequiredTypeDefaultDescription
resourceYESStringN / APostmen API resourse ('rates', 'labels', 'manifests')
payloadYESArray or StringN / APayload according to API
**kwargsNONamed argumentsN / AOverride constructor config

API Docs:

Examples:

get(self, resource, id_=None, **kwargs)

Gets API $resource objects (list or a single objects).

ArgumentRequiredTypeDefaultDescription
resourceYESStringN / APostmen API resourse ('rates', 'labels', 'manifests')
idNOStringNoneObject ID, if not set 'list all' API method is used
queryNODictionary or StringN / AHTTP GET query (named argument)
**kwargsNONamed argumentsN / AOverride constructor config

API Docs:

Examples:

getError()

Returns SDK error, PostmenException type if named argument safe = True was set.

Check Error Handling for details.

GET(self, path, **kwargs)

Performs HTTP GET request, returns an Dictionary object holding API response.

ArgumentRequiredTypeDefaultDescription
pathYESStringN / AURL path (e.g. 'v3/labels' for https://sandbox-api.postmen.com/v3/labels)
queryNODictionary or StringN / AHTTP GET query (named argument)
**kwargsNONamed argumentsarray ()query, and other values overriding constructor config

POST(self, path, **kwargs)

PUT(self, path, **kwargs)

DELETE(self, path, **kwargs)

Performs HTTP POST/PUT/DELETE request, returns a Dictionary object holding API response.

ArgumentRequiredTypeDefaultDescription
pathYESStringN / AURL path (e.g. 'v3/labels' for https://sandbox-api.postmen.com/v3/labels)
bodyNODictionary or StringN / AHTTP POST/PUT/DELETE request body (named argument)
**kwargsNONamed argumentsN / AOverride constructor config

Error Handling

Particular error details are listed in the documentation.

All SDK methods may throw an exception described below.

class PostmenException

MethodReturn typeDescription
code()IntegerError code
retryable( )BooleanIndicates if error is retryable
message()StringError message (e.g. The request was invalid or cannot be otherwise ser ved)
details()ListError details (e.g. Destination country must be RUS or KAZ)

In case of safe = True SDK would not throw exceptions, getError() must be used instead.

Example: error.py

Automatic retry on retryable error

If API error is retryable, SDK will wait for delay and retry. Delay starts from 1 second. After each try, delay time is doubled. Maximum number of attempts is 5.

To disable this option set retry = False

Examples

Full list

All examples avalible listed in the table below.

FileDescription
rates_create.pyrates object creation
rates_retrieve.pyrates object(s) retrieve
labels_create.pylabels object creation
labels_retrieve.pylabels object(s) retrieve
manifests_create.pymanifests object creation
manifests_retrieve.pymanifests object(s) retrieve
cancel_labels_create.pycancel-labels object creation
cancel_labels_retrieve.pycancel-labels object(s) retrieve
proxy.pyProxy usage
error.pyAvalible ways to catch/get errors

How to run

Download the source code, go to examples directory.

If you already installed Postmen SDK for Python you can proceed, otherwise install it by running python setup.py install or using PyPI.

Put your API key and region to credentials.py

Check the file you want to run before run. Some require you to set additional variables.

Navigation table

For each API method SDK provides Python wrapper. Use the table below to find SDK method and example that match your need.

Model \ Actioncreateget allget by id
rates.create('rates', payload).get('rates').get('rates', id)
labels.create('labels', payload).get('labels').get('labels', id)
manifests.create('manifests', payload).get('manifests').get('manifests', id)
cancel-labels.create('cancel-labels', payload).get('cancel-labels').get('cancel-labels', id)

Testing

If you contribute to SDK, run automated test before you make pull request.

pip install -r requirements.txt
python setup.py test

License

Released under the MIT license. See the LICENSE file for details.

Contributors

About

AfterShip Shipping (Postmen) API Client Library for Python (USPS, FedEx, UPS, DHL and more)

Topics

Resources

Stars

11 stars

Watchers

8 watching

Forks

Releases

Packages

Used by

Contributors

Languages