Angular Service Custom Method


Angular Service method:


import { Injectable } from '@angular/core';
import { HttpClient, HttpParams, HttpHeaders } from '@angular/common/http';
import { Observable, of } from 'rxjs';

const BASE_URL = window['BASE_URL'];

@Injectable({
providedIn: 'root'
})
export class RestService {
public baseUrl:string = BASE_URL;
public ACCESS_TOKEN = localStorage.getItem('access_token');

constructor( private http: HttpClient ) { }
public getRequest(url, params): Observable<any> {
let httpHeaders = new HttpHeaders().set('Content-Type', 'application/json');
let httpParams = new HttpParams();
url = BASE_URL + url;
for (let key in params) {
let value = params[key];
httpParams = httpParams.set(key, value);
}
return this.http.get(url, { headers: httpHeaders, responseType: 'json', params: httpParams });
}

public postKeyValue(url: string, data: any): Observable<any> {
let httpHeaders = new HttpHeaders().set('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
let httpParams = new HttpParams();
url = BASE_URL + url;
for (let key in data) {
let value = data[key];
if(Array.isArray(value)) {
for(var i =0; i<value.length; i++) {
let prmObj = value[i];
for(var k in prmObj) {
let val = prmObj[k], httpKey = key.toString()+'['+i.toString()+']'+'['+k.toString()+']';
httpParams = httpParams.set(httpKey, val);
}
}
} else {
httpParams = httpParams.set(key, value);
}
}
let body = httpParams.toString();
return this.http.post(url, body, { headers: httpHeaders, responseType: 'json',/* params: httpParams*/ });
}

public postJson(url: string, data: any): Observable<any> {
var url = BASE_URL + url;
let httpHeaders = new HttpHeaders().set('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8')
let httpParams = new HttpParams();
httpParams = httpParams.set('data', JSON.stringify(data));
let body = httpParams.toString();
return this.http.post(url, body, { headers: httpHeaders, responseType: 'json' });
}

public getKeyValue(url: string, data: any): Observable<any> {
let httpHeaders = new HttpHeaders().set('Content-Type', 'application/json');
let httpParams = new HttpParams();
for (let key in data) {
let value = JSON.stringify(data[key]);
if(Array.isArray(value)){
for(var i =0; i<value.length; i++) {
let prmObj = value[i];
for(var k in prmObj){
httpParams = httpParams.set(key, value);
}
}
} else {
httpParams = httpParams.set(key, value);
}
}
//httpParams = httpParams.set('_csrf', this.CSRF_TOKEN);
let body = httpParams.toString();
return this.http.get(url, { headers: httpHeaders, responseType: 'json', params: httpParams });
}

public postRequest(url, params): Observable<any> {
// let httpHeaders = new HttpHeaders()
// .set('Content-Type', 'application/json');
let httpHeaders = new HttpHeaders().set('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8')
let httpParams = new HttpParams();

url = BASE_URL + url;
if(params.hasOwnProperty('records')){
httpParams = httpParams.set('records', params.records);
} else if(params.hasOwnProperty('data')) {
httpParams = httpParams.set('data', params.data);
}
let body = httpParams;//httpParams.toString();
return this.http.post(url,body, { headers: httpHeaders, responseType: 'json'});
}

public postFormData(url, params): Observable<any>{
url = BASE_URL + url;
const formData = new FormData();
formData.append('access_token',this.ACCESS_TOKEN);
if(params.hasOwnProperty('file')){
for(let key in params['file']){
formData.append('file[]', params['file'][key]);
}
}
if(params.hasOwnProperty('data')){
formData.append('data',JSON.stringify(params['data']));
}
// formData.append(data : JSON.stringify({order_master: orderMasterData}))
// formData.append('file', params['file']);
return this.http.post(url, formData, {});
}
}

Share on Google Plus

About Ram Pukar

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment

0 comments:

Post a Comment