From 0879bed42fa3dbd16338e4428a47362a8c07891e Mon Sep 17 00:00:00 2001 From: zhanq Date: Wed, 31 Jan 2024 11:23:30 +0800 Subject: [PATCH] use OKHttpClient. --- .../cn/jiguang/app/config/JiguangApiConfig.java | 5 ++++- jiguang-sdk/pom.xml | 12 ++++++++++++ .../src/main/java/cn/jiguang/sdk/api/AdminApi.java | 14 ++++++++++++++ .../main/java/cn/jiguang/sdk/api/DeviceApi.java | 14 ++++++++++++++ .../src/main/java/cn/jiguang/sdk/api/PushApi.java | 14 ++++++++++++++ .../main/java/cn/jiguang/sdk/api/ReportApi.java | 13 +++++++++++++ 6 files changed, 71 insertions(+), 1 deletion(-) diff --git a/example-for-spring/src/main/java/cn/jiguang/app/config/JiguangApiConfig.java b/example-for-spring/src/main/java/cn/jiguang/app/config/JiguangApiConfig.java index 85db873..7101869 100644 --- a/example-for-spring/src/main/java/cn/jiguang/app/config/JiguangApiConfig.java +++ b/example-for-spring/src/main/java/cn/jiguang/app/config/JiguangApiConfig.java @@ -1,6 +1,9 @@ package cn.jiguang.app.config; -import cn.jiguang.sdk.api.*; +import cn.jiguang.sdk.api.AdminApi; +import cn.jiguang.sdk.api.DeviceApi; +import cn.jiguang.sdk.api.PushApi; +import cn.jiguang.sdk.api.ReportApi; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; diff --git a/jiguang-sdk/pom.xml b/jiguang-sdk/pom.xml index 9d03cf9..3e5ce9b 100644 --- a/jiguang-sdk/pom.xml +++ b/jiguang-sdk/pom.xml @@ -20,6 +20,7 @@ 8 8 UTF-8 + 4.12.0 1.18.30 13.1 3.8.0 @@ -50,11 +51,22 @@ feign-slf4j ${feign-core.version} + + io.github.openfeign + feign-okhttp + ${feign-core.version} + io.github.openfeign feign-jackson ${feign-core.version} + + + com.squareup.okhttp3 + okhttp + ${okhttp.version} + com.fasterxml.jackson.datatype diff --git a/jiguang-sdk/src/main/java/cn/jiguang/sdk/api/AdminApi.java b/jiguang-sdk/src/main/java/cn/jiguang/sdk/api/AdminApi.java index 494b3b3..8317094 100644 --- a/jiguang-sdk/src/main/java/cn/jiguang/sdk/api/AdminApi.java +++ b/jiguang-sdk/src/main/java/cn/jiguang/sdk/api/AdminApi.java @@ -8,9 +8,12 @@ import feign.Feign; import feign.Logger; import feign.auth.BasicAuthRequestInterceptor; +import feign.okhttp.OkHttpClient; import feign.slf4j.Slf4jLogger; import lombok.NonNull; +import java.net.Proxy; + public class AdminApi { private final AdminClient adminClient; @@ -34,6 +37,7 @@ public CertificateUploadResult uploadCertificate(@NonNull String appKey, @NonNul public static class Builder { private String host = "https://admin.jpush.cn"; + private Proxy proxy; private String devKey; private String devSecret; private Logger.Level loggerLevel = Logger.Level.BASIC; @@ -43,6 +47,11 @@ public Builder setHost(@NonNull String host) { return this; } + public Builder setProxy(@NonNull Proxy proxy) { + this.proxy = proxy; + return this; + } + public Builder setDevKey(@NonNull String devKey) { this.devKey = devKey; return this; @@ -59,7 +68,12 @@ public Builder setLoggerLevel(@NonNull Logger.Level loggerLevel) { } public AdminApi build() { + okhttp3.OkHttpClient.Builder delegateBuilder = new okhttp3.OkHttpClient().newBuilder(); + if (proxy != null) { + delegateBuilder.proxy(proxy); + } AdminClient adminClient = Feign.builder() + .client(new OkHttpClient(delegateBuilder.build())) .requestInterceptor(new BasicAuthRequestInterceptor(devKey, devSecret)) .encoder(new ApiEncoder()) .decoder(new ApiDecoder()) diff --git a/jiguang-sdk/src/main/java/cn/jiguang/sdk/api/DeviceApi.java b/jiguang-sdk/src/main/java/cn/jiguang/sdk/api/DeviceApi.java index 0ad40a0..93a6268 100644 --- a/jiguang-sdk/src/main/java/cn/jiguang/sdk/api/DeviceApi.java +++ b/jiguang-sdk/src/main/java/cn/jiguang/sdk/api/DeviceApi.java @@ -9,9 +9,12 @@ import feign.Feign; import feign.Logger; import feign.auth.BasicAuthRequestInterceptor; +import feign.okhttp.OkHttpClient; import feign.slf4j.Slf4jLogger; import lombok.NonNull; +import java.net.Proxy; + public class DeviceApi { private final DeviceClient deviceClient; @@ -75,6 +78,7 @@ public DeviceStatusGetResult getDeviceStatus() { public static class Builder { private String host = "https://device.jpush.cn"; + private Proxy proxy; private String appKey; private String masterSecret; private Logger.Level loggerLevel = Logger.Level.BASIC; @@ -84,6 +88,11 @@ public Builder setHost(@NonNull String host) { return this; } + public Builder setProxy(@NonNull Proxy proxy) { + this.proxy = proxy; + return this; + } + public Builder setAppKey(@NonNull String appKey) { this.appKey = appKey; return this; @@ -100,7 +109,12 @@ public Builder setLoggerLevel(@NonNull Logger.Level loggerLevel) { } public DeviceApi build() { + okhttp3.OkHttpClient.Builder delegateBuilder = new okhttp3.OkHttpClient().newBuilder(); + if (proxy != null) { + delegateBuilder.proxy(proxy); + } DeviceClient deviceClient = Feign.builder() + .client(new OkHttpClient(delegateBuilder.build())) .requestInterceptor(new BasicAuthRequestInterceptor(appKey, masterSecret)) .encoder(new ApiEncoder()) .decoder(new ApiDecoder()) diff --git a/jiguang-sdk/src/main/java/cn/jiguang/sdk/api/PushApi.java b/jiguang-sdk/src/main/java/cn/jiguang/sdk/api/PushApi.java index ac28044..963aa41 100644 --- a/jiguang-sdk/src/main/java/cn/jiguang/sdk/api/PushApi.java +++ b/jiguang-sdk/src/main/java/cn/jiguang/sdk/api/PushApi.java @@ -18,9 +18,12 @@ import feign.Feign; import feign.Logger; import feign.auth.BasicAuthRequestInterceptor; +import feign.okhttp.OkHttpClient; import feign.slf4j.Slf4jLogger; import lombok.NonNull; +import java.net.Proxy; + public class PushApi { private final PushClient pushClient; @@ -100,6 +103,7 @@ public ImageFileUpdateResult updateImageFile(@NonNull String mediaId, @NonNull I public static class Builder { private String host = "https://api.jpush.cn"; + private Proxy proxy; private String appKey; private String masterSecret; private Logger.Level loggerLevel = Logger.Level.BASIC; @@ -109,6 +113,11 @@ public Builder setHost(@NonNull String host) { return this; } + public Builder setProxy(@NonNull Proxy proxy) { + this.proxy = proxy; + return this; + } + public Builder setAppKey(@NonNull String appKey) { this.appKey = appKey; return this; @@ -125,7 +134,12 @@ public Builder setLoggerLevel(@NonNull Logger.Level loggerLevel) { } public PushApi build() { + okhttp3.OkHttpClient.Builder delegateBuilder = new okhttp3.OkHttpClient().newBuilder(); + if (proxy != null) { + delegateBuilder.proxy(proxy); + } PushClient pushClient = Feign.builder() + .client(new OkHttpClient(delegateBuilder.build())) .requestInterceptor(new BasicAuthRequestInterceptor(appKey, masterSecret)) .encoder(new ApiEncoder()) .decoder(new ApiDecoder()) diff --git a/jiguang-sdk/src/main/java/cn/jiguang/sdk/api/ReportApi.java b/jiguang-sdk/src/main/java/cn/jiguang/sdk/api/ReportApi.java index 19906b5..20455ad 100644 --- a/jiguang-sdk/src/main/java/cn/jiguang/sdk/api/ReportApi.java +++ b/jiguang-sdk/src/main/java/cn/jiguang/sdk/api/ReportApi.java @@ -8,9 +8,11 @@ import feign.Feign; import feign.Logger; import feign.auth.BasicAuthRequestInterceptor; +import feign.okhttp.OkHttpClient; import feign.slf4j.Slf4jLogger; import lombok.NonNull; +import java.net.Proxy; import java.time.LocalDate; import java.util.List; import java.util.Map; @@ -42,6 +44,7 @@ public UserDetailGetResult getUserDetail(@NonNull LocalDate startDate, @NonNull public static class Builder { private String host = "https://report.jpush.cn"; + private Proxy proxy; private String appKey; private String masterSecret; private Logger.Level loggerLevel = Logger.Level.BASIC; @@ -51,6 +54,11 @@ public Builder setHost(@NonNull String host) { return this; } + public Builder setProxy(@NonNull Proxy proxy) { + this.proxy = proxy; + return this; + } + public Builder setAppKey(@NonNull String appKey) { this.appKey = appKey; return this; @@ -67,7 +75,12 @@ public Builder setLoggerLevel(@NonNull Logger.Level loggerLevel) { } public ReportApi build() { + okhttp3.OkHttpClient.Builder delegateBuilder = new okhttp3.OkHttpClient().newBuilder(); + if (proxy != null) { + delegateBuilder.proxy(proxy); + } ReportClient reportClient = Feign.builder() + .client(new OkHttpClient(delegateBuilder.build())) .requestInterceptor(new BasicAuthRequestInterceptor(appKey, masterSecret)) .encoder(new ApiEncoder()) .decoder(new ApiDecoder())