Skip to content

Commit

Permalink
[java/resttemplate] Generate valid code if no Authentication implemen…
Browse files Browse the repository at this point in the history
…tations present

Take the logic used to decide which instances to add to the authentications map and re-use to not import classes or offer non-functional util methods
  • Loading branch information
jonfreedman authored May 10, 2019
1 parent 757fcfe commit 6e45090
Showing 1 changed file with 32 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,11 @@ import java.util.Map.Entry;
import java.util.TimeZone;

import {{invokerPackage}}.auth.Authentication;
import {{invokerPackage}}.auth.HttpBasicAuth;
import {{invokerPackage}}.auth.HttpBearerAuth;
import {{invokerPackage}}.auth.ApiKeyAuth;
{{#hasOAuthMethods}}
import {{invokerPackage}}.auth.OAuth;
{{/hasOAuthMethods}}

{{#authMethods}}{{#isBasic}}{{#isBasicBasic}}
import {{invokerPackage}}.auth.HttpBasicAuth;{{/isBasicBasic}}{{^isBasicBasic}}
import {{invokerPackage}}.auth.HttpBearerAuth;{{/isBasicBasic}}{{/isBasic}}{{#isApiKey}}
import {{invokerPackage}}.auth.ApiKeyAuth;{{/isApiKey}}{{#isOAuth}}
import {{invokerPackage}}.auth.OAuth;{{/isOAuth}}{{/authMethods}}
{{>generatedAnnotation}}
@Component("{{invokerPackage}}.ApiClient")
public class ApiClient {
Expand Down Expand Up @@ -176,31 +174,7 @@ public class ApiClient {
public Map<String, Authentication> getAuthentications() {
return authentications;
}

/**
* Get authentication for the given name.
*
* @param authName The authentication name
* @return The authentication, null if not found
*/
public Authentication getAuthentication(String authName) {
return authentications.get(authName);
}

/**
* Helper method to set token for HTTP bearer authentication.
* @param bearerToken the token
*/
public void setBearerToken(String bearerToken) {
for (Authentication auth : authentications.values()) {
if (auth instanceof HttpBearerAuth) {
((HttpBearerAuth) auth).setBearerToken(bearerToken);
return;
}
}
throw new RuntimeException("No Bearer authentication configured!");
}

{{#authMethods}}{{#isBasic}}{{#isBasicBasic}}
/**
* Helper method to set username for the first HTTP basic authentication.
* @param username the username
Expand Down Expand Up @@ -228,7 +202,21 @@ public class ApiClient {
}
throw new RuntimeException("No HTTP basic authentication configured!");
}

{{/isBasicBasic}}{{^isBasicBasic}}
/**
* Helper method to set token for HTTP bearer authentication.
* @param bearerToken the token
*/
public void setBearerToken(String bearerToken) {
for (Authentication auth : authentications.values()) {
if (auth instanceof HttpBearerAuth) {
((HttpBearerAuth) auth).setBearerToken(bearerToken);
return;
}
}
throw new RuntimeException("No Bearer authentication configured!");
}
{{/isBasicBasic}}{{/isBasic}}{{#isApiKey}}
/**
* Helper method to set API key value for the first API key authentication.
* @param apiKey the API key
Expand Down Expand Up @@ -256,8 +244,7 @@ public class ApiClient {
}
throw new RuntimeException("No API key authentication configured!");
}

{{#hasOAuthMethods}}
{{/isApiKey}}{{#isOAuth}}
/**
* Helper method to set access token for the first OAuth2 authentication.
* @param accessToken the access token
Expand All @@ -271,8 +258,17 @@ public class ApiClient {
}
throw new RuntimeException("No OAuth2 authentication configured!");
}
{{/isOAuth}}{{/authMethods}}
/**
* Get authentication for the given name.
*
* @param authName The authentication name
* @return The authentication, null if not found
*/
public Authentication getAuthentication(String authName) {
return authentications.get(authName);
}

{{/hasOAuthMethods}}
/**
* Set the User-Agent header's value (by adding to the default header map).
* @param userAgent the user agent string
Expand Down

0 comments on commit 6e45090

Please sign in to comment.