Compare commits

...

3 Commits

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

View File

@ -1,13 +1,8 @@
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`)
@ -16,7 +11,7 @@ class ApiController {
}
async login() {
async login(username: string , password: string) {
let promise = await fetch(`${this.apiurl}/token`, {
method: "POST",
headers: {
@ -24,8 +19,8 @@ class ApiController {
"accept": "application/json",
},
body: new URLSearchParams({
username: this.username,
password: this.password
username: username,
password: password
})
});
@ -34,13 +29,45 @@ class ApiController {
console.log(this.token);
}
}
setToken(token: string) {
this.token = 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;
}
}
getToken() :string {
return this.token;
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();

View File

@ -8,13 +8,17 @@ import { Api } from '../api';
})
export class AppHome {
constructor() {
this.start()
this.start();
}
async start() {
await Api.getApiurl();
await Api.login();
await Api.login('test', 'prozac-prowler-stoop-patriot');
let data = await Api.get("/users/me");
console.log(data);
await Api.post("/server/dummy/start");
}
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();
};