Created
February 26, 2019 16:58
-
-
Save ismits/0ded3a026bc25ba2ba8984e742909c8e to your computer and use it in GitHub Desktop.
Lambda http get using aws-sdk
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
const AWS = require('aws-sdk'); | |
// define our target API as a "service" | |
const svc = new AWS.Service({ | |
// the API base URL | |
endpoint: 'https://www.metaweather.com/api', | |
// don't parse API responses | |
// (this is optional if you want to define shapes of all your endpoint responses) | |
convertResponseTypes: false, | |
// and now, our API endpoints | |
apiConfig: { | |
metadata: { | |
protocol: 'rest-json' // we assume our API is JSON-based | |
}, | |
operations: { | |
getWeather: { | |
http: { | |
method: 'GET', | |
// note the placeholder in the URI | |
// requestUri: '/accounts/{accountId}' | |
requestUri: '/location/{locationId}/' | |
}, | |
input: { | |
type: 'structure', | |
required: [ 'locationId' ], | |
members: { | |
'locationId': { | |
// all kinds of validators are available | |
type: 'integer', | |
// include it in the call URI | |
location: 'uri', | |
// this is the name of the placeholder in the URI | |
locationName: 'locationId' | |
} | |
} | |
} | |
} | |
} | |
} | |
}); | |
svc.isGlobalEndpoint = true; | |
exports.handler = async (event, context) => { | |
return new Promise((resolve, reject) => { | |
svc.getWeather({ | |
locationId: event.locationId | |
}, (err, data) => { | |
if (err) { | |
// console.error('>>> operation error:', err); | |
const result = { | |
"statusCode": err.statusCode, | |
"message": err.message, | |
"code": err.code, | |
}; | |
reject(JSON.stringify(result)); | |
} | |
resolve(data); | |
}); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment