Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
yhyzgn committed Sep 4, 2019
1 parent 230cb5e commit 176e94c
Show file tree
Hide file tree
Showing 20 changed files with 534 additions and 212 deletions.
6 changes: 6 additions & 0 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 20 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,21 @@
<artifactId>guava</artifactId>
<version>28.1-jre</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>2.0.0-alpha0</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-simple -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>2.0.0-alpha0</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand All @@ -86,8 +101,11 @@
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
<compilerArgument>-parameters</compilerArgument>
<parameters>true</parameters>
<testCompilerArgument>-parameters</testCompilerArgument>
</configuration>
</plugin>

Expand Down
151 changes: 107 additions & 44 deletions src/main/java/com/yhy/http/pigeon/Pigeon.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@

import com.yhy.http.pigeon.adapter.CallAdapter;
import com.yhy.http.pigeon.converter.Converter;
import com.yhy.http.pigeon.def.DefCallAdapter;
import com.yhy.http.pigeon.def.DefCallFactory;
import com.yhy.http.pigeon.def.DefConverter;
import com.yhy.http.pigeon.http.HttpMethod;
import okhttp3.HttpUrl;
import okhttp3.Interceptor;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import com.yhy.http.pigeon.offer.GsonConverter;
import com.yhy.http.pigeon.offer.GuavaCallAdapter;
import com.yhy.http.pigeon.offer.HttpLoggerInterceptor;
import okhttp3.*;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
Expand All @@ -31,29 +28,31 @@ 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 CallAdapter.Factory callAdapterFactory;
private Converter.Factory stringConverterFactory;
private Converter.Factory requestConverterFactory;
private Converter.Factory responseConverterFactory;
private okhttp3.Call.Factory callFactory;
private List<CallAdapter.Factory> callFactories;
private List<Converter.Factory> converterFactories;
private OkHttpClient.Builder client;

private Pigeon(Builder builder) {
this.host = builder.host;
this.netInterceptors = builder.netInterceptors;
this.interceptors = builder.interceptors;
this.headers = builder.headers;
this.callAdapterFactory = builder.callAdapterFactory;
this.stringConverterFactory = builder.stringConverterFactory;
this.requestConverterFactory = builder.requestConverterFactory;
this.responseConverterFactory = builder.responseConverterFactory;
this.callFactory = builder.callFactory;
this.callFactories = builder.adapterFactories;
this.converterFactories = builder.converterFactories;
this.client = builder.client;
}

public HttpUrl host() {
return host;
}

public List<Interceptor> netInterceptors() {
return netInterceptors;
}

public List<Interceptor> interceptors() {
return interceptors;
}
Expand All @@ -62,27 +61,25 @@ public Map<String, Object> headers() {
return headers;
}

