2.0.0 • Published 1 year ago

postmen v2.0.0

Weekly downloads
200
License
MIT
Repository
github
Last release
1 year ago

postmen

Build Status codecov Dependency Status

node npm npm npm

codecov.io

Introduction

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

Table of Contents

Installation

NPM installation

npm install postmen

Quick Start

In order to get API key refer to the documentation.

'use strict';

const Postmen = require('postmen');
// TODO key of the Postmen instance
let api_key = 'api-key',
// TODO region of the Postmen instance
let region = 'sandbox';

let postmen = Postmen(api_key, region);

// get all labels by using callback
postmen.get('/labels', function (err, result) {
	if (err) {
		console.log(err);
	} else {
		console.log(result);
	}
});

// get all labels by using promise
postmen.get('/labels').then(function (result) {
		console.log(result);
}).catch(function (err) {
    console.log(err);
});

// get all labels by using promise with chainable function
postmen.useApiKey('ANOTHER_API_KEY').setRetry(false).get('/labels').then(function (result) {
    console.log(result);
}).catch(function (err) {
    console.log(err);
});

// get a particular  labels
postmen.get('/rates/put-your-label-id-here', function (err, result) {
	if (err) {
		console.log(err);
	} else {
		console.log(result);
	}
});

class Postmen

Postmen(api_key, region, config)

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

ArgumentRequiredTypeDefaultDescription
api_keyYESstringN/AAPI key
regionYESstringN/AAPI region (sandbox, production)
configNOobjectnullOptions
config['endpoint']stringnullCustom URL API endpoint
config['retry']booleantrueoverride default retry if set, see Retry policy
config['rate']booleantrueWait before API call if rate limit exceeded or retry on 429 error
config['raw']booleanfalseTo return API response as a raw string
config['proxy']stringnullProxy credentials

create(path, input, config, callback)

Creates postmen api object

ArgumentRequiredTypeDefaultDescription
pathYESstringN/Astart with /, see available path here key
inputYESobjectnullobject of request config
input['body']YESstringnullPOST body
input['query']NOobjectnullquery object
configNOobjectnullobject of request config
config['retry']NObooleantrueoverride default retry if set, see Retry policy
config['raw']NObooleanfalseif true, return result as string, else return as object
callbackNOfunctionN/Athe callback to handle error and result, the result is the response body of the request

API Docs:

Examples:

get(path, input, config,callback)

Get Postmen API objects (list or a single objects).

ArgumentRequiredTypeDefaultDescription
pathYESstringN/Astart with /, see available path here key
inputNOobjectnullobject of request config
input['body']NOstringnullPOST body
input['query']NOobjectnullquery object or string
configNOobjectnullobject of request config
config['retry']NObooleantrueoverride default retry if set, see Retry policy
config['raw']NObooleanfalseif true, return result as string, else return as object
callbackNOfunctionN/Athe callback to handle error and result, the result is the response body of the request
postmen.get( '/path/label-id', callback);
// is equivalent to
postmen.call('GET', '/path/label-id', input, config, callback);

postmen.get( '/path', input, config, callback);
// is equivalent to
postmen.call('GET', '/path', input, config, callback);

API Docs:

Examples:

Proxy Method (GET, POST, PUT, DELETE)

There are also interface GET, POST, PUT, DELETE which are proxy to Postmen.call(...)

postmen.call('GET', '/path', input, config, callback);
// is equivalent to
postmen.GET('/path', input, config, callback);

// So as `POST`, `PUT` and `DELETE`

Chainable Function

Using chainable function to config now is accepted. Now postmen instance has these chainable function:

  • useApiKey() temporarily use an api_key to make request
  • setProxy() overwrite postmen proxy property
  • setRetry() overwrite postmen retry property
  • setRaw() overwrite postmen raw property
postmen.useApiKey('ANOTHER_API_KEY').get('labels').then();
// is equivalent to
let input = {};
let config = {
	api_key: 'ANOTHER_API_KEY'
}
postmen.get('labels', input, config).then();

Promise:

Only create(path, config, callback) and get(path, config, callback) function support promise.

Rate Limiter:

To understand Postmen rate limit policy, please see limit session in https://docs.postmen.com/ratelimit.html

You can get the recent rate limit by postmen.rate_limit. Initially all value is {}.

let postmen = Postmen('YOUR_API_KEY', 'region');
console.log(postmen.rate_limit);

// console output
// {}

After making an API call, it will be set.

postmen.get('/labels', function (err, result) {
	console.log(postmen.rate_limit);
});

// console output
// { 'YOUR_API-KEY' : { limit: 600, remaining: 599, reset: 1453281417 } }

When the API response with 429 Too Many request error

  • if rate is true, it wont throw, will delay the job, retry when the rate limit is reset.
  • if rate is false, it will return 429 Too Many request error to the callback

Retry policy

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.

You can set the retry flag

  • in constructor as default retry flag
  • specify in config of get() or create() method

Examples

Full list

All examples avalible listed in the table below.

FileDescription
rates_create.jsrates object creation
rates_retrieve.jsrates object(s) retrieve
labels_create.jslabels object creation
labels_retrieve.jslabels object(s) retrieve
manifests_create.jsmanifests object creation
manifests_retrieve.jsmanifests object(s) retrieve
cancel_labels_create.jscancel-labels object creation
cancel_labels_retrieve.jscancel-labels object(s) retrieve
address_validation_create.jsaddress_validation object(s) creation
proxy.jsProxy usage
error.jsAvalible ways to catch/get errors
response.jsAvalible output types

How to run

Download the source code, go to examples directory.

Put your API key and region to credentials.js

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

Navigation table

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

Model \ Actioncreateget allget by id
rates.create('/rates', input, config, callback).get('/rates', input, config, callback).get('rates/rate-id-here', input, config, callback)
labels.create('/labels', input, config, callback).get('/labels', input, config, callback)).get('/labels/label-id-here', input, config, callback))
manifest.create('/manifest', input, config, callback).get('/manifest', input, config, callback).get('/manifest/manifest-id-here', input, config, callback)
cancel-labels.create('/cancel-labels', input, config, callback).get('/cancel-labels', input, config, callback).get('/cancel-labels/cancel-labels-id-here', input, config, callback)
address-validations.create('/address-validations', input, config, callback)

Testing

mocha --recursive

License

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

2.0.0

1 year ago

1.0.5

3 years ago

1.0.4

7 years ago

1.0.3

7 years ago

1.0.2

8 years ago

1.0.1

8 years ago

1.0.0

8 years ago

0.1.0

8 years ago

0.0.1

8 years ago