Skip to content

Commit c09bfce

Browse files
committed
add possibility to execute requests in sync manner for OkHttp, AHC and Ning (and any other)
1 parent a1cb90a commit c09bfce

File tree

7 files changed

+115
-21
lines changed

7 files changed

+115
-21
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.github.scribejava.core.httpclient;
2+
3+
import com.github.scribejava.core.model.OAuthRequestAsync;
4+
import com.github.scribejava.core.model.Response;
5+
import com.github.scribejava.core.model.Verb;
6+
import java.io.File;
7+
import java.io.IOException;
8+
import java.util.Map;
9+
import java.util.concurrent.ExecutionException;
10+
11+
public abstract class AbstractAsyncOnlyHttpClient implements HttpClient {
12+
13+
@Override
14+
public Response execute(String userAgent, Map<String, String> headers, Verb httpVerb, String completeUrl,
15+
byte[] bodyContents) throws InterruptedException, ExecutionException, IOException {
16+
return executeAsync(userAgent, headers, httpVerb, completeUrl, bodyContents, null,
17+
(OAuthRequestAsync.ResponseConverter<Response>) null).get();
18+
}
19+
20+
@Override
21+
public Response execute(String userAgent, Map<String, String> headers, Verb httpVerb, String completeUrl,
22+
String bodyContents) throws InterruptedException, ExecutionException, IOException {
23+
return executeAsync(userAgent, headers, httpVerb, completeUrl, bodyContents, null,
24+
(OAuthRequestAsync.ResponseConverter<Response>) null).get();
25+
}
26+
27+
@Override
28+
public Response execute(String userAgent, Map<String, String> headers, Verb httpVerb, String completeUrl,
29+
File bodyContents) throws InterruptedException, ExecutionException, IOException {
30+
return executeAsync(userAgent, headers, httpVerb, completeUrl, bodyContents, null,
31+
(OAuthRequestAsync.ResponseConverter<Response>) null).get();
32+
}
33+
}

scribejava-core/src/main/java/com/github/scribejava/core/httpclient/HttpClient.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22

33
import com.github.scribejava.core.model.OAuthAsyncRequestCallback;
44
import com.github.scribejava.core.model.OAuthRequestAsync;
5+
import com.github.scribejava.core.model.Response;
56
import com.github.scribejava.core.model.Verb;
67
import java.io.File;
78
import java.io.IOException;
89
import java.util.Map;
10+
import java.util.concurrent.ExecutionException;
911
import java.util.concurrent.Future;
1012

