File: /var/www/vhost/disk-apps/pwa.sports-crowd.com/src/app/services/api.service.ts
import { Injectable, Inject, forwardRef } from "@angular/core";
import { HttpClient, HttpParams, HttpHeaders } from "@angular/common/http";
import { CityService } from "./city.service";
import { StorageService } from "./storage.service";
@Injectable({
providedIn: "root",
})
export class ApiService {
constructor(
public http: HttpClient,
@Inject(forwardRef(() => CityService)) public cityService: CityService,
private storageService: StorageService
) {
}
public getUrl() {
return this.cityService._urlAdmin;
}
async get(endpoint: string, token?: any, params?: any, reqOpts?: any) {
const myToken = await this.getAccessToken();
try {
token = myToken || token;
} catch (error) {
console.log("unauthorized", endpoint);
}
let headers = new HttpHeaders({
Authorization: "Bearer " + token,
});
if (!reqOpts) {
reqOpts = {
params: new HttpParams(),
headers: headers,
};
}
// Support easy query params for GET requests
if (params) {
reqOpts.params = new HttpParams();
for (let k in params) {
reqOpts.params = reqOpts.params.set(k, params[k]);
}
}
return this.http.get(await this.cityService.getApiUrl() + "/" + endpoint, reqOpts);
}
async getFree(endpoint: string, params?: any, reqOpts?: any) {
if (!reqOpts) {
reqOpts = {
params: new HttpParams(),
};
}
// Support easy query params for GET requests
if (params) {
reqOpts.params = new HttpParams();
for (let k in params) {
reqOpts.params = reqOpts.params.set(k, params[k]);
}
}
return this.http.get(await this.cityService.getApiUrl() + "/" + endpoint, reqOpts);
}
async postFile(endpoint: string, formData: FormData, token: any, reqOpts?: any) {
let headers = new HttpHeaders({
Authorization: "Bearer " + token,
});
return this.http.post(await this.cityService.getApiUrl() + "/" + endpoint, formData, { headers });
}
async post(endpoint: string, body: any, token?: any) {
const myToken = await this.getAccessToken();
try {
token = myToken || token;
} catch (error) {
console.log("unauthorized", endpoint);
}
let headers = new HttpHeaders({
Authorization: "Bearer " + token,
});
return this.http.post(await this.cityService.getApiUrl() + "/" + endpoint, body, { headers });
}
postFree(endpoint: string, body: any, reqOpts?: any) {
return this.http.post(this.cityService._urlApi + "/" + endpoint, body, reqOpts);
}
put(endpoint: string, body: any, token: any, reqOpts?: any) {
let headers = new HttpHeaders({
Authorization: "Bearer " + token,
});
return this.http.put(this.cityService._urlApi + "/" + endpoint, body, { headers });
}
putFree(endpoint: string, body: any, reqOpts?: any) {
return this.http.put(this.cityService._urlApi + "/" + endpoint, body, reqOpts);
}
delete(endpoint: string, token, reqOpts?: any) {
let headers = new HttpHeaders({
Authorization: "Bearer " + token,
});
if (!reqOpts) {
reqOpts = {
params: new HttpParams(),
headers: headers,
};
}
return this.http.delete(this.cityService._urlApi + "/" + endpoint, reqOpts);
}
patch(endpoint: string, body: any, reqOpts?: any) {
return this.http.patch(this.cityService._urlApi + "/" + endpoint, body, reqOpts);
}
public getAccessToken(): Promise<string> {
return new Promise((resolve, reject) => {
this.storageService.get("token").then((token) => {
resolve(token ? token.access_token : '');
});
});
}
}