h-selfdelivery-admin-panel-p/src/app/services/jsonrpc.service.ts
2023-01-20 14:57:59 +04:00

97 lines
2.5 KiB
TypeScript

import { Injectable } from '@angular/core';
import { environment } from 'src/environments/environment';
import {HttpClient, HttpErrorResponse, HttpHeaders} from '@angular/common/http';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { CookiesService } from './cookies.service';
import { v4 as uuidv4 } from 'uuid';
import { Areas } from "src/app/interface/data";
export enum RpcService{
authService,
bonusService
}
export interface JsonRpcBody {
id: string;
jsonrpc: string;
method: string;
params: any;
}
@Injectable({
providedIn: 'root'
})
export class JsonrpcService {
protected readonly api = environment.appSelfDeliveryEndPoint;
private jsonrpc = '2.0';
private body!: JsonRpcBody;
public ClientChoose = "";
public ClientChooseName = "";
public changeId = "";
public areas: Areas[] = [];
constructor(
private http: HttpClient,
private cookiesService: CookiesService
) { }
rpc(data: {method: string, params: any}, service: RpcService, auth?: boolean): Observable<any> {
let headers = new HttpHeaders();
const token = decodeURI(this.cookiesService.getItem('token') ?? '');
console.log(token);
headers = headers.set('Content-Type', 'application/json');
auth ? headers = headers.append('Authorization', token) : null;
const options = {
headers: headers,
};
this.body = {
id: uuidv4(),
jsonrpc: this.jsonrpc,
method: data.method,
params: [...data.params],
};
try {
return this.http
.post(environment.appSelfDeliveryEndPoint, this.body, options)
.pipe(map((res: any) => res));
} catch (error) {
return new Observable()
}
}
rpc2(data: { method: string, params: any }, service: RpcService, auth = false): Observable<any> {
let headers = new HttpHeaders();
const token = decodeURI(this.cookiesService.getItem('token') ?? '');
const options = {
headers: {
'Content-Type': 'application/json',
'Authorization': token
},
};
this.body = {
id: uuidv4(),
jsonrpc: this.jsonrpc,
method: data.method,
params: { ...data.params },
};
try {
return this.http
.post(environment.appSelfDeliveryEndPoint, this.body, options)
.pipe(map((res: any) => res));
} catch (error) {
return new Observable()
}
}
handleHttpError(error: HttpErrorResponse): void {
console.log(error.message);
}
}