diff --git a/src/Request.js b/src/Request.js new file mode 100644 index 0000000..e1cedcc --- /dev/null +++ b/src/Request.js @@ -0,0 +1,27 @@ +module.exports = class Request { + constructor() { + this.completed = false; + } + + get(url, callback) { + const request = new XMLHttpRequest(); + + // Set up the callback for when the response comes in + request.onreadystatechange = () => { + if (request.readyState === 4) { + this.completed = true; + if (request.status === 200) { + callback(null, request.responseText); + } else { + callback(new Error(`Request failed with status ${request.status}: ${request.statusText}`), null); + } + } + } + + // Open the request, as asynchronous + request.open("GET", url, true); + + // Send the request + request.send(null); + } +} \ No newline at end of file diff --git a/src/TenorJSv2.js b/src/TenorJSv2.js index 6827984..9350b97 100644 --- a/src/TenorJSv2.js +++ b/src/TenorJSv2.js @@ -1,24 +1,59 @@ +const Request = require('./Request'); + module.exports = { Tenor: class Tenor { constructor(tenorApiKey) { this.token = tenorApiKey; } - configure() { + async configure() { + // Sanity tests + if (!this.token) { + throw new Error('No API key provided'); + } - } - }, - Request: class Request { - constructor() { + // Search with an invalid key + try { + const response = await this.search('invalid', 'query', 1); + if (response.code !== 16) { + throw new Error('Unexpected response from known-bad API query.'); + } + } catch (error) { + throw new Error(`Error searching with invalid key: ${error.message}`); + } + // Search with a valid key + try { + const response = await this.search(this.token, 'cat', 1); + if (response.code === 16) { + throw new Error(response.error); + } else if (response.results.length !== 1) { + throw new Error('Unexpected response from known-good API query.'); + } + } catch (error) { + throw new Error(`Error searching with valid key: ${error.message}`); + } } - get() { - - } - - post() { - + search(token, query, limit) { + return new Promise((resolve, reject) => { + // Form the search url + const searchUrl = `https://g.tenor.com/v1/search?q=${query}&key=${token}&limit=${limit}`; + + // Submit the search + new Request().get(searchUrl, (error, response) => { + if (error) { + return reject(new Error(`Error fetching GIFs: ${error.message}`)); + } + try { + // Parse the json response + const responseJson = JSON.parse(response); + resolve(responseJson); + } catch (parseError) { + reject(new Error(`Error parsing response: ${parseError.message}`)); + } + }); + }); } } } \ No newline at end of file diff --git a/src/main.js b/src/main.js index 052a641..19369f4 100644 --- a/src/main.js +++ b/src/main.js @@ -1,27 +1,3 @@ -// url Async requesting function -function httpGetAsync(theUrl, callback) -{ - // create the request object - var xmlHttp = new XMLHttpRequest(); - - // set the state change callback to capture when the response comes in - xmlHttp.onreadystatechange = function() - { - if (xmlHttp.readyState == 4 && xmlHttp.status == 200) - { - callback(xmlHttp.responseText); - } - } - - // open as a GET call, pass in the url and set async = True - xmlHttp.open("GET", theUrl, true); - - // call send with no params as they were passed in on the url string - xmlHttp.send(null); - - return; -} - // callback for the top 8 GIFs of search function tenorCallback_search(responsetext) {