Add get and post request
This commit is contained in:
parent
6789fcfd35
commit
cce1d6c730
|
@ -1,13 +1,8 @@
|
||||||
|
|
||||||
class ApiController {
|
class ApiController {
|
||||||
username: string;
|
|
||||||
password: string;
|
|
||||||
token: string;
|
token: string;
|
||||||
apiurl: string;
|
apiurl: string;
|
||||||
constructor() {
|
|
||||||
this.username = 'test';
|
|
||||||
this.password = 'prozac-prowler-stoop-patriot';
|
|
||||||
}
|
|
||||||
|
|
||||||
async getApiurl() {
|
async getApiurl() {
|
||||||
let response = await fetch(`${window.location.href}apiurl`)
|
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`, {
|
let promise = await fetch(`${this.apiurl}/token`, {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: {
|
headers: {
|
||||||
|
@ -24,8 +19,8 @@ class ApiController {
|
||||||
"accept": "application/json",
|
"accept": "application/json",
|
||||||
},
|
},
|
||||||
body: new URLSearchParams({
|
body: new URLSearchParams({
|
||||||
username: this.username,
|
username: username,
|
||||||
password: this.password
|
password: password
|
||||||
})
|
})
|
||||||
|
|
||||||
});
|
});
|
||||||
|
@ -34,13 +29,45 @@ class ApiController {
|
||||||
console.log(this.token);
|
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 {
|
async post(path) {
|
||||||
return this.token;
|
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();
|
export const Api = new ApiController();
|
||||||
|
|
Loading…
Reference in New Issue