src/app/modules/core/services/data/data.service.ts
Service to provide base CRUD methods to make api request.
constructor(http: HttpClient)
|
Constructor
Parameters :
|
getWithHeaders |
getWithHeaders(requestParam: any)
|
for making get api calls which needs headers in response
Parameters :
Returns:
Observable<any>
|
get |
get(requestParam: any)
|
for making get api calls
Parameters :
Returns:
Observable<any>
|
postWithHeaders |
postWithHeaders(requestParam: any)
|
for making post api calls with headers in response object
Parameters :
Returns:
Observable<any>
|
post |
post(requestParam: any)
|
for making post api calls
Parameters :
Returns:
Observable<any>
|
patch |
patch(requestParam: any)
|
for making patch api calls
Parameters :
Returns:
Observable<any>
|
delete |
delete(requestParam: any)
|
for making delete api calls
Parameters :
Returns:
Observable<any>
|
Private getHeader |
getHeader(headers: any)
|
for preparing headers
Returns:
any
|
Private getDateDiff |
getDateDiff(serverdate: any)
|
Returns:
number
|
appId |
appId: |
Contains appId |
baseUrl |
baseUrl: |
Contains base Url for api end points |
channelId |
channelId: |
Contains channel Id |
deviceId |
deviceId: |
Contains devoce Id |
http |
http: |
angular HttpClient |
rootOrgId |
rootOrgId: |
Contains rootOrg Id |
import { of as observableOf, throwError as observableThrowError, Observable } from 'rxjs';
import { mergeMap } from 'rxjs/operators';
import { ServerResponse, RequestParam, HttpOptions } from '@sunbird/shared';
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { v4 as UUID } from 'uuid';
import * as moment from 'moment';
import * as _ from 'lodash-es';
/**
* Service to provide base CRUD methods to make api request.
*
*/
@Injectable({
providedIn: 'root'
})
export class DataService {
/**
* Contains rootOrg Id
*/
rootOrgId: string;
/**
* Contains channel Id
*/
channelId: string;
/**
* Contains appId
*/
appId: string;
/**
* Contains devoce Id
*/
deviceId: string;
/**
* Contains base Url for api end points
*/
baseUrl: string;
/**
* angular HttpClient
*/
http: HttpClient;
/**
* Constructor
* @param {HttpClient} http HttpClient reference
*/
constructor(http: HttpClient) {
this.http = http;
}
/**
* for making get api calls which needs headers in response
* headers are fetched to get server time using Date attribute in header
* @param requestParam interface
*/
getWithHeaders(requestParam: RequestParam): Observable<ServerResponse> {
const httpOptions: HttpOptions = {
headers: requestParam.header ? requestParam.header : this.getHeader(),
params: requestParam.param,
observe: 'response'
};
return this.http.get(this.baseUrl + requestParam.url, httpOptions).pipe(
mergeMap(({body, headers}: any) => {
// replace ts time with header date , this value is used in telemetry
body.ts = this.getDateDiff((headers.get('Date')));
if (body.responseCode !== 'OK') {
return observableThrowError(body);
}
return observableOf(body);
}));
}
/**
* for making get api calls
*
* @param requestParam interface
*/
get(requestParam: RequestParam): Observable<ServerResponse> {
const httpOptions: HttpOptions = {
headers: requestParam.header ? requestParam.header : this.getHeader(),
params: requestParam.param
};
return this.http.get(this.baseUrl + requestParam.url, httpOptions).pipe(
mergeMap((data: ServerResponse) => {
if (data.responseCode !== 'OK') {
return observableThrowError(data);
}
return observableOf(data);
}));
}
/**
* for making post api calls with headers in response object
*
* @param {RequestParam} requestParam interface
*
*/
postWithHeaders(requestParam: RequestParam): Observable<any> {
const httpOptions: HttpOptions = {
headers: requestParam.header ? this.getHeader(requestParam.header) : this.getHeader(),
params: requestParam.param,
observe: 'response'
};
return this.http.post(this.baseUrl + requestParam.url, requestParam.data, httpOptions).pipe(
mergeMap(({body, headers}: any) => {
// replace ts time with header date , this value is used in telemetry
body.ts = this.getDateDiff((headers.get('Date')));
if (body.responseCode !== 'OK') {
return observableThrowError(body);
}
return observableOf(body);
}));
}
/**
* for making post api calls
* @param {RequestParam} requestParam interface
*/
post(requestParam: RequestParam): Observable<ServerResponse> {
const httpOptions: HttpOptions = {
headers: requestParam.header ? this.getHeader(requestParam.header) : this.getHeader(),
params: requestParam.param
};
return this.http.post(this.baseUrl + requestParam.url, requestParam.data, httpOptions).pipe(
mergeMap((data: ServerResponse) => {
if (data.responseCode !== 'OK') {
return observableThrowError(data);
}
return observableOf(data);
}));
}
/**
* for making patch api calls
*
* @param {RequestParam} requestParam interface
*
*/
patch(requestParam: RequestParam): Observable<ServerResponse> {
const httpOptions: HttpOptions = {
headers: requestParam.header ? requestParam.header : this.getHeader(),
params: requestParam.param
};
return this.http.patch(this.baseUrl + requestParam.url, requestParam.data, httpOptions).pipe(
mergeMap((data: ServerResponse) => {
if (data.responseCode !== 'OK') {
return observableThrowError(data);
}
return observableOf(data);
}));
}
/**
* for making delete api calls
* @param {RequestParam} requestParam interface
*/
delete(requestParam: RequestParam): Observable<ServerResponse> {
const httpOptions: HttpOptions = {
headers: requestParam.header ? requestParam.header : this.getHeader(),
params: requestParam.param,
body: requestParam.data
};
return this.http.delete(this.baseUrl + requestParam.url, httpOptions).pipe(
mergeMap((data: ServerResponse) => {
if (data.responseCode !== 'OK') {
return observableThrowError(data);
}
return observableOf(data);
}));
}
/**
* for preparing headers
*/
private getHeader(headers?: HttpOptions['headers']): HttpOptions['headers'] {
const default_headers = {
'Accept': 'application/json',
// 'X-Consumer-ID': 'X-Consumer-ID',
'X-Source': 'web',
'ts': moment().format(),
'X-msgid': UUID(),
'X-Request-ID': UUID()
};
try {
this.deviceId = (<HTMLInputElement>document.getElementById('deviceId')).value;
this.appId = (<HTMLInputElement>document.getElementById('appId')).value;
} catch (err) { }
if (this.deviceId) {
default_headers['X-Device-ID'] = this.deviceId;
}
if (this.rootOrgId) {
default_headers['X-Org-code'] = this.rootOrgId;
}
if (this.channelId) {
default_headers['X-Channel-Id'] = this.channelId;
}
if (this.appId) {
default_headers['X-App-Id'] = this.appId;
}
if (headers) {
return { ...default_headers, ...headers };
} else {
return { ...default_headers };
}
}
private getDateDiff (serverdate): number {
const currentdate: any = new Date();
const serverDate: any = new Date(serverdate);
if (serverdate) {
return ( serverDate - currentdate ) / 1000;
} else {
return 0;
}
}
}