javascript - The SailsJS way of connecting to an API. What's the best method? -



javascript - The SailsJS way of connecting to an API. What's the best method? -

i'm trying connect artsy public api , i'm having trouble. please go easy on me, i'm new nodejs , sailsjs. i've created file called api/services/artsy.js , i'm hung on whether approach (see below) sails way of doing things. that's 1 question. if not, proper way set basic phone call api in sailsjs? or more specifically, artsy api?

issues come mind:

i need wait until token retrieved before can phone call api method requires it. done via pubsub, or other technique. there practical examples show how works in sailsjs?

can utilize service calls within of views? how? create sense? seems might useful api call.

note: i've set file config/artsy.js holds of import variables. (see below)

/api/services/artsy.js class="snippet-code-js lang-js prettyprint-override">/** * * artsy.js => in api/services * @description service connects artsy.net public api. * @url https://developers.artsy.net/ **/ var request = require('superagent'), traverson = require('traverson'), xapptoken; module.exports = { init: function(){ sails.log.info('----- artsy api initialized -----'); var clientid = sails.config.artsy.clientid, clientsecret = sails.config.artsy.clientsecret, apitokenurl = sails.config.artsy.apitokenurl; try{ request .post(apitokenurl) .send({ client_id: clientid, client_secret: clientsecret }) .end(function(res) { if (res) { xapptoken = res.body.token; } else { sails.log.error('api/services/artsy.js:'); sails.log.error(res.text); } }); } catch(e) { sails.log.error('api/services/artsy.js:'); sails.log.error(e); } }, getartiststatement: function(){ var api = traverson.jsonhal.from(sails.config.artsy.apiurl); var request = api.newrequest() .follow('artist') .withrequestoptions({ headers: { 'x-xapp-token': xapptoken, 'accept': 'application/vnd.artsy-v2+json' } }) .withtemplateparameters({ id: 'andy-warhol' }) .getresource(function(error, andywarhol) { console.log(andywarhol.name + 'was born in ' + andywarhol.birthday + ' in ' + andywarhol.hometown); }); } };

/config/artsy.js

class="snippet-code-js lang-js prettyprint-override">/** * artsy.js * * @description :: brains allow app connect artsy. * @docs :: http://sailsjs.org/#!documentation/models */ module.exports.artsy = { clientid: 'client_id', clientsecret: 'client_secret', apiurl: 'https://api.artsy.net/api', apitokenurl: 'https://api.artsy.net/api/tokens/xapp_token' };

if understand questions correctly so

that's 1 question. if not, proper way set basic phone call api in sailsjs?

service quite right place such case. , can phone call init method in config/bootstrap.js, or when user has log in or somewhere else.

i need wait until token retrieved before can phone call api method requires it. done via pubsub

yes, can utilize pubsub, seems improve utilize promises. illustration can utilize lib https://github.com/istavros/vowjs

can utilize service calls within of views? how? create sense? seems might useful api call.

i think yes, seems should not want it. looks betted when templates works controller passed info , nil else. utilize service in controller (or mb model) pass results template.

javascript node.js api sails.js sails.io.js

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -