-
Notifications
You must be signed in to change notification settings - Fork 9
/
CdsRestDataProvider.qc
183 lines (159 loc) · 6.55 KB
/
CdsRestDataProvider.qc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# -*- mode: qore; indent-tabs-mode: nil -*-
#! Qore CdsRestDataProvider module definition
/** CdsRestDataProvider.qc Copyright 2022 Qore Technologies, s.r.o.
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
#! contains all public definitions in the CdsRestDataProvider module
public namespace CdsRestDataProvider {
#! The CdsRest data provider class
public class CdsRestDataProvider inherits DataProvider::AbstractDataProvider {
public {
#! The REST client object for API calls
CdsRestClient::CdsRestClient rest;
#! Provider info
const ProviderInfo = <DataProviderInfo>{
"type": "CdsRestDataProvider",
"supports_children": True,
"constructor_options": ConstructorOptions,
"children_can_support_apis": False,
"children_can_support_records": True,
"children_can_support_observers": False,
};
#! Constructor options
const ConstructorOptions = {
"api": <DataProviderOptionInfo>{
"type": AbstractDataProviderType::get(StringType),
"desc": "the CDS API version to use",
},
"cdsrestclient_options": <DataProviderOptionInfo>{
"type": AbstractDataProviderType::get(AutoHashType),
"desc": "options to the CdsRestClient constructor",
},
"client_id": <DataProviderOptionInfo>{
"type": AbstractDataProviderType::get(StringType),
"desc": "the client ID (required if 'client_secret' provided)",
},
"client_secret": <DataProviderOptionInfo>{
"type": AbstractDataProviderType::get(StringType),
"desc": "the client secret (required if 'client_id' provided)",
},
"scope": <DataProviderOptionInfo>{
"type": AbstractDataProviderType::get(StringType),
"desc": "the scope string",
},
"tenant": <DataProviderOptionInfo>{
"type": AbstractDataProviderType::get(StringType),
"desc": "the tenant string",
},
"url": <DataProviderOptionInfo>{
"type": AbstractDataProviderType::get(StringType),
"desc": "the URL to the CDS server",
"required": True,
},
};
#! Environment options
const EnvOptions = {
"url": "CDS_URL",
"client_id": "CDS_CLIENT_ID",
"client_secret": "CDS_CLIENT_SECRET",
"tenant": "CDS_TENANT",
"scope": "CDS_SCOPE",
};
#! Hash of HTTP Methods
const HttpMethods = {
"GET": True,
"PUT": True,
"PATCH": True,
"POST": True,
"DELETE": True,
};
}
private {
#! Metadata keyed by entity type
*hash<auto> meta;
#! Metadata mutex
Qore::Thread::Mutex lck();
}
#! Creates the object from the arguments
constructor(CdsRestClient::CdsRestClient rest) {
self.rest = rest;
}
#! Creates the object from constructor options
constructor(*hash<auto> options) {
bool auto_config = DataProvider::getAutoConfig();
if (auto_config) {
map options{$1.key} = ENV{$1.value}, EnvOptions.pairIterator(),
exists ENV{$1.value} && !exists options{$1.key};
}
*hash<auto> copts = checkOptions("CONSTRUCTOR-ERROR", ConstructorOptions, options);
rest = new CdsRestClient(getClientOptions(copts));
}
#! Returns the data provider name
string getName() {
return "/";
}
#! Returns a list of child data provider names, if any
/** @return a list of child data provider names, if any
*/
private *list<string> getChildProviderNamesImpl() {
return keys getMetadata();
}
#! Returns the given child provider or @ref nothing if the given child is unknown
/** @return the given child provider or @ref nothing if the given child is unknown
@see getChildProviderEx()
*/
private *DataProvider::AbstractDataProvider getChildProviderImpl(string name) {
*hash<auto> obj_meta = getMetadata(){name};
if (obj_meta) {
return new CdsEntityDataProvider(rest, name, obj_meta);
}
}
#! Returns metadata
private *hash<auto> getMetadata() {
if (meta) {
return meta;
}
lck.lock();
on_exit lck.unlock();
# check again in the lock
if (meta) {
return meta;
}
hash<auto> resp;
hash<auto> info;
try {
resp = rest.get("$metadata?annotations=true", NOTHING, \info).body."edmx:Edmx"."edmx:DataServices".Schema;
} catch (hash<ExceptionInfo> ex) {
# ensure that any error response body is included in the exception
hash<auto> ex_arg = info{"request-uri", "response-code", "response-body"};
rethrow ex.err, ex.desc, ex.arg + ex_arg;
}
#printf("%N\n", resp.EntityType);
meta = map {$1."^attributes^".Name: $1}, resp.EntityType, ($1.Property || ($1.Abstract == "true"));
return meta;
}
#! Returns data provider static info
private hash<DataProvider::DataProviderInfo> getStaticInfoImpl() {
return ProviderInfo;
}
#! Returns options for the CdsRestClient
static private *hash<auto> getClientOptions(*hash<auto> copts) {
return copts - ("cdsrestclient_options", "cdsrestclient")
+ copts.cdsrestclient_options;
}
}
}