Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2019
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.98 KB | None | 0 0
  1. import {Injectable} from '@angular/core';
  2. import {HttpClient, HttpParams, HttpRequest, HttpResponse, HttpEventType, HttpHeaders} from '@angular/common/http';
  3. import { Observable } from 'rxjs';
  4. import {Router} from '@angular/router';
  5.  
  6. @Injectable({
  7. providedIn: 'root'
  8. })
  9. export class BackendService {
  10. private apiRoot: string;
  11. constructor(private http: HttpClient, private router: Router) {
  12. this.apiRoot = 'http://' + location.hostname + ':1221';
  13. }
  14.  
  15. checkHttpParams(params?: any) {
  16. let httpParams: HttpParams;
  17. if (params instanceof HttpParams) {
  18. httpParams = params;
  19. } else {
  20. httpParams = new HttpParams();
  21. // tslint:disable-next-line:forin
  22. for (const param in params) {
  23. httpParams = httpParams.append(param, params[param]);
  24. }
  25. }
  26. return httpParams;
  27. }
  28.  
  29. public getBlob(
  30. url: string,
  31. params?: HttpParams | object,
  32. progress?: (loaded: number, total: number) => void
  33. ): Observable<HttpResponse<Blob>> {
  34. const httpParams: HttpParams = this.checkHttpParams(params);
  35. return Observable.create(observer => {
  36. this.http.get(`${this.apiRoot}/${url}`, {
  37. params: httpParams,
  38. observe: 'events',
  39. responseType: 'blob',
  40. reportProgress: true
  41. }).subscribe(event => {
  42. if (event.type === HttpEventType.DownloadProgress && progress) {
  43. progress(event.loaded, event.total);
  44. } else if (event instanceof HttpResponse) {
  45. observer.next(event);
  46. }
  47. }, error => observer.error(error));
  48. });
  49. }
  50.  
  51. public get<T>(url: string, params?: HttpParams | object): Observable<T> {
  52. const httpParams: HttpParams = this.checkHttpParams(params);
  53. if (url.slice(0, 1) === '/') {
  54. url = url.slice(1);
  55. }
  56. return this.http.get<T>(`${this.apiRoot}/${url}`, {
  57. headers: this.getHeaders(),
  58. params: httpParams
  59. });
  60. }
  61.  
  62. public post<TResult>(
  63. url: string,
  64. content: any = {},
  65. progress?: (uploaded: number, total: number) => void,
  66. params?: HttpParams | object): Observable<TResult> {
  67. const httpParams: HttpParams = this.checkHttpParams(params);
  68.  
  69. if (progress) {
  70. return Observable.create(observer => {
  71. const request = new HttpRequest('POST', `${this.apiRoot}/${url}`, content, {
  72. headers: this.getHeaders(),
  73. reportProgress: true,
  74. params: httpParams
  75. });
  76. this.http.request(request).subscribe(event => {
  77. if (event.type === HttpEventType.UploadProgress) {
  78. progress(event.loaded, event.total);
  79. } else if (event instanceof HttpResponse) {
  80. observer.next(event.body);
  81. observer.complete();
  82. }
  83. }, error => observer.error(error));
  84. });
  85. }
  86. return this.http.post<TResult>(`${this.apiRoot}/${url}`, content, {params: httpParams, headers: this.getHeaders()});
  87. }
  88.  
  89. public put<TResult>(url: string, content: any = {}): Observable<TResult> {
  90. return this.http.put<TResult>(`${this.apiRoot}/${url}`, content, {headers: this.getHeaders()});
  91. }
  92.  
  93. public delete<TResult>(url: string): Observable<TResult> {
  94. const options = {
  95. headers: this.getHeaders()
  96. };
  97. return this.http.delete<TResult>(`${this.apiRoot}/${url}`, options);
  98. }
  99.  
  100. private getHeaders() {
  101. const login = localStorage.getItem('login');
  102. const password = localStorage.getItem('password');
  103. if (login && password) {
  104. return new HttpHeaders({login, password});
  105. }
  106. }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement