2022-12-13 00:09:06 +00:00
|
|
|
class ResponseError extends Error {
|
|
|
|
constructor(message) {
|
|
|
|
super(message);
|
2022-12-13 00:11:00 +00:00
|
|
|
this.name = "ResponseError";
|
2022-12-13 00:09:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class NetworkError extends Error {
|
|
|
|
constructor(message) {
|
|
|
|
super(message);
|
2022-12-13 00:11:00 +00:00
|
|
|
this.name = "NetworkError";
|
2022-12-13 00:09:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-11 06:49:10 +00:00
|
|
|
export async function requestTicket (username, password) {
|
|
|
|
let prms = new URLSearchParams({username: `${username}@pve`, password: password});
|
2022-12-13 00:00:11 +00:00
|
|
|
|
|
|
|
let content = {
|
2022-12-11 06:49:10 +00:00
|
|
|
method: "POST",
|
|
|
|
mode: "cors",
|
|
|
|
credentials: "include",
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/x-www-form-urlencoded"
|
|
|
|
},
|
|
|
|
body: prms.toString()
|
2022-12-13 00:00:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let response = await fetch("https://pve.tronnet.net/api2/json/access/ticket", content)
|
2022-12-12 23:56:57 +00:00
|
|
|
.then((response) => {
|
|
|
|
if (!response.ok) {
|
2022-12-13 00:09:06 +00:00
|
|
|
throw new ResponseError(`recieved response status code ${response.status}`);
|
2022-12-12 23:56:57 +00:00
|
|
|
}
|
|
|
|
return response;
|
|
|
|
})
|
|
|
|
.catch((error) => {
|
2022-12-13 00:09:57 +00:00
|
|
|
if (error instanceof ResponseError) {
|
2022-12-13 00:09:06 +00:00
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
throw new NetworkError(error);
|
2022-12-11 06:49:10 +00:00
|
|
|
});
|
2022-12-13 00:09:06 +00:00
|
|
|
|
2022-12-11 06:49:10 +00:00
|
|
|
let data = await response.json();
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function setTicket (ticket) {
|
|
|
|
let d = new Date();
|
|
|
|
d.setTime(d.getTime() + (2*60*60*1000));
|
|
|
|
document.cookie = `PVEAuthCookie=${ticket}; path=/; expires=${d.toUTCString()}; domain=.tronnet.net`;
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function request (path, method, body) {
|
|
|
|
let prms = new URLSearchParams(body);
|
2022-12-13 00:00:11 +00:00
|
|
|
|
|
|
|
let content = {
|
2022-12-11 06:49:10 +00:00
|
|
|
method: method,
|
|
|
|
mode: "cors",
|
|
|
|
credentials: "include",
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/x-www-form-urlencoded",
|
|
|
|
"Cookie": document.cookie
|
|
|
|
}
|
2022-12-13 00:00:11 +00:00
|
|
|
}
|
2022-12-11 06:49:10 +00:00
|
|
|
if(method == "POST") {
|
2022-12-13 00:00:11 +00:00
|
|
|
content.body = prms.toString();
|
2022-12-11 06:49:10 +00:00
|
|
|
}
|
2022-12-13 00:00:11 +00:00
|
|
|
|
|
|
|
let response = await fetch(`https://pve.tronnet.net/api2/json${path}`, content)
|
|
|
|
.then((response) => {
|
|
|
|
if (!response.ok) {
|
2022-12-13 00:09:06 +00:00
|
|
|
throw new ResponseError(`recieved response status code ${response.status}`);
|
2022-12-13 00:00:11 +00:00
|
|
|
}
|
|
|
|
return response;
|
|
|
|
})
|
|
|
|
.catch((error) => {
|
2022-12-13 00:09:57 +00:00
|
|
|
if (error instanceof ResponseError) {
|
2022-12-13 00:09:06 +00:00
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
throw new NetworkError(error);
|
2022-12-13 00:00:11 +00:00
|
|
|
});
|
|
|
|
|
2022-12-11 06:49:10 +00:00
|
|
|
let data = await response.json();
|
|
|
|
return data;
|
|
|
|
}
|