Skip to content

Commit 6f21f15

Browse files
committed
Support for more HTTP methods
1 parent 3a8d4f6 commit 6f21f15

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

scribejava-httpclient-okhttp/src/main/java/com/github/scribejava/httpclient/okhttp/OkHttpHttpClient.java

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import okhttp3.OkHttpClient;
1212
import okhttp3.Request;
1313
import okhttp3.RequestBody;
14+
import okhttp3.internal.http.HttpMethod;
1415

1516
import java.io.IOException;
1617
import java.util.Map;
@@ -42,27 +43,28 @@ public <T> Future<T> executeAsync(String userAgent, Map<String, String> headers,
4243
final Request.Builder requestBuilder = new Request.Builder();
4344
requestBuilder.url(completeUrl);
4445

45-
switch (httpVerb) {
46-
case GET:
47-
requestBuilder.get();
48-
break;
49-
case POST:
50-
final String contentType = headers.containsKey(AbstractRequest.CONTENT_TYPE) ?
51-
headers.get(AbstractRequest.CONTENT_TYPE) : DEFAULT_CONTENT_TYPE;
46+
final String method = httpVerb.name();
5247

53-
requestBuilder.post(RequestBody.create(MediaType.parse(contentType), bodyContents));
54-
break;
55-
default:
56-
throw new IllegalArgumentException("message build error: unknown verb type");
48+
// prepare body
49+
RequestBody body = null;
50+
if (bodyContents != null && !bodyContents.isEmpty() && HttpMethod.permitsRequestBody(method)) {
51+
final String contentType = headers.containsKey(AbstractRequest.CONTENT_TYPE) ?
52+
headers.get(AbstractRequest.CONTENT_TYPE) : DEFAULT_CONTENT_TYPE;
53+
body = RequestBody.create(MediaType.parse(contentType), bodyContents);
5754
}
5855

56+
// fill HTTP method and body
57+
requestBuilder.method(method, body);
58+
59+
// fill headers
5960
for (Map.Entry<String, String> header : headers.entrySet()) {
6061
requestBuilder.addHeader(header.getKey(), header.getValue());
6162
}
6263
if (userAgent != null) {
6364
requestBuilder.header(OAuthConstants.USER_AGENT_HEADER_NAME, userAgent);
6465
}
6566

67+
// create a new call
6668
final Call call = client.newCall(requestBuilder.build());
6769
return new OAuthAsyncCompletionHandler<>(callback, converter, call);
6870
}

0 commit comments

Comments
 (0)