pdnsmanager/frontend/src/app/services/http.service.ts

139 lines
3.8 KiB
TypeScript
Raw Normal View History

2018-04-13 17:29:20 +02:00
import { Router } from '@angular/router';
2018-04-08 13:02:00 +02:00
import { Injectable } from '@angular/core';
2018-04-13 17:29:20 +02:00
import { AxiosInstance, AxiosResponse, AxiosError } from 'axios';
2018-04-08 13:02:00 +02:00
import axios from 'axios';
import { StateService } from './state.service';
2018-04-13 17:29:20 +02:00
import { ModalService } from './modal.service';
import { ModalOptionsDatatype } from '../datatypes/modal-options.datatype';
2018-04-08 13:02:00 +02:00
@Injectable()
export class HttpService {
http: AxiosInstance;
2018-04-13 17:29:20 +02:00
constructor(private gs: StateService, private router: Router, private modal: ModalService) {
2018-04-08 13:02:00 +02:00
this.http = axios.create({
baseURL: 'api/v1/'
});
}
2018-04-09 17:13:47 +02:00
public async get(url: string | Array<string>, params: Object = {}): Promise<any> {
2018-04-08 13:02:00 +02:00
const parts = [];
for (const [k, v] of Object.entries(params)) {
2018-04-09 11:16:06 +02:00
if (v === undefined || v === null) {
continue;
}
2018-04-08 13:02:00 +02:00
let value;
if (v instanceof Array) {
value = v.join(',');
} else {
value = v.toString();
}
parts.push(k + '=' + value);
}
const queryStr = parts.join('&');
2018-04-09 17:13:47 +02:00
const reqUrl = queryStr.length > 0 ? this.makeUrl(url) + '?' + queryStr : this.makeUrl(url);
2018-04-08 13:02:00 +02:00
2018-04-13 17:29:20 +02:00
try {
return (await this.http({
url: reqUrl,
method: 'get',
headers: this.buildHeaders()
})).data;
} catch (e) {
2018-04-29 19:57:14 +02:00
if (!await this.handleException(e)) {
throw e;
}
2018-04-13 17:29:20 +02:00
}
2018-04-08 13:02:00 +02:00
}
public async post(url: string | Array<string>, data: Object = {}): Promise<any> {
2018-04-13 17:29:20 +02:00
try {
return (await this.http({
url: this.makeUrl(url),
method: 'post',
data: data,
headers: this.buildHeaders()
})).data;
} catch (e) {
2018-04-29 19:57:14 +02:00
if (!await this.handleException(e)) {
throw e;
}
2018-04-13 17:29:20 +02:00
}
2018-04-08 13:02:00 +02:00
}
public async put(url: string | Array<string>, data: Object = {}): Promise<any> {
2018-04-13 17:29:20 +02:00
try {
return (await this.http({
url: this.makeUrl(url),
method: 'put',
data: data,
headers: this.buildHeaders()
})).data;
} catch (e) {
2018-04-29 19:57:14 +02:00
if (!await this.handleException(e)) {
throw e;
}
2018-04-13 17:29:20 +02:00
}
2018-04-08 13:02:00 +02:00
}
public async delete(url: string | Array<string>): Promise<any> {
2018-04-13 17:29:20 +02:00
try {
return (await this.http({
url: this.makeUrl(url),
method: 'delete',
headers: this.buildHeaders()
})).data;
} catch (e) {
2018-04-29 19:57:14 +02:00
if (!await this.handleException(e)) {
throw e;
}
2018-04-13 17:29:20 +02:00
}
2018-04-08 13:02:00 +02:00
}
private buildHeaders(): Object {
if (this.gs.apiToken !== null) {
return {
'X-Authentication': this.gs.apiToken
};
}
}
private makeUrl(url: string | Array<string>): string {
if (url instanceof Array) {
return url.join('/');
} else {
return url;
}
}
2018-04-13 17:29:20 +02:00
private async handleException(e: AxiosError) {
if (e.response && e.response.status === 403 &&
e.response.data.hasOwnProperty('code') &&
e.response.data.code === 'invalid_session') {
await this.modal.showMessage(new ModalOptionsDatatype({
heading: 'Session expired!',
body: 'Your session has been expired please log in again!',
acceptText: 'OK',
acceptClass: 'warning',
dismisText: ''
}));
this.gs.apiToken = '';
this.gs.isLoggedIn = false;
this.router.navigate(['/']);
2018-04-29 19:57:14 +02:00
return true;
2018-04-13 17:29:20 +02:00
} else {
2018-04-29 19:57:14 +02:00
return false;
2018-04-13 17:29:20 +02:00
}
}
2018-04-08 13:02:00 +02:00
}