Created
September 4, 2018 13:01
-
-
Save macdonst/d66f4d085e8171ea86d986f7b19ff5d5 to your computer and use it in GitHub Desktop.
MS Azure Function to call Google Translate
This file contains hidden or 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
const fetch = require('node-fetch'); | |
const credentials = require('./credentials'); | |
module.exports = async function(context, req) { | |
if (req.query.searchTerm || (req.body && req.body.searchTerm)) { | |
const searchTerm = req.query.searchTerm || | |
req.body.searchTerm; | |
let url = `https://www.googleapis.com/language/translate/v2?key=${ | |
credentials.google | |
}&source=en&target=fr&q=${searchTerm}`; | |
await fetch(url) | |
.then(res => res.json()) | |
.then(json => { | |
context.res = { | |
status: 200, | |
headers: { 'Content-Type': 'application/json' }, | |
body: json | |
}; | |
context.done(); | |
}); | |
} else { | |
context.res = { | |
status: 400, | |
body: 'Please pass a name on the query string or in the request body' | |
}; | |
} | |
context.done(); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment