]>
Commit | Line | Data |
---|---|---|
1e59de90 TL |
1 | import { HttpClient, HttpParams } from '@angular/common/http'; |
2 | import { Injectable } from '@angular/core'; | |
3 | ||
4 | import { Observable } from 'rxjs'; | |
5 | import { map } from 'rxjs/operators'; | |
6 | import { JsonFormUISchema } from '../forms/crud-form/crud-form.model'; | |
7 | import { CrudFormAdapterService } from './crud-form-adapter.service'; | |
8 | ||
9 | @Injectable({ | |
10 | providedIn: 'root' | |
11 | }) | |
12 | export class DataGatewayService { | |
13 | cache: { [keys: string]: Observable<any> } = {}; | |
14 | selected: any; | |
15 | ||
16 | constructor(private http: HttpClient, private crudFormAdapater: CrudFormAdapterService) {} | |
17 | ||
18 | list(dataPath: string): Observable<any> { | |
19 | const cacheable = this.getCacheable(dataPath, 'get'); | |
20 | if (this.cache[cacheable] === undefined) { | |
21 | const { url, version } = this.getUrlAndVersion(dataPath); | |
22 | ||
23 | this.cache[cacheable] = this.http.get<any>(url, { | |
24 | headers: { Accept: `application/vnd.ceph.api.v${version}+json` } | |
25 | }); | |
26 | } | |
27 | ||
28 | return this.cache[cacheable]; | |
29 | } | |
30 | ||
31 | submit(dataPath: string, data: any, methodType: string): Observable<any> { | |
32 | const { url, version } = this.getUrlAndVersion(dataPath); | |
33 | ||
34 | return this.http[methodType]<any>(url, data, { | |
35 | headers: { Accept: `application/vnd.ceph.api.v${version}+json` } | |
36 | }); | |
37 | } | |
38 | ||
39 | delete(dataPath: string, key: string): Observable<any> { | |
40 | const { url, version } = this.getUrlAndVersion(dataPath); | |
41 | ||
42 | return this.http.delete<any>(`${url}/${key}`, { | |
43 | headers: { Accept: `application/vnd.ceph.api.v${version}+json` }, | |
44 | observe: 'response' | |
45 | }); | |
46 | } | |
47 | ||
48 | form(dataPath: string, formPath: string, modelKey: string = ''): Observable<JsonFormUISchema> { | |
49 | const cacheable = this.getCacheable(dataPath, 'get', modelKey); | |
50 | const params = { model_key: modelKey }; | |
51 | if (this.cache[cacheable] === undefined) { | |
52 | const { url, version } = this.getUrlAndVersion(dataPath); | |
53 | ||
54 | this.cache[cacheable] = this.http.get<any>(url, { | |
55 | headers: { Accept: `application/vnd.ceph.api.v${version}+json` }, | |
56 | params: params | |
57 | }); | |
58 | } | |
59 | return this.cache[cacheable].pipe( | |
60 | map((response) => { | |
61 | return this.crudFormAdapater.processJsonSchemaForm(response, formPath); | |
62 | }) | |
63 | ); | |
64 | } | |
65 | ||
66 | model(dataPath: string, params: HttpParams): Observable<any> { | |
67 | const cacheable = this.getCacheable(dataPath, 'get'); | |
68 | if (this.cache[cacheable] === undefined) { | |
69 | const { url, version } = this.getUrlAndVersion(dataPath); | |
70 | ||
71 | this.cache[cacheable] = this.http.get<any>(`${url}/model`, { | |
72 | headers: { Accept: `application/vnd.ceph.api.v${version}+json` }, | |
73 | params: params | |
74 | }); | |
75 | } | |
76 | return this.cache[cacheable]; | |
77 | } | |
78 | ||
79 | getCacheable(dataPath: string, method: string, key: string = '') { | |
80 | return dataPath + method + key; | |
81 | } | |
82 | ||
83 | getUrlAndVersion(dataPath: string) { | |
84 | const match = dataPath.match(/(?<url>[^@]+)(?:@(?<version>.+))?/); | |
85 | const url = match.groups.url.split('.').join('/'); | |
86 | const version = match.groups.version || '1.0'; | |
87 | ||
88 | return { url: url, version: version }; | |
89 | } | |
90 | } |