Skip to content

Commit ce8cbb4

Browse files
committed
[cpp-qt-client]Utilize enum for OauthMethod flow
1 parent edc984a commit ce8cbb4

File tree

22 files changed

+247
-170
lines changed

22 files changed

+247
-170
lines changed

modules/openapi-generator/src/main/resources/cpp-qt-client/api-body.mustache

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -697,16 +697,16 @@ void {{classname}}::{{nickname}}({{#allParams}}{{#required}}const {{{dataType}}}
697697
Q_EMIT allPendingRequestsCompleted();
698698
}
699699
});{{#authMethods}}{{#isOAuth}}{{#isCode}}
700-
_OauthMethod = 2;
700+
_OauthMethod = OauthMethod::AuthorizationFlow;
701701
_implicitFlow.unlink();
702702
_credentialFlow.unlink();
703703
_passwordFlow.unlink();
704704
_authFlow.link();
705-
QStringList scope2;
705+
QStringList scopeAuthorizationFlow;
706706
{{#scopes}}
707-
scope2.append("{{scope}}");
707+
scopeAuthorizationFlow.append("{{scope}}");
708708
{{/scopes}}
709-
auto token2 = _authFlow.getToken(scope2.join(" "));
709+
auto token2 = _authFlow.getToken(scopeAuthorizationFlow.join(" "));
710710
if(token2.isValid())
711711
input.headers.insert("Authorization", "Bearer " + token2.getToken());
712712

@@ -725,18 +725,18 @@ void {{classname}}::{{nickname}}({{#allParams}}{{#required}}const {{{dataType}}}
725725
});
726726

727727
_latestInput = input;
728-
_latestScope = scope2;{{/isCode}}
728+
_latestScope = scopeAuthorizationFlow;{{/isCode}}
729729
{{#isImplicit}}
730-
_OauthMethod = 1;
730+
_OauthMethod = OauthMethod::ImplicitFlow;
731731
_implicitFlow.link();
732732
_passwordFlow.unlink();
733733
_authFlow.unlink();
734734
_credentialFlow.unlink();
735-
QStringList scope1;
735+
QStringList scopeImplicitFlow;
736736
{{#scopes}}
737-
scope1.append("{{scope}}");
737+
scopeImplicitFlow.append("{{scope}}");
738738
{{/scopes}}
739-
auto token1 = _implicitFlow.getToken(scope1.join(" "));
739+
auto token1 = _implicitFlow.getToken(scopeImplicitFlow.join(" "));
740740
if(token1.isValid())
741741
input.headers.insert("Authorization", "Bearer " + token1.getToken());
742742

@@ -755,18 +755,18 @@ void {{classname}}::{{nickname}}({{#allParams}}{{#required}}const {{{dataType}}}
755755
});
756756

757757
_latestInput = input;
758-
_latestScope = scope1;{{/isImplicit}}
758+
_latestScope = scopeImplicitFlow;{{/isImplicit}}
759759
{{#isApplication}}
760-
_OauthMethod = 3;
760+
_OauthMethod = OauthMethod::ClientCredentialsFlow;
761761
_authFlow.unlink();
762762
_implicitFlow.unlink();
763763
_passwordFlow.unlink();
764764
_credentialFlow.link();
765-
QStringList scope3;
765+
QStringList scopeClientCredentialsFlow;
766766
{{#scopes}}
767-
scope3.append("{{scope}}");
767+
scopeClientCredentialsFlow.append("{{scope}}");
768768
{{/scopes}}
769-
auto token3 = _credentialFlow.getToken(scope3.join(" "));
769+
auto token3 = _credentialFlow.getToken(scopeClientCredentialsFlow.join(" "));
770770
if(token3.isValid())
771771
input.headers.insert("Authorization", "Bearer " + token3.getToken());
772772

@@ -785,18 +785,18 @@ void {{classname}}::{{nickname}}({{#allParams}}{{#required}}const {{{dataType}}}
785785
});
786786

787787
_latestInput = input;
788-
_latestScope = scope3;{{/isApplication}}
788+
_latestScope = scopeClientCredentialsFlow;{{/isApplication}}
789789
{{#isPassword}}
790-
_OauthMethod = 4;
790+
_OauthMethod = OauthMethod::ResourceOwnserPasswordFlow;
791791
_passwordFlow.link();
792792
_authFlow.unlink();
793793
_implicitFlow.unlink();
794794
_credentialFlow.unlink();
795-
QStringList scope4;
795+
QStringList scopeResourceOwnerPasswordFlow;
796796
{{#scopes}}
797-
scope4.append("{{scope}}");
797+
scopeResourceOwnerPasswordFlow.append("{{scope}}");
798798
{{/scopes}}
799-
auto token4 = _passwordFlow.getToken(scope4.join(" "));
799+
auto token4 = _passwordFlow.getToken(scopeResourceOwnerPasswordFlow.join(" "));
800800
if(token4.isValid())
801801
input.headers.insert("Authorization", "Bearer " + token4.getToken());
802802

@@ -815,7 +815,7 @@ void {{classname}}::{{nickname}}({{#allParams}}{{#required}}const {{{dataType}}}
815815
});
816816

817817
_latestInput = input;
818-
_latestScope = scope4;
818+
_latestScope = scopeResourceOwnerPasswordFlow;
819819
{{/isPassword}}{{/isOAuth}}{{/authMethods}}
820820

821821
worker->execute(&input);
@@ -960,11 +960,11 @@ void {{classname}}::{{nickname}}Callback({{prefix}}HttpRequestWorker *worker) {
960960

961961
{{/operation}}
962962
{{/operations}}
963-
void {{classname}}::tokenAvailable(){
963+
void {{classname}}::tokenAvailable() {
964964
965965
oauthToken token;
966966
switch (_OauthMethod) {
967-
case 1: //implicit flow
967+
case OauthMethod::ImplicitFlow:
968968
token = _implicitFlow.getToken(_latestScope.join(" "));
969969
if(token.isValid()){
970970
_latestInput.headers.insert("Authorization", "Bearer " + token.getToken());
@@ -974,7 +974,7 @@ void {{classname}}::tokenAvailable(){
974974
qDebug() << "Could not retrieve a valid token";
975975
}
976976
break;
977-
case 2: //authorization flow
977+
case OauthMethod::AuthorizationFlow:
978978
token = _authFlow.getToken(_latestScope.join(" "));
979979
if(token.isValid()){
980980
_latestInput.headers.insert("Authorization", "Bearer " + token.getToken());
@@ -984,7 +984,7 @@ void {{classname}}::tokenAvailable(){
984984
qDebug() << "Could not retrieve a valid token";
985985
}
986986
break;
987-
case 3: //client credentials flow
987+
case OauthMethod::ClientCredentialsFlow:
988988
token = _credentialFlow.getToken(_latestScope.join(" "));
989989
if(token.isValid()){
990990
_latestInput.headers.insert("Authorization", "Bearer " + token.getToken());
@@ -994,7 +994,7 @@ void {{classname}}::tokenAvailable(){
994994
qDebug() << "Could not retrieve a valid token";
995995
}
996996
break;
997-
case 4: //resource owner password flow
997+
case OauthMethod::ResourceOwnerPasswordFlow:
998998
token = _passwordFlow.getToken(_latestScope.join(" "));
999999
if(token.isValid()){
10001000
_latestInput.headers.insert("Authorization", "Bearer " + token.getToken());

modules/openapi-generator/src/main/resources/cpp-qt-client/api-header.mustache

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,13 @@ public:
6363
{{/operation}}{{/operations}}
6464

6565
private:
66+
enum class OauthMethod : int {
67+
INVALID_VALUE_OPENAPI_GENERATED = 0,
68+
ImplicitFlow = 1,
69+
AuthorizationFlow = 2,
70+
ClientCredentialsFlow = 3,
71+
ResourceOwnerPasswordFlow = 4
72+
};
6673
QMap<QString,int> _serverIndices;
6774
QMap<QString,QList<{{prefix}}ServerConfiguration>> _serverConfigs;
6875
QMap<QString, QString> _apiKeys;
@@ -82,7 +89,7 @@ private:
8289
OauthImplicit _implicitFlow;
8390
OauthCredentials _credentialFlow;
8491
OauthPassword _passwordFlow;
85-
int _OauthMethod = 0;
92+
OauthMethod _OauthMethod = OauthMethod::INVALID_VALUE_OPENAPI_GENERATED;
8693
{{#operations}}{{#operation}}
8794
void {{nickname}}Callback({{prefix}}HttpRequestWorker *worker);{{/operation}}{{/operations}}
8895

samples/client/petstore/cpp-qt-addDownloadProgress/client/PFXFakeApi.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -326,11 +326,11 @@ void PFXFakeApi::getEnumInlineOrRefCallback(PFXHttpRequestWorker *worker) {
326326
}
327327
}
328328

329-
void PFXFakeApi::tokenAvailable(){
329+
void PFXFakeApi::tokenAvailable() {
330330

331331
oauthToken token;
332332
switch (_OauthMethod) {
333-
case 1: //implicit flow
333+
case OauthMethod::ImplicitFlow:
334334
token = _implicitFlow.getToken(_latestScope.join(" "));
335335
if(token.isValid()){
336336
_latestInput.headers.insert("Authorization", "Bearer " + token.getToken());
@@ -340,7 +340,7 @@ void PFXFakeApi::tokenAvailable(){
340340
qDebug() << "Could not retrieve a valid token";
341341
}
342342
break;
343-
case 2: //authorization flow
343+
case OauthMethod::AuthorizationFlow:
344344
token = _authFlow.getToken(_latestScope.join(" "));
345345
if(token.isValid()){
346346
_latestInput.headers.insert("Authorization", "Bearer " + token.getToken());
@@ -350,7 +350,7 @@ void PFXFakeApi::tokenAvailable(){
350350
qDebug() << "Could not retrieve a valid token";
351351
}
352352
break;
353-
case 3: //client credentials flow
353+
case OauthMethod::ClientCredentialsFlow:
354354
token = _credentialFlow.getToken(_latestScope.join(" "));
355355
if(token.isValid()){
356356
_latestInput.headers.insert("Authorization", "Bearer " + token.getToken());
@@ -360,7 +360,7 @@ void PFXFakeApi::tokenAvailable(){
360360
qDebug() << "Could not retrieve a valid token";
361361
}
362362
break;
363-
case 4: //resource owner password flow
363+
case OauthMethod::ResourceOwnerPasswordFlow:
364364
token = _passwordFlow.getToken(_latestScope.join(" "));
365365
if(token.isValid()){
366366
_latestInput.headers.insert("Authorization", "Bearer " + token.getToken());

samples/client/petstore/cpp-qt-addDownloadProgress/client/PFXFakeApi.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,13 @@ class PFXFakeApi : public QObject {
6565

6666

6767
private:
68+
enum class OauthMethod : int {
69+
INVALID_VALUE_OPENAPI_GENERATED = 0,
70+
ImplicitFlow = 1,
71+
AuthorizationFlow = 2,
72+
ClientCredentialsFlow = 3,
73+
ResourceOwnerPasswordFlow = 4
74+
};
6875
QMap<QString,int> _serverIndices;
6976
QMap<QString,QList<PFXServerConfiguration>> _serverConfigs;
7077
QMap<QString, QString> _apiKeys;
@@ -84,7 +91,7 @@ class PFXFakeApi : public QObject {
8491
OauthImplicit _implicitFlow;
8592
OauthCredentials _credentialFlow;
8693
OauthPassword _passwordFlow;
87-
int _OauthMethod = 0;
94+
OauthMethod _OauthMethod = OauthMethod::INVALID_VALUE_OPENAPI_GENERATED;
8895

8996
void getEnumInlineOrRefCallback(PFXHttpRequestWorker *worker);
9097

0 commit comments

Comments
 (0)