http.provider.ts
import { Injectable } from '@angular/core'; import { HttpClient } from '@angular/common/http'; import { Observable } from "rxjs/Observable"; @Injectable() export class HttpProvider { constructor( private httpClient: HttpClient, ) { } get(url: string): Observable<any> { return this.httpClient.get(url) .map(this.extractData) .catch(this.handleError); } list(url: string): Observable<any> { return this.get(url); } post(url: string, body: any): Observable<any> { return this.httpClient.post(url, body) .map(this.extractData) .catch(this.handleError); } delete(url: string): Observable<any> { return this.httpClient.delete(url) .map(this.extractData) .catch(this.handleError); } patch(url: string, body: any): Observable<any> { return this.httpClient.patch(url, body) .map(this.extractData) .catch(this.handleError); } put(url: string, body: any): Observable<any> { return this.httpClient.put(url, body) .map(this.extractData) .catch(this.handleError); } head(url: string): Observable<any> { return this.httpClient.head(url) .map(this.extractData) .catch(this.handleError); } private extractData(res: Response) { if (res.status < 200 || res.status >= 300) { throw new Error('Bad response status: ' + res.status); } let body = {}; if (res.status !== 204) { body = res; } return body || {}; } private handleError(error: any) { const errMsg = error.message || 'Server error'; //console.error(JSON.stringify(error)); return Observable.throw(errMsg); } }
import { HttpClient, HttpErrorResponse } from '@angular/common/http'; import { Injectable } from '@angular/core'; @Injectable() export class AppStoreService { //private baseUrl: string = ''; constructor( private httpProvider: HttpClient) { } SaveAppInfo(app: any, callback: any): void { if (app.Id) { let editApp = { DisplayName: app.DisplayName || '', Name: app.Name || '', Info: app.Info, Port: app.Port, Category: app.Category } this.httpProvider .put("/api/Applications/", editApp, { responseType: "text" }) .subscribe(result => { callback(result); }, this.HandleError); } else { let appBody: {} = {}; if (app.DisplayName) { appBody['DisplayName'] = app.DisplayName; } if (app.Name) { appBody['Name'] = app.Name; } if (app.Info) { appBody['Info'] = app.Info; } if (app.Port) { appBody['Port'] = app.Port; } if (app.Category) { appBody['Category'] = app.Category; } this.httpProvider .post('/api/Applications', appBody, { responseType: "text" }) .subscribe(result => { callback(result); }, this.HandleError); } } DeleteApp(id: string, callback: any): void { this.httpProvider .delete('/api/Applications/'+ id, { responseType: "text" }) .subscribe(result => { callback(result); }, this.HandleError); } GetAppOne(id: string, callback: any = null): any { this.httpProvider .get('/api/Applications/' + id) .subscribe((result: any) => { callback(result); }, this.HandleError); } AllApp(displayName: string, category: any, callback: any = null): any { this.httpProvider .get("/api/Applications?category=" + category + (displayName ? '&displayName=' + displayName:'')) .subscribe((result: any) => { callback(result); }, this.HandleError); } GetCategoryList(callback: any = null): void { this.httpProvider .get('/api/Applications/Categorys') .subscribe((result: any) => { callback(result); }, this.HandleError); } GetAppSatus(callback: any = null): void { this.httpProvider .get('/api/Applications/Status') .subscribe((result: any) => { callback(result); }, this.HandleError); } HandleError(err: HttpErrorResponse): void { console.log(err) //let errInfo = JSON.parse(err.error); //alert(errInfo["odata.error"].message.value); } AllPkg(id: string, callback: any = null): any { this.httpProvider .get("/api/DockerPackages?id=" + id) .subscribe((result: any) => { callback(result); }, this.HandleError); } SaveAppPkgInfo(pkg: any, callback: any): void { let pkgBody: {} = {}; if (pkg.AppId) { pkgBody['AppId'] = pkg.AppId; } if (pkg.Description) { pkgBody['Description'] = pkg.Description; } if (pkg.Ports) { pkgBody['Ports'] = pkg.Ports; } pkgBody['Version'] = 0; pkgBody['Status'] = 0; this.httpProvider .post('/api/DockerPackages', pkgBody) .subscribe(result => { callback(result); }, this.HandleError); } GetPkgOne(id: string, callback: any = null): any { this.httpProvider .get('/api/DockerPackages/' + id) .subscribe((result: any) => { callback(result); }, this.HandleError); } GetAppDpyList(id: string, callback: any = null) { this.httpProvider .get("/api/Deploys?id=" + id) .subscribe((result: any) => { callback(result); }, this.HandleError); } DeleteDpy(id: string, callback: any): void { this.httpProvider .delete('/api/Deploys/' + id) .subscribe(result => { callback(result); }, this.HandleError); } ValidatePackage(id: string, callback: any = null): any { this.httpProvider .get('/api/DockerPackages/ValidatePackage/' + id) .subscribe((result: any) => { callback(result); }, this.HandleError); } SaveAppDpyInfo(dpy: any, callback: any): void { let dpyBody: {} = {}; if (dpy.AppId) { dpyBody['AppId'] = dpy.AppId; } if (dpy.PackageId) { dpyBody['PackageId'] = dpy.PackageId; } if (dpy.Level) { dpyBody['Level'] = dpy.Level; } if (dpy.InstanceCount) { dpyBody['InstanceCount'] = dpy.InstanceCount; } if (dpy.Description) { dpyBody['Description'] = dpy.Description; } if (dpy.Ports) { dpyBody['Ports'] = dpy.Ports; } dpyBody['Status'] = 0; this.httpProvider .post('/api/Deploys', dpyBody) .subscribe(result => { callback(result); }, this.HandleError); } }