|
| 1 | +package {{invokerPackage}}; |
| 2 | + |
| 3 | +import java.io.File; |
| 4 | +import java.io.IOException; |
| 5 | +import java.lang.annotation.Annotation; |
| 6 | +import java.lang.reflect.Type; |
| 7 | +import java.nio.file.Files; |
| 8 | +import java.nio.file.Paths; |
| 9 | +import java.util.*; |
| 10 | + |
| 11 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 12 | +import retrofit2.Converter; |
| 13 | +import retrofit2.Retrofit; |
| 14 | +import retrofit2.converter.scalars.ScalarsConverterFactory; |
| 15 | +import retrofit2.converter.jackson.JacksonConverterFactory; |
| 16 | + |
| 17 | +import play.libs.Json; |
| 18 | +import play.libs.ws.WSClient; |
| 19 | + |
| 20 | +import {{invokerPackage}}.Play26CallAdapterFactory; |
| 21 | +import {{invokerPackage}}.Play26CallFactory; |
| 22 | + |
| 23 | +import okhttp3.Interceptor; |
| 24 | +import okhttp3.ResponseBody; |
| 25 | +import {{invokerPackage}}.auth.ApiKeyAuth; |
| 26 | +import {{invokerPackage}}.auth.Authentication; |
| 27 | + |
| 28 | +/** |
| 29 | + * API client |
| 30 | + */ |
| 31 | +public class ApiClient { |
| 32 | +
|
| 33 | + /** Underlying HTTP-client */ |
| 34 | + private WSClient wsClient; |
| 35 | +
|
| 36 | + /** Creates HTTP call instances */ |
| 37 | + private Play26CallFactory callFactory; |
| 38 | +
|
| 39 | + /** Create {@link java.util.concurrent.CompletionStage} instances from HTTP calls */ |
| 40 | + private Play26CallAdapterFactory callAdapterFactory; |
| 41 | + |
| 42 | + /** Supported auths */ |
| 43 | + private Map<String, Authentication> authentications; |
| 44 | + |
| 45 | + /** API base path */ |
| 46 | + private String basePath = "{{{basePath}}}"; |
| 47 | + |
| 48 | + /** Default ObjectMapper */ |
| 49 | + private ObjectMapper defaultMapper; |
| 50 | + |
| 51 | + public ApiClient(WSClient wsClient) { |
| 52 | + this(); |
| 53 | + this.wsClient = wsClient; |
| 54 | + } |
| 55 | + |
| 56 | + public ApiClient() { |
| 57 | + // Setup authentications (key: authentication name, value: authentication). |
| 58 | + authentications = new HashMap<>();{{#authMethods}}{{#isBasic}} |
| 59 | + // authentications.put("{{name}}", new HttpBasicAuth());{{/isBasic}}{{#isApiKey}} |
| 60 | + authentications.put("{{name}}", new ApiKeyAuth({{#isKeyInHeader}}"header"{{/isKeyInHeader}}{{^isKeyInHeader}}"query"{{/isKeyInHeader}}, "{{keyParamName}}"));{{/isApiKey}}{{#isOAuth}} |
| 61 | + // authentications.put("{{name}}", new OAuth());{{/isOAuth}}{{/authMethods}} |
| 62 | + // Prevent the authentications from being modified. |
| 63 | + authentications = Collections.unmodifiableMap(authentications); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Creates a retrofit2 client for given API interface |
| 68 | + */ |
| 69 | + public <S> S createService(Class<S> serviceClass) { |
| 70 | + if(!basePath.endsWith("/")) { |
| 71 | + basePath = basePath + "/"; |
| 72 | + } |
| 73 | + |
| 74 | + Map<String, String> extraHeaders = new HashMap<>(); |
| 75 | + List<Pair> extraQueryParams = new ArrayList<>(); |
| 76 | + |
| 77 | + for (String authName : authentications.keySet()) { |
| 78 | + Authentication auth = authentications.get(authName); |
| 79 | + if (auth == null) throw new RuntimeException("Authentication undefined: " + authName); |
| 80 | +
|
| 81 | + auth.applyToParams(extraQueryParams, extraHeaders); |
| 82 | + } |
| 83 | + |
| 84 | + if (callFactory == null) { |
| 85 | + callFactory = new Play26CallFactory(wsClient, extraHeaders, extraQueryParams); |
| 86 | + } |
| 87 | + if (callAdapterFactory == null) { |
| 88 | + callAdapterFactory = new Play26CallAdapterFactory(); |
| 89 | + } |
| 90 | + if (defaultMapper == null) { |
| 91 | + defaultMapper = Json.mapper(); |
| 92 | + } |
| 93 | + |
| 94 | + return new Retrofit.Builder() |
| 95 | + .baseUrl(basePath) |
| 96 | + .addConverterFactory(new FileConverter()) |
| 97 | + .addConverterFactory(ScalarsConverterFactory.create()) |
| 98 | + .addConverterFactory(JacksonConverterFactory.create(defaultMapper)) |
| 99 | + .callFactory(callFactory) |
| 100 | + .addCallAdapterFactory(callAdapterFactory) |
| 101 | + .build() |
| 102 | + .create(serviceClass); |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Helper method to set API base path |
| 107 | + */ |
| 108 | + public ApiClient setBasePath(String basePath) { |
| 109 | + this.basePath = basePath; |
| 110 | + return this; |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Get authentications (key: authentication name, value: authentication). |
| 115 | + */ |
| 116 | + public Map<String, Authentication> getAuthentications() { |
| 117 | + return authentications; |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * Get authentication for the given name. |
| 122 | + * |
| 123 | + * @param authName The authentication name |
| 124 | + * @return The authentication, null if not found |
| 125 | + */ |
| 126 | + public Authentication getAuthentication(String authName) { |
| 127 | + return authentications.get(authName); |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * Helper method to set API key value for the first API key authentication. |
| 132 | + */ |
| 133 | + public ApiClient setApiKey(String apiKey) { |
| 134 | + for (Authentication auth : authentications.values()) { |
| 135 | + if (auth instanceof ApiKeyAuth) { |
| 136 | + ((ApiKeyAuth) auth).setApiKey(apiKey); |
| 137 | + return this; |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + throw new RuntimeException("No API key authentication configured!"); |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * Helper method to set API key prefix for the first API key authentication. |
| 146 | + */ |
| 147 | + public ApiClient setApiKeyPrefix(String apiKeyPrefix) { |
| 148 | + for (Authentication auth : authentications.values()) { |
| 149 | + if (auth instanceof ApiKeyAuth) { |
| 150 | + ((ApiKeyAuth) auth).setApiKeyPrefix(apiKeyPrefix); |
| 151 | + return this; |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + throw new RuntimeException("No API key authentication configured!"); |
| 156 | + } |
| 157 | + |
| 158 | + /** |
| 159 | + * Helper method to set HTTP call instances factory |
| 160 | + */ |
| 161 | + public ApiClient setCallFactory(Play26CallFactory callFactory) { |
| 162 | + this.callFactory = callFactory; |
| 163 | + return this; |
| 164 | + } |
| 165 | + |
| 166 | + /** |
| 167 | + * Helper method to set {@link java.util.concurrent.CompletionStage} instances factory |
| 168 | + */ |
| 169 | + public ApiClient setCallAdapterFactory(Play26CallAdapterFactory callAdapterFactory) { |
| 170 | + this.callAdapterFactory = callAdapterFactory; |
| 171 | + return this; |
| 172 | + } |
| 173 | + |
| 174 | + /** |
| 175 | + * Helper method to set Jackson's {@link ObjectMapper} |
| 176 | + */ |
| 177 | + public ApiClient setObjectMapper(ObjectMapper mapper) { |
| 178 | + this.defaultMapper = mapper; |
| 179 | + return this; |
| 180 | + } |
| 181 | + |
| 182 | + static class FileConverter extends Converter.Factory { |
| 183 | +
|
| 184 | + @Override |
| 185 | + public Converter<ResponseBody, File> responseBodyConverter(Type type, |
| 186 | + Annotation[] annotations, Retrofit retrofit) { |
| 187 | +
|
| 188 | + if (!File.class.getTypeName().equals(type.getTypeName())) { |
| 189 | + return null; |
| 190 | + } |
| 191 | + |
| 192 | + return new Converter<ResponseBody, File>() { |
| 193 | +
|
| 194 | + @Override |
| 195 | + public File convert(ResponseBody value) throws IOException { |
| 196 | +
|
| 197 | + File file = File.createTempFile("retrofit-file", ".tmp"); |
| 198 | + Files.write(Paths.get(file.getPath()), value.bytes()); |
| 199 | + return file; |
| 200 | + } |
| 201 | + }; |
| 202 | + } |
| 203 | + } |
| 204 | + |
| 205 | +} |
0 commit comments