Skip to content

Instantly share code, notes, and snippets.

@mattandrews
Created July 14, 2020 14:29
Show Gist options
  • Save mattandrews/0858987856df6f77d4c22bbe633cb6ab to your computer and use it in GitHub Desktop.
Save mattandrews/0858987856df6f77d4c22bbe633cb6ab to your computer and use it in GitHub Desktop.
const got = require('got'); // HTTP request client
const { CONTENT_API_URL, CONTENT_API_SANDBOX_URL } = require('./urls');
class ContentApiClient {
constructor() {
this.API_ENDPOINT = CONTENT_API_URL;
this.queryContentApi = null;
}
init(options = {}) {
this.API_ENDPOINT =
options.flags.sandboxMode === true
? CONTENT_API_SANDBOX_URL
: CONTENT_API_URL;
// Create an instance of got to run queries with
// bound to the correct endpoint URL based on options
this.queryContentApi = got.extend({
prefixUrl: this.API_ENDPOINT
});
return this;
}
getRoutes() {
return this.queryContentApi('v1/list-routes').json().then(mapAttrs);
}
}
module.exports = { ContentApiClient };
// Usage:
const { ContentApiClient } = require('./content-api');
const ContentApi = new ContentApiClient();
const routes = await ContentApi.init({
flags: {
sandboxMode: true
}
}).getRoutes();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment