ft_transcendence/frontend/static/js/api/client.js

125 lines
2.5 KiB
JavaScript
Raw Normal View History

2023-11-30 19:29:56 -05:00
import { Account } from "./account.js";
2023-12-06 09:19:41 -05:00
import { Profile } from "./profile.js";
2023-12-08 11:36:41 -05:00
import { Profiles } from "./profiles.js";
2023-11-30 19:29:56 -05:00
function getCookie(name)
{
let cookie = {};
document.cookie.split(';').forEach(function(el) {
2023-12-04 06:32:15 -05:00
let split = el.split('=');
cookie[split[0].trim()] = split.slice(1).join("=");
2023-11-30 19:29:56 -05:00
})
return cookie[name];
}
2023-11-27 09:32:17 -05:00
2023-11-23 11:26:09 -05:00
class Client
{
constructor(url)
{
this._url = url;
2023-11-30 19:29:56 -05:00
this.account = new Account(this);
2023-12-08 11:36:41 -05:00
this.profiles = new Profiles(this);
2023-11-29 10:05:49 -05:00
this._logged = undefined;
2023-11-23 11:26:09 -05:00
}
2023-11-29 10:05:49 -05:00
async isAuthentificate()
2023-11-23 11:26:09 -05:00
{
2023-11-29 10:05:49 -05:00
if (this._logged == undefined)
this.logged = await this._test_logged();
return this.logged;
}
async _get(uri)
{
let response = await fetch(this._url + uri, {
method: "GET",
});
return response;
2023-11-23 11:26:09 -05:00
}
async _post(uri, data)
{
let response = await fetch(this._url + uri, {
method: "POST",
headers: {
"Content-Type": "application/json",
2023-11-30 19:29:56 -05:00
"X-CSRFToken": getCookie("csrftoken"),
},
body: JSON.stringify(data),
});
return response;
}
async _delete(uri, data)
{
let response = await fetch(this._url + uri, {
method: "DELETE",
headers: {
"Content-Type": "application/json",
"X-CSRFToken": getCookie("csrftoken"),
},
body: JSON.stringify(data),
});
return response;
}
async _patch_json(uri, data)
{
let response = await fetch(this._url + uri, {
method: "PATCH",
headers: {
"X-CSRFToken": getCookie("csrftoken"),
"Content-Type": "application/json",
2023-11-29 10:05:49 -05:00
},
2023-11-23 11:26:09 -05:00
body: JSON.stringify(data),
});
return response;
}
2023-12-06 09:19:41 -05:00
async _patch_file(uri, file)
{
let response = await fetch(this._url + uri, {
method: "PATCH",
headers: {
"X-CSRFToken": getCookie("csrftoken"),
},
body: file,
});
return response;
}
2023-11-23 11:26:09 -05:00
async login(username, password)
{
2023-11-27 09:32:17 -05:00
let response = await this._post("/api/accounts/login", {username: username, password: password})
2023-11-29 10:05:49 -05:00
let data = await response.json();
2023-12-06 09:19:41 -05:00
if (data.id != undefined)
2023-11-29 10:05:49 -05:00
{
2023-12-06 09:19:41 -05:00
this.me = new Profile(this)
await this.me.init(data.id)
2023-11-29 10:05:49 -05:00
this.logged = true;
return null;
}
return data;
}
async logout()
{
await this._get("/api/accounts/logout");
this.logged = false;
}
async _test_logged()
{
let response = await this._get("/api/accounts/logged");
let data = await response.json();
2023-12-06 09:19:41 -05:00
if (data.id !== undefined)
{
this.me = new Profile(this)
await this.me.init(data.id)
}
return data.id !== undefined;
2023-11-23 11:26:09 -05:00
}
}
export {Client}