From c3018d7f229621cf7b3b162d1ecdfdb64108e5af Mon Sep 17 00:00:00 2001 From: evgeny Date: Thu, 20 Nov 2025 23:56:51 +0000 Subject: [PATCH] refactor: replace default network client with OkHttp and adjust proxy support setup - Updated build scripts to use `network-client-okhttp` by default where appropriate. - Revised README to simplify proxy setup instructions. - Adjusted integration workflows to correctly configure HTTP client preferences. --- .github/workflows/emulate.yml | 2 +- .github/workflows/integration-test.yml | 12 ++++++------ README.md | 12 +----------- android/build.gradle.kts | 9 ++++++++- java/build.gradle.kts | 6 +++--- .../java/io/ably/lib/network/OkHttpCall.java | 17 ++++++++++++----- 6 files changed, 31 insertions(+), 27 deletions(-) diff --git a/.github/workflows/emulate.yml b/.github/workflows/emulate.yml index 6b07c3824..4773f61b9 100644 --- a/.github/workflows/emulate.yml +++ b/.github/workflows/emulate.yml @@ -39,7 +39,7 @@ jobs: emulator-options: -no-snapshot-save -no-window -gpu swiftshader_indirect -noaudio -no-boot-anim -camera-back none disable-animations: true # Print emulator logs if tests fail - script: ./gradlew :android:connectedAndroidTest || (adb logcat -d System.out:I && exit 1) + script: ./gradlew :android:connectedAndroidTest ${{ matrix.android-api-level == 19 && '-PhttpURLConnection' || '' }} || (adb logcat -d System.out:I && exit 1) - uses: actions/upload-artifact@v4 if: always() diff --git a/.github/workflows/integration-test.yml b/.github/workflows/integration-test.yml index 89368a8a8..373926f2d 100644 --- a/.github/workflows/integration-test.yml +++ b/.github/workflows/integration-test.yml @@ -8,7 +8,7 @@ on: - main jobs: - check-rest: + check-rest-httpurlconnection: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 @@ -24,7 +24,7 @@ jobs: - name: Set up Gradle uses: gradle/actions/setup-gradle@v3 - - run: ./gradlew :java:testRestSuite + - run: ./gradlew :java:testRestSuite -PhttpURLConnection - uses: actions/upload-artifact@v4 if: always() @@ -32,7 +32,7 @@ jobs: name: java-build-reports-rest path: java/build/reports/ - check-realtime: + check-realtime-httpurlconnection: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 @@ -48,7 +48,7 @@ jobs: - name: Set up Gradle uses: gradle/actions/setup-gradle@v3 - - run: ./gradlew :java:testRealtimeSuite + - run: ./gradlew :java:testRealtimeSuite -PhttpURLConnection - uses: actions/upload-artifact@v4 if: always() @@ -71,7 +71,7 @@ jobs: - name: Set up Gradle uses: gradle/actions/setup-gradle@v3 - - run: ./gradlew :java:testRestSuite -Pokhttp + - run: ./gradlew :java:testRestSuite check-realtime-okhttp: runs-on: ubuntu-latest @@ -89,7 +89,7 @@ jobs: - name: Set up Gradle uses: gradle/actions/setup-gradle@v3 - - run: ./gradlew :java:testRealtimeSuite -Pokhttp + - run: ./gradlew :java:testRealtimeSuite check-liveobjects: runs-on: ubuntu-latest diff --git a/README.md b/README.md index 7b20f8693..61d788732 100644 --- a/README.md +++ b/README.md @@ -134,17 +134,7 @@ You can add proxy support to the Ably Java SDK by configuring `ProxyOptions` in
Proxy support setup details. -To enable proxy support for both REST and Realtime clients in the Ably SDK, use the OkHttp library to handle HTTP requests and WebSocket connections. - -Add the following dependency to your `build.gradle` file: - -```groovy -dependencies { - runtimeOnly("io.ably:network-client-okhttp:1.4.2") -} -``` - -After adding the OkHttp dependency, enable proxy support by specifying proxy settings in the ClientOptions when initializing your Ably client. +Enable proxy support by specifying proxy settings in the ClientOptions when initializing your Ably client. The following example sets up a proxy using the Pub/Sub Java SDK: diff --git a/android/build.gradle.kts b/android/build.gradle.kts index fb70f02e0..50a11c309 100644 --- a/android/build.gradle.kts +++ b/android/build.gradle.kts @@ -54,7 +54,11 @@ dependencies { compileOnly(libs.jetbrains) testImplementation(libs.bundles.tests) implementation(project(":network-client-core")) - runtimeOnly(project(":network-client-default")) + if (findProperty("httpURLConnection") == null) { + runtimeOnly(project(":network-client-okhttp")) + } else { + runtimeOnly(project(":network-client-default")) + } implementation(libs.firebase.messaging) androidTestImplementation(libs.bundles.instrumental.android) } @@ -62,6 +66,9 @@ dependencies { configurations { all { exclude(group = "org.hamcrest", module = "hamcrest-core") + resolutionStrategy { + force(libs.jetbrains) + } } getByName("androidTestImplementation") { extendsFrom(configurations.getByName("testImplementation")) diff --git a/java/build.gradle.kts b/java/build.gradle.kts index 33c89a2f8..e42f6809b 100644 --- a/java/build.gradle.kts +++ b/java/build.gradle.kts @@ -22,10 +22,10 @@ dependencies { implementation(libs.bundles.common) compileOnly(libs.jetbrains) implementation(project(":network-client-core")) - if (findProperty("okhttp") == null) { - runtimeOnly(project(":network-client-default")) - } else { + if (findProperty("httpURLConnection") == null) { runtimeOnly(project(":network-client-okhttp")) + } else { + runtimeOnly(project(":network-client-default")) } testImplementation(libs.bundles.tests) } diff --git a/network-client-okhttp/src/main/java/io/ably/lib/network/OkHttpCall.java b/network-client-okhttp/src/main/java/io/ably/lib/network/OkHttpCall.java index 643697391..c366fd7b8 100644 --- a/network-client-okhttp/src/main/java/io/ably/lib/network/OkHttpCall.java +++ b/network-client-okhttp/src/main/java/io/ably/lib/network/OkHttpCall.java @@ -1,7 +1,9 @@ package io.ably.lib.network; import okhttp3.Call; +import okhttp3.MediaType; import okhttp3.Response; +import okhttp3.ResponseBody; import java.io.IOException; import java.net.ConnectException; @@ -23,11 +25,7 @@ public HttpResponse execute() { .headers(response.headers().toMultimap()) .code(response.code()) .message(response.message()) - .body( - response.body() != null && response.body().contentType() != null - ? new HttpBody(response.body().contentType().toString(), response.body().bytes()) - : null - ) + .body(buildHttpBody(response)) .build(); } catch (ConnectException | SocketTimeoutException | UnknownHostException | NoRouteToHostException fce) { @@ -42,4 +40,13 @@ public HttpResponse execute() { public void cancel() { call.cancel(); } + + private HttpBody buildHttpBody(Response response) throws IOException { + try (ResponseBody body = response.body()) { + MediaType contentType = body != null ? body.contentType() : null; + return contentType != null + ? new HttpBody(contentType.toString(), body.bytes()) + : null; + } + } }