Skip to content

Commit 65761bb

Browse files
committed
add default HTTP client configs (to use like 'new ServiceBuilder().httpClientConfig(OkHttpHttpClientConfig.defaultConfig())')
1 parent 5e560cc commit 65761bb

File tree

20 files changed

+165
-39
lines changed

20 files changed

+165
-39
lines changed

changelog

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* add support for byte[] and File (async only) payload in OAuth Requests (thanks to https://github.com/keijohyttinen)
55
* add support for HTTP verbs (thanks to https://github.com/keijohyttinen)
66
* add OkHttp http client support (thanks to https://github.com/arcao)
7+
* add default HTTP client configs (to use like 'new ServiceBuilder().httpClientConfig(OkHttpHttpClientConfig.defaultConfig())')
78

89
[3.3.0]
910
* update Facebook v2.6 -> v2.8

scribejava-apis/src/test/java/com/github/scribejava/apis/examples/GitHubAsyncOkHttpExample.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import com.github.scribejava.core.model.Verb;
99
import com.github.scribejava.core.oauth.OAuth20Service;
1010
import com.github.scribejava.httpclient.okhttp.OkHttpHttpClientConfig;
11-
import okhttp3.OkHttpClient;
1211

1312
import java.io.IOException;
1413
import java.util.Random;
@@ -28,13 +27,12 @@ public static void main(String... args) throws IOException, ExecutionException,
2827
final String clientId = "your client id";
2928
final String clientSecret = "your client secret";
3029
final String secretState = "secret" + new Random().nextInt(999_999);
31-
final OkHttpClient.Builder okHttpBuilder = new OkHttpClient.Builder();
3230
final OAuth20Service service = new ServiceBuilder()
3331
.apiKey(clientId)
3432
.apiSecret(clientSecret)
3533
.state(secretState)
3634
.callback("http://www.example.com/oauth_callback/")
37-
.httpClientConfig(new OkHttpHttpClientConfig(okHttpBuilder))
35+
.httpClientConfig(OkHttpHttpClientConfig.defaultConfig())
3836
.build(GitHubApi.instance());
3937
final Scanner in = new Scanner(System.in, "UTF-8");
4038

scribejava-apis/src/test/java/com/github/scribejava/apis/examples/Google20AsyncAHCExample.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import com.github.scribejava.httpclient.ahc.AhcHttpClientConfig;
77
import com.github.scribejava.core.builder.ServiceBuilder;
88
import com.github.scribejava.core.model.ForceTypeOfHttpRequest;
9-
import com.github.scribejava.core.model.HttpClient;
9+
import com.github.scribejava.core.httpclient.HttpClientConfig;
1010
import com.github.scribejava.core.model.OAuth2AccessToken;
1111
import com.github.scribejava.core.model.OAuthRequestAsync;
1212
import com.github.scribejava.core.model.Response;
@@ -33,7 +33,7 @@ public static void main(String... args) throws InterruptedException, ExecutionEx
3333
final String clientSecret = "your client secret";
3434
final String secretState = "secret" + new Random().nextInt(999_999);
3535
ScribeJavaConfig.setForceTypeOfHttpRequests(ForceTypeOfHttpRequest.FORCE_ASYNC_ONLY_HTTP_REQUESTS);
36-
final HttpClient.Config clientConfig = new AhcHttpClientConfig(new DefaultAsyncHttpClientConfig.Builder()
36+
final HttpClientConfig clientConfig = new AhcHttpClientConfig(new DefaultAsyncHttpClientConfig.Builder()
3737
.setMaxConnections(5)
3838
.setRequestTimeout(10_000)
3939
.setPooledConnectionIdleTimeout(1_000)

scribejava-core/src/main/java/com/github/scribejava/core/builder/ServiceBuilder.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
package com.github.scribejava.core.builder;
22

33
import com.github.scribejava.core.builder.api.BaseApi;
4-
import com.github.scribejava.core.model.HttpClient;
4+
import com.github.scribejava.core.httpclient.HttpClient;
5+
import com.github.scribejava.core.httpclient.HttpClientConfig;
56
import com.github.scribejava.core.model.OAuthConfig;
67
import com.github.scribejava.core.model.OAuthConstants;
78
import com.github.scribejava.core.model.SignatureType;
@@ -30,7 +31,7 @@ public class ServiceBuilder {
3031
private Integer readTimeout;
3132

3233
//not-default httpclient only
33-
private HttpClient.Config httpClientConfig;
34+
private HttpClientConfig httpClientConfig;
3435
private HttpClient httpClient;
3536

3637
public ServiceBuilder() {
@@ -134,7 +135,19 @@ public ServiceBuilder readTimeout(Integer readTimeout) {
134135
return this;
135136
}
136137

137-
public ServiceBuilder httpClientConfig(HttpClient.Config httpClientConfig) {
138+
/**
139+
* throws UnsupportedOperationException
140+
*
141+
* @param httpClientConfig httpClientConfig
142+
* @return never
143+
* @deprecated use {@link #httpClientConfig(com.github.scribejava.core.httpclient.HttpClientConfig)}
144+
*/
145+
@Deprecated
146+
public ServiceBuilder httpClientConfig(com.github.scribejava.core.model.HttpClient.Config httpClientConfig) {
147+
throw new UnsupportedOperationException("deprecated, use another method, see javadocs");
148+
}
149+
150+
public ServiceBuilder httpClientConfig(HttpClientConfig httpClientConfig) {
138151
Preconditions.checkNotNull(httpClientConfig, "httpClientConfig can't be null");
139152
this.httpClientConfig = httpClientConfig;
140153
return this;
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.github.scribejava.core.httpclient;
2+
3+
import com.github.scribejava.core.model.OAuthAsyncRequestCallback;
4+
import com.github.scribejava.core.model.OAuthRequestAsync;
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.Future;
10+
11+
public interface HttpClient {
12+
13+
void close() throws IOException;
14+
15+
<T> Future<T> executeAsync(String userAgent, Map<String, String> headers, Verb httpVerb, String completeUrl,
16+
byte[] bodyContents, OAuthAsyncRequestCallback<T> callback,
17+
OAuthRequestAsync.ResponseConverter<T> converter);
18+
19+
<T> Future<T> executeAsync(String userAgent, Map<String, String> headers, Verb httpVerb, String completeUrl,
20+
String bodyContents, OAuthAsyncRequestCallback<T> callback,
21+
OAuthRequestAsync.ResponseConverter<T> converter);
22+
23+
<T> Future<T> executeAsync(String userAgent, Map<String, String> headers, Verb httpVerb, String completeUrl,
24+
File bodyContents, OAuthAsyncRequestCallback<T> callback, OAuthRequestAsync.ResponseConverter<T> converter);
25+
26+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.github.scribejava.core.httpclient;
2+
3+
public interface HttpClientConfig {
4+
5+
HttpClientConfig createDefaultConfig();
6+
}
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package com.github.scribejava.core.httpclient;
22

3-
import com.github.scribejava.core.model.HttpClient;
4-
53
public interface HttpClientProvider {
64

7-
HttpClient createClient(HttpClient.Config httpClientConfig);
5+
HttpClient createClient(HttpClientConfig httpClientConfig);
86
}

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
import java.util.Map;
66
import java.util.concurrent.Future;
77

8+
/**
9+
*
10+
* @deprecated use {@link com.github.scribejava.core.httpclient.HttpClient}
11+
*/
12+
@Deprecated
813
public interface HttpClient {
914

1015
void close() throws IOException;
@@ -20,6 +25,12 @@ <T> Future<T> executeAsync(String userAgent, Map<String, String> headers, Verb h
2025
<T> Future<T> executeAsync(String userAgent, Map<String, String> headers, Verb httpVerb, String completeUrl,
2126
File bodyContents, OAuthAsyncRequestCallback<T> callback, OAuthRequestAsync.ResponseConverter<T> converter);
2227

28+
/**
29+
*
30+
* @deprecated use {@link com.github.scribejava.core.httpclient.HttpClientConfig}
31+
*/
32+
@Deprecated
2333
interface Config {
34+
2435
}
2536
}

scribejava-core/src/main/java/com/github/scribejava/core/model/OAuthConfig.java

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.github.scribejava.core.model;
22

3+
import com.github.scribejava.core.httpclient.HttpClientConfig;
34
import java.io.IOException;
45
import java.io.OutputStream;
56

@@ -23,16 +24,46 @@ public class OAuthConfig {
2324
private final Integer readTimeout;
2425

2526
//async version only
26-
private HttpClient.Config httpClientConfig;
27-
private HttpClient httpClient;
27+
private HttpClientConfig httpClientConfig;
28+
private com.github.scribejava.core.httpclient.HttpClient httpClient;
2829

2930
public OAuthConfig(String key, String secret) {
30-
this(key, secret, null, null, null, null, null, null, null, null, null, null, null);
31+
this(key, secret, null, null, null, null, null, null, null, null, null, (HttpClientConfig) null, null);
3132
}
3233

34+
/**
35+
* throws UnsupportedOperationException
36+
*
37+
* @param apiKey apiKey
38+
* @param apiSecret apiSecret
39+
* @param callback callback
40+
* @param signatureType signatureType
41+
* @param scope scope
42+
* @param debugStream debugStream
43+
* @param state state
44+
* @param responseType responseType
45+
* @param userAgent userAgent
46+
* @param connectTimeout connectTimeout
47+
* @param readTimeout readTimeout
48+
* @param httpClientConfig httpClientConfig
49+
* @param httpClient httpClient
50+
* @deprecated use
51+
* {@link #OAuthConfig(java.lang.String, java.lang.String, java.lang.String,
52+
* com.github.scribejava.core.model.SignatureType, java.lang.String, java.io.OutputStream, java.lang.String,
53+
* java.lang.String, java.lang.String, java.lang.Integer, java.lang.Integer,
54+
* com.github.scribejava.core.httpclient.HttpClientConfig, com.github.scribejava.core.httpclient.HttpClient)}
55+
*/
56+
@Deprecated
3357
public OAuthConfig(String apiKey, String apiSecret, String callback, SignatureType signatureType, String scope,
3458
OutputStream debugStream, String state, String responseType, String userAgent, Integer connectTimeout,
3559
Integer readTimeout, HttpClient.Config httpClientConfig, HttpClient httpClient) {
60+
throw new UnsupportedOperationException("deprecated, use another method, see javadocs");
61+
}
62+
63+
public OAuthConfig(String apiKey, String apiSecret, String callback, SignatureType signatureType, String scope,
64+
OutputStream debugStream, String state, String responseType, String userAgent, Integer connectTimeout,
65+
Integer readTimeout, HttpClientConfig httpClientConfig,
66+
com.github.scribejava.core.httpclient.HttpClient httpClient) {
3667
this.apiKey = apiKey;
3768
this.apiSecret = apiSecret;
3869
this.callback = callback;
@@ -99,11 +130,11 @@ public Integer getReadTimeout() {
99130
return readTimeout;
100131
}
101132

102-
public HttpClient.Config getHttpClientConfig() {
133+
public HttpClientConfig getHttpClientConfig() {
103134
return httpClientConfig;
104135
}
105136

106-
public HttpClient getHttpClient() {
137+
public com.github.scribejava.core.httpclient.HttpClient getHttpClient() {
107138
return httpClient;
108139
}
109140
}

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
import com.github.scribejava.core.httpclient.HttpClientProvider;
55
import com.github.scribejava.core.model.AbstractRequest;
66
import com.github.scribejava.core.model.ForceTypeOfHttpRequest;
7-
import com.github.scribejava.core.model.HttpClient;
7+
import com.github.scribejava.core.httpclient.HttpClient;
8+
import com.github.scribejava.core.httpclient.HttpClientConfig;
89
import com.github.scribejava.core.model.OAuthAsyncRequestCallback;
910
import com.github.scribejava.core.model.OAuthConfig;
1011
import com.github.scribejava.core.model.OAuthRequest;
@@ -34,7 +35,7 @@ public abstract class OAuthService<T extends Token> {
3435
public OAuthService(OAuthConfig config) {
3536
this.config = config;
3637
final ForceTypeOfHttpRequest forceTypeOfHttpRequest = ScribeJavaConfig.getForceTypeOfHttpRequests();
37-
final HttpClient.Config httpClientConfig = config.getHttpClientConfig();
38+
final HttpClientConfig httpClientConfig = config.getHttpClientConfig();
3839
final HttpClient externalHttpClient = config.getHttpClient();
3940

4041
if (httpClientConfig == null && externalHttpClient == null) {
@@ -57,7 +58,7 @@ public OAuthService(OAuthConfig config) {
5758
}
5859
}
5960

60-
private static HttpClient getClient(HttpClient.Config config) {
61+
private static HttpClient getClient(HttpClientConfig config) {
6162
for (HttpClientProvider provider : ServiceLoader.load(HttpClientProvider.class)) {
6263
final HttpClient client = provider.createClient(config);
6364
if (client != null) {

0 commit comments

Comments
 (0)