forked from OpenAPITools/openapi-generator
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Java] Retrofit2 Play! Framework 2.6.x support (OpenAPITools#901)
* added play framework 2.6 support * generated petstore sample * generated petstore sample #2 * generated petstore sample #3 * Revert "generated petstore sample #3" * generated petstore sample #4 * fixed circleci configs * one more time samples regen
- Loading branch information
Showing
131 changed files
with
10,842 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"useBeanValidation":"true","enableBuilderSupport":"true","library":"retrofit2","usePlayWS":"true","playVersion":"play26"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#!/bin/sh | ||
|
||
SCRIPT="$0" | ||
echo "# START SCRIPT: $SCRIPT" | ||
|
||
while [ -h "$SCRIPT" ] ; do | ||
ls=`ls -ld "$SCRIPT"` | ||
link=`expr "$ls" : '.*-> \(.*\)$'` | ||
if expr "$link" : '/.*' > /dev/null; then | ||
SCRIPT="$link" | ||
else | ||
SCRIPT=`dirname "$SCRIPT"`/"$link" | ||
fi | ||
done | ||
|
||
if [ ! -d "${APP_DIR}" ]; then | ||
APP_DIR=`dirname "$SCRIPT"`/.. | ||
APP_DIR=`cd "${APP_DIR}"; pwd` | ||
fi | ||
|
||
executable="./modules/openapi-generator-cli/target/openapi-generator-cli.jar" | ||
|
||
if [ ! -f "$executable" ] | ||
then | ||
mvn -B clean package | ||
fi | ||
|
||
# if you've executed sbt assembly previously it will use that instead. | ||
export JAVA_OPTS="${JAVA_OPTS} -XX:MaxPermSize=256M -Xmx1024M -DloggerPath=conf/log4j.properties" | ||
ags="generate --artifact-id petstore-java-client-retrofit2-play26 -i modules/openapi-generator/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml -g java -c bin/java-petstore-retrofit2-play26.json -o samples/client/petstore/java/retrofit2-play26 -DhideGenerationTimestamp=true $@" | ||
|
||
echo "Removing files and folders under samples/client/petstore/java/retrofit2-play26/src/main" | ||
rm -rf samples/client/petstore/java/retrofit2-play26/src/main | ||
find samples/client/petstore/java/retrofit2-play26 -maxdepth 1 -type f ! -name "README.md" -exec rm {} + | ||
java $JAVA_OPTS -jar $executable $ags |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
205 changes: 205 additions & 0 deletions
205
...s/openapi-generator/src/main/resources/Java/libraries/retrofit2/play26/ApiClient.mustache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,205 @@ | ||
package {{invokerPackage}}; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.lang.annotation.Annotation; | ||
import java.lang.reflect.Type; | ||
import java.nio.file.Files; | ||
import java.nio.file.Paths; | ||
import java.util.*; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import retrofit2.Converter; | ||
import retrofit2.Retrofit; | ||
import retrofit2.converter.scalars.ScalarsConverterFactory; | ||
import retrofit2.converter.jackson.JacksonConverterFactory; | ||
|
||
import play.libs.Json; | ||
import play.libs.ws.WSClient; | ||
|
||
import {{invokerPackage}}.Play26CallAdapterFactory; | ||
import {{invokerPackage}}.Play26CallFactory; | ||
|
||
import okhttp3.Interceptor; | ||
import okhttp3.ResponseBody; | ||
import {{invokerPackage}}.auth.ApiKeyAuth; | ||
import {{invokerPackage}}.auth.Authentication; | ||
|
||
/** | ||
* API client | ||
*/ | ||
public class ApiClient { | ||
/** Underlying HTTP-client */ | ||
private WSClient wsClient; | ||
/** Creates HTTP call instances */ | ||
private Play26CallFactory callFactory; | ||
/** Create {@link java.util.concurrent.CompletionStage} instances from HTTP calls */ | ||
private Play26CallAdapterFactory callAdapterFactory; | ||
|
||
/** Supported auths */ | ||
private Map<String, Authentication> authentications; | ||
|
||
/** API base path */ | ||
private String basePath = "{{{basePath}}}"; | ||
|
||
/** Default ObjectMapper */ | ||
private ObjectMapper defaultMapper; | ||
|
||
public ApiClient(WSClient wsClient) { | ||
this(); | ||
this.wsClient = wsClient; | ||
} | ||
|
||
public ApiClient() { | ||
// Setup authentications (key: authentication name, value: authentication). | ||
authentications = new HashMap<>();{{#authMethods}}{{#isBasic}} | ||
// authentications.put("{{name}}", new HttpBasicAuth());{{/isBasic}}{{#isApiKey}} | ||
authentications.put("{{name}}", new ApiKeyAuth({{#isKeyInHeader}}"header"{{/isKeyInHeader}}{{^isKeyInHeader}}"query"{{/isKeyInHeader}}, "{{keyParamName}}"));{{/isApiKey}}{{#isOAuth}} | ||
// authentications.put("{{name}}", new OAuth());{{/isOAuth}}{{/authMethods}} | ||
// Prevent the authentications from being modified. | ||
authentications = Collections.unmodifiableMap(authentications); | ||
} | ||
|
||
/** | ||
* Creates a retrofit2 client for given API interface | ||
*/ | ||
public <S> S createService(Class<S> serviceClass) { | ||
if(!basePath.endsWith("/")) { | ||
basePath = basePath + "/"; | ||
} | ||
|
||
Map<String, String> extraHeaders = new HashMap<>(); | ||
List<Pair> extraQueryParams = new ArrayList<>(); | ||
|
||
for (String authName : authentications.keySet()) { | ||
Authentication auth = authentications.get(authName); | ||
if (auth == null) throw new RuntimeException("Authentication undefined: " + authName); | ||
auth.applyToParams(extraQueryParams, extraHeaders); | ||
} | ||
|
||
if (callFactory == null) { | ||
callFactory = new Play26CallFactory(wsClient, extraHeaders, extraQueryParams); | ||
} | ||
if (callAdapterFactory == null) { | ||
callAdapterFactory = new Play26CallAdapterFactory(); | ||
} | ||
if (defaultMapper == null) { | ||
defaultMapper = Json.mapper(); | ||
} | ||
|
||
return new Retrofit.Builder() | ||
.baseUrl(basePath) | ||
.addConverterFactory(new FileConverter()) | ||
.addConverterFactory(ScalarsConverterFactory.create()) | ||
.addConverterFactory(JacksonConverterFactory.create(defaultMapper)) | ||
.callFactory(callFactory) | ||
.addCallAdapterFactory(callAdapterFactory) | ||
.build() | ||
.create(serviceClass); | ||
} | ||
|
||
/** | ||
* Helper method to set API base path | ||
*/ | ||
public ApiClient setBasePath(String basePath) { | ||
this.basePath = basePath; | ||
return this; | ||
} | ||
|
||
/** | ||
* Get authentications (key: authentication name, value: authentication). | ||
*/ | ||
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 API key value for the first API key authentication. | ||
*/ | ||
public ApiClient setApiKey(String apiKey) { | ||
for (Authentication auth : authentications.values()) { | ||
if (auth instanceof ApiKeyAuth) { | ||
((ApiKeyAuth) auth).setApiKey(apiKey); | ||
return this; | ||
} | ||
} | ||
|
||
throw new RuntimeException("No API key authentication configured!"); | ||
} | ||
|
||
/** | ||
* Helper method to set API key prefix for the first API key authentication. | ||
*/ | ||
public ApiClient setApiKeyPrefix(String apiKeyPrefix) { | ||
for (Authentication auth : authentications.values()) { | ||
if (auth instanceof ApiKeyAuth) { | ||
((ApiKeyAuth) auth).setApiKeyPrefix(apiKeyPrefix); | ||
return this; | ||
} | ||
} | ||
|
||
throw new RuntimeException("No API key authentication configured!"); | ||
} | ||
|
||
/** | ||
* Helper method to set HTTP call instances factory | ||
*/ | ||
public ApiClient setCallFactory(Play26CallFactory callFactory) { | ||
this.callFactory = callFactory; | ||
return this; | ||
} | ||
|
||
/** | ||
* Helper method to set {@link java.util.concurrent.CompletionStage} instances factory | ||
*/ | ||
public ApiClient setCallAdapterFactory(Play26CallAdapterFactory callAdapterFactory) { | ||
this.callAdapterFactory = callAdapterFactory; | ||
return this; | ||
} | ||
|
||
/** | ||
* Helper method to set Jackson's {@link ObjectMapper} | ||
*/ | ||
public ApiClient setObjectMapper(ObjectMapper mapper) { | ||
this.defaultMapper = mapper; | ||
return this; | ||
} | ||
|
||
static class FileConverter extends Converter.Factory { | ||
@Override | ||
public Converter<ResponseBody, File> responseBodyConverter(Type type, | ||
Annotation[] annotations, Retrofit retrofit) { | ||
if (!File.class.getTypeName().equals(type.getTypeName())) { | ||
return null; | ||
} | ||
|
||
return new Converter<ResponseBody, File>() { | ||
@Override | ||
public File convert(ResponseBody value) throws IOException { | ||
File file = File.createTempFile("retrofit-file", ".tmp"); | ||
Files.write(Paths.get(file.getPath()), value.bytes()); | ||
return file; | ||
} | ||
}; | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.