1113
public interface HttpClient {
@@ -23,4 +25,12 @@ <T> Future<T> executeAsync(String userAgent, Map<String, String> headers, Verb h
2325
<T> Future<T> executeAsync(String userAgent, Map<String, String> headers, Verb httpVerb, String completeUrl,
2426
File bodyContents, OAuthAsyncRequestCallback<T> callback, OAuthRequestAsync.ResponseConverter<T> converter);
2527

28+
Response execute(String userAgent, Map<String, String> headers, Verb httpVerb, String completeUrl,
29+
byte[] bodyContents) throws InterruptedException, ExecutionException, IOException;
30+
31+
Response execute(String userAgent, Map<String, String> headers, Verb httpVerb, String completeUrl,
32+
String bodyContents) throws InterruptedException, ExecutionException, IOException;
33+
34+
Response execute(String userAgent, Map<String, String> headers, Verb httpVerb, String completeUrl,
35+
File bodyContents) throws InterruptedException, ExecutionException, IOException;
2636
}

scribejava-core/src/main/java/com/github/scribejava/core/oauth/OAuthService.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import java.io.IOException;
1616
import java.util.ServiceLoader;
17+
import java.util.concurrent.ExecutionException;
1718
import java.util.concurrent.Future;
1819

1920
/**
@@ -66,8 +67,8 @@ public OAuthConfig getConfig() {
6667

6768
public abstract void signRequest(T token, AbstractRequest request);
6869

69-
public <T> Future<T> execute(OAuthRequestAsync request, OAuthAsyncRequestCallback<T> callback,
70-
OAuthRequestAsync.ResponseConverter<T> converter) {
70+
public <R> Future<R> execute(OAuthRequestAsync request, OAuthAsyncRequestCallback<R> callback,
71+
OAuthRequestAsync.ResponseConverter<R> converter) {
7172

7273
final File filePayload = request.getFilePayload();
7374
if (filePayload != null) {
@@ -86,6 +87,20 @@ public Future<Response> execute(OAuthRequestAsync request, OAuthAsyncRequestCall
8687
return execute(request, callback, null);
8788
}
8889

90+
public Response execute(OAuthRequestAsync request) throws InterruptedException, ExecutionException, IOException {
91+
final File filePayload = request.getFilePayload();
92+
if (filePayload != null) {
93+
return httpClient.execute(config.getUserAgent(), request.getHeaders(), request.getVerb(),
94+
request.getCompleteUrl(), filePayload);
95+
} else if (request.getStringPayload() != null) {
96+
return httpClient.execute(config.getUserAgent(), request.getHeaders(), request.getVerb(),
97+
request.getCompleteUrl(), request.getStringPayload());
98+
} else {
99+
return httpClient.execute(config.getUserAgent(), request.getHeaders(), request.getVerb(),
100+
request.getCompleteUrl(), request.getByteArrayPayload());
101+
}
102+
}
103+
89104
/**
90105
* the same as {@link OAuthRequest#send()}
91106
*

scribejava-httpclient-ahc/src/main/java/com/github/scribejava/httpclient/ahc/AhcHttpClient.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.github.scribejava.httpclient.ahc;
22

3+
import com.github.scribejava.core.httpclient.AbstractAsyncOnlyHttpClient;
34
import com.github.scribejava.core.model.AbstractRequest;
4-
import com.github.scribejava.core.httpclient.HttpClient;
55
import com.github.scribejava.core.model.OAuthAsyncRequestCallback;
66
import com.github.scribejava.core.model.OAuthConstants;
77
import com.github.scribejava.core.model.OAuthRequestAsync;
@@ -18,7 +18,7 @@
1818
import org.asynchttpclient.AsyncHttpClientConfig;
1919
import org.asynchttpclient.BoundRequestBuilder;
2020

21-
public class AhcHttpClient implements HttpClient {
21+
public class AhcHttpClient extends AbstractAsyncOnlyHttpClient {
2222

2323
private final AsyncHttpClient client;
2424

scribejava-httpclient-ning/src/main/java/com/github/scribejava/httpclient/ning/NingHttpClient.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.github.scribejava.httpclient.ning;
22

3+
import com.github.scribejava.core.httpclient.AbstractAsyncOnlyHttpClient;
34
import com.github.scribejava.core.model.AbstractRequest;
4-
import com.github.scribejava.core.httpclient.HttpClient;
55
import com.github.scribejava.core.model.OAuthAsyncRequestCallback;
66
import com.github.scribejava.core.model.OAuthConstants;
77
import com.github.scribejava.core.model.OAuthRequestAsync;
@@ -15,7 +15,7 @@
1515
import com.ning.http.client.AsyncHttpClientConfig;
1616
import java.io.File;
1717

18-
public class NingHttpClient implements HttpClient {
18+
public class NingHttpClient extends AbstractAsyncOnlyHttpClient {
1919

2020
private final AsyncHttpClient client;
2121

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

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,8 @@
55
import com.github.scribejava.core.model.Response;
66
import okhttp3.Call;
77
import okhttp3.Callback;
8-
import okhttp3.Headers;
98

109
import java.io.IOException;
11-
import java.util.HashMap;
12-
import java.util.Map;
1310

1411
class OAuthAsyncCompletionHandler<T> implements Callback {
1512

@@ -38,15 +35,8 @@ public void onFailure(Call call, IOException e) {
3835
@Override
3936
public void onResponse(Call call, okhttp3.Response okHttpResponse) throws IOException {
4037
try {
41-
final Headers headers = okHttpResponse.headers();
42-
final Map<String, String> headersMap = new HashMap<>();
4338

44-
for (String name : headers.names()) {
45-
headersMap.put(name, headers.get(name));
46-
}
47-
48-
final Response response = new Response(okHttpResponse.code(), okHttpResponse.message(), headersMap,
49-
okHttpResponse.body().byteStream());
39+
final Response response = OkHttpHttpClient.convertResponse(okHttpResponse);
5040

5141
@SuppressWarnings("unchecked")
5242
final T t = converter == null ? (T) response : converter.convert(response);

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

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,12 @@
1818
import java.util.concurrent.Future;
1919

2020
import static com.github.scribejava.core.model.AbstractRequest.DEFAULT_CONTENT_TYPE;
21+
import com.github.scribejava.core.model.Response;
2122
import java.io.File;
23+
import java.util.HashMap;
24+
import java.util.concurrent.ExecutionException;
2225
import okhttp3.Cache;
26+
import okhttp3.Headers;
2327

2428
public class OkHttpHttpClient implements HttpClient {
2529

@@ -73,6 +77,38 @@ public <T> Future<T> executeAsync(String userAgent, Map<String, String> headers,
7377
private <T> Future<T> doExecuteAsync(String userAgent, Map<String, String> headers, Verb httpVerb,
7478
String completeUrl, BodyType bodyType, Object bodyContents, OAuthAsyncRequestCallback<T> callback,
7579
OAuthRequestAsync.ResponseConverter<T> converter) {
80+
final Call call = createCall(userAgent, headers, httpVerb, completeUrl, bodyType, bodyContents);
81+
final OkHttpFuture<T> okHttpFuture = new OkHttpFuture<>(call);
82+
call.enqueue(new OAuthAsyncCompletionHandler<>(callback, converter, okHttpFuture));
83+
return okHttpFuture;
84+
}
85+
86+
@Override
87+
public Response execute(String userAgent, Map<String, String> headers, Verb httpVerb, String completeUrl,
88+
byte[] bodyContents) throws InterruptedException, ExecutionException, IOException {
89+
return doExecute(userAgent, headers, httpVerb, completeUrl, BodyType.BYTE_ARRAY, bodyContents);
90+
}
91+
92+
@Override
93+
public Response execute(String userAgent, Map<String, String> headers, Verb httpVerb, String completeUrl,
94+
String bodyContents) throws InterruptedException, ExecutionException, IOException {
95+
return doExecute(userAgent, headers, httpVerb, completeUrl, BodyType.STRING, bodyContents);
96+
}
97+
98+
@Override
99+
public Response execute(String userAgent, Map<String, String> headers, Verb httpVerb, String completeUrl,
100+
File bodyContents) throws InterruptedException, ExecutionException, IOException {
101+
return doExecute(userAgent, headers, httpVerb, completeUrl, BodyType.FILE, bodyContents);
102+
}
103+
104+
private Response doExecute(String userAgent, Map<String, String> headers, Verb httpVerb, String completeUrl,
105+
BodyType bodyType, Object bodyContents) throws IOException {
106+
final Call call = createCall(userAgent, headers, httpVerb, completeUrl, bodyType, bodyContents);
107+
return convertResponse(call.execute());
108+
}
109+
110+
private Call createCall(String userAgent, Map<String, String> headers, Verb httpVerb, String completeUrl,
111+
BodyType bodyType, Object bodyContents) {
76112
final Request.Builder requestBuilder = new Request.Builder();
77113
requestBuilder.url(completeUrl);
78114

@@ -101,10 +137,7 @@ private <T> Future<T> doExecuteAsync(String userAgent, Map<String, String> heade
101137
}
102138

103139
// create a new call
104-
final Call call = client.newCall(requestBuilder.build());
105-
final OkHttpFuture<T> okHttpFuture = new OkHttpFuture<>(call);
106-
call.enqueue(new OAuthAsyncCompletionHandler<>(callback, converter, okHttpFuture));
107-
return okHttpFuture;
140+
return client.newCall(requestBuilder.build());
108141
}
109142

110143
private enum BodyType {
@@ -129,4 +162,17 @@ RequestBody createBody(MediaType mediaType, Object bodyContents) {
129162

130163
abstract RequestBody createBody(MediaType mediaType, Object bodyContents);
131164
}
165+
166+
static Response convertResponse(okhttp3.Response okHttpResponse) {
167+
final Headers headers = okHttpResponse.headers();
168+
final Map<String, String> headersMap = new HashMap<>();
169+
170+
for (String name : headers.names()) {
171+
headersMap.put(name, headers.get(name));
172+
}
173+
174+
return new Response(okHttpResponse.code(), okHttpResponse.message(), headersMap,
175+
okHttpResponse.body().byteStream());
176+
177+
}
132178
}

0 commit comments

Comments
 (0)