remove repeated code

This commit is contained in:
Arthur Lu 2022-12-12 16:18:40 -08:00
parent 81e9c28a7a
commit 12a733f556
2 changed files with 8 additions and 30 deletions

View File

@ -75,7 +75,7 @@ class Instance extends HTMLElement {
let instanceParagraph = document.createElement("p"); let instanceParagraph = document.createElement("p");
instanceParagraph.innerText = `CT | ${data.vmid} | ${data.name} | ${data.status}`; instanceParagraph.innerText = `CT | ${data.vmid} | ${data.name} | ${data.status}`;
instanceParagraph.style.color = data.status == "running" ? "#00ff00" : "#ff0000"; instanceParagraph.style.color = data.status === "running" ? "#00ff00" : "#ff0000";
instanceDiv.append(instanceParagraph); instanceDiv.append(instanceParagraph);
} }
} }

View File

@ -13,31 +13,7 @@ class NetworkError extends Error {
} }
export async function requestTicket (username, password) { export async function requestTicket (username, password) {
let prms = new URLSearchParams({username: `${username}@pve`, password: password}); let response = await request("/access/ticket", "POST", {username: `${username}@pve`, password: password}, auth = false);
let content = {
method: "POST",
mode: "cors",
credentials: "include",
headers: {
"Content-Type": "application/x-www-form-urlencoded"
},
body: prms.toString()
}
let response = await fetch("https://pve.tronnet.net/api2/json/access/ticket", content)
.then((response) => {
if (!response.ok) {
throw new ResponseError(`recieved response status code ${response.status}`);
}
return response;
})
.catch((error) => {
if (error instanceof ResponseError) {
throw error;
}
throw new NetworkError(error);
});
let data = await response.json(); let data = await response.json();
return data; return data;
@ -49,7 +25,7 @@ export function setTicket (ticket) {
document.cookie = `PVEAuthCookie=${ticket}; path=/; expires=${d.toUTCString()}; domain=.tronnet.net`; document.cookie = `PVEAuthCookie=${ticket}; path=/; expires=${d.toUTCString()}; domain=.tronnet.net`;
} }
export async function request (path, method, body) { export async function request (path, method, body, auth = true) {
let prms = new URLSearchParams(body); let prms = new URLSearchParams(body);
let content = { let content = {
@ -57,13 +33,15 @@ export async function request (path, method, body) {
mode: "cors", mode: "cors",
credentials: "include", credentials: "include",
headers: { headers: {
"Content-Type": "application/x-www-form-urlencoded", "Content-Type": "application/x-www-form-urlencoded"
"Cookie": document.cookie
} }
} }
if(method == "POST") { if(method === "POST") {
content.body = prms.toString(); content.body = prms.toString();
} }
if(auth) {
content.headers.Cookie = document.cookie;
}
let response = await fetch(`https://pve.tronnet.net/api2/json${path}`, content) let response = await fetch(`https://pve.tronnet.net/api2/json${path}`, content)
.then((response) => { .then((response) => {