Skip to content

Commit 7d4ba27

Browse files
committed
Merge pull request #1162 from xhh/java-okhttp-gson
[Java] Add a new library template: okhttp-gson
2 parents c8ed2b0 + 3c79a69 commit 7d4ba27

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+5343
-12
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,7 @@ CONFIG OPTIONS
264264
library template (sub-template) to use:
265265
<default> - HTTP client: Jersey client 1.18. JSON processing: Jackson 2.4.2
266266
jersey2 - HTTP client: Jersey client 2.6
267+
okhttp-gson - HTTP client: OkHttp 2.4.0. JSON processing: Gson 2.3.1
267268
```
268269

269270
Your config file for java can look like

bin/all-petstore.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ cd $APP_DIR
2525
./bin/html-petstore.sh
2626
./bin/java-petstore.sh
2727
./bin/java-petstore-jersey2.sh
28+
./bin/java-petstore-okhttp-gson.sh
2829
./bin/jaxrs-petstore-server.sh
2930
./bin/nodejs-petstore-server.sh
3031
./bin/objc-petstore.sh

bin/java-petstore-okhttp-gson.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"library": "okhttp-gson",
3+
"artifactId": "swagger-petstore-okhttp-gson"
4+
}

bin/java-petstore-okhttp-gson.sh

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/bin/sh
2+
3+
SCRIPT="$0"
4+
5+
while [ -h "$SCRIPT" ] ; do
6+
ls=`ls -ld "$SCRIPT"`
7+
link=`expr "$ls" : '.*-> \(.*\)$'`
8+
if expr "$link" : '/.*' > /dev/null; then
9+
SCRIPT="$link"
10+
else
11+
SCRIPT=`dirname "$SCRIPT"`/"$link"
12+
fi
13+
done
14+
15+
if [ ! -d "${APP_DIR}" ]; then
16+
APP_DIR=`dirname "$SCRIPT"`/..
17+
APP_DIR=`cd "${APP_DIR}"; pwd`
18+
fi
19+
20+
executable="./modules/swagger-codegen-cli/target/swagger-codegen-cli.jar"
21+
22+
if [ ! -f "$executable" ]
23+
then
24+
mvn clean package
25+
fi
26+
27+
# if you've executed sbt assembly previously it will use that instead.
28+
export JAVA_OPTS="${JAVA_OPTS} -XX:MaxPermSize=256M -Xmx1024M -DloggerPath=conf/log4j.properties"
29+
ags="$@ generate -i modules/swagger-codegen/src/test/resources/2_0/petstore.json -l java -c bin/java-petstore-okhttp-gson.json -o samples/client/petstore/java/okhttp-gson"
30+
31+
java $JAVA_OPTS -jar $executable $ags

modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/JavaClientCodegen.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ public JavaClientCodegen() {
8383

8484
supportedLibraries.put("<default>", "HTTP client: Jersey client 1.18. JSON processing: Jackson 2.4.2");
8585
supportedLibraries.put("jersey2", "HTTP client: Jersey client 2.6");
86+
supportedLibraries.put("okhttp-gson", "HTTP client: OkHttp 2.4.0. JSON processing: Gson 2.3.1");
8687
cliOptions.add(buildLibraryCliOption(supportedLibraries));
8788
}
8889

@@ -159,7 +160,19 @@ public void processOpts() {
159160
supportingFiles.add(new SupportingFile("JSON.mustache", invokerFolder, "JSON.java"));
160161
supportingFiles.add(new SupportingFile("Pair.mustache", invokerFolder, "Pair.java"));
161162
supportingFiles.add(new SupportingFile("StringUtil.mustache", invokerFolder, "StringUtil.java"));
162-
supportingFiles.add(new SupportingFile("TypeRef.mustache", invokerFolder, "TypeRef.java"));
163+
164+
// library-specific files
165+
if ("okhttp-gson".equals(getLibrary())) {
166+
// the "okhttp-gson" library template requires "ApiCallback.mustache" for async call
167+
supportingFiles.add(new SupportingFile("ApiCallback.mustache", invokerFolder, "ApiCallback.java"));
168+
// "build.gradle" is for development with Gradle
169+
supportingFiles.add(new SupportingFile("build.gradle.mustache", "", "build.gradle"));
170+
// "build.sbt" is for development with SBT
171+
supportingFiles.add(new SupportingFile("build.sbt.mustache", "", "build.sbt"));
172+
// and does not require "TypeRef.mustache"
173+
} else {
174+
supportingFiles.add(new SupportingFile("TypeRef.mustache", invokerFolder, "TypeRef.java"));
175+
}
163176

164177
final String authFolder = (sourceFolder + File.separator + invokerPackage + ".auth").replace(".", File.separator);
165178
supportingFiles.add(new SupportingFile("auth/Authentication.mustache", authFolder, "Authentication.java"));

modules/swagger-codegen/src/main/resources/Java/StringUtil.mustache

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,13 @@ public class StringUtil {
3939
}
4040
return out.toString();
4141
}
42+
43+
/**
44+
* Convert the given object to string with each line indented by 4 spaces
45+
* (except the first line).
46+
*/
47+
public static String toIndentedString(Object o) {
48+
if (o == null) return "null";
49+
return o.toString().replace("\n", "\n ");
50+
}
4251
}

modules/swagger-codegen/src/main/resources/Java/apiException.mustache

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,45 @@ import java.util.List;
66
{{>generatedAnnotation}}
77
public class ApiException extends Exception {
88
private int code = 0;
9-
private String message = null;
109
private Map<String, List<String>> responseHeaders = null;
1110
private String responseBody = null;
1211
1312
public ApiException() {}
1413

14+
public ApiException(Throwable throwable) {
15+
super(throwable);
16+
}
17+
18+
public ApiException(String message) {
19+
super(message);
20+
}
21+
22+
public ApiException(String message, Throwable throwable, int code, Map<String, List<String>> responseHeaders, String responseBody) {
23+
super(message, throwable);
24+
this.code = code;
25+
this.responseHeaders = responseHeaders;
26+
this.responseBody = responseBody;
27+
}
28+
29+
public ApiException(String message, int code, Map<String, List<String>> responseHeaders, String responseBody) {
30+
this(message, (Throwable) null, code, responseHeaders, responseBody);
31+
}
32+
33+
public ApiException(String message, Throwable throwable, int code, Map<String, List<String>> responseHeaders) {
34+
this(message, throwable, code, responseHeaders, null);
35+
}
36+
37+
public ApiException(int code, Map<String, List<String>> responseHeaders, String responseBody) {
38+
this((String) null, (Throwable) null, code, responseHeaders, responseBody);
39+
}
40+
1541
public ApiException(int code, String message) {
42+
super(message);
1643
this.code = code;
17-
this.message = message;
1844
}
1945

2046
public ApiException(int code, String message, Map<String, List<String>> responseHeaders, String responseBody) {
21-
this.code = code;
22-
this.message = message;
47+
this(code, message);
2348
this.responseHeaders = responseHeaders;
2449
this.responseBody = responseBody;
2550
}
@@ -28,10 +53,6 @@ public class ApiException extends Exception {
2853
return code;
2954
}
3055

31-
public String getMessage() {
32-
return message;
33-
}
34-
3556
/**
3657
* Get the HTTP response headers.
3758
*/
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package {{invokerPackage}};
2+
3+
import java.io.IOException;
4+
5+
/**
6+
* Callback for asynchronous API call.
7+
*
8+
* @param <T> The return type
9+
*/
10+
public interface ApiCallback<T> {
11+
/**
12+
* This is called when the API call fails.
13+
*/
14+
void onFailure(ApiException e);
15+
16+
/**
17+
* This is called when the API call succeeded.
18+
*
19+
* @param result The result deserialized from response
20+
*/
21+
void onSuccess(T result);
22+
}

0 commit comments

Comments
 (0)