Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrhamm/b0f847fc2e344f9cfb2e12f81e11dae1 to your computer and use it in GitHub Desktop.
Save andrhamm/b0f847fc2e344f9cfb2e12f81e11dae1 to your computer and use it in GitHub Desktop.
AWS JavaScript SDK - Configure AWS Service Client from an API Service Model (JSON files)

The AWS JavaScript SDK (and presumably SDKs for other langauges) are dynamically configured from JSON files that specify all the possible API endpoints and their request/response structure for the given service.

In the case of Amazon Personalize, the minimal documentation only describes how to "load" these JSON files for the CLI SDK:

https://docs.aws.amazon.com/personalize/latest/dg/aws-personalize-set-up-aws-cli.html

Here is an example of how to configure a service client object in the JavaScript SDK. I arrived at this conclusion by exploring the node_modules/aws_sdk directory, specifically the apis, and clients subdirectory.

(Download the service models from the page linked above.)

import AWS from 'aws-sdk';
const { Service, apiLoader } = AWS;
apiLoader.services.personalize = {};
AWS.Personalize = Service.defineService('personalize', ['2018-05-22']);
Object.defineProperty(apiLoader.services.personalize, '2018-05-22', {
get: function get() {
const model = require('./aws-api-model-personalize.json');
model.paginators = {};
return model;
},
enumerable: true,
configurable: true,
});
apiLoader.services['personalize-runtime'] = {};
AWS.PersonalizeRuntime = Service.defineService('personalize-runtime', ['2018-05-22']);
Object.defineProperty(apiLoader.services['personalize-runtime'], '2018-05-22', {
get: function get() {
const model = require('./aws-api-model-personalize-runtime.json');
model.paginators = {};
return model;
},
enumerable: true,
configurable: true,
});
// initialize an instance of the Personalize service client
const personalize = new AWS.Personalize({ apiVersion: '2018-05-22' });
// use it
personalize.listDatasetGroups().promise().then((resp) => {
console.log(resp);
});
@Tejaswinisana
Copy link

how do we do this in v3 ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment