Kinda works
This commit is contained in:
parent
cce1d6c730
commit
52fa232722
|
@ -6,17 +6,24 @@ class ApiController {
|
||||||
|
|
||||||
async getApiurl() {
|
async getApiurl() {
|
||||||
let response = await fetch(`${window.location.href}apiurl`)
|
let response = await fetch(`${window.location.href}apiurl`)
|
||||||
this.apiurl = await response.text();
|
if(await response.ok) {
|
||||||
console.log(this.apiurl);
|
this.apiurl = await response.text();
|
||||||
|
console.log(this.apiurl);
|
||||||
|
} else {
|
||||||
|
console.log("Failed to get apiurl");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
async login(username: string , password: string) {
|
async login(username: string , password: string) {
|
||||||
let promise = await fetch(`${this.apiurl}/token`, {
|
let promise = await fetch(`http://localhost:8000/token/`, {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: {
|
headers: {
|
||||||
"Content-Type": "application/x-www-form-urlencoded",
|
"Content-Type": "application/x-www-form-urlencoded",
|
||||||
"accept": "application/json",
|
"accept": "application/json",
|
||||||
|
"access-control-allow-origin": "*",
|
||||||
|
"Access-Control-Allow-Methods": "POST",
|
||||||
|
"Access-Control-Allow-Credentials": "true",
|
||||||
},
|
},
|
||||||
body: new URLSearchParams({
|
body: new URLSearchParams({
|
||||||
username: username,
|
username: username,
|
||||||
|
@ -24,47 +31,48 @@ class ApiController {
|
||||||
})
|
})
|
||||||
|
|
||||||
});
|
});
|
||||||
if(promise.ok) {
|
if(await promise.ok) {
|
||||||
this.token = (await promise.json())['access_token'];
|
this.token = (await promise.json())['access_token'];
|
||||||
console.log(this.token);
|
console.log(this.token);
|
||||||
|
} else {
|
||||||
|
console.log("Failed to login");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async get(path) {
|
async get(path) {
|
||||||
|
console.log(this.token);
|
||||||
let promise = await fetch(`${this.apiurl}${path}/`, {
|
let promise = await fetch(`${this.apiurl}${path}/`, {
|
||||||
method: "GET",
|
method: "GET",
|
||||||
headers: {
|
headers: {
|
||||||
"accept": "application/json",
|
"accept": "application/json",
|
||||||
"Authorization": `Bearer ${this.token}`,
|
"Authorization": `Bearer ${this.token}`,
|
||||||
"Access-Control-Allow-Origin": this.apiurl,
|
"Access-Control-Allow-Origin": "http://localhost:8000",
|
||||||
"Access-Control-Allow-Methods": "GET"
|
"Access-Control-Allow-Methods": "GET",
|
||||||
|
"Access-Control-Allow-Credentials": "true",
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
if(promise.ok) {
|
if(await promise.ok) {
|
||||||
let data = await promise.json();
|
let data = await promise.json();
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async post(path) {
|
async post(path){
|
||||||
let url = `${this.apiurl}/server/dummy/stop/`;
|
let promise = await fetch(`${this.apiurl}${path}/`, {
|
||||||
console.log(url);
|
method: "POST",
|
||||||
let promise = await fetch(url, {
|
|
||||||
method: "POST",
|
|
||||||
mode: 'no-cors',
|
|
||||||
headers: {
|
headers: {
|
||||||
"accept": "application/json",
|
"accept": "application/json",
|
||||||
"Access-Control-Allow-Origin": "*",
|
"Authorization": `Bearer ${this.token}`,
|
||||||
"Access-Control-Allow-Methods": "POST",
|
"Access-Control-Allow-Origin": "http://localhost:8000",
|
||||||
"Authorization": `Bearer ${this.token}`,
|
"Access-Control-Allow-Methods": "POST",
|
||||||
|
"Access-Control-Allow-Credentials": "true",
|
||||||
},
|
}
|
||||||
});
|
});
|
||||||
console.log(promise.status, promise.statusText);
|
if(await promise.ok) {
|
||||||
if(promise.ok) {
|
|
||||||
let data = await promise.json();
|
let data = await promise.json();
|
||||||
console.log(data);
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -13,10 +13,13 @@ export class AppHome {
|
||||||
}
|
}
|
||||||
|
|
||||||
async start() {
|
async start() {
|
||||||
await Api.login('test', 'prozac-prowler-stoop-patriot');
|
await Api.login('test', 'test');
|
||||||
let data = await Api.get("/users/me");
|
let data = await Api.get("/users/me");
|
||||||
console.log(data);
|
console.log(data);
|
||||||
await Api.post("/server/dummy/start");
|
data = await Api.post("/server/dummy/start");
|
||||||
|
console.log(data);
|
||||||
|
data = await Api.post("/server/dummy/stop");
|
||||||
|
console.log(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
|
|
Loading…
Reference in New Issue