|
11 | 11 | import okhttp3.OkHttpClient; |
12 | 12 | import okhttp3.Request; |
13 | 13 | import okhttp3.RequestBody; |
| 14 | +import okhttp3.internal.http.HttpMethod; |
14 | 15 |
|
15 | 16 | import java.io.IOException; |
16 | 17 | import java.util.Map; |
@@ -42,27 +43,28 @@ public <T> Future<T> executeAsync(String userAgent, Map<String, String> headers, |
42 | 43 | final Request.Builder requestBuilder = new Request.Builder(); |
43 | 44 | requestBuilder.url(completeUrl); |
44 | 45 |
|
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(); |
52 | 47 |
|
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); |
57 | 54 | } |
58 | 55 |
|
| 56 | + // fill HTTP method and body |
| 57 | + requestBuilder.method(method, body); |
| 58 | + |
| 59 | + // fill headers |
59 | 60 | for (Map.Entry<String, String> header : headers.entrySet()) { |
60 | 61 | requestBuilder.addHeader(header.getKey(), header.getValue()); |
61 | 62 | } |
62 | 63 | if (userAgent != null) { |
63 | 64 | requestBuilder.header(OAuthConstants.USER_AGENT_HEADER_NAME, userAgent); |
64 | 65 | } |
65 | 66 |
|
| 67 | + // create a new call |
66 | 68 | final Call call = client.newCall(requestBuilder.build()); |
67 | 69 | return new OAuthAsyncCompletionHandler<>(callback, converter, call); |
68 | 70 | } |
|
0 commit comments