Kinda works
This commit is contained in:
parent
cce1d6c730
commit
52fa232722
src/components
|
@ -6,17 +6,24 @@ class ApiController {
|
|||
|
||||
async getApiurl() {
|
||||
let response = await fetch(`${window.location.href}apiurl`)
|
||||
this.apiurl = await response.text();
|
||||
console.log(this.apiurl);
|
||||
if(await response.ok) {
|
||||
this.apiurl = await response.text();
|
||||
console.log(this.apiurl);
|
||||
} else {
|
||||
console.log("Failed to get apiurl");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
async login(username: string , password: string) {
|
||||
let promise = await fetch(`${this.apiurl}/token`, {
|
||||
let promise = await fetch(`http://localhost:8000/token/`, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"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({
|
||||
username: username,
|
||||
|
@ -24,47 +31,48 @@ class ApiController {
|
|||
})
|
||||
|
||||
});
|
||||
if(promise.ok) {
|
||||
if(await promise.ok) {
|
||||
this.token = (await promise.json())['access_token'];
|
||||
console.log(this.token);
|
||||
} else {
|
||||
console.log("Failed to login");
|
||||
}
|
||||
}
|
||||
|
||||
async get(path) {
|
||||
console.log(this.token);
|
||||
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"
|
||||
"Access-Control-Allow-Origin": "http://localhost:8000",
|
||||
"Access-Control-Allow-Methods": "GET",
|
||||
"Access-Control-Allow-Credentials": "true",
|
||||
}
|
||||
});
|
||||
if(promise.ok) {
|
||||
if(await 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',
|
||||
async post(path){
|
||||
let promise = await fetch(`${this.apiurl}${path}/`, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"accept": "application/json",
|
||||
"Access-Control-Allow-Origin": "*",
|
||||
"Access-Control-Allow-Methods": "POST",
|
||||
"Authorization": `Bearer ${this.token}`,
|
||||
|
||||
},
|
||||
"accept": "application/json",
|
||||
"Authorization": `Bearer ${this.token}`,
|
||||
"Access-Control-Allow-Origin": "http://localhost:8000",
|
||||
"Access-Control-Allow-Methods": "POST",
|
||||
"Access-Control-Allow-Credentials": "true",
|
||||
}
|
||||
});
|
||||
console.log(promise.status, promise.statusText);
|
||||
if(promise.ok) {
|
||||
if(await promise.ok) {
|
||||
let data = await promise.json();
|
||||
console.log(data);
|
||||
return data;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -13,10 +13,13 @@ export class AppHome {
|
|||
}
|
||||
|
||||
async start() {
|
||||
await Api.login('test', 'prozac-prowler-stoop-patriot');
|
||||
await Api.login('test', 'test');
|
||||
let data = await Api.get("/users/me");
|
||||
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() {
|
||||
|
|
Loading…
Reference in New Issue