Compare commits

...

4 Commits

Author SHA1 Message Date
52fa232722 Kinda works 2022-01-21 09:16:03 +00:00
cce1d6c730 Add get and post request 2022-01-21 13:55:32 +13:00
6789fcfd35 Test api 2022-01-21 13:55:16 +13:00
0b80fe4a93 Get api url 2022-01-21 13:54:18 +13:00
3 changed files with 65 additions and 21 deletions

View File

@@ -1,46 +1,81 @@
class ApiController {
username: string;
password: string;
token: string;
apiurl: string;
constructor() {
this.username = 'test';
this.password = 'prozac-prowler-stoop-patriot';
}
async getApiurl() {
let response = await fetch(`${window.location.href}apiurl`)
if(await response.ok) {
this.apiurl = await response.text();
console.log(this.apiurl);
} else {
console.log("Failed to get apiurl");
}
}
async login() {
let promise = await fetch(`${this.apiurl}/token`, {
async login(username: string , password: string) {
let promise = await fetch(`http://localhost:8000/token/`, {
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
"accept": "application/json",
"access-control-allow-origin": "*",
"Access-Control-Allow-Methods": "POST",
"Access-Control-Allow-Credentials": "true",
},
body: new URLSearchParams({
username: this.username,
password: this.password
username: username,
password: password
})
});
if(promise.ok) {
if(await promise.ok) {
this.token = (await promise.json())['access_token'];
console.log(this.token);
} else {
console.log("Failed to login");
}
}
setToken(token: string) {
this.token = token;
}
getToken() :string {
return this.token;
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": "http://localhost:8000",
"Access-Control-Allow-Methods": "GET",
"Access-Control-Allow-Credentials": "true",
}
});
if(await promise.ok) {
let data = await promise.json();
return data;
}
}
async post(path){
let promise = await fetch(`${this.apiurl}${path}/`, {
method: "POST",
headers: {
"accept": "application/json",
"Authorization": `Bearer ${this.token}`,
"Access-Control-Allow-Origin": "http://localhost:8000",
"Access-Control-Allow-Methods": "POST",
"Access-Control-Allow-Credentials": "true",
}
});
if(await promise.ok) {
let data = await promise.json();
return data;
}
}
}
export const Api = new ApiController();

View File

@@ -8,13 +8,20 @@ import { Api } from '../api';
})
export class AppHome {
constructor() {
this.start()
this.start();
}
async start() {
await Api.getApiurl();
await Api.login();
await Api.login('test', 'test');
let data = await Api.get("/users/me");
console.log(data);
data = await Api.post("/server/dummy/start");
console.log(data);
data = await Api.post("/server/dummy/stop");
console.log(data);
}
render() {
return (
<div class="app-home">

View File

@@ -1,7 +1,9 @@
import { Api } from "../components/api"
export default async () => {
/**
* The code to be executed should be placed within a default function that is
* exported by the global script. Ensure all of the code in the global script
* is wrapped in the function() that is exported.
*/
Api.getApiurl();
};