src/app/data/http-client.service.ts
Custom Http Client Service for CRUD operations and related actions
Methods |
|
constructor(http: HttpClient, authService: AuthService)
|
||||||||||||
Defined in src/app/data/http-client.service.ts:14
|
||||||||||||
Builds an instance of the service
Parameters :
|
delete | ||||||
delete(url)
|
||||||
Defined in src/app/data/http-client.service.ts:95
|
||||||
Custom delete method
Parameters :
Returns :
any
the response of the request |
get | ||||||
get(url)
|
||||||
Defined in src/app/data/http-client.service.ts:57
|
||||||
Custom get method
Parameters :
Returns :
any
the response of the request |
getHttpHeaders |
getHttpHeaders()
|
Defined in src/app/data/http-client.service.ts:31
|
Builds and returns HttpHeaders for the requests, including the token for requests
Returns :
HttpHeaders
http headers |
Private handleError | ||||||
handleError(error: any)
|
||||||
Defined in src/app/data/http-client.service.ts:47
|
||||||
Method to manage httpRequest errors
Parameters :
Returns :
Promise<any>
|
post | |||||||||
post(url, data)
|
|||||||||
Defined in src/app/data/http-client.service.ts:70
|
|||||||||
Custom post method
Parameters :
Returns :
any
the response of the request |
put | |||||||||
put(url, data)
|
|||||||||
Defined in src/app/data/http-client.service.ts:83
|
|||||||||
Custom put method
Parameters :
Returns :
any
the response of the request |
read_url | ||||||||
read_url(url: string)
|
||||||||
Defined in src/app/data/http-client.service.ts:107
|
||||||||
Proesses the url for the request by adding the base url for http requests
Parameters :
Returns :
string
the processed target url |
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { HttpHeaders } from '@angular/common/http';
import { environment } from '../../environments/environment';
import { AuthService } from '../auth/auth.service';
/**
* Custom Http Client Service for CRUD operations
* and related actions
*/
@Injectable()
export class HttpClientService {
/**
* Builds an instance of the service
* @param {HttpClient} http Angular HTTP Service used to perform HTTP requests
* @param {AuthService} authService service used to check and handle authorization
*/
constructor(
private http: HttpClient,
private authService: AuthService,
) {
}
/**
* Builds and returns HttpHeaders for the requests, including the token for requests
* @returns {HttpHeaders} http headers
*/
getHttpHeaders(): HttpHeaders {
if (this.authService.getToken()) {
return new HttpHeaders({
'Content-Type': 'application/json',
'Authorization': 'Token ' + this.authService.getToken()
});
} else {
return new HttpHeaders({
'Content-Type': 'application/json',
});
}
}
/**
* Method to manage httpRequest errors
*/
private handleError(error: any): Promise<any> {
console.error('An error occurred', error); // for demo purposes only
return Promise.reject(error.message || error);
}
/**
* Custom get method
* @param {string} url target url
* @returns {Response} the response of the request
*/
get(url) {
const httpOptions = {
headers: this.getHttpHeaders()
};
return this.http.get(this.read_url(url), httpOptions);
}
/**
* Custom post method
* @param {string} url target url
* @param data json with data
* @returns {Response} the response of the request
*/
post(url, data) {
const httpOptions = {
headers: this.getHttpHeaders()
};
return this.http.post(this.read_url(url), data, httpOptions);
}
/**
* Custom put method
* @param {string} url target url
* @param data json with data
* @returns {Response} the response of the request
*/
put(url, data) {
const httpOptions = {
headers: this.getHttpHeaders()
};
return this.http.put(this.read_url(url), data, httpOptions);
}
/**
* Custom delete method
* @param {string} url target url with the selected object id
* @returns {Response} the response of the request
*/
delete(url) {
const httpOptions = {
headers: this.getHttpHeaders()
};
return this.http.delete(this.read_url(url), httpOptions);
}
/**
* Proesses the url for the request by adding the base url for http requests
* @param {string} url target url
* @returns {string} the processed target url
*/
read_url(url: string): string {
return environment.httpUrl + url;
}
}