-
Notifications
You must be signed in to change notification settings - Fork 2
/
apiManager.js
68 lines (62 loc) · 2.41 KB
/
apiManager.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
const request = require('request-promise');
class ApiManager {
constructor() {}
// Recieve type ("id" / "name" / "type" / "height" / "weight") and value (number or string)
// Return an object with a key of "status" (status code) and a key of "body" (array of objects with the filtered pokemons)
async getPokemons(type, value) {
let response = {};
await request.get('http://localhost:3000/pokemon', { form: { type, value } }, function(
err,
http,
body
) {
response.body = body.length == 2 ? 'Error: The pokemon was not found' : JSON.parse(body);
response.status = http.statusCode;
});
return response;
}
// Recieve id (number), name (string), type (string), height (number), weight (number)
// Return an object with a key of "status" (status code) and a key of "body" (a message)
async postPokemon(id, name, type, height, weight) {
let response = {};
await request.post(
'http://localhost:3000/pokemon',
{ form: { id, name, type, height, weight } },
function(err, http, body) {
response.status = http.statusCode;
response.body = body;
}
);
return response;
}
// Recieve type ("id" / "name" / "type" / "height" / "weight"), value (number or string) - and find ONE pokemon that has those attributes
// Recieve changedType ("id" / "name" / "type" / "height" / "weight"), changedValue (number or string) - and change that attribute for the poken that was found
// Return an object with a key of "status" (status code) and a key of "body" (a message)
async putPokemon(type, value, changedType, changedValue) {
let response = {};
await request.put(
'http://localhost:3000/pokemon',
{ form: { type, value, changedType, changedValue } },
function(err, http, body) {
response.status = http.statusCode;
response.body = body;
}
);
return response;
}
// Recieve type ("id" / "name" / "type" / "height" / "weight") and value (number or string) and delete the pokemon
// Return an object with a key of "status" (status code) and a key of "body" (a message)
async deletePokemon(type, value) {
let response = {};
await request.delete('http://localhost:3000/pokemon', { form: { type, value } }, function(
err,
http,
body
) {
response.status = http.statusCode;
response.body = body;
});
return response;
}
}
module.exports = ApiManager;