Skip to content

Commit 6c8aed5

Browse files
committed
Merge pull request #112 from vaelen/fix-no-content
Updated Java client to handle the case of a NO_CONTENT response.
2 parents cf95d4e + bfb5b72 commit 6c8aed5

File tree

2 files changed

+22
-17
lines changed

2 files changed

+22
-17
lines changed

samples/client/petstore/java/src/main/java/com/wordnik/client/ApiInvoker.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public class ApiInvoker {
2828
public static ApiInvoker getInstance() {
2929
return INSTANCE;
3030
}
31-
31+
3232
public void addDefaultHeader(String key, String value) {
3333
defaultHeaderMap.put(key, value);
3434
}
@@ -47,7 +47,7 @@ public static Object deserialize(String json, String containerType, Class cls) t
4747
else if(String.class.equals(cls)) {
4848
if(json != null && json.startsWith("\"") && json.endsWith("\"") && json.length() > 1)
4949
return json.substring(1, json.length() - 2);
50-
else
50+
else
5151
return json;
5252
}
5353
else {
@@ -61,9 +61,9 @@ else if(String.class.equals(cls)) {
6161

6262
public static String serialize(Object obj) throws ApiException {
6363
try {
64-
if (obj != null)
64+
if (obj != null)
6565
return JsonUtil.getJsonMapper().writeValueAsString(obj);
66-
else
66+
else
6767
return null;
6868
}
6969
catch (Exception e) {
@@ -75,7 +75,7 @@ public String invokeAPI(String host, String path, String method, Map<String, Str
7575
Client client = getClient(host);
7676

7777
StringBuilder b = new StringBuilder();
78-
78+
7979
for(String key : queryParams.keySet()) {
8080
String value = queryParams.get(key);
8181
if (value != null){
@@ -92,7 +92,7 @@ public String invokeAPI(String host, String path, String method, Map<String, Str
9292
for(String key : headerParams.keySet()) {
9393
builder.header(key, headerParams.get(key));
9494
}
95-
95+
9696
for(String key : defaultHeaderMap.keySet()) {
9797
if(!headerParams.containsKey(key)) {
9898
builder.header(key, defaultHeaderMap.get(key));
@@ -124,13 +124,16 @@ else if ("DELETE".equals(method)) {
124124
else {
125125
throw new ApiException(500, "unknown method type " + method);
126126
}
127-
if(response.getClientResponseStatus().getFamily() == Family.SUCCESSFUL) {
127+
if(response.getClientResponseStatus() == ClientResponse.Status.NO_CONTENT) {
128+
return null;
129+
}
130+
else if(response.getClientResponseStatus().getFamily() == Family.SUCCESSFUL) {
128131
return (String) response.getEntity(String.class);
129132
}
130133
else {
131134
throw new ApiException(
132135
response.getClientResponseStatus().getStatusCode(),
133-
response.getEntity(String.class));
136+
response.getEntity(String.class));
134137
}
135138
}
136139

@@ -143,4 +146,3 @@ private Client getClient(String host) {
143146
return hostMap.get(host);
144147
}
145148
}
146-

src/main/resources/Java/apiInvoker.mustache

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public class ApiInvoker {
2828
public static ApiInvoker getInstance() {
2929
return INSTANCE;
3030
}
31-
31+
3232
public void addDefaultHeader(String key, String value) {
3333
defaultHeaderMap.put(key, value);
3434
}
@@ -47,7 +47,7 @@ public class ApiInvoker {
4747
else if(String.class.equals(cls)) {
4848
if(json != null && json.startsWith("\"") && json.endsWith("\"") && json.length() > 1)
4949
return json.substring(1, json.length() - 2);
50-
else
50+
else
5151
return json;
5252
}
5353
else {
@@ -61,9 +61,9 @@ public class ApiInvoker {
6161

6262
public static String serialize(Object obj) throws ApiException {
6363
try {
64-
if (obj != null)
64+
if (obj != null)
6565
return JsonUtil.getJsonMapper().writeValueAsString(obj);
66-
else
66+
else
6767
return null;
6868
}
6969
catch (Exception e) {
@@ -75,7 +75,7 @@ public class ApiInvoker {
7575
Client client = getClient(host);
7676
7777
StringBuilder b = new StringBuilder();
78-
78+
7979
for(String key : queryParams.keySet()) {
8080
String value = queryParams.get(key);
8181
if (value != null){
@@ -92,7 +92,7 @@ public class ApiInvoker {
9292
for(String key : headerParams.keySet()) {
9393
builder.header(key, headerParams.get(key));
9494
}
95-
95+
9696
for(String key : defaultHeaderMap.keySet()) {
9797
if(!headerParams.containsKey(key)) {
9898
builder.header(key, defaultHeaderMap.get(key));
@@ -124,13 +124,16 @@ public class ApiInvoker {
124124
else {
125125
throw new ApiException(500, "unknown method type " + method);
126126
}
127-
if(response.getClientResponseStatus().getFamily() == Family.SUCCESSFUL) {
127+
if(response.getClientResponseStatus() == ClientResponse.Status.NO_CONTENT) {
128+
return null;
129+
}
130+
else if(response.getClientResponseStatus().getFamily() == Family.SUCCESSFUL) {
128131
return (String) response.getEntity(String.class);
129132
}
130133
else {
131134
throw new ApiException(
132135
response.getClientResponseStatus().getStatusCode(),
133-
response.getEntity(String.class));
136+
response.getEntity(String.class));
134137
}
135138
}
136139

0 commit comments

Comments
 (0)