Python SDK of Postmen API
Project description
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.
import pprint
pp = pprint.PrettyPrinter(indent=4)
from postmen import Postmen, PostmenException
api_key = 'YOUR_API_KEY'
region = 'sandbox'
# create Postmen API handler object
api = Postmen(api_key, region)
try:
# as an example we request all the labels
result = api.get('labels')
print("RESULT:")
pp.pprint(result)
except PostmenException as e:
# if error occurs we can access all
# the details in following way
print("ERROR")
print(e.code()) # error code
print(e.message()) # error message
pp.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.
Argument |
Required |
Type |
Default |
Description |
---|---|---|---|---|
api_key |
YES |
String |
N / A |
API key |
region |
NO if endpoint is set |
String |
N / A |
API region (sandbox, production) |
endpoint |
— |
String |
N / A |
Custom URL API endpoint |
retry |
— |
Boolean |
True |
Automatic retry on retryable errors |
rate |
— |
Boolean |
True |
Wait before API call if rate limit exceeded or retry on 429 error |
safe |
— |
Boolean |
False |
Suppress exceptions on errors, None would be returned instead, check Error Handling |
raw |
— |
Boolean |
False |
To return API response as a raw string |
proxy |
— |
Dictionary |
{} |
Proxy credentials, handled as in requests library |
time |
— |
Boolean |
False |
Convert ISO time strings into datetime objects |
create(self, resource, payload, **kwargs)
Creates API resource object, returns new object payload as Dictionary.
Argument |
Required |
Type |
Default |
Description |
---|---|---|---|---|
resource |
YES |
String |
N / A |
Postmen API resourse (‘rates’, ‘labels’, ‘manifests’) |
payload |
YES |
Array or String |
N / A |
Payload according to API |
**kwargs |
NO |
Named arguments |
N / A |
Override constructor config |
API Docs:
Examples:
get(self, resource, id_=None, **kwargs)
Gets API $resource objects (list or a single objects).
Argument |
Required |
Type |
Default |
Description |
---|---|---|---|---|
resource |
YES |
String |
N / A |
Postmen API resourse (‘rates’, ‘labels’, ‘manifests’) |
id |
NO |
String |
None |
Object ID, if not set ‘list all’ API method is used |
query |
NO |
Dictionary or String |
N / A |
HTTP GET query (named argument) |
**kwargs |
NO |
Named arguments |
N / A |
Override 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.
Argument |
Required |
Type |
Default |
Description |
---|---|---|---|---|
path |
YES |
String |
N / A |
URL path (e.g. ‘v3/labels’ for https://sandbox-api.postmen.com/v3/labels) |
query |
NO |
Dictionary or String |
N / A |
HTTP GET query (named argument) |
**kwargs |
NO |
Named arguments |
array () |
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.
Argument |
Required |
Type |
Default |
Description |
---|---|---|---|---|
path |
YES |
String |
N / A |
URL path (e.g. ‘v3/labels’ for https://sandbox-api.postmen.com/v3/labels) |
body |
NO |
Dictionary or String |
N / A |
HTTP POST/PUT/DELETE request body (named argument) |
**kwargs |
NO |
Named arguments |
N / A |
Override constructor config |
Error Handling
Particular error details are listed in the documentation.
All SDK methods may throw an exception described below.
class PostmenException
Method |
Return type |
Description |
---|---|---|
code() |
Integer |
Error code |
retryable( ) |
Boolean |
Indicates if error is retryable |
message() |
String |
Error message (e.g. The request was invalid or cannot be otherwise ser ved) |
details() |
List |
Error 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.
File |
Description |
---|---|
rates object creation |
|
rates object(s) retrieve |
|
labels object creation |
|
labels object(s) retrieve |
|
manifests object creation |
|
manifests object(s) retrieve |
|
cancel-labels object creation |
|
cancel-labels object(s) retrieve |
|
Proxy usage |
|
Avalible 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.
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
Fedor Korshunov - view contributions
Marek Narozniak - view contributions
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file postmen-1.2.tar.gz
.
File metadata
- Download URL: postmen-1.2.tar.gz
- Upload date:
- Size: 16.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 064a3eb9d659491309ed597bf0467a26a7c3e4a18df9b8e32395477f170266a2 |
|
MD5 | 0c59ab19a2cbe519f7d8890009fada13 |
|
BLAKE2b-256 | 0547fb94ca144de714223abb4b11748e708c13f615aebbce7c5f9d9ce7348b83 |