Installation
npm install anx-api --save
Usage Example
var AnxApi = require('anx-api');
var anxApi = new AnxApi({
target: 'https://api.appnexus.com'
token: 'SESSION_TOKEN',
rateLimiting: true
});
anxApi.get(<serviceName>).then(function (res) {
...
}).catch(function (err) {
...
})
Links
Constructor
var anxApi = new AnxApi(config);
Parameters
config[object]:
target
- (string) required base api urltoken
- (string) optional session tokenrequest
- (object) optional request objectuserAgent
- (string) optional user agentrateLimiting
- (boolean) optional rate limitingconcurrencyLimit
- (integer) optional max concurrent requestsbeforeRequest
- (function) optional before request opts filter (see beforeRequest)afterRequest
- (function) optional after request response filter (see afterRequest)
Instance Methods
#get
Issues a GET request
anxApi.get('service url')
anxApi.get('service url', opts)
anxApi.get(opts)
Parameters
#getAllJson
** Experimental Feature **
Usage and parameters are the same as #get accept it pages through api calls.
Response body is parsed as json.
#post
Issues a POST request with a payload
anxApi.post('service url', <payload>)
anxApi.post('service url', <payload>, opts)
anxApi.post(opts)
Parameters
- service uri - (string)
- payload - (string|object)
- opts - (object) see Request Options
#put
Issues a PUT request with a payload
anxApi.put('service url', <payload>)
anxApi.put('service url', <payload>, opts)
anxApi.put(opts)
Parameters
- service uri - (string)
- payload - (string|object)
- opts - (object) see Request Options
#delete
Issues a DELETE request
anxApi.delete('service url')
anxApi.delete('service url', opts)
anxApi.delete(opts)
Parameters
#login
Authenticates with the API and returns a token. The token will be reused for future requests.
anxApi.login('username', 'password').then(function (token) {
...
})
#switchUser
anxApi.switchUser(userId).then(...)
Request Options
The get, post, put, and delete methods can be called with an opts object. The
opts object has the following request options.
uri
- (string) service uribody
- (object) required payload for .post
and .put
headers
- (object) optional request header overridesstartElement
- (string) optional start indexnumElements
- (integer) optional number of records to returnparams
- (object) optional query string parametersmimeType
- (string) optional mimetype
Examples
anxApi.get({
uri: 'creative',
startElement: 50,
numElements: 25
})
anxApi.get('creative', {
params: {
start_element: 50,
num_elements: 25
}
})
anxApi.get('creative?start_element=50&num_elements=25')
Transforming Request Options and Responses
beforeRequest
Request options can be modified prior to request execution by supplying a
beforeRequest
transform function in the constructor. The function should
either return a new options object or null
which will be ignored.
var anxApi = new AnxApi({
...,
beforeRequest: function (opts) {
var modifiedOpts = _.assign({}, opts, {
})
return modifiedOpts;
}
});
afterRequest
Request responses can be modified prior to being delivered by supplying a
afterRequest
transform function in the constructor. The function should return
either a new response object or null
which will be ignored.
var anxApi = new AnxApi({
...,
afterRequest: function (res) {
var modifiedRes = _.assign({}, res, {
})
return modifiedRes;
}
});
Error Handling
anxApi.get('creative').then(function (res) {
...
}).catch(function (err) {
if (err instanceof NotAuthenticatedError) {
console.log('Your not logged in!');
}
})
Error Types
Error
- generic error typeApiError
- base api error type
DNSLookupError
- target host could not be looked upNotAuthenticatedError
- token is invalid or expiredNotAuthorizedError
- Unauthorized to make requestRateLimitExceededError
SystemServiceUnavailableError
SystemUnknownError
TargetError
- target was not supplied
Custom Request and Debugging
The following are two different methods of modifying and or spying on requests
made to the api.
Wrap the internal request function
anxApi._config.request = _.wrap(anxApi._config.request, function (request, opts) {
console.log('DEBUG: ', opts);
return request.call(api, opts);
});
Pass in a custom request object
var request = require('request');
function customRequest(opts) {
return new Promise(function (resolve, reject) {
request(opts, function (err, res) {
if (err) {
reject(err);
} else {
resolve(res);
}
});
});
}
var anxApi = new AnxApi({
target: process.env.ANX_TARGET,
token: 'SESSION_TOKEN',
request: customRequest
});
Tests
Running unit tests
Run the unit test suite from the project root, make sure you've run npm install
first:
npm test
License
See LICENSE file