74 lines
2.0 KiB
TypeScript
74 lines
2.0 KiB
TypeScript
|
|
class ApiController {
|
|
token: string;
|
|
apiurl: string;
|
|
|
|
|
|
async getApiurl() {
|
|
let response = await fetch(`${window.location.href}apiurl`)
|
|
this.apiurl = await response.text();
|
|
console.log(this.apiurl);
|
|
}
|
|
|
|
|
|
async login(username: string , password: string) {
|
|
let promise = await fetch(`${this.apiurl}/token`, {
|
|
method: "POST",
|
|
headers: {
|
|
"Content-Type": "application/x-www-form-urlencoded",
|
|
"accept": "application/json",
|
|
},
|
|
body: new URLSearchParams({
|
|
username: username,
|
|
password: password
|
|
})
|
|
|
|
});
|
|
if(promise.ok) {
|
|
this.token = (await promise.json())['access_token'];
|
|
console.log(this.token);
|
|
}
|
|
}
|
|
|
|
async get(path) {
|
|
let promise = await fetch(`${this.apiurl}${path}/`, {
|
|
method: "GET",
|
|
headers: {
|
|
"accept": "application/json",
|
|
"Authorization": `Bearer ${this.token}`,
|
|
"Access-Control-Allow-Origin": this.apiurl,
|
|
"Access-Control-Allow-Methods": "GET"
|
|
}
|
|
});
|
|
if(promise.ok) {
|
|
let data = await promise.json();
|
|
return data;
|
|
}
|
|
}
|
|
|
|
async post(path) {
|
|
let url = `${this.apiurl}/server/dummy/stop/`;
|
|
console.log(url);
|
|
let promise = await fetch(url, {
|
|
method: "POST",
|
|
mode: 'no-cors',
|
|
headers: {
|
|
"accept": "application/json",
|
|
"Access-Control-Allow-Origin": "*",
|
|
"Access-Control-Allow-Methods": "POST",
|
|
"Authorization": `Bearer ${this.token}`,
|
|
|
|
},
|
|
});
|
|
console.log(promise.status, promise.statusText);
|
|
if(promise.ok) {
|
|
let data = await promise.json();
|
|
console.log(data);
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
|
|
export const Api = new ApiController();
|