0.0.1 • Published 7 years ago

example-apitest v0.0.1

Weekly downloads
1
License
Apache 2.0
Repository
github
Last release
7 years ago

Example API

Browser and node module for making API requests against Example API.

Please note: This module uses Popsicle to make API requests. Promises must be supported or polyfilled on all target environments.

Installation

npm install example-api --save
bower install example-api --save

Usage

Node

var ExampleApi = require('example-api');

var client = new ExampleApi();

Browsers

<script src="example-api/index.js"></script>

<script>
  var client = new ExampleApi();
</script>

Options

You can set options when you initialize a client or at any time with the options property. You may also override options for a single request by passing an object as the second argument of any request method. For example:

var client = new ExampleApi({ ... });

client.options = { ... };

client.resource('/').get(null, {
  baseUri: 'http://example.com',
  headers: {
    'Content-Type': 'application/json'
  }
});

Base URI

You can override the base URI by setting the baseUri property, or initializing a client with a base URI. For example:

new ExampleApi({
  baseUri: 'https://example.com'
});

Base URI Parameters

If the base URI has parameters inline, you can set them by updating the baseUriParameters property. For example:

client.options.baseUriParameters.version = 'v3';

Resources

All methods return a HTTP request instance of Popsicle, which allows the use of promises (and streaming in node).

resources.resource

var resource = client.resources.resource;
OPTIONS

Some requests require authentication

resource.options().then(function (res) { ... });
CONNECT

Some requests require authentication

resource.connect().then(function (res) { ... });
TRACE

Some requests require authentication

resource.trace().then(function (res) { ... });
PATCH

Some requests require authentication

resource.patch().then(function (res) { ... });
DELETE

Some requests require authentication

resource.delete().then(function (res) { ... });
PUT

Some requests require authentication

resource.put().then(function (res) { ... });
GET

get the first one

resource.get().then(function (res) { ... });
Headers
resource.get(null, {
  headers: { ... }
});
  • x-custom string

resources.resource.resourceId(resourceId)

  • resourceId string

Which resoure would you like to view

This is a resource description with some markdown embedded in it

var resource = client.resources.resource.resourceId(resourceId);
GET

Instagram’s API uses the OAuth 2.0 protocol for simple, but effective authentication and authorization. OAuth 2.0 is much easier to use than previous schemes; developers can start using the Instagram API almost immediately. The one thing to keep in mind is that all requests to the API must be made over SSL (https:// not http://)

Do you need to authenticate?

For the most part, Instagram’s API only requires the use of a client_id). A client_id simply associates your server, script, or program with a specific application. However, some requests require authentication - specifically requests made on behalf of a user. Authenticated requests require an _access_token. These tokens are unique to a user and should be stored securely. Access tokens may expire at any time in the future.

Note that in many situations, you may not need to authenticate users at all. For instance, you may request popular photos without authenticating (i.e. you do not need to provide an access_token; just use your client ID with your request). We only require authentication in cases where your application is making requests on behalf of a user (commenting, liking, browsing a user’s feed, etc.).

Receiving an access_token

resource.get().then(function (res) { ... });
Query Parameters
resource.get({ ... });
  • filter string

What to filter

POST
resource.post().then(function (res) { ... });
Body

application/json

application/x-www-form-urlencoded

{ "name": { "description": "The name of the resource to create", "type": "string", "example": "Comment", "displayName": "name" }, "description": { "description": "A description of the resource to create", "type": "string", "example": "User-generated content pertinent to the associated blog post", "displayName": "description" } }

multipart/form-data

{ "name": { "description": "The name of the resource to create", "type": "string", "example": "Comment", "displayName": "name" }, "description": { "description": "A description of the resource to create", "type": "string", "example": "User-generated content pertinent to the associated blog post", "displayName": "description" } }

resources.another.resource

This is a description. It can be long.

Headers

It can have headers, and italic, and bold text.

Length

Because it is arbitrary markdown, it can be arbitrarily long.

var resource = client.resources.another.resource;
CONNECT

Some requests require authentication

resource.connect().then(function (res) { ... });
HEAD

Some requests require authentication

resource.head().then(function (res) { ... });
GET

Some requests require authentication

resource.get().then(function (res) { ... });
Query Parameters
resource.get({ ... });
  • page integer

Which page to display

  • order string, one of (oldest, newest), default: newest

The sort order of resources

  • query string

A query parameter

resources.resource-with-headers

var resource = client.resources.resource-with-headers;
GET
resource.get().then(function (res) { ... });
Headers
resource.get(null, {
  headers: { ... }
});
  • Custom header string

This header is used to send data that...

  • Parameterized header string

resources.secured-resource

var resource = client.resources.secured-resource;
GET
resource.get().then(function (res) { ... });

resources.resource-with-method-level-traits

var resource = client.resources.resource-with-method-level-traits;
GET

get the first one

resource.get().then(function (res) { ... });

resources.resource-with-form-and-multipart-form-parameters

var resource = client.resources.resource-with-form-and-multipart-form-parameters;
GET
resource.get().then(function (res) { ... });
Query Parameters
resource.get({ ... });
  • Some Query Param string

Your value for some thing.

Body

application/json

application/x-www-form-urlencoded

{ "api_key": { "displayName": "API Key", "description": "Your license key for the application. Please contact developer@nzpost.co.nz for a license key", "type": "string", "required": true, "example": "c4f820f0420a013ea143230c290fbf99" } }

multipart/form-data

{ "api_key": { "displayName": "API Key", "description": "Your license key for the application. Please contact developer@nzpost.co.nz for a license key", "type": "string", "required": true, "example": "c4f820f0420a013ea143230c290fbf99" } }

resources.resource-with-repeatable-params

var resource = client.resources.resource-with-repeatable-params;
POST
resource.post().then(function (res) { ... });
Query Parameters
resource.post(null, { query: { ... } });
  • someParam string

  • notRepeatable string

Headers
resource.post(null, {
  headers: { ... }
});
  • someHeader string

  • x-meta-{*} string

Body

application/x-www-form-urlencoded

{ "someFormParam": { "repeat": true, "displayName": "someFormParam", "type": "string" } }

multipart/form-data

{ "someMultipartFormParam": { "type": "file", "repeat": true, "displayName": "someMultipartFormParam" }, "someMultipartFormParamWithMultipleTypes": { "type": "file", "displayName": "someMultipartFormParamWithMultipleTypes" }, { "type": "string", "repeat": true, "displayName": "someMultipartFormParamWithMultipleTypes" } }

Custom Resources

You can make requests to a custom path in the API using the #resource(path) method.

client.resource('/example/path').get();

License

Apache 2.0

0.0.1

7 years ago

0.0.0

7 years ago