Skip to content

Commit 3452f8e

Browse files
author
Jarkko Laitinen
committed
Added support for DELETE verb.
1 parent 1c271ba commit 3452f8e

File tree

1 file changed

+30
-8
lines changed

1 file changed

+30
-8
lines changed

fi/cosky/sdk/API.java

+30-8
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,8 @@ public <T extends BaseData> T navigate(Class<T> tClass, Link l, HashMap<String,
129129
result = sendRequest(Verb.PUT, this.baseUrl + uri, tClass, null);
130130
} else if (l.getMethod().equals("POST")) {
131131
result = sendRequest(Verb.POST, this.baseUrl + uri, tClass, null);
132+
} else if (l.getMethod().equals("DELETE")) {
133+
result = sendRequest(Verb.DELETE, this.baseUrl + uri, tClass, new Object());
132134
} else {
133135
result = sendRequest(Verb.GET, uri, tClass, null);
134136
}
@@ -170,6 +172,11 @@ public <T extends BaseData> T navigate(Class<T> tClass, Link l, Object object) t
170172
String url = this.baseUrl + l.getUri();
171173
result = sendRequest(Verb.PATCH, url, tClass, object);
172174
}
175+
176+
if (l.getMethod().equals("DELETE")) {
177+
String url = this.baseUrl + l.getUri();
178+
result = sendRequest(Verb.DELETE, url, tClass, object);
179+
}
173180
if (isTimed()) {
174181
end = System.currentTimeMillis();
175182
long time = end - start;
@@ -198,7 +205,7 @@ private <T extends BaseData> T sendRequest(Verb verb, String url, Class<T> tClas
198205
try {
199206
serverAddress = new URL(url);
200207
connection = (HttpURLConnection) serverAddress.openConnection();
201-
boolean doOutput = (verb != Verb.GET);
208+
boolean doOutput = doOutput(verb);
202209
connection.setDoOutput(doOutput);
203210
connection.setRequestMethod(method(verb));
204211
connection.setInstanceFollowRedirects(false);
@@ -270,6 +277,10 @@ else if (verb == Verb.POST && object == null) {
270277
return (T) objectCache.getObject(url);
271278
}
272279

280+
if (connection.getResponseCode() == HttpURLConnection.HTTP_NO_CONTENT) {
281+
return (T) new ResponseData();
282+
}
283+
273284
if (connection.getResponseCode() >= HttpURLConnection.HTTP_BAD_REQUEST && connection.getResponseCode() < HttpURLConnection.HTTP_INTERNAL_ERROR) {
274285
System.out.println("ErrorCode: " + connection.getResponseCode() + " " + connection.getResponseMessage() +
275286
" " + url + ", verb: " + verb);
@@ -342,16 +353,18 @@ private <T extends BaseData> T sendRequestWithAddedHeaders(Verb verb, String url
342353
serverAddress = new URL(url);
343354
connection = (HttpURLConnection) serverAddress.openConnection();
344355
connection.setInstanceFollowRedirects(false);
345-
connection.setDoOutput(true);
356+
boolean doOutput = doOutput(verb);
357+
connection.setDoOutput(doOutput);
346358
connection.setRequestMethod(method(verb));
347359
connection.setRequestProperty("Authorization", headers.get("authorization"));
348360
connection.addRequestProperty("Accept", "application/json");
349-
connection.addRequestProperty("Content-Length", "0");
350-
351-
OutputStreamWriter os = new OutputStreamWriter(connection.getOutputStream());
352-
os.write("");
353-
os.flush();
354-
os.close();
361+
if (doOutput){
362+
connection.addRequestProperty("Content-Length", "0");
363+
OutputStreamWriter os = new OutputStreamWriter(connection.getOutputStream());
364+
os.write("");
365+
os.flush();
366+
os.close();
367+
}
355368
connection.connect();
356369

357370
if (connection.getResponseCode() == HttpURLConnection.HTTP_SEE_OTHER || connection.getResponseCode() == HttpURLConnection.HTTP_CREATED) {
@@ -459,6 +472,15 @@ private Link parseLocationLinkFromString(String s) {
459472
return new Link("location", s, "GET", true);
460473
}
461474

475+
private boolean doOutput(Verb verb) {
476+
switch (verb) {
477+
case GET:
478+
case DELETE:
479+
return false;
480+
default:
481+
return true;
482+
}
483+
}
462484
private enum Verb {
463485
GET, PUT, POST, DELETE, PATCH
464486
}

0 commit comments

Comments
 (0)