Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import {Injectable} from '@angular/core';
- import {HttpClient, HttpParams, HttpRequest, HttpResponse, HttpEventType, HttpHeaders} from '@angular/common/http';
- import { Observable } from 'rxjs';
- import {Router} from '@angular/router';
- @Injectable({
- providedIn: 'root'
- })
- export class BackendService {
- private apiRoot: string;
- constructor(private http: HttpClient, private router: Router) {
- this.apiRoot = 'http://' + location.hostname + ':1221';
- }
- checkHttpParams(params?: any) {
- let httpParams: HttpParams;
- if (params instanceof HttpParams) {
- httpParams = params;
- } else {
- httpParams = new HttpParams();
- // tslint:disable-next-line:forin
- for (const param in params) {
- httpParams = httpParams.append(param, params[param]);
- }
- }
- return httpParams;
- }
- public getBlob(
- url: string,
- params?: HttpParams | object,
- progress?: (loaded: number, total: number) => void
- ): Observable<HttpResponse<Blob>> {
- const httpParams: HttpParams = this.checkHttpParams(params);
- return Observable.create(observer => {
- this.http.get(`${this.apiRoot}/${url}`, {
- params: httpParams,
- observe: 'events',
- responseType: 'blob',
- reportProgress: true
- }).subscribe(event => {
- if (event.type === HttpEventType.DownloadProgress && progress) {
- progress(event.loaded, event.total);
- } else if (event instanceof HttpResponse) {
- observer.next(event);
- }
- }, error => observer.error(error));
- });
- }
- public get<T>(url: string, params?: HttpParams | object): Observable<T> {
- const httpParams: HttpParams = this.checkHttpParams(params);
- if (url.slice(0, 1) === '/') {
- url = url.slice(1);
- }
- return this.http.get<T>(`${this.apiRoot}/${url}`, {
- headers: this.getHeaders(),
- params: httpParams
- });
- }
- public post<TResult>(
- url: string,
- content: any = {},
- progress?: (uploaded: number, total: number) => void,
- params?: HttpParams | object): Observable<TResult> {
- const httpParams: HttpParams = this.checkHttpParams(params);
- if (progress) {
- return Observable.create(observer => {
- const request = new HttpRequest('POST', `${this.apiRoot}/${url}`, content, {
- headers: this.getHeaders(),
- reportProgress: true,
- params: httpParams
- });
- this.http.request(request).subscribe(event => {
- if (event.type === HttpEventType.UploadProgress) {
- progress(event.loaded, event.total);
- } else if (event instanceof HttpResponse) {
- observer.next(event.body);
- observer.complete();
- }
- }, error => observer.error(error));
- });
- }
- return this.http.post<TResult>(`${this.apiRoot}/${url}`, content, {params: httpParams, headers: this.getHeaders()});
- }
- public put<TResult>(url: string, content: any = {}): Observable<TResult> {
- return this.http.put<TResult>(`${this.apiRoot}/${url}`, content, {headers: this.getHeaders()});
- }
- public delete<TResult>(url: string): Observable<TResult> {
- const options = {
- headers: this.getHeaders()
- };
- return this.http.delete<TResult>(`${this.apiRoot}/${url}`, options);
- }
- private getHeaders() {
- const login = localStorage.getItem('login');
- const password = localStorage.getItem('password');
- if (login && password) {
- return new HttpHeaders({login, password});
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement