1818import java .util .concurrent .Future ;
1919
2020import static com .github .scribejava .core .model .AbstractRequest .DEFAULT_CONTENT_TYPE ;
21+ import com .github .scribejava .core .model .Response ;
2122import java .io .File ;
23+ import java .util .HashMap ;
24+ import java .util .concurrent .ExecutionException ;
2225import okhttp3 .Cache ;
26+ import okhttp3 .Headers ;
2327
2428public 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