Skip to content

Commit

Permalink
兼容OkHttp3
Browse files Browse the repository at this point in the history
  • Loading branch information
颜洪毅 committed Jun 18, 2020
1 parent a0680d1 commit a8c182b
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 19 deletions.
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,13 @@
<version>2.0.0-alpha0</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
<version>19.0.0</version>
<scope>compile</scope>
</dependency>
</dependencies>

<build>
Expand Down
74 changes: 60 additions & 14 deletions src/main/java/com/yhy/http/pigeon/Pigeon.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,28 @@
* e-mail : yhyzgn@gmail.com
* time : 2019-09-02 12:34
* version: 1.0.0
* desc :
* desc : 入口
*/
@SuppressWarnings("unchecked")
public class Pigeon {
private final Map<Method, HttpMethod<?>> httpMethodMap = new ConcurrentHashMap<>();

private HttpUrl host;
private List<Interceptor> netInterceptors;
private List<Interceptor> interceptors;
private Map<String, Object> headers;
private List<CallAdapter.Factory> callFactories;
private List<Converter.Factory> converterFactories;
private OkHttpClient.Builder client;
private final HttpUrl host;
private final SSLSocketFactory sslSocketFactory;
private final X509TrustManager sslTrustManager;
private final HostnameVerifier sslHostnameVerifier;
private final List<Interceptor> netInterceptors;
private final List<Interceptor> interceptors;
private final Map<String, Object> headers;
private final List<CallAdapter.Factory> callFactories;
private final List<Converter.Factory> converterFactories;
private final OkHttpClient.Builder client;

private Pigeon(Builder builder) {
this.host = builder.host;
this.sslSocketFactory = builder.sslSocketFactory;
this.sslTrustManager = builder.sslTrustManager;
this.sslHostnameVerifier = builder.sslHostnameVerifier;
this.netInterceptors = builder.netInterceptors;
this.interceptors = builder.interceptors;
this.headers = builder.headers;
Expand Down Expand Up @@ -83,7 +89,8 @@ public <T> Converter<ResponseBody, T> responseConverter(Type responseType, Annot

public OkHttpClient.Builder client() {
// 返回干净的builder,‘client’中只包含全局拦截器,而不含自定义拦截器的builder
return new OkHttpClient.Builder(client.build());
// 兼容低版本的 OkHttpClient.Builder
return newBuilder();
}

public <T> T create(Class<T> api) {
Expand Down Expand Up @@ -172,13 +179,52 @@ private HttpMethod<?> loadHttpMethod(Method method) {
}
}

private OkHttpClient.Builder newBuilder() {
OkHttpClient ok = client.build();
OkHttpClient.Builder builder = new OkHttpClient.Builder()
.dispatcher(ok.dispatcher())
.connectionPool(ok.connectionPool())
.eventListenerFactory(ok.eventListenerFactory())
.retryOnConnectionFailure(ok.retryOnConnectionFailure())
.authenticator(ok.authenticator())
.followRedirects(ok.followRedirects())
.followSslRedirects(ok.followSslRedirects())
.cookieJar(ok.cookieJar())
.cache(ok.cache())
.dns(ok.dns())
.proxy(ok.proxy())
.proxySelector(ok.proxySelector())
.proxyAuthenticator(ok.proxyAuthenticator())
.socketFactory(ok.socketFactory())
.connectionSpecs(ok.connectionSpecs())
.protocols(ok.protocols())
.hostnameVerifier(ok.hostnameVerifier())
.certificatePinner(ok.certificatePinner())
.callTimeout(Duration.ofMillis(ok.callTimeoutMillis()))
.connectTimeout(Duration.ofMillis(ok.connectTimeoutMillis()))
.readTimeout(Duration.ofMillis(ok.readTimeoutMillis()))
.writeTimeout(Duration.ofMillis(ok.writeTimeoutMillis()))
.pingInterval(Duration.ofMillis(ok.pingIntervalMillis()))
.certificatePinner(ok.certificatePinner());

// 配置 ssl
if (null != sslSocketFactory && null != sslTrustManager && null != sslHostnameVerifier) {
builder.sslSocketFactory(sslSocketFactory, sslTrustManager).hostnameVerifier(sslHostnameVerifier);
}

ok.interceptors().forEach(builder::addInterceptor);
ok.networkInterceptors().forEach(builder::addNetworkInterceptor);

return builder;
}

public static class Builder {
private HttpUrl host;
private List<Interceptor> netInterceptors = new ArrayList<>();
private List<Interceptor> interceptors = new ArrayList<>();
private Map<String, Object> headers = new HashMap<>();
private List<CallAdapter.Factory> adapterFactories = new ArrayList<>();
private List<Converter.Factory> converterFactories = new ArrayList<>();
private final List<Interceptor> netInterceptors = new ArrayList<>();
private final List<Interceptor> interceptors = new ArrayList<>();
private final Map<String, Object> headers = new HashMap<>();
private final List<CallAdapter.Factory> adapterFactories = new ArrayList<>();
private final List<Converter.Factory> converterFactories = new ArrayList<>();
private OkHttpClient.Builder client;
private boolean logging = true;
private SSLSocketFactory sslSocketFactory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public Request.Builder get() {
body = multipartBuilder.build();
} else if (hasBody) {
// 如果强行有body,则设置个空body
body = RequestBody.create(new byte[0]);
body = RequestBody.create(MediaType.parse("application/json"), new byte[0]);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@
* e-mail : yhyzgn@gmail.com
* time : 2019-09-02 16:44
* version: 1.0.0
* desc :
* desc : 参数处理器
*/
@SuppressWarnings("unchecked")
public abstract class ParameterHandler<T> {

public abstract void apply(RequestBuilder builder, @Nullable T value) throws IOException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,16 +89,16 @@ public Response intercept(@NotNull Chain chain) throws IOException {
private void log(Object tag, LogLines lines) {
StringBuffer sb = new StringBuffer(System.lineSeparator());
sb
.append("┌────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────")
.append("┌────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────")
.append(System.lineSeparator())
.append("│ ").append(tag.toString())
.append(System.lineSeparator())
.append("├┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄")
.append("├────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────")
.append(System.lineSeparator());
lines.lines().forEach(item -> {
sb.append("│ ").append(String.format(item.msg.replace("{}", "%s"), item.args)).append(System.lineSeparator());
});
sb.append("└────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────");
sb.append("└────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────");
LOGGER.info(sb.toString());
}

Expand Down
4 changes: 4 additions & 0 deletions src/test/java/pigeon/get/ApiTester.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,9 @@ public static void main(String[] args) throws IOException {
// Cat cat = api.mp(header, params);
Cat cat = api.def(header, params);
System.out.println(cat);
cat = api.def(header, params);
System.out.println(cat);
cat = api.def(header, params);
System.out.println(cat);
}
}

0 comments on commit a8c182b

Please sign in to comment.