Skip to content

Commit

Permalink
1.2.2
Browse files Browse the repository at this point in the history
  • Loading branch information
颜洪毅 committed Jun 18, 2020
2 parents a8c182b + a8b4f0d commit 9e4a4a1
Show file tree
Hide file tree
Showing 10 changed files with 98 additions and 56 deletions.
12 changes: 6 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.yhyzgn.http</groupId>
<artifactId>pigeon</artifactId>
<version>1.1.5</version>
<version>1.2.2</version>
<name>Pigeon</name>
<description>Java http proxy.</description>
<url>/~https://github.com/yhyzgn/Pigeon</url>
Expand Down Expand Up @@ -67,28 +67,28 @@
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
<version>2.8.6</version>
</dependency>

<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>28.1-jre</version>
<version>29.0-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>
<version>2.0.0-alpha1</version>
<optional>true</optional>
</dependency>

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

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/yhy/http/pigeon/Pigeon.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
* e-mail : yhyzgn@gmail.com
* time : 2019-09-02 12:34
* version: 1.0.0
* desc : 入口
* desc : Pigeon
*/
@SuppressWarnings("unchecked")
public class Pigeon {
Expand Down
62 changes: 32 additions & 30 deletions src/main/java/com/yhy/http/pigeon/common/OkCall.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* e-mail : yhyzgn@gmail.com
* time : 2019-09-03 10:03
* version: 1.0.0
* desc :
* desc : 全世界最老实的人总是踏实肯干
*/
public class OkCall<T> implements Call<T> {
private final RequestFactory requestFactory;
Expand Down Expand Up @@ -69,10 +69,10 @@ public Response<T> execute() throws IOException {
}
}
}
if (canceled) {
if (canceled && null != call) {
call.cancel();
}
return parseResponse(call.execute());
return null != call ? parseResponse(call.execute()) : null;
}

