Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

anx-api

Package Overview
Dependencies
Maintainers
1
Versions
59
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

anx-api

AppNexus Api Wrapper

  • 3.1.0-beta.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
261
decreased by-1.14%
Maintainers
1
Weekly downloads
 
Created
Source
AppNexus Api Wrapper

npm version Build Status Build Dependencies

Installation

npm install anx-api --save

Usage Example

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) {
    ...
})

Constructor

var anxApi = new AnxApi(config);
Parameters

config[object]:

  • target - (string) base api url
  • token - (string) optional session token
  • request - (object) optional request object
  • userAgent - (string) optional user agent
  • rateLimiting - (boolean) optional rate limiting
  • concurrencyLimit - (integer) optional max concurrent requests

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) {

    // The api object is now logged in. Optionally store the 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 uri
  • body - (object) options payload for .post and .put
  • startElement - (string) optional start index
  • numElements - (integer) optional number of records to return
  • params - (object) optional query string parameters
  • mimeType - (string) optional mimetype

Example

// Fetch the third page of 25 creatives
anxApi.get({
    uri: 'creative',
    startElement: 50,
    numElements: 25
})

Custom Requests and Debugging

The following are two different methods of modifying and or spying on requests made to the api.

Wrap the interal 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) {

        // 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
});

Tests

Running unit tests

Run the unit test suite from the project root, make sure you've run npm install first:

npm test

Mocking

Coming soon

Todos

  • Document before and after request events
  • Document advanced error handling
  • Add mocking examples to README.md
  • Add Service Wrapper

License

See LICENSE file

Keywords

FAQs

Package last updated on 24 Mar 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc