reading-notes

API’s Continued & REST

Building a team

Source: What Google Learned From Its Quest to Build the Perfect Team

Biggest thing: Psychological safety

Other important norms:

REST

Source: A Conversation about REST with my brother

SuperAgent

Source: SuperAgent

Ajax API to help with flexibility/readability for interacting with APIs. Works with Node.js.

Example

 request
   .post('/api/pet')
   .send({ name: 'Manny', species: 'cat' })
   .set('X-API-Key', 'foobar')
   .set('Accept', 'application/json')
   .then(res => {
      alert('yay got ' + JSON.stringify(res.body));
   });

To initiate a request:

  1. Invoke appropriate method (GET/POST/DELETE/etc)
  2. Call .then() or .end() or await

A Simple GET:

 request
   .get('/search')
   .then(res => {
      // res.body, res.headers, res.status
   })
   .catch(err => {
      // err.message, err.response
   });

Tips

HTTP method can also be passed as a string:

request('GET', '/search').then(success, failure);

Absolute URLS

Server needs CORS

request
   .get('https://example.com/search')
   .then(res => {

   });

Setting Header Fields

 request
   .get('/search')
   .set('API-Key', 'foobar')
   .set('Accept', 'application/json')
   .then(callback);

Pass in object

 request
   .get('/search')
   .set({ 'API-Key': 'foobar', Accept: 'application/json' })
   .then(callback);

GET

.query() accepts objects, can be used to form query-string

 request
   .get('/search')
   .query({ query: 'Manny' })
   .query({ range: '1..5' })
   .query({ order: 'desc' })
   .then(res => {

   });

See link for full documenation

Back