-
Notifications
You must be signed in to change notification settings - Fork 5
/
OAIDefaultApi.cpp
436 lines (376 loc) · 15.7 KB
/
OAIDefaultApi.cpp
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
/**
* GAMS Engine
* With GAMS Engine you can register and solve GAMS models. It has a namespace management system, so you can restrict your users to certain models.
*
* The version of the OpenAPI document: latest
*
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
* https://openapi-generator.tech
* Do not edit the class manually.
*/
#include "OAIDefaultApi.h"
#include "OAIServerConfiguration.h"
#include <QJsonArray>
#include <QJsonDocument>
namespace OpenAPI {
OAIDefaultApi::OAIDefaultApi(const int timeOut)
: _timeOut(timeOut),
_manager(nullptr),
_isResponseCompressionEnabled(false),
_isRequestCompressionEnabled(false) {
initializeServerConfigs();
}
OAIDefaultApi::~OAIDefaultApi() {
}
void OAIDefaultApi::initializeServerConfigs() {
//Default server
QList<OAIServerConfiguration> defaultConf = QList<OAIServerConfiguration>();
//varying endpoint server
defaultConf.append(OAIServerConfiguration(
QUrl("//localhost/"),
"No description provided",
QMap<QString, OAIServerVariable>()));
_serverConfigs.insert("getConfiguration", defaultConf);
_serverIndices.insert("getConfiguration", 0);
_serverConfigs.insert("getVersion", defaultConf);
_serverIndices.insert("getVersion", 0);
_serverConfigs.insert("updateConfiguration", defaultConf);
_serverIndices.insert("updateConfiguration", 0);
}
/**
* returns 0 on success and -1, -2 or -3 on failure.
* -1 when the variable does not exist and -2 if the value is not defined in the enum and -3 if the operation or server index is not found
*/
int OAIDefaultApi::setDefaultServerValue(int serverIndex, const QString &operation, const QString &variable, const QString &value) {
auto it = _serverConfigs.find(operation);
if (it != _serverConfigs.end() && serverIndex < it.value().size()) {
return _serverConfigs[operation][serverIndex].setDefaultValue(variable,value);
}
return -3;
}
void OAIDefaultApi::setServerIndex(const QString &operation, int serverIndex) {
if (_serverIndices.contains(operation) && serverIndex < _serverConfigs.find(operation).value().size()) {
_serverIndices[operation] = serverIndex;
}
}
void OAIDefaultApi::setApiKey(const QString &apiKeyName, const QString &apiKey) {
_apiKeys.insert(apiKeyName, apiKey);
}
void OAIDefaultApi::setBearerToken(const QString &token) {
_bearerToken = token;
}
void OAIDefaultApi::setUsername(const QString &username) {
_username = username;
}
void OAIDefaultApi::setPassword(const QString &password) {
_password = password;
}
void OAIDefaultApi::setTimeOut(const int timeOut) {
_timeOut = timeOut;
}
void OAIDefaultApi::setWorkingDirectory(const QString &path) {
_workingDirectory = path;
}
void OAIDefaultApi::setNetworkAccessManager(QNetworkAccessManager* manager) {
_manager = manager;
}
/**
* Appends a new ServerConfiguration to the config map for a specific operation.
* @param operation The id to the target operation.
* @param url A string that contains the URL of the server
* @param description A String that describes the server
* @param variables A map between a variable name and its value. The value is used for substitution in the server's URL template.
* returns the index of the new server config on success and -1 if the operation is not found
*/
int OAIDefaultApi::addServerConfiguration(const QString &operation, const QUrl &url, const QString &description, const QMap<QString, OAIServerVariable> &variables) {
if (_serverConfigs.contains(operation)) {
_serverConfigs[operation].append(OAIServerConfiguration(
url,
description,
variables));
return _serverConfigs[operation].size()-1;
} else {
return -1;
}
}
/**
* Appends a new ServerConfiguration to the config map for a all operations and sets the index to that server.
* @param url A string that contains the URL of the server
* @param description A String that describes the server
* @param variables A map between a variable name and its value. The value is used for substitution in the server's URL template.
*/
void OAIDefaultApi::setNewServerForAllOperations(const QUrl &url, const QString &description, const QMap<QString, OAIServerVariable> &variables) {
#if QT_VERSION >= QT_VERSION_CHECK(5, 12, 0)
for (auto keyIt = _serverIndices.keyBegin(); keyIt != _serverIndices.keyEnd(); keyIt++) {
setServerIndex(*keyIt, addServerConfiguration(*keyIt, url, description, variables));
}
#else
for (auto &e : _serverIndices.keys()) {
setServerIndex(e, addServerConfiguration(e, url, description, variables));
}
#endif
}
/**
* Appends a new ServerConfiguration to the config map for an operations and sets the index to that server.
* @param URL A string that contains the URL of the server
* @param description A String that describes the server
* @param variables A map between a variable name and its value. The value is used for substitution in the server's URL template.
*/
void OAIDefaultApi::setNewServer(const QString &operation, const QUrl &url, const QString &description, const QMap<QString, OAIServerVariable> &variables) {
setServerIndex(operation, addServerConfiguration(operation, url, description, variables));
}
void OAIDefaultApi::addHeaders(const QString &key, const QString &value) {
_defaultHeaders.insert(key, value);
}
void OAIDefaultApi::enableRequestCompression() {
_isRequestCompressionEnabled = true;
}
void OAIDefaultApi::enableResponseCompression() {
_isResponseCompressionEnabled = true;
}
void OAIDefaultApi::abortRequests() {
emit abortRequestsSignal();
}
QString OAIDefaultApi::getParamStylePrefix(const QString &style) {
if (style == "matrix") {
return ";";
} else if (style == "label") {
return ".";
} else if (style == "form") {
return "&";
} else if (style == "simple") {
return "";
} else if (style == "spaceDelimited") {
return "&";
} else if (style == "pipeDelimited") {
return "&";
} else {
return "none";
}
}
QString OAIDefaultApi::getParamStyleSuffix(const QString &style) {
if (style == "matrix") {
return "=";
} else if (style == "label") {
return "";
} else if (style == "form") {
return "=";
} else if (style == "simple") {
return "";
} else if (style == "spaceDelimited") {
return "=";
} else if (style == "pipeDelimited") {
return "=";
} else {
return "none";
}
}
QString OAIDefaultApi::getParamStyleDelimiter(const QString &style, const QString &name, bool isExplode) {
if (style == "matrix") {
return (isExplode) ? ";" + name + "=" : ",";
} else if (style == "label") {
return (isExplode) ? "." : ",";
} else if (style == "form") {
return (isExplode) ? "&" + name + "=" : ",";
} else if (style == "simple") {
return ",";
} else if (style == "spaceDelimited") {
return (isExplode) ? "&" + name + "=" : " ";
} else if (style == "pipeDelimited") {
return (isExplode) ? "&" + name + "=" : "|";
} else if (style == "deepObject") {
return (isExplode) ? "&" : "none";
} else {
return "none";
}
}
void OAIDefaultApi::getConfiguration() {
QString fullPath = QString(_serverConfigs["getConfiguration"][_serverIndices.value("getConfiguration")].URL()+"/configuration");
OAIHttpRequestWorker *worker = new OAIHttpRequestWorker(this, _manager);
worker->setTimeOut(_timeOut);
worker->setWorkingDirectory(_workingDirectory);
OAIHttpRequestInput input(fullPath, "GET");
#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
for (auto keyValueIt = _defaultHeaders.keyValueBegin(); keyValueIt != _defaultHeaders.keyValueEnd(); keyValueIt++) {
input.headers.insert(keyValueIt->first, keyValueIt->second);
}
#else
for (auto key : _defaultHeaders.keys()) {
input.headers.insert(key, _defaultHeaders[key]);
}
#endif
connect(worker, &OAIHttpRequestWorker::on_execution_finished, this, &OAIDefaultApi::getConfigurationCallback);
connect(this, &OAIDefaultApi::abortRequestsSignal, worker, &QObject::deleteLater);
connect(worker, &QObject::destroyed, this, [this]() {
if (findChildren<OAIHttpRequestWorker*>().count() == 0) {
emit allPendingRequestsCompleted();
}
});
worker->execute(&input);
}
void OAIDefaultApi::getConfigurationCallback(OAIHttpRequestWorker *worker) {
QString error_str = worker->error_str;
QNetworkReply::NetworkError error_type = worker->error_type;
if (worker->error_type != QNetworkReply::NoError) {
error_str = QString("%1, %2").arg(worker->error_str, QString(worker->response));
}
OAIModel_configuration output(QString(worker->response));
worker->deleteLater();
if (worker->error_type == QNetworkReply::NoError) {
emit getConfigurationSignal(output);
emit getConfigurationSignalFull(worker, output);
} else {
emit getConfigurationSignalE(output, error_type, error_str);
emit getConfigurationSignalEFull(worker, error_type, error_str);
}
}
void OAIDefaultApi::getVersion() {
QString fullPath = QString(_serverConfigs["getVersion"][_serverIndices.value("getVersion")].URL()+"/version");
OAIHttpRequestWorker *worker = new OAIHttpRequestWorker(this, _manager);
worker->setTimeOut(_timeOut);
worker->setWorkingDirectory(_workingDirectory);
OAIHttpRequestInput input(fullPath, "GET");
#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
for (auto keyValueIt = _defaultHeaders.keyValueBegin(); keyValueIt != _defaultHeaders.keyValueEnd(); keyValueIt++) {
input.headers.insert(keyValueIt->first, keyValueIt->second);
}
#else
for (auto key : _defaultHeaders.keys()) {
input.headers.insert(key, _defaultHeaders[key]);
}
#endif
connect(worker, &OAIHttpRequestWorker::on_execution_finished, this, &OAIDefaultApi::getVersionCallback);
connect(this, &OAIDefaultApi::abortRequestsSignal, worker, &QObject::deleteLater);
connect(worker, &QObject::destroyed, this, [this]() {
if (findChildren<OAIHttpRequestWorker*>().count() == 0) {
emit allPendingRequestsCompleted();
}
});
worker->execute(&input);
}
void OAIDefaultApi::getVersionCallback(OAIHttpRequestWorker *worker) {
QString error_str = worker->error_str;
QNetworkReply::NetworkError error_type = worker->error_type;
if (worker->error_type != QNetworkReply::NoError) {
error_str = QString("%1, %2").arg(worker->error_str, QString(worker->response));
}
OAIModel_version output(QString(worker->response));
worker->deleteLater();
if (worker->error_type == QNetworkReply::NoError) {
emit getVersionSignal(output);
emit getVersionSignalFull(worker, output);
} else {
emit getVersionSignalE(output, error_type, error_str);
emit getVersionSignalEFull(worker, error_type, error_str);
}
}
void OAIDefaultApi::updateConfiguration(const ::OpenAPI::OptionalParam<QString> &webhook_access, const ::OpenAPI::OptionalParam<QString> &instance_pool_access, const ::OpenAPI::OptionalParam<qint32> &text_entries_max_size, const ::OpenAPI::OptionalParam<QString> &hostname) {
QString fullPath = QString(_serverConfigs["updateConfiguration"][_serverIndices.value("updateConfiguration")].URL()+"/configuration");
if (!_username.isEmpty() && !_password.isEmpty()) {
QByteArray b64;
b64.append(_username.toUtf8() + ":" + _password.toUtf8());
addHeaders("Authorization","Basic " + b64.toBase64());
}
OAIHttpRequestWorker *worker = new OAIHttpRequestWorker(this, _manager);
worker->setTimeOut(_timeOut);
worker->setWorkingDirectory(_workingDirectory);
OAIHttpRequestInput input(fullPath, "PATCH");
if (webhook_access.hasValue())
{
input.add_var("webhook_access", ::OpenAPI::toStringValue(webhook_access.value()));
}
if (instance_pool_access.hasValue())
{
input.add_var("instance_pool_access", ::OpenAPI::toStringValue(instance_pool_access.value()));
}
if (text_entries_max_size.hasValue())
{
input.add_var("text_entries_max_size", ::OpenAPI::toStringValue(text_entries_max_size.value()));
}
if (hostname.hasValue())
{
input.add_var("hostname", ::OpenAPI::toStringValue(hostname.value()));
}
#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
for (auto keyValueIt = _defaultHeaders.keyValueBegin(); keyValueIt != _defaultHeaders.keyValueEnd(); keyValueIt++) {
input.headers.insert(keyValueIt->first, keyValueIt->second);
}
#else
for (auto key : _defaultHeaders.keys()) {
input.headers.insert(key, _defaultHeaders[key]);
}
#endif
connect(worker, &OAIHttpRequestWorker::on_execution_finished, this, &OAIDefaultApi::updateConfigurationCallback);
connect(this, &OAIDefaultApi::abortRequestsSignal, worker, &QObject::deleteLater);
connect(worker, &QObject::destroyed, this, [this]() {
if (findChildren<OAIHttpRequestWorker*>().count() == 0) {
emit allPendingRequestsCompleted();
}
});
worker->execute(&input);
}
void OAIDefaultApi::updateConfigurationCallback(OAIHttpRequestWorker *worker) {
QString error_str = worker->error_str;
QNetworkReply::NetworkError error_type = worker->error_type;
if (worker->error_type != QNetworkReply::NoError) {
error_str = QString("%1, %2").arg(worker->error_str, QString(worker->response));
}
OAIMessage output(QString(worker->response));
worker->deleteLater();
if (worker->error_type == QNetworkReply::NoError) {
emit updateConfigurationSignal(output);
emit updateConfigurationSignalFull(worker, output);
} else {
emit updateConfigurationSignalE(output, error_type, error_str);
emit updateConfigurationSignalEFull(worker, error_type, error_str);
}
}
void OAIDefaultApi::tokenAvailable(){
oauthToken token;
switch (_OauthMethod) {
case 1: //implicit flow
token = _implicitFlow.getToken(_latestScope.join(" "));
if(token.isValid()){
_latestInput.headers.insert("Authorization", "Bearer " + token.getToken());
_latestWorker->execute(&_latestInput);
}else{
_implicitFlow.removeToken(_latestScope.join(" "));
qDebug() << "Could not retrieve a valid token";
}
break;
case 2: //authorization flow
token = _authFlow.getToken(_latestScope.join(" "));
if(token.isValid()){
_latestInput.headers.insert("Authorization", "Bearer " + token.getToken());
_latestWorker->execute(&_latestInput);
}else{
_authFlow.removeToken(_latestScope.join(" "));
qDebug() << "Could not retrieve a valid token";
}
break;
case 3: //client credentials flow
token = _credentialFlow.getToken(_latestScope.join(" "));
if(token.isValid()){
_latestInput.headers.insert("Authorization", "Bearer " + token.getToken());
_latestWorker->execute(&_latestInput);
}else{
_credentialFlow.removeToken(_latestScope.join(" "));
qDebug() << "Could not retrieve a valid token";
}
break;
case 4: //resource owner password flow
token = _passwordFlow.getToken(_latestScope.join(" "));
if(token.isValid()){
_latestInput.headers.insert("Authorization", "Bearer " + token.getToken());
_latestWorker->execute(&_latestInput);
}else{
_credentialFlow.removeToken(_latestScope.join(" "));
qDebug() << "Could not retrieve a valid token";
}
break;
default:
qDebug() << "No Oauth method set!";
break;
}
}
} // namespace OpenAPI