Skip to content

Commit

Permalink
MethodCache
Browse files Browse the repository at this point in the history
  • Loading branch information
yhyzgn committed Jul 1, 2021
1 parent e1cd013 commit 63606ee
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 26 deletions.
47 changes: 36 additions & 11 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,19 @@ version rootProject.ext.version
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8

dependencies {
testImplementation group: 'junit', name: 'junit', version: '4.12'

api 'com.squareup.okhttp3:okhttp:5.0.0-alpha.2'
api 'com.fasterxml.jackson.core:jackson-core:2.12.3'
api 'com.fasterxml.jackson.core:jackson-databind:2.12.3'
api 'com.google.code.gson:gson:2.8.6'
api 'com.google.guava:guava:29.0-jre'
compileOnly 'org.slf4j:slf4j-api:2.0.0-alpha1'
compileOnly 'org.jetbrains:annotations:19.0.0'
testImplementation 'org.slf4j:slf4j-simple:2.0.0-alpha1'
}

tasks.withType(JavaCompile) {
options.encoding = 'UTF-8'
options.warnings = false
Expand Down Expand Up @@ -50,17 +63,29 @@ jar {
}
}

dependencies {
testImplementation group: 'junit', name: 'junit', version: '4.12'

api 'com.squareup.okhttp3:okhttp:5.0.0-alpha.2'
api 'com.fasterxml.jackson.core:jackson-core:2.12.3'
api 'com.fasterxml.jackson.core:jackson-databind:2.12.3'
api 'com.google.code.gson:gson:2.8.6'
api 'com.google.guava:guava:29.0-jre'
compileOnly 'org.slf4j:slf4j-api:2.0.0-alpha1'
compileOnly 'org.jetbrains:annotations:19.0.0'
testImplementation 'org.slf4j:slf4j-simple:2.0.0-alpha1'
javadoc {
description = "生成jar格式的javadoc。"
// 只显示 protected 和 public 的类和成员
options.memberLevel = JavadocMemberLevel.PROTECTED
options.author = true
options.version = true
options.header = project.name
// 静默javadoc检查(比如不支持@date会报错等),jdk 8+
options.addStringOption('Xdoclint:none', '-quiet')
// 防止本地打开中文乱码
options.addStringOption("charset", "UTF-8")
// suppress warnings due to cross-module @see and @link references;
// note that global 'api' task does display all warnings.
logging.captureStandardError LogLevel.INFO
// suppress "## warnings" message
logging.captureStandardOutput LogLevel.INFO
// 编码一定要配置否则直接出错
options.encoding = "UTF-8"
options.charSet = "UTF-8"
// java9
if (JavaVersion.current().isJava9Compatible()) {
options.addBooleanOption('html5', true)
}
}

task replaceVersion() {
Expand Down
2 changes: 1 addition & 1 deletion ext.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
ext {
groupId = 'com.yhyzgn.http'
artifactId = 'pigeon'
version = '1.5.0'
version = '1.5.1'
description = 'Java http client with OkHttp3.'
url = 'github.com/yhyzgn/pigeon'
}
17 changes: 15 additions & 2 deletions src/main/java/com/yhy/http/pigeon/Pigeon.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public class Pigeon {
private final HeaderDelegate headerDelegate;
private final InterceptorDelegate interceptorDelegate;
private final OkHttpClient.Builder client;
private final boolean methodCacheEnabled;

private Pigeon(Builder builder) {
this.host = builder.host;
Expand All @@ -62,6 +63,7 @@ private Pigeon(Builder builder) {
this.headerDelegate = builder.headerDelegate;
this.interceptorDelegate = builder.interceptorDelegate;
this.client = builder.client;
this.methodCacheEnabled = builder.methodCacheEnabled;
}

public HttpUrl host() {
Expand Down Expand Up @@ -188,6 +190,11 @@ private void validateInterface(Class<?> api) {
}

private HttpMethod<?> loadHttpMethod(Method method) {
if (!methodCacheEnabled) {
return HttpMethod.parseAnnotations(this, method);
}

// 启用了代理缓存
HttpMethod<?> result = httpMethodMap.get(method);
if (null != result) return result;
synchronized (httpMethodMap) {
Expand Down Expand Up @@ -251,6 +258,7 @@ public static class Builder {
private InterceptorDelegate interceptorDelegate;
private OkHttpClient.Builder client;
private boolean logging = true;
private boolean methodCacheEnabled = true;
private SSLSocketFactory sslSocketFactory;
private X509TrustManager sslTrustManager;
private HostnameVerifier sslHostnameVerifier;
Expand Down Expand Up @@ -353,8 +361,13 @@ public Builder timeout(Duration timeout) {
return this;
}

public Builder logging(boolean logging) {
this.logging = logging;
public Builder logging(boolean enabled) {
this.logging = enabled;
return this;
}

public Builder methodCache(boolean enabled) {
this.methodCacheEnabled = enabled;
return this;
}

Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/yhy/http/pigeon/annotation/Header.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

/**
* Header support dynamic value
* Must disable 'methodCache' of 'Pigeon' by way {@link com.yhy.http.pigeon.Pigeon.Builder#methodCache(boolean)} while using 'Header.Dynamic'
*/
interface Dynamic {

Expand Down
37 changes: 26 additions & 11 deletions src/main/java/com/yhy/http/pigeon/http/request/RequestFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,22 @@ private void parseHeader(Header... annotation) {
Header.Dynamic headerDynamic = delegate.apply(pairClass);
headerName = headerDynamic.name();
headerValue = headerDynamic.value();

if (null != headerName && null != headerValue) {
headersBuilder.add(headerName, headerValue);
}

// 新式动态请求头
Map<String, String> dmh = headerDynamic.pairs(method);
if (null != dmh && !dmh.isEmpty()) {
dmh.forEach((k, v) -> {
if (null != k && null != v) {
headersBuilder.add(k, v);
}
});
}

continue;
} catch (Exception e) {
throw new IllegalArgumentException("The dynamic header class must implements Header.Dynamic and provide a empty argument constructor or a HeaderProvider.");
}
Expand All @@ -596,19 +612,18 @@ private void parseHeader(Header... annotation) {
headerValue = header.pairValue();
}

if ("Content-Type".equalsIgnoreCase(headerName) && null != headerValue) {
if ("Content-Type".equalsIgnoreCase(headerName)) {
contentType = MediaType.get(headerValue);
}
if (null != headerName && null != headerValue) {
Converter<String, String> converter = pigeon.stringConverter(String.class, new Annotation[]{});
try {
headerValue = converter.convert(headerValue);
} catch (IOException e) {
e.printStackTrace();
}
if (null != headerValue) {
headersBuilder.add(headerName, headerValue);
}

Converter<String, String> converter = pigeon.stringConverter(String.class, new Annotation[]{});
try {
headerValue = converter.convert(headerValue);
} catch (IOException e) {
e.printStackTrace();
}
if (null != headerValue) {
headersBuilder.add(headerName, headerValue);
}
}
headers = headersBuilder.build();
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/yhy/http/pigeon/utils/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/
public class Utils {
private static final Type[] EMPTY_TYPE_ARRAY = new Type[0];
public final static String VERSION = "1.5.0";
public final static String VERSION = "1.5.1";

public static boolean isEmpty(Object object) {
if (null == object) return true;
Expand Down

0 comments on commit 63606ee

Please sign in to comment.