typescript API-Provider.service
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了typescript API-Provider.service相关的知识,希望对你有一定的参考价值。
import {Injectable} from '@angular/core';
import {HttpClient, HttpParams} from '@angular/common/http';
import {Observable} from 'rxjs/Observable';
import 'rxjs/add/operator/map';
import {environment} from '../../environments/environment';
import {ApiProviderService} from './api-provider.service';
import {Workflow} from '../models/workflow';
import {RunResult} from '../models/run-result';
import {Parser, PreviewParserResponse} from '../models/parser';
import {LogRecord} from '../models/log-record';
@Injectable()
export class WorkflowService {
constructor(private api: ApiProviderService, private http: HttpClient) {
}
getWorkFlows(): Observable<Workflow[]> {
return this.api.get('Schema/GetWorkflow').map(response => {
let workflows: Workflow[] = [];
if (response.ok) {
let obj = response.body;
if (obj && obj.StartPointEntity && obj.StartPointEntity.Id) {
workflows.push(obj);
}
return workflows;
}
return workflows;
});
}
getWorkFlow(workflowId: string): Observable<Workflow> {
return this.api.get(`Schema/GetWorkflow?startPointId=${workflowId}`).map(response => {
if (response.ok) {
let obj = response.body;
if (obj && obj.StartPointEntity && obj.StartPointEntity.Id) {
return obj;
}
}
return null;
});
}
addWorkFlow(workFlowName) {
return this.api.post('Schema/AddWorkFlow', {description: workFlowName});
}
getFolderFiles(workflowId: string): Observable<string[]> {
return this.api.get('Schema/GetFolderFiles').map(response => {
if (response.ok) {
let obj = response.body;
if (obj && obj instanceof Array) {
return obj.filter(str => typeof str === 'string' && str.length > 0);
}
}
return null;
});
}
getRunningHistory(workflowId: string): Observable<RunResult[]> {
return this.api.get('Reporting/GetRunningHistory').map(response => {
if (response.ok) {
let obj = response.body.RunResults;
if (obj && obj instanceof Array) {
return obj.map(o => new RunResult(o)).filter(rr => rr.runId !== null);
}
}
return null;
});
}
startDispatcher(spoolFilePath: string): Observable<RunResult> {
return this.api.post('Dispatcher/StartDispatcher', {SpoolFilePath: spoolFilePath}).map(response => {
return response.ok ? response.body : null;
});
}
getRunLog(runId: string): Observable<LogRecord[]> {
return this.api.get('Reporting/GetRunLog', {runId}).map(response => {
if (response.ok) {
let obj = response.body.LogRecords;
if (obj && obj instanceof Array) {
return obj.map(o => new LogRecord(o)).filter(lr => lr.entityType !== null);
}
}
return null;
});
}
getParser(): Observable<Parser> {
return this.api.get('Schema/GetParser').map(response => {
return response.ok ? response.body : null;
});
}
updateParser(parser: Parser): Observable<RunResult> {
return this.api.put('Schema/UpdateParser', parser).map(response => {
return response.ok ? response.body : null;
});
}
getPreviewParser(sampleFile: File | Blob): Observable<PreviewParserResponse> {
let formData = new FormData();
formData.append('sample', sampleFile);
return this.api.post('Schema/GetPreviewParser', formData).map(response => {
return response.ok ? new PreviewParserResponse(response.body) : null;
});
}
getPreviewParserFileContent(fileName: string): Observable<string> {
let params = new HttpParams();
params = params.set('fileName', fileName);
return this.http.get(environment.apiUrl + 'Schema/GetPreviewParserFileContent', {params, responseType: 'text'});
}
}
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders, HttpParams, HttpResponse } from '@angular/common/http';
import { environment } from '../../environments/environment';
import { Observable } from 'rxjs/Observable';
export class UnhandledException { }
export enum ReqType {
head,
get,
post,
put,
patch,
delete
}
@Injectable()
export class ApiProviderService {
constructor(private http: HttpClient) { }
get(path, params?: object, headers?: HttpHeaders) {
return this.doRequest(ReqType.get, path, null, params, headers);
}
delete(path, params?: object, headers?: HttpHeaders) {
return this.doRequest(ReqType.delete, path, null, params, headers);
}
head(path, params?: object, headers?: HttpHeaders) {
return this.doRequest(ReqType.head, path, null, params, headers);
}
post(path, data?: FormData|object, params?: object, headers?: HttpHeaders) {
return this.doRequest(ReqType.post, path, data, params, headers);
}
put(path, data?: FormData|object, params?: object, headers?: HttpHeaders) {
return this.doRequest(ReqType.put, path, data, params, headers);
}
patch(path, data?: FormData|object, params?: object, headers?: HttpHeaders) {
return this.doRequest(ReqType.patch, path, data, params, headers);
}
doRequest(type: ReqType, path: string, data?: FormData|object, params?: object, headers?: HttpHeaders): Observable<HttpResponse<any>> {
headers = headers instanceof HttpHeaders ? headers : new HttpHeaders();
let httpParams = null;
if (typeof params === 'object' && params !== null) {
httpParams = new HttpParams();
for (let i in params)
httpParams = httpParams.set(i, params[i]);
}
switch (type) {
case ReqType.get:
case ReqType.delete:
case ReqType.head:
if (typeof data === 'object' && data !== null) {
if (httpParams === null)
httpParams = new HttpParams();
for (let i in data)
httpParams = httpParams.set(i, data[i]);
}
if (type === ReqType.head)
return this.http.head(environment.apiUrl + path, {observe: 'response', params: httpParams, headers: headers});
if (type === ReqType.delete)
return this.http.delete(environment.apiUrl + path, {observe: 'response', params: httpParams, headers: headers});
if (type === ReqType.get)
return this.http.get(environment.apiUrl + path, {observe: 'response', params: httpParams, headers: headers});
break;
case ReqType.put:
case ReqType.post:
case ReqType.patch:
if (!(data instanceof FormData)) {
headers.append('Content-Type', 'application/json');
}
if (type === ReqType.put)
return this.http.put(environment.apiUrl + path, data, {observe: 'response', params: httpParams, headers: headers});
if (type === ReqType.patch)
return this.http.patch(environment.apiUrl + path, data, {observe: 'response', params: httpParams, headers: headers});
if (type === ReqType.post)
return this.http.post(environment.apiUrl + path, data, {observe: 'response', params: httpParams, headers: headers});
break;
default:
}
throw new UnhandledException();
}
}
以上是关于typescript API-Provider.service的主要内容,如果未能解决你的问题,请参考以下文章