Last active
May 20, 2019 14:12
-
-
Save rosiegrant/5da201b00886b6d1b6ea558e2fed2155 to your computer and use it in GitHub Desktop.
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'; | |
// load AWS SDK module, which is always included in the runtime environment | |
const AWS = require('aws-sdk'); | |
// define the Guru API as a "service" | |
const svc = new AWS.Service({ | |
//Guru base URL | |
endpoint: 'https://api.getguru.com/api/v1', | |
//not really sure what this does | |
convertResponseTypes: false, | |
apiConfig: { | |
metadata: { | |
protocol: 'rest-json' // API is JSON-based | |
}, | |
operations: { | |
// get a card | |
getCard: { | |
http: { | |
method: 'GET', | |
requestUri: '/search/query' | |
}, | |
input: { | |
type: 'structure', | |
required: ['query', 'authorization'], | |
members: { | |
'query': { | |
// include the query as param in the call to Guru | |
location: 'querystring', | |
// param name | |
locationName: 'searchTerms' | |
}, | |
'maxResults': { | |
location: 'querystring', | |
locationName: 'maxResults' | |
}, | |
'authorization': { // guru authorization header | |
location: 'header', | |
locationName: 'Authorization' | |
} | |
} | |
} | |
} | |
} | |
} | |
}); | |
// disable AWS region related login in the SDK <- wut is that | |
svc.isGlobalEndpoint = true; | |
function formatCards(cards) { | |
var cardArray = []; | |
var url = ""; | |
for (var n in cards) { | |
if (cards.hasOwnProperty(n)) { | |
url = "https://app.getguru.com/card/" + cards[n].slug; | |
cardArray.push({ | |
"title" : cards[n].preferredPhrase, | |
"url" : url, | |
"collection" : cards[n].collection.name, | |
"verified" : cards[n].lastVerified | |
}); | |
} | |
} | |
return cardArray; | |
} | |
exports.handler = function(event, context, callback) { | |
//Assume we're getting the query params passed as URL-encoded json. | |
//Parse the json and access the "input" key | |
var decodedParams = JSON.parse(decodeURIComponent(event.queryStringParameters.body)); | |
var userQuery = decodedParams['input']; | |
var basicAuthCredentials = 'Basic ' + Buffer.from('authcredentialsremovedfromgist').toString('base64'); | |
//lol | |
var fiveInt = 5; | |
var fiveString = fiveInt.toString(); | |
svc.getCard({ | |
query: userQuery, | |
maxResults: fiveString, | |
authorization: basicAuthCredentials | |
}, (err, data) => { | |
if (err) { | |
console.log('whoops'); | |
return callback(err); | |
} | |
// construct an array of cards to send in the response | |
var cardArray = formatCards(data); | |
const response = { | |
statusCode: 200, | |
body: JSON.stringify({ | |
"results": cardArray, | |
"totalCount": cardArray.length, | |
"encodedState" : "" | |
}) | |
}; | |
callback(null, response); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment