【angular5项目积累总结】http请求服务封装
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);
}
}
【angular5项目积累总结】http请求服务封装的更多相关文章
- 【angular5项目积累总结】消息订阅服务
code import { Injectable } from '@angular/core'; import { Subject } from 'rxjs/Subject'; @Injectable ...
- 【angular5项目积累总结】遇到的一些问题以及解决办法
1.项目中字符串特别是\r\n,替换成br之后,在页面换行无法生效? 答:绑定元素 innerHTML. <div class="panel-body" [innerHTML ...
- 【angular5项目积累总结】侧栏菜单 navmenu
View Code import { Component, OnInit } from '@angular/core'; import { HttpClient } from '@angular/co ...
- 【angular5项目积累总结】avatar组件
View Code import { Component, HostListener, ElementRef } from '@angular/core'; import { Adal4Service ...
- 【angular5项目积累总结】breadcrumb面包屑组件
view code <div class="fxs-breadcrumb-wrapper" aria-label="Navigation history" ...
- 【angular5项目积累总结】结合adal4实现http拦截器(token)
import { Injectable } from '@angular/core'; import { HttpEvent, HttpInterceptor, HttpHandler, HttpRe ...
- 【angular5项目积累总结】文件上传
<div class="form-group row"> <label class="col-sm-2 col-form-label"> ...
- 【angular5项目积累总结】文件下载
download() { const token = localStorage.getItem('token'); let headers: HttpHeaders = new HttpHeaders ...
- 【angular5项目积累总结】自定义管道 OrderBy
import { Injectable, Pipe } from '@angular/core'; @Pipe({ name: 'orderBy' }) @Injectable() export cl ...
随机推荐
- PostgreSQL查询数据(基本查询)
原料:数据表 create table "SysUser"( "UserId" serial, --用户Id,自增 "UserName" ) ...
- 后台生产验证码code和byte[]图片
引用命名空间 using System.Drawing;using System.Drawing.Drawing2D;using System.Drawing.Imaging;using System ...
- if、else if 、else及switch...case使用小记(C#)
有时候编程编的久了,如果不停下来认真思考一下,即便是一些最基础的知识点,也可能让自己懵圈.其实,说到底还是打基础的时候没打牢,或者说自以为是地认为自己懂了,然后在打基础的时候就懒得思考懒得看了,结果就 ...
- Window 10 单机配置MYSQL主从同步
Master数据库:127.0.0.1:3306 Slave数据库:127.0.0.1:3307 Master操作 修改ini信息 Master MYSQL安装目录下,找到my.ini,在[mysql ...
- cesium编程入门(六)添加 3D Tiles,并调整位置,贴地
添加 3D Tiles,并调整位置 3D Tiles 是什么 3DTiles数据集是cesium小组AnalyticlGraphics与2016年3月定义的一种数据集,3DTiles数据集以分块.分级 ...
- AppDomain.CurrentDomain.BaseDirectory项目目录相关操作
链接:https://www.cnblogs.com/guolianyu/p/3980971.html 经常用到,每次都百度,所以自己备份一下!
- [uwp]MVVM模式实战之必应壁纸查看器
最近学习MVVM,至于什么是MVVM我也在这儿不多说了,一是关于它的解释解释网上非常多,二是我怕自己讲不清,误导自己没关系,误导别人就不好了.. 好了,废话结束,看是实战...... 这个必应壁纸的d ...
- 服务器变量 $_SERVER 详解
摘抄:http://ha.cker.in/942.seo 1.$_SESSION[‘PHP_SELF’] — 获取当前正在执行脚本的文件名 2.$_SERVER[‘SERVER_PROTOCOL’] ...
- Win10桌面图标无法拖动
1.右键桌面>查看>取消自动排列 2.桌面多按几次Esc 3.gpedit.msc “用户配置/管理模板/Windows组件/任务计划程序/禁止拖放>改为未配置
- python 第一天学习(画个正方体)
import turtleturtle.goto(200,0)turtle.goto(200,200)turtle.goto(0,200)turtle.goto(0,0)turtle.penup()t ...