public CallAdapter<?, ?> callAdapter(Type returnType, Annotation[] annotations) {
return callAdapterFactory.get(returnType, annotations, this);
public CallAdapter<?, ?> adapter(Type returnType, Annotation[] annotations) {
return findCallAdapter(returnType, annotations);
}

public <T> Converter<T, String> stringConverter(Type type, Annotation[] annotations) {
Converter<?, String> converter = stringConverterFactory.stringConverter(type, annotations, this);
return null != converter ? (Converter<T, String>) converter : null;
return findStringConverter(type, annotations);
}

public <T> Converter<T, RequestBody> requestConverter(Type type, Annotation[] methodAnnotations, Annotation[] parameterAnnotations) {
Converter<?, RequestBody> converter = requestConverterFactory.requestBodyConverter(type, methodAnnotations, parameterAnnotations, this);
return null != converter ? (Converter<T, RequestBody>) converter : null;
return findRequestConverter(type, methodAnnotations, parameterAnnotations);
}

public <T> Converter<ResponseBody, T> responseConverter(Type responseType, Annotation[] annotations) {
Converter<ResponseBody, ?> converter = responseConverterFactory.responseBodyConverter(responseType, annotations, this);
return null != converter ? (Converter<ResponseBody, T>) converter : null;
return findResponseConverter(responseType, annotations);
}

public okhttp3.Call.Factory callFactory() {
return callFactory;
public OkHttpClient.Builder client() {
// 返回干净的builder,‘client’中只包含全局拦截器,而不含自定义拦截器的builder
return new OkHttpClient.Builder(client.build());
}

public <T> T create(Class<T> api) {
Expand All @@ -96,6 +93,50 @@ public <T> T create(Class<T> api) {
});
}

private <T> Converter<T, RequestBody> findRequestConverter(Type type, Annotation[] annotations, Annotation[] parameterAnnotations) {
Converter<T, RequestBody> converter;
for (Converter.Factory factory : converterFactories) {
converter = (Converter<T, RequestBody>) factory.requestBodyConverter(type, annotations, parameterAnnotations, this);
if (null != converter) {
return converter;
}
}
throw new IllegalStateException("Can not found adapted RequestConverter.");
}

private <T> Converter<ResponseBody, T> findResponseConverter(Type type, Annotation[] annotations) {
Converter<ResponseBody, T> converter;
for (Converter.Factory factory : converterFactories) {
converter = (Converter<ResponseBody, T>) factory.responseBodyConverter(type, annotations, this);
if (null != converter) {
return converter;
}
}
throw new IllegalStateException("Can not found adapted ResponseConverter.");
}

private <T> Converter<T, String> findStringConverter(Type type, Annotation[] annotations) {
Converter<T, String> converter;
for (Converter.Factory factory : converterFactories) {
converter = (Converter<T, String>) factory.stringConverter(type, annotations, this);
if (null != converter) {
return converter;
}
}
throw new IllegalStateException("Can not found adapted StringConverter.");
}

private CallAdapter<?, ?> findCallAdapter(Type returnType, Annotation[] annotations) {
CallAdapter<?, ?> adapter;
for (CallAdapter.Factory factory : callFactories) {
adapter = factory.get(returnType, annotations, this);
if (null != adapter) {
return adapter;
}
}
throw new IllegalStateException("Can not found adapted CallAdapter.");
}

private void validateInterface(Class<?> api) {
if (!api.isInterface()) {
throw new IllegalArgumentException("[" + api.getCanonicalName() + "] must be interface.");
Expand Down Expand Up @@ -125,13 +166,13 @@ private HttpMethod<?> loadHttpMethod(Method method) {

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 CallAdapter.Factory callAdapterFactory = new DefCallAdapter();
private Converter.Factory stringConverterFactory = new DefConverter();
private Converter.Factory requestConverterFactory = new DefConverter();
private Converter.Factory responseConverterFactory = new DefConverter();
private okhttp3.Call.Factory callFactory = new DefCallFactory();
private List<CallAdapter.Factory> adapterFactories = new ArrayList<>();
private List<Converter.Factory> converterFactories = new ArrayList<>();
private OkHttpClient.Builder client;
private boolean logging = true;

public Builder host(String url) {
Objects.requireNonNull(url, "URL can not be null.");
Expand All @@ -144,37 +185,59 @@ public Builder interceptor(Interceptor interceptor) {
return this;
}

public Builder header(String name, Object value) {
this.headers.put(name, value);
public Builder netInterceptor(Interceptor interceptor) {
this.netInterceptors.add(interceptor);
return this;
}

public Builder callAdapterFactory(CallAdapter.Factory factory) {
this.callAdapterFactory = factory;
public Builder header(String name, Object value) {
this.headers.put(name, value);
return this;
}

public Builder stringConverterFactory(Converter.Factory factory) {
this.stringConverterFactory = factory;
public Builder addAdapterFactory(CallAdapter.Factory factory) {
this.adapterFactories.add(factory);
return this;
}

public Builder requestConverterFactory(Converter.Factory factory) {
this.requestConverterFactory = factory;
public Builder addConverterFactory(Converter.Factory factory) {
this.converterFactories.add(factory);
return this;
}

public Builder responseConverterFactory(Converter.Factory factory) {
this.responseConverterFactory = factory;
public Builder client(OkHttpClient.Builder client) {
this.client = client;
return this;
}

public Builder callFactory(okhttp3.Call.Factory factory) {
this.callFactory = factory;
public Builder logging(boolean logging) {
this.logging = logging;
return this;
}

public Pigeon build() {
if (null == host) {
throw new IllegalStateException("host can not be null.");
}

adapterFactories.add(new GuavaCallAdapter());
converterFactories.add(new GsonConverter());
if (logging) {
netInterceptors.add(new HttpLoggerInterceptor());
}

if (null == client) {
client = new OkHttpClient.Builder();
}

// 配置全局拦截器
if (!netInterceptors.isEmpty()) {
netInterceptors.forEach(client::addNetworkInterceptor);
}
if (!interceptors.isEmpty()) {
interceptors.forEach(client::addInterceptor);
}

return new Pigeon(this);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/yhy/http/pigeon/adapter/CallAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public interface CallAdapter<R, T> {

Type responseType();

T adapt(Call<R> call);
T adapt(Call<R> call, Object[] args) throws Exception;

abstract class Factory {

Expand Down
21 changes: 21 additions & 0 deletions src/main/java/com/yhy/http/pigeon/annotation/Interceptor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.yhy.http.pigeon.annotation;

import java.lang.annotation.*;

/**
* author : 颜洪毅
* e-mail : yhyzgn@gmail.com
* time : 2019-09-04 17:10
* version: 1.0.0
* desc :
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Repeatable(Interceptors.class)
public @interface Interceptor {

Class<? extends okhttp3.Interceptor> value();

boolean net() default false;
}
18 changes: 18 additions & 0 deletions src/main/java/com/yhy/http/pigeon/annotation/Interceptors.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.yhy.http.pigeon.annotation;

import java.lang.annotation.*;

/**
* author : 颜洪毅
* e-mail : yhyzgn@gmail.com
* time : 2019-09-04 17:11
* version: 1.0.0
* desc :
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Interceptors {

Interceptor[] value() default {};
}
13 changes: 7 additions & 6 deletions src/main/java/com/yhy/http/pigeon/common/OkCall.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.yhy.http.pigeon.http.request.RequestFactory;
import com.yhy.http.pigeon.utils.Utils;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.ResponseBody;
import okio.Buffer;
Expand All @@ -25,7 +26,7 @@
*/
public class OkCall<T> implements Call<T> {
private final RequestFactory requestFactory;
private final okhttp3.Call.Factory callFactory;
private final OkHttpClient.Builder client;
private final Converter<ResponseBody, T> responseConverter;
private final Object[] args;

Expand All @@ -36,9 +37,9 @@ public class OkCall<T> implements Call<T> {
private Throwable failureHandler;
private boolean executed;

public OkCall(RequestFactory requestFactory, okhttp3.Call.Factory callFactory, Converter<ResponseBody, T> responseConverter, Object[] args) {
public OkCall(RequestFactory requestFactory, OkHttpClient.Builder client, Converter<ResponseBody, T> responseConverter, Object[] args) {
this.requestFactory = requestFactory;
this.callFactory = callFactory;
this.client = client;
this.responseConverter = responseConverter;
this.args = args;
}
Expand Down Expand Up @@ -188,14 +189,14 @@ public boolean isCanceled() {
@Override
@SuppressWarnings("MethodDoesntCallSuperMethod")
public OkCall<T> clone() {
return new OkCall<>(requestFactory, callFactory, responseConverter, args);
return new OkCall<>(requestFactory, client, responseConverter, args);
}

private okhttp3.Call createRawCall() {
Request request = null;
try {
request = requestFactory.create(args);
return callFactory.newCall(request);
request = requestFactory.create(client, args);
return client.build().newCall(request);
} catch (IOException e) {
e.printStackTrace();
}
Expand Down
13 changes: 0 additions & 13 deletions src/main/java/com/yhy/http/pigeon/def/DefCallFactory.java

This file was deleted.

Loading

0 comments on commit 176e94c

Please sign in to comment.