@Override
Expand All @@ -92,7 +92,7 @@ public synchronized Request request() {
}
try {
call = rawCall = createRawCall();
return call.request();
return null != call ? call.request() : null;
} catch (RuntimeException | Error e) {
failureHandler = e;
throw e;
Expand Down Expand Up @@ -123,40 +123,42 @@ public void enqueue(Callback<T> callback) {
callback.onFailure(this, failure);
return;
}
if (canceled) {
if (canceled && null != call) {
call.cancel();
}

call.enqueue(new okhttp3.Callback() {
@Override
public void onFailure(@NotNull okhttp3.Call call, @NotNull IOException e) {
callFailure(e);
}

@Override
public void onResponse(@NotNull okhttp3.Call call, @NotNull okhttp3.Response rawResponse) throws IOException {
Response<T> response;
try {
response = parseResponse(rawResponse);
} catch (Throwable e) {
if (null != call) {
call.enqueue(new okhttp3.Callback() {
@Override
public void onFailure(@NotNull okhttp3.Call call, @NotNull IOException e) {
callFailure(e);
return;
}
try {
callback.onResponse(OkCall.this, response);
} catch (Throwable t) {
t.printStackTrace();

@Override
public void onResponse(@NotNull okhttp3.Call call, @NotNull okhttp3.Response rawResponse) throws IOException {
Response<T> response;
try {
response = parseResponse(rawResponse);
} catch (Throwable e) {
callFailure(e);
return;
}
try {
callback.onResponse(OkCall.this, response);
} catch (Throwable t) {
t.printStackTrace();
}
}
}

private void callFailure(Throwable e) {
try {
callback.onFailure(OkCall.this, e);
} catch (Throwable t) {
t.printStackTrace();
private void callFailure(Throwable e) {
try {
callback.onFailure(OkCall.this, e);
} catch (Throwable t) {
t.printStackTrace();
}
}
}
});
});
}
}

@Override
Expand Down
19 changes: 9 additions & 10 deletions src/main/java/com/yhy/http/pigeon/http/HttpHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import com.yhy.http.pigeon.common.OkCall;
import com.yhy.http.pigeon.converter.Converter;
import com.yhy.http.pigeon.http.request.RequestFactory;
import okhttp3.OkHttpClient;
import okhttp3.ResponseBody;

import java.lang.annotation.Annotation;
Expand All @@ -18,24 +17,24 @@
* e-mail : yhyzgn@gmail.com
* time : 2019-09-02 17:31
* version: 1.0.0
* desc :
* desc : http请求处理器
*/
@SuppressWarnings("unchecked")
public abstract class HttpHandler<Res, Ret> extends HttpMethod<Ret> {

private final RequestFactory requestFactory;
private final OkHttpClient.Builder client;
private final Pigeon pigeon;
private final Converter<ResponseBody, Res> responseConverter;

private HttpHandler(RequestFactory requestFactory, OkHttpClient.Builder client, Converter<ResponseBody, Res> responseConverter) {
private HttpHandler(RequestFactory requestFactory, Pigeon pigeon, Converter<ResponseBody, Res> responseConverter) {
this.requestFactory = requestFactory;
this.client = client;
this.pigeon = pigeon;
this.responseConverter = responseConverter;
}

@Override
public Ret invoke(Object[] args) throws Exception {
OkCall<Res> call = new OkCall<>(requestFactory, client, responseConverter, args);
// 每次请求都用最新(干净)的 pigeon.client()
OkCall<Res> call = new OkCall<>(requestFactory, pigeon.client(), responseConverter, args);
return adapt(call, args);
}

Expand All @@ -48,7 +47,7 @@ public static <Res, Ret> HttpHandler<Res, Ret> parseAnnotations(Pigeon pigeon, M
Type responseType = callAdapter.responseType();
Converter<ResponseBody, Res> responseConverter = createResponseConverter(pigeon, annotations, responseType);

return new AdaptedCall<>(factory, pigeon.client(), responseConverter, callAdapter);
return new AdaptedCall<>(factory, pigeon, responseConverter, callAdapter);
}

private static <Res> Converter<ResponseBody, Res> createResponseConverter(Pigeon pigeon, Annotation[] annotations, Type responseType) {
Expand All @@ -63,8 +62,8 @@ public static class AdaptedCall<Res, Ret> extends HttpHandler<Res, Ret> {

private final CallAdapter<Res, Ret> callAdapter;

AdaptedCall(RequestFactory requestFactory, OkHttpClient.Builder client, Converter<ResponseBody, Res> responseConverter, CallAdapter<Res, Ret> callAdapter) {
super(requestFactory, client, responseConverter);
AdaptedCall(RequestFactory requestFactory, Pigeon pigeon, Converter<ResponseBody, Res> responseConverter, CallAdapter<Res, Ret> callAdapter) {
super(requestFactory, pigeon, responseConverter);
this.callAdapter = callAdapter;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,10 @@ public Request create(OkHttpClient.Builder client, Object[] args) throws IOExcep
interceptors.forEach(client::addInterceptor);
}

return builder.get().tag(Invocation.class, Invocation.of(method, argsList)).build();
Request.Builder bld = builder.get().tag(Invocation.class, Invocation.of(method, argsList));
// 加上 User-Agent 信息
bld.header("User-Agent", "Pigeon/" + Utils.VERSION);
return bld.build();
}

public static RequestFactory parseAnnotations(Pigeon pigeon, Method method) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
* e-mail : yhyzgn@gmail.com
* time : 2019-09-04 18:14
* version: 1.0.0
* desc :
* desc : 默认的日志打印器
*/
public class HttpLoggerInterceptor implements Interceptor {
private final static Logger LOGGER = LoggerFactory.getLogger(HttpLoggerInterceptor.class);
Expand Down Expand Up @@ -109,7 +109,7 @@ private String requestBodyToString(RequestBody body) throws IOException {
}

private static class LogLines {
private static List<LogLine> lines = new ArrayList<>();
private static final List<LogLine> lines = new ArrayList<>();

static LogLines start(String msg, Object... args) {
lines.add(new LogLine(msg, args));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.yhy.http.pigeon.offer;

import com.yhy.http.pigeon.Pigeon;
import com.yhy.http.pigeon.converter.Converter;
import okhttp3.ResponseBody;
import org.jetbrains.annotations.Nullable;

import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;

/**
* author : 颜洪毅
* e-mail : yhyzgn@gmail.com
* time : 2020-06-05 1:15 下午
* version: 1.0.0
* desc : 内置字符串响应转换器
*/
public class StringResponseConverter extends GsonConverter {

@Nullable
@Override
public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, Pigeon pigeon) {
return new StringResponseBodyConverter();
}

private static class StringResponseBodyConverter implements Converter<ResponseBody, String> {
@Nullable
@Override
public String convert(ResponseBody from) throws IOException {
return from.string();
}
}
}
7 changes: 4 additions & 3 deletions src/main/java/com/yhy/http/pigeon/utils/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
* desc :
*/
public class Utils {
static final Type[] EMPTY_TYPE_ARRAY = new Type[0];
private static final Type[] EMPTY_TYPE_ARRAY = new Type[0];
public final static String VERSION = "1.2.2";

public static boolean isEmpty(Object object) {
if (null == object) return true;
Expand Down Expand Up @@ -77,10 +78,10 @@ public static boolean hasUnresolvableType(@Nullable Type type) {
return hasUnresolvableType(((GenericArrayType) type).getGenericComponentType());
}
if (type instanceof TypeVariable) {
return true;
return false;
}
if (type instanceof WildcardType) {
return true;
return false;
}
String className = type == null ? "null" : type.getClass().getName();
throw new IllegalArgumentException("Expected a Class, ParameterizedType, or GenericArrayType, but <" + type + "> is of type " + className);
Expand Down
4 changes: 3 additions & 1 deletion src/test/java/pigeon/get/Api.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.yhy.http.pigeon.annotation.method.GET;
import com.yhy.http.pigeon.annotation.param.Path;
import com.yhy.http.pigeon.annotation.param.Query;
import com.yhy.http.pigeon.common.Response;
import pigeon.interceptor.TestInterceptor;

import java.util.Map;
Expand Down Expand Up @@ -41,5 +42,6 @@ public interface Api {
Cat mp(@Header Map<String, Object> header, @Query Map<String, Object> params);

@GET
Cat def(@Header Map<String, Object> header, @Query Map<String, Object> params);
@Interceptor(value = TestInterceptor.class, net = true)
String def(@Header Map<String, ?> header, @Query Map<String, Object> params);
}
5 changes: 3 additions & 2 deletions src/test/java/pigeon/get/ApiTester.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package pigeon.get;

import com.yhy.http.pigeon.Pigeon;
import com.yhy.http.pigeon.offer.StringResponseConverter;

import java.io.IOException;
import java.util.HashMap;
Expand All @@ -16,7 +17,7 @@
public class ApiTester {

public static void main(String[] args) throws IOException {
Pigeon pigeon = new Pigeon.Builder().host("http://localhost:8080/api/get/cat").build();
Pigeon pigeon = new Pigeon.Builder().host("http://localhost:8080/api/get/cat").addConverterFactory(new StringResponseConverter()).build();
Api api = pigeon.create(Api.class);

// String test = api.test();
Expand Down Expand Up @@ -44,7 +45,7 @@ public static void main(String[] args) throws IOException {
params.put("age", "8");
params.put("remark", "附加信息");
// Cat cat = api.mp(header, params);
Cat cat = api.def(header, params);
String cat = api.def(header, params);
System.out.println(cat);
cat = api.def(header, params);
System.out.println(cat);
Expand Down

0 comments on commit 9e4a4a1

Please sign in to comment.