github.com/phrase/openapi@v0.0.0-20240514140800-49e8a106740e/openapi-generator/templates/java/libraries/retrofit2/play25/ApiClient.mustache (about) 1 package {{invokerPackage}}; 2 3 import java.io.IOException; 4 import java.lang.annotation.Annotation; 5 import java.lang.reflect.Type; 6 import java.util.*; 7 8 import retrofit2.Retrofit; 9 import retrofit2.converter.scalars.ScalarsConverterFactory; 10 import retrofit2.converter.jackson.JacksonConverterFactory; 11 import com.fasterxml.jackson.databind.ObjectMapper; 12 import org.openapitools.jackson.nullable.JsonNullableModule; 13 14 import play.libs.Json; 15 import play.libs.ws.WSClient; 16 17 import {{invokerPackage}}.Play25CallAdapterFactory; 18 import {{invokerPackage}}.Play25CallFactory; 19 20 import okhttp3.Interceptor; 21 import {{invokerPackage}}.auth.ApiKeyAuth; 22 import {{invokerPackage}}.auth.Authentication; 23 24 /** 25 * API client 26 */ 27 public class ApiClient { 28 29 /** Underlying HTTP-client */ 30 private WSClient wsClient; 31 32 /** Supported auths */ 33 private Map<String, Authentication> authentications; 34 35 /** API base path */ 36 private String basePath = "{{{basePath}}}"; 37 38 public ApiClient(WSClient wsClient) { 39 this(); 40 this.wsClient = wsClient; 41 } 42 43 public ApiClient() { 44 // Setup authentications (key: authentication name, value: authentication). 45 authentications = new HashMap<{{#supportJava6}}String, Authentication{{/supportJava6}}>();{{#authMethods}}{{#isBasic}} 46 // authentications.put("{{name}}", new HttpBasicAuth());{{/isBasic}}{{#isApiKey}} 47 authentications.put("{{name}}", new ApiKeyAuth({{#isKeyInHeader}}"header"{{/isKeyInHeader}}{{#isKeyInQuery}}"query"{{/isKeyInQuery}}{{#isKeyInCookie}}"query"{{/isKeyInCookie}}, "{{keyParamName}}"));{{/isApiKey}}{{#isOAuth}} 48 // authentications.put("{{name}}", new OAuth());{{/isOAuth}}{{/authMethods}} 49 // Prevent the authentications from being modified. 50 authentications = Collections.unmodifiableMap(authentications); 51 52 } 53 54 /** 55 * Creates a retrofit2 client for given API interface 56 */ 57 public <S> S createService(Class<S> serviceClass) { 58 if(!basePath.endsWith("/")) { 59 basePath = basePath + "/"; 60 } 61 62 Map<String, String> extraHeaders = new HashMap<{{#supportJava6}}String, String{{/supportJava6}}>(); 63 Map<String, String> extraCookies = new HashMap<{{#supportJava6}}String, String{{/supportJava6}}>(); 64 List<Pair> extraQueryParams = new ArrayList<{{#supportJava6}}Pair{{/supportJava6}}>(); 65 66 for (String authName : authentications.keySet()) { 67 Authentication auth = authentications.get(authName); 68 if (auth == null) throw new RuntimeException("Authentication undefined: " + authName); 69 70 auth.applyToParams(extraQueryParams, extraHeaders, extraCookies); 71 } 72 73 ObjectMapper mapper = Json.mapper(); 74 JsonNullableModule jnm = new JsonNullableModule(); 75 mapper.registerModule(jnm); 76 77 return new Retrofit.Builder() 78 .baseUrl(basePath) 79 .addConverterFactory(ScalarsConverterFactory.create()) 80 .addConverterFactory(JacksonConverterFactory.create(mapper)) 81 .callFactory(new Play25CallFactory(wsClient, extraHeaders, extraCookies, extraQueryParams)) 82 .addCallAdapterFactory(new Play25CallAdapterFactory()) 83 .build() 84 .create(serviceClass); 85 } 86 87 /** 88 * Helper method to set API base path 89 */ 90 public ApiClient setBasePath(String basePath) { 91 this.basePath = basePath; 92 return this; 93 } 94 95 /** 96 * Get authentications (key: authentication name, value: authentication). 97 */ 98 public Map<String, Authentication> getAuthentications() { 99 return authentications; 100 } 101 102 /** 103 * Get authentication for the given name. 104 * 105 * @param authName The authentication name 106 * @return The authentication, null if not found 107 */ 108 public Authentication getAuthentication(String authName) { 109 return authentications.get(authName); 110 } 111 112 /** 113 * Helper method to set API key value for the first API key authentication. 114 */ 115 public ApiClient setApiKey(String apiKey) { 116 for (Authentication auth : authentications.values()) { 117 if (auth instanceof ApiKeyAuth) { 118 ((ApiKeyAuth) auth).setApiKey(apiKey); 119 return this; 120 } 121 } 122 123 throw new RuntimeException("No API key authentication configured!"); 124 } 125 126 /** 127 * Helper method to set API key prefix for the first API key authentication. 128 */ 129 public ApiClient setApiKeyPrefix(String apiKeyPrefix) { 130 for (Authentication auth : authentications.values()) { 131 if (auth instanceof ApiKeyAuth) { 132 ((ApiKeyAuth) auth).setApiKeyPrefix(apiKeyPrefix); 133 return this; 134 } 135 } 136 137 throw new RuntimeException("No API key authentication configured!"); 138 } 139 140 141 }