an angular service to make api calls
import { Injectable, Component } from '@angular/core';
import { Http, Headers, URLSearchParams } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import { Supplier } from '../model/supplier';
import { Resource } from '../resources/resource';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/toPromise';
import 'rxjs/add/observable/of';
@Injectable()
export class ApiCallService {
private headers = new Headers({ "Content-type": "application/json" });
constructor(
private http: Http,
private rs: Resource
) { }
public getSuppliers(params: URLSearchParams): Promise<Supplier[]> {
return this.http.get(this.rs.rs.api.supplier.all, { search: params, headers: this.headers }).toPromise().then(res => res.json() as Supplier[]);
}
public addSupplier(supplier: any): Promise<any> {
return this.http.post(this.rs.rs.api.supplier.add, JSON.stringify(supplier), { headers: this.headers }).toPromise().then(r => r);
}
public addItem(item: any): Promise<any> {
return this.http.post(this.rs.rs.api.item.add, JSON.stringify(item), { headers: this.headers }).toPromise().then(r => r);
}
public getCountSuppliers(params: URLSearchParams): Promise<any> {
return this.http.get(this.rs.rs.api.supplier.count, { search: params, headers: this.headers }).toPromise().then(r => r.json() as number);
}
public countSuppliers(params: URLSearchParams): Observable<number> {
return this.http.get(this.rs.rs.api.supplier.count, { search: params, headers: this.headers }).map(r => r.json() as number);
}
public suppliersSearch(params: URLSearchParams): Observable<Supplier[]> {
return this.http.get(this.rs.rs.api.supplier.all, { search: params, headers: this.headers }).map(res => res.json() as Supplier[]);
}
}