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请求服务封装的更多相关文章

  1. 【angular5项目积累总结】消息订阅服务

    code import { Injectable } from '@angular/core'; import { Subject } from 'rxjs/Subject'; @Injectable ...

  2. 【angular5项目积累总结】遇到的一些问题以及解决办法

    1.项目中字符串特别是\r\n,替换成br之后,在页面换行无法生效? 答:绑定元素 innerHTML. <div class="panel-body" [innerHTML ...

  3. 【angular5项目积累总结】侧栏菜单 navmenu

    View Code import { Component, OnInit } from '@angular/core'; import { HttpClient } from '@angular/co ...

  4. 【angular5项目积累总结】avatar组件

    View Code import { Component, HostListener, ElementRef } from '@angular/core'; import { Adal4Service ...

  5. 【angular5项目积累总结】breadcrumb面包屑组件

    view code <div class="fxs-breadcrumb-wrapper" aria-label="Navigation history" ...

  6. 【angular5项目积累总结】结合adal4实现http拦截器(token)

    import { Injectable } from '@angular/core'; import { HttpEvent, HttpInterceptor, HttpHandler, HttpRe ...

  7. 【angular5项目积累总结】文件上传

    <div class="form-group row"> <label class="col-sm-2 col-form-label"> ...

  8. 【angular5项目积累总结】文件下载

    download() { const token = localStorage.getItem('token'); let headers: HttpHeaders = new HttpHeaders ...

  9. 【angular5项目积累总结】自定义管道 OrderBy

    import { Injectable, Pipe } from '@angular/core'; @Pipe({ name: 'orderBy' }) @Injectable() export cl ...

随机推荐

  1. windows下简单验证码识别——完美验证码识别系统

    此文已由作者徐迪授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 讲到验证码识别,大家第一个可能想到tesseract.诚然,对于OCR而言,tesseract确实很强大,自带 ...

  2. 类文件结构与javap的使用

    此文已由作者赵计刚薪授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 1.javap的使用与类文件结构 使用过程: java源代码:  1 package compile;   ...

  3. TestNG学习笔记目录

    学习TestNG主要用于GUI自动化测试使用,学习目录随进度不断更新.文档内容主要是翻译官方doc,同时加入自己的理解和案例.如有理解偏差欢迎指正 一.TestNG Eclipse plug-in 安 ...

  4. Nova 通过Python API 查询,创建,删除flavor

    [root@controller ~]# cat flavor.py from novaclient import client as nvclient from novaclient import ...

  5. C/C++,python,java,C#月经贴问题

    在刚开始的时候,一直纠结于语言之争,什么什么有前途,什么什么没前途.对于什么的支持不好啦,个人信仰问题啦.什么都有. 首先最主要的一个个人观点:“语言不是老婆,不是一夫一妻制”.你可以同时拥有许多的女 ...

  6. 深入了解java虚拟机(JVM) 第十二章 类加载器

    一.什么是类加载器 类加载器是一个用来加载类文件的类,Java源代码通过javac编译器编译成类文件,然后JVM来执行类文件中的字节码来执行程序.需要注意的是,只有被同一个类加载器加载的类才可能会相等 ...

  7. leecode刷题(16)-- 字符串转换整数

    leecode刷题(16)-- 字符串转换整数 字符串转换整数 描述: 请你来实现一个 atoi 函数,使其能将字符串转换成整数. 首先,该函数会根据需要丢弃无用的开头空格字符,直到寻找到第一个非空格 ...

  8. 关于OI中简单的常数优化

    有些东西借鉴了这里qwq 1.IO(istream/ostream) 输入输出优化 之后能,在赛场上常见的几种输入输出: 输入: $1.cin$ 呵呵,不说什么了,慢的要死.大概$1e8$个数要读1分 ...

  9. python --爬虫基础 --爬取今日头条 使用 requests 库的基本操作, Ajax

    '''思路一: 由于是Ajax的网页,需要先往下划几下看看XHR的内容变化二:分析js中的代码内容三:获取一页中的内容四:获取图片五:保存在本地 使用的库1. requests 网页获取库 2.fro ...

  10. ftpClient.retrieveFileStream导致FTPClient的后面操作失败

    问题代码: FTPFile[] fs = ftpClient.listFiles(); for (FTPFile ff : fs) { if (ff.getName().equals(fileName ...