more no workie
This commit is contained in:
parent
efdc51ab85
commit
0e8a2bbd2c
27
src/Request.js
Normal file
27
src/Request.js
Normal file
@ -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);
|
||||
}
|
||||
}
|
@ -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}`));
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
24
src/main.js
24
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)
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user