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

138 lines
2.8 KiB
JavaScript
Raw Normal View History

2023-11-30 19:29:56 -05:00
import { Account } from "./account.js";
2023-12-12 06:06:13 -05:00
import { MatchMaking } from "./matchmaking.js";
2023-12-08 11:36:41 -05:00
import { Profiles } from "./profiles.js";
2023-12-15 14:32:43 -05:00
import { Channels } from './chat/channels.js';
2023-12-16 12:00:38 -05:00
import { MyProfile } from "./MyProfile.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-12-12 06:06:13 -05:00
this.matchmaking = new MatchMaking(this);
2023-11-29 10:05:49 -05:00
this._logged = undefined;
2023-12-15 14:32:43 -05:00
this.channels = new Channels(this);
this.channel = 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;
}
2023-12-20 17:48:52 -05:00
async _get(uri, data)
2023-11-29 10:05:49 -05:00
{
let response = await fetch(this._url + uri, {
method: "GET",
2023-12-20 17:48:52 -05:00
body: JSON.stringify(data),
2023-11-29 10:05:49 -05:00
});
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-12-16 12:00:38 -05:00
async _update_logged(state)
{
2023-12-18 15:27:10 -05:00
if (!this.logged && state)
2023-12-16 12:00:38 -05:00
{
this.me = new MyProfile(this);
await this.me.init();
}
this.logged = state;
}
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-16 12:00:38 -05:00
await this._update_logged(true);
2023-11-29 10:05:49 -05:00
return null;
}
return data;
}
async logout()
{
await this._get("/api/accounts/logout");
2023-12-16 12:00:38 -05:00
await this._update_logged(false);
2023-11-29 10:05:49 -05:00
}
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)
2023-12-16 12:00:38 -05:00
await this._update_logged(true);
2023-12-06 09:19:41 -05:00
return data.id !== undefined;
2023-11-23 11:26:09 -05:00
}
}
2023-12-11 04:53:34 -05:00
export {Client}