Git Product home page Git Product logo

apiconnect's Introduction

APIConnect

This class creates a very easy and intuitive way to interact with APIs, most commonly those hosted on other domains.

Setup:

var twitter = new APIConnect();
twitter.domain('api.twitter.com');
twitter.get('statuses/home_timeline');
twitter.getHomeTimeline();

> GET http://api.twitter.com/statuses/home_timeline.json

The route statuses/home_timeline will be automatically set up and accessible through a camelized method name in the api object. Calling the route is as simple as calling the method.

When making the call, the first object is params. This will most commonly be added to the end of the query string, but in cases where the route contains params such as tweets/:tweet_id, it will be placed here instead and removed from the query string.

The second object passed is an options hash. This will override any default params and also end up being passed to the AJAX lib, in this case jQuery. Any valid option for jQuery.ajax is allowed here. This means that all the standard callbacks will work:

twitter.getHomeTimeline({}, {
  success: function() {
    // Congratulations!
  },
  error: function() {
    // Oh noeee... failure!
  }
});

API calls will also pass back jQuery deferred objects, so these can be used as well:

twitter.getHomeTimeline().then(function() {
  // You're done!
});

Any route can be set up with any level of context. These contexts are always optional, and will only be added if they exist when passed as params:

twitter.get(':user/:list_id/members');
twitter.getMembers({ user: 'bob', list_id: 5 });

> GET http://api.twitter.com/bob/5/members.json

To connect routes, the 4 main HTTP verbs, "GET", "POST", "PUT", and "DELETE" are supported, and map to the method names "get", "create", "update", and "destroy", respectively (note "del", which is a reserved keyword):

twitter.post(':user/:list_id/members');
twitter.del(':user/:list_id/members');

twitter.createMember({ user: 'bob', list_id: 5, member_name: 'harry' });

> POST http://api.twitter.com/bob/5/members.json?member_name=harry

twitter.destroyMember({ user: 'bob', list_id: 5 });

> DELETE http://api.twitter.com/bob/5/members.json

"resource" serves as a shortcut to all 4 HTTP verbs:

twitter.resource('member');

twitter.getMember();     > GET     http://api.twitter.com/member.json
twitter.createMember();  > POST    http://api.twitter.com/member.json
twitter.updateMember();  > PUT     http://api.twitter.com/member.json
twitter.destroyMember(); > DELETE  http://api.twitter.com/member.json

If a resource is a collection, passing collection: true in the options for "resource" will create standard collection routes including an "index" method:

twitter.resource('status', { collection: true });

twitter.getStatus();              > GET     http://api.twitter.com/status.json
twitter.getStatus({ id: 3});      > GET     http://api.twitter.com/status/3.json
twitter.createStatus();           > POST    http://api.twitter.com/status.json
twitter.updateStatus({ id: 3 });  > PUT     http://api.twitter.com/status/3.json
twitter.destroyStatus({ id: 3 }); > DELETE  http://api.twitter.com/status/3.json

If a "collection" is omitted, it will attempt to be intelligently detected by the pluralization of the resource passed. In this case, pluralization of the methods will also use intelligent detection:

twitter.resource('members');

twitter.getMembers();              > GET     http://api.twitter.com/members.json
twitter.getMembers({ id: 3});      > GET     http://api.twitter.com/members/3.json
twitter.createMembers();           > POST    http://api.twitter.com/members.json
twitter.updateMembers({ id: 3 });  > PUT     http://api.twitter.com/members/3.json
twitter.destroyMembers({ id: 3 }); > DELETE  http://api.twitter.com/members/3.json

... more docs to come!

apiconnect's People

Contributors

andrewplummer avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.