untitled notebook

node v4.9.1
version: master
endpointsharetweet
require('request'); // request promise is a popular, well tested lib wrapping request in a promise const request = require('request-promise'); // i like to use url handling libs, the do good things like escape string input. const url = require('url'); class Search { constructor(query) { this.query = query; } fetch() { const endpoint = url.parse('https://www.googleapis.com/books/v1/volumes'); const options = { maxResults: 40, fields: 'items(id,volumeInfo(title))', q: this.query } endpoint.query = options; const callUrl = url.format(endpoint); return request.get(callUrl).then(result => JSON.parse(result)); } } const search = new Search('javascript'); search.fetch() .then(result => console.log(result)) .catch(e => console.error('catch:', e));
Loading…

no comments

    sign in to comment