This repository has been archived by the owner on Jun 9, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathApi.js
58 lines (51 loc) · 1.68 KB
/
Api.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
const METHOD_GET = "GET";
const METHOD_POST = "POST";
const API_GRAPHICS_URL = "http://thetvdb.com/banners/";
const API_POSTERS_URL = API_GRAPHICS_URL + "posters/";
export const API_URL = "https://api.thetvdb.com";
export const API_KEY = "51P59HCWHL5GJ46G";
class api {
constructor() {
this.token = null;
}
login = () => this._exec(METHOD_POST, "/login", {apikey: API_KEY});
getSeriesDetails = id => this._exec(METHOD_GET, "/series/" + id + "/episodes");
searchSeries = name => this._exec(METHOD_GET, "/search/series?name=" + name);
getSeriesImage = id => API_POSTERS_URL + id + "-1.jpg";
getEpisodeImage = name => API_GRAPHICS_URL + name;
_exec = (method, path, body) => {
let headers = {
"Content-Type": "application/json",
"Accept": "application/json",
// API: At the moment, you may only pass one language abbreviation in the header at a time
"Accept-Language": "en",
};
if (this.token) {
headers["Authorization"] = "Bearer " + this.token;
}
let options = {
method: method,
headers: headers,
};
if (method === METHOD_POST) {
options.body = JSON.stringify(body);
}
console.log("Api executing", API_URL + path, options);
return new Promise((resolve, reject) => {
fetch(API_URL + path, options)
.then(response => {
if (response.status === 200) {
return response.json();
} else {
throw new Error(response.status + " " + response._bodyText);
}
})
.then(json => {
console.log("Api response", json);
resolve(json);
})
.catch(error => reject(error));
})
}
}
export default new api();