【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 ...
随机推荐
- MySQL多线程备份工具mydumper 之 RDS外部实例迁移平台
此文已由作者温正湖授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 1.Format_description_event问题: BINLOG ' kTXkUxMKAAAALQA ...
- 赛肯德 | 2017NEERC某题
我们枚举每一条边的流量x,将它作为底流(也就是比它大的的流量变成差值,比它小的流量为0),然后我们设x就是路径上第K大的那个边的流量.然后跑最短路,加上dis[n]就是当前的答案.然后取min即可. ...
- “全栈2019”Java异常第九章:throws关键字详解
难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java异 ...
- 利用C# CefSharp Python采集某网站简历并自动发送邀请短信
以往爬虫没怎么研究过,最近有个需求,要从某网站采集敏感信息,稍稍考虑了一下,决定利用C# Winform和Python一起来解决这个事件. 整个解决方案不复杂:C#编写WinForm窗体,进行数据分析 ...
- 详解sizeof与strlen
一,sizeof是C语言的一种单目运算符,与C语言的其他运算符++,--一样,它并不是函数:sizeof()以字节为单位给出了操作数的大小:sizeof的值是无符号int. strlen是一个函数,只 ...
- eclipse的classpath(build path)和classpaht几种设置的方式
1,默认eclipse有自己的classpath的路径并不是环境变量中配置的classpah. 2,eclipse的classpath每个项目不同,一般是在工作区的当前项目的class下. 2.1,可 ...
- [原]时间格式化hh:mm:ss和HH:mm:ss区别
hh:mm:ss 按照12小时制的格式进行字符串格式化 如果时间处于00:00:00——12:59:59,则返回的字符串正常 如果时间处于13:00:00——23:59:59,则返回的字符串是实际 ...
- php实现图片base64编码解码
1.图片的base64编码 /*首先要确定图片的类型,需要安装一个php拓展php_fileinfo 如已安装可以在extension_dir目录下找到php_fileinfo.dll(windows ...
- python3的嵌套函数
背景 最近在学python3 嵌套函数 顾名思义,即使在函数中还有函数,实现了函数的多级嵌套 def test1(): age = 10 print(age) def test2(): te = 5 ...
- L06-Ubuntu系统中部署Vagrant和VirtualBox
一.前言 1.Vagrant是一个搭建完整的虚拟开发环境的工具~~~更多关于Vagrant理论可查看这篇博文https://www.cnblogs.com/davenkin/p/vagrant-vir ...