fixed content type for java client

This commit is contained in:
Tony Tam
2013-08-08 16:05:05 -07:00
parent 17b63b2be8
commit a3f5c0c688
14 changed files with 398 additions and 304 deletions

View File

@@ -44,9 +44,8 @@ public class ApiInvoker {
return response;
}
else if(String.class.equals(cls)) {
if(json != null && json.startsWith("\"") && json.endsWith("\"") && json.length() > 1) {
if(json != null && json.startsWith("\"") && json.endsWith("\"") && json.length() > 1)
return json.substring(1, json.length() - 2);
}
else
return json;
}
@@ -61,15 +60,17 @@ public class ApiInvoker {
public static String serialize(Object obj) throws ApiException {
try {
if (obj != null) return JsonUtil.getJsonMapper().writeValueAsString(obj);
else return null;
if (obj != null)
return JsonUtil.getJsonMapper().writeValueAsString(obj);
else
return null;
}
catch (Exception e) {
throw new ApiException(500, e.getMessage());
}
}
public String invokeAPI(String host, String path, String method, Map<String, String> queryParams, Object body, Map<String, String> headerParams) throws ApiException {
public String invokeAPI(String host, String path, String method, Map<String, String> queryParams, Object body, Map<String, String> headerParams, String contentType) throws ApiException {
Client client = getClient(host);
StringBuilder b = new StringBuilder();
@@ -77,14 +78,16 @@ public class ApiInvoker {
for(String key : queryParams.keySet()) {
String value = queryParams.get(key);
if (value != null){
if(b.toString().length() == 0) b.append("?");
else b.append("&");
if(b.toString().length() == 0)
b.append("?");
else
b.append("&");
b.append(escapeString(key)).append("=").append(escapeString(value));
}
}
String querystring = b.toString();
Builder builder = client.resource(host + path + querystring).type("application/json");
Builder builder = client.resource(host + path + querystring).accept("application/json");
for(String key : headerParams.keySet()) {
builder.header(key, headerParams.get(key));
}
@@ -100,13 +103,22 @@ public class ApiInvoker {
response = (ClientResponse) builder.get(ClientResponse.class);
}
else if ("POST".equals(method)) {
if(body == null)
response = builder.post(ClientResponse.class, serialize(body));
else
response = builder.type("application/json").post(ClientResponse.class, serialize(body));
}
else if ("PUT".equals(method)) {
response = builder.put(ClientResponse.class, serialize(body));
}
if(body == null)
response = builder.put(ClientResponse.class, serialize(body));
else
response = builder.type("application/json").put(ClientResponse.class, serialize(body));
}
else if ("DELETE".equals(method)) {
if(body == null)
response = builder.delete(ClientResponse.class, serialize(body));
else
response = builder.type("application/json").delete(ClientResponse.class, serialize(body));
}
else {
throw new ApiException(500, "unknown method type " + method);
@@ -122,11 +134,11 @@ public class ApiInvoker {
}
private Client getClient(String host) {
if(!hostMap.containsKey(host)) {
Client client = Client.create();
client.addFilter(new LoggingFilter());
hostMap.put(host, client);
}
return hostMap.get(host);
if(!hostMap.containsKey(host)) {
Client client = Client.create();
client.addFilter(new LoggingFilter());
hostMap.put(host, client);
}
return hostMap.get(host);
}
}