Skip to content

Commit

Permalink
generator changes for Splitit Java SDK (#325)
Browse files Browse the repository at this point in the history
  • Loading branch information
eddiechayes authored Nov 2, 2023
1 parent 137ac92 commit 5e4ee5a
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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}};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String> defaultHeaderMap = new HashMap<String, String>();
private Map<String, String> defaultCookieMap = new HashMap<String, String>();
Expand Down Expand Up @@ -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) {
Expand All @@ -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}}
Expand Down Expand Up @@ -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.<Interceptor>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<String, String> 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}}
Expand All @@ -245,7 +261,6 @@ public class ApiClient extends ApiClientCustom {
((ApiKeyAuth) this.getAuthentication("{{{name}}}")).setApiKeyPrefix(prefix);
}
{{/apiKeyMethods}}

private void initHttpClient() {
initHttpClient(Collections.<Interceptor>emptyList());
}
Expand Down Expand Up @@ -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) {
Expand All @@ -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
*
Expand Down

0 comments on commit 5e4ee5a

Please sign in to comment.