
npm install anx-api --save
var AnxApi = require('anx-api');
// Create a new instance with api target
var anxApi = new AnxApi({
target: 'https://api.appnexus.com'
token: 'SESSION_TOKEN', // (optional) see also anxApi.login(...)
rateLimiting: true
});
anxApi.get(<serviceName>).then(function (res) {
...
}).catch(function (err) {
...
})
var anxApi = new AnxApi(config);
config[object]:
* target - (string) required base api url
* token - (string) optional session token
* request - (object) optional request object
* timeout - (integer) optional request timeout in milliseconds (default: 60000) Triggers ConnectionAbortedError error.
* userAgent - (string) optional user agent
* rateLimiting - (boolean) optional rate limiting
* concurrencyLimit - (integer) optional max concurrent requests
* beforeRequest - (function) optional before request opts filter (see beforeRequest)
* afterRequest - (function) optional after request response filter (see afterRequest)
* environment - (enum:node|browser) optional. node - force axios to run as if in node and not the browser (usefull for testing with jest/jasmine).
Issues a GET request
anxApi.get('service url')
anxApi.get('service url', opts)
anxApi.get(opts)
Usage and parameters are the same as #get accept it pages through api calls. Response body is parsed as json.
Issues a POST request with a payload
anxApi.post('service url', <payload>)
anxApi.post('service url', <payload>, opts)
anxApi.post(opts)
Issues a PUT request with a payload
anxApi.put('service url', <payload>)
anxApi.put('service url', <payload>, opts)
anxApi.put(opts)
Issues a DELETE request
anxApi.delete('service url')
anxApi.delete('service url', opts)
anxApi.delete(opts)
Authenticates with the API and returns a token. The token will be reused for future requests.
anxApi.login('username', 'password').then(function (token) {
// The api object is now logged in. Optionally store the token.
...
})
anxApi.switchUser(userId).then(...)
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 .putheaders - (object) optional request header overridesstartElement - (string) optional start indexnumElements - (integer) optional number of records to returnparams - (object) optional query string parametersmimeType - (string) optional mimetypetimeout - (integer) optional request timeout in milliseconds (defaults to config timeout)encodeParams - (boolean) optional whether or not params object should be encoded. (default: false)// Fetch the third page of 25 creatives
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')
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, {
// make changes to options
})
return modifiedOpts;
}
});
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, {
// make changes to response
})
return modifiedRes;
}
});
anxApi.get('creative').then(function (res) {
...
}).catch(function (err) {
if (err instanceof NotAuthenticatedError) {
console.log('Your not logged in!');
}
})
Error - generic error typeNetworkErrorDNSLookupError - target host could not be looked up [ENOTFOUND]ConnectionAbortedError - request timeout was reached [ECONNABORTED]ConnectionRefusedError - [ECONNREFUSED]ConnectionResetError - [ECONNRESET]ConnectionTimeoutError - [ETIMEDOUT]SocketTimeoutError - [ESOCKETTIMEDOUT]ApiError - base api error typeNotAuthenticatedError - token is invalid or expiredNotAuthorizedError - Unauthorized to make requestRateLimitExceededErrorSystemServiceUnavailableErrorSystemUnknownErrorTargetError - target was not suppliedThe following are two different methods of modifying and or spying on requests made to the api.
anxApi._config.request = _.wrap(anxApi._config.request, function (request, opts) {
console.log('DEBUG: ', opts);
return request.call(api, opts);
});
var request = require('request');
function customRequest(opts) {
return new Promise(function (resolve, reject) {
// Customize the request
request(opts, function (err, res) {
if (err) {
// Add additional error handling
reject(err);
} else {
// Customize the response
resolve(res);
}
});
});
}
var anxApi = new AnxApi({
target: process.env.ANX_TARGET,
token: 'SESSION_TOKEN',
request: customRequest
});
Run the unit test suite from the project root, make sure you've run npm
install first:
npm test
See LICENSE file
$ claude mcp add anx-api \
-- python -m otcore.mcp_server <graph>