From 5e4ee5a83c8c2b18e623d0b9c0a3aea98ae3a6d7 Mon Sep 17 00:00:00 2001 From: Eddie Chayes <35413716+eddiechayes@users.noreply.github.com> Date: Wed, 1 Nov 2023 17:16:00 -0700 Subject: [PATCH] generator changes for Splitit Java SDK (#325) --- .../resources/Java/Configuration.mustache | 7 ++ .../libraries/okhttp-gson/ApiClient.mustache | 103 +++++++++++++----- 2 files changed, 81 insertions(+), 29 deletions(-) diff --git a/generator/konfig-generator-api/src/main/resources/Java/Configuration.mustache b/generator/konfig-generator-api/src/main/resources/Java/Configuration.mustache index 3c9507287..52cba14d1 100644 --- a/generator/konfig-generator-api/src/main/resources/Java/Configuration.mustache +++ b/generator/konfig-generator-api/src/main/resources/Java/Configuration.mustache @@ -7,6 +7,13 @@ public class Configuration { private static ApiClient defaultApiClient = new ApiClient(); public boolean verifyingSsl = true; public String host = "{{{basePath}}}"; +{{#hasOAuthMethods}} +{{#oauthMethods}} +{{#-first}} + public String tokenUrl = "{{{tokenUrl}}}"; +{{/-first}} +{{/oauthMethods}} +{{/hasOAuthMethods}} {{#authMethods}} {{#isApiKey}} public String {{keyParamName}}; diff --git a/generator/konfig-generator-api/src/main/resources/Java/libraries/okhttp-gson/ApiClient.mustache b/generator/konfig-generator-api/src/main/resources/Java/libraries/okhttp-gson/ApiClient.mustache index ac6202c09..0c62ed9ce 100644 --- a/generator/konfig-generator-api/src/main/resources/Java/libraries/okhttp-gson/ApiClient.mustache +++ b/generator/konfig-generator-api/src/main/resources/Java/libraries/okhttp-gson/ApiClient.mustache @@ -72,6 +72,13 @@ import {{invokerPackage}}.auth.OAuthFlow; public class ApiClient extends ApiClientCustom { private String basePath = "{{{basePath}}}"; + {{#hasOAuthMethods}} + {{#oauthMethods}} + {{#-first}} + private String tokenUrl = "{{{tokenUrl}}}"; + {{/-first}} + {{/oauthMethods}} + {{/hasOAuthMethods}} private boolean debugging = false; private Map defaultHeaderMap = new HashMap(); private Map defaultCookieMap = new HashMap(); @@ -119,20 +126,32 @@ public class ApiClient extends ApiClientCustom { public ApiClient(OkHttpClient client, Configuration configuration) { init(); - if (client == null) { - initHttpClient(); - } else { - this.httpClient = client; + if (configuration != null) { + // Setting up authentications, if applicable, may rely on the following, so we initialize them first + setBasePath(configuration.host); + {{#hasOAuthMethods}} + setTokenUrl(configuration.tokenUrl); + {{/hasOAuthMethods}} } - // Setup authentications (key: authentication name, value: authentication).{{#authMethods}}{{#isBasic}}{{#isBasicBasic}} authentications.put("{{name}}", new HttpBasicAuth());{{/isBasicBasic}}{{^isBasicBasic}} authentications.put("{{name}}", new HttpBearerAuth("{{scheme}}"));{{/isBasicBasic}}{{/isBasic}}{{#isApiKey}} authentications.put("{{name}}", new ApiKeyAuth({{#isKeyInHeader}}"header"{{/isKeyInHeader}}{{#isKeyInQuery}}"query"{{/isKeyInQuery}}{{#isKeyInCookie}}"cookie"{{/isKeyInCookie}}, "{{keyParamName}}"));{{/isApiKey}}{{#isOAuth}} - authentications.put("{{name}}", new OAuth());{{/isOAuth}}{{/authMethods}} + RetryingOAuth oauth = configuration != null ? setupOauth(configuration.clientId, configuration.clientSecret, null) : null;{{/isOAuth}}{{/authMethods}} // Prevent the authentications from being modified. authentications = Collections.unmodifiableMap(authentications); + if (client == null) { + {{#hasOAuthMethods}} + initHttpClient(oauth != null ? Collections.singletonList(oauth) : Collections.emptyList()); + {{/hasOAuthMethods}} + {{^hasOAuthMethods}} + initHttpClient(); + {{/hasOAuthMethods}} + } else { + this.httpClient = client; + } + if (configuration != null) { {{#apiKeyMethods}} if (configuration.{{keyParamName}} != null) { @@ -146,14 +165,12 @@ public class ApiClient extends ApiClientCustom { } {{/isBasicBearer}} {{/authMethods}} - {{#clientState}} if (configuration.{{.}} != null) { this.{{.}} = configuration.{{.}}; } {{/clientState}} setVerifyingSsl(configuration.verifyingSsl); - setBasePath(configuration.host); } } {{#hasOAuthMethods}} @@ -202,32 +219,31 @@ public class ApiClient extends ApiClientCustom { if (basePath != null) { this.basePath = basePath; } - - String tokenUrl = "{{{tokenUrl}}}"; - if (!"".equals(tokenUrl) && !URI.create(tokenUrl).isAbsolute()) { - URI uri = URI.create(getBasePath()); - tokenUrl = uri.getScheme() + ":" + - (uri.getAuthority() != null ? "//" + uri.getAuthority() : "") + - tokenUrl; - if (!URI.create(tokenUrl).isAbsolute()) { - throw new IllegalArgumentException("OAuth2 token URL must be an absolute URL"); - } - } - RetryingOAuth retryingOAuth = new RetryingOAuth(tokenUrl, clientId, OAuthFlow.{{#lambda.uppercase}}{{#lambda.snakecase}}{{flow}}{{/lambda.snakecase}}{{/lambda.uppercase}}, clientSecret, parameters); - authentications.put( - "{{name}}", - retryingOAuth - ); - initHttpClient(Collections.singletonList(retryingOAuth)); - // Setup authentications (key: authentication name, value: authentication).{{#authMethods}}{{#isBasic}}{{#isBasicBasic}} + // Setup authentications (key: authentication name, value: authentication). + RetryingOAuth oauth = setupOauth(clientId, clientSecret, parameters); + initHttpClient(Collections.singletonList(oauth)); + {{#authMethods}}{{#isBasic}}{{#isBasicBasic}} authentications.put("{{name}}", new HttpBasicAuth());{{/isBasicBasic}}{{^isBasicBasic}} authentications.put("{{name}}", new HttpBearerAuth("{{scheme}}"));{{/isBasicBasic}}{{/isBasic}}{{#isApiKey}} authentications.put("{{name}}", new ApiKeyAuth({{#isKeyInHeader}}"header"{{/isKeyInHeader}}{{#isKeyInQuery}}"query"{{/isKeyInQuery}}{{#isKeyInCookie}}"cookie"{{/isKeyInCookie}}, "{{keyParamName}}"));{{/isApiKey}}{{/authMethods}} - // Prevent the authentications from being modified. authentications = Collections.unmodifiableMap(authentications); } + private RetryingOAuth setupOauth(String clientId, String clientSecret, Map parameters) { + if (!"".equals(getTokenUrl()) && !URI.create(getTokenUrl()).isAbsolute()) { + URI uri = URI.create(getBasePath()); + setTokenUrl(uri.getScheme() + ":" + + (uri.getAuthority() != null ? "//" + uri.getAuthority() : "") + + getTokenUrl()); + if (!URI.create(getTokenUrl()).isAbsolute()) { + throw new IllegalArgumentException("OAuth2 token URL must be an absolute URL"); + } + } + RetryingOAuth oauth = new RetryingOAuth(getTokenUrl(), clientId, OAuthFlow.{{#lambda.uppercase}}{{#lambda.snakecase}}{{flow}}{{/lambda.snakecase}}{{/lambda.uppercase}}, clientSecret, parameters); + authentications.put("{{name}}", oauth); + return oauth; + } {{/-first}} {{/oauthMethods}} {{/hasOAuthMethods}} @@ -245,7 +261,6 @@ public class ApiClient extends ApiClientCustom { ((ApiKeyAuth) this.getAuthentication("{{{name}}}")).setApiKeyPrefix(prefix); } {{/apiKeyMethods}} - private void initHttpClient() { initHttpClient(Collections.emptyList()); } @@ -293,7 +308,7 @@ public class ApiClient extends ApiClientCustom { /** * Set base path * - * @param basePath Base path of the URL (e.g {{{basePath}}} + * @param basePath Base path of the URL (e.g {{{basePath}}}) * @return An instance of OkHttpClient */ public ApiClient setBasePath(String basePath) { @@ -305,6 +320,36 @@ public class ApiClient extends ApiClientCustom { return this; } + {{#hasOAuthMethods}} + {{#oauthMethods}} + {{#-first}} + /** + * Get token url + * + * @return token url + */ + public String getTokenUrl() { + return tokenUrl; + } + + /** + * Set token url + * + * @param tokenUrl Token URL for oauth (e.g {{{tokenUrl}}}) + * @return An instance of OkHttpClient + */ + public ApiClient setTokenUrl(String tokenUrl) { + // strip trailing slash from tokenUrl + if (tokenUrl != null && tokenUrl.endsWith("/")) { + tokenUrl = tokenUrl.substring(0, tokenUrl.length() - 1); + } + this.tokenUrl = tokenUrl; + return this; + } + {{/-first}} + {{/oauthMethods}} + {{/hasOAuthMethods}} + /** * Get HTTP client *