-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: OkHttp implementation for making HTTP calls and WebSocket conne…
…ctions
- Loading branch information
Showing
19 changed files
with
407 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
plugins { | ||
`java-library` | ||
alias(libs.plugins.lombok) | ||
alias(libs.plugins.maven.publish) | ||
} | ||
|
||
java { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
POM_ARTIFACT_ID=network-client-core | ||
POM_NAME=Core HTTP client abstraction | ||
POM_DESCRIPTION=Core HTTP client abstraction | ||
POM_PACKAGING=jar |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
plugins { | ||
`java-library` | ||
alias(libs.plugins.lombok) | ||
alias(libs.plugins.maven.publish) | ||
} | ||
|
||
java { | ||
sourceCompatibility = JavaVersion.VERSION_1_8 | ||
targetCompatibility = JavaVersion.VERSION_1_8 | ||
} | ||
|
||
dependencies { | ||
implementation(project(":network-client-core")) | ||
implementation(libs.okhttp) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
POM_ARTIFACT_ID=network-client-okhttp | ||
POM_NAME=Default HTTP client | ||
POM_DESCRIPTION=Default implementation for HTTP client | ||
POM_PACKAGING=jar |
45 changes: 45 additions & 0 deletions
45
network-client-okhttp/src/main/java/io/ably/lib/network/OkHttpCall.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package io.ably.lib.network; | ||
|
||
import okhttp3.Call; | ||
import okhttp3.Response; | ||
|
||
import java.io.IOException; | ||
import java.net.ConnectException; | ||
import java.net.NoRouteToHostException; | ||
import java.net.SocketTimeoutException; | ||
import java.net.UnknownHostException; | ||
|
||
public class OkHttpCall implements HttpCall { | ||
private final Call call; | ||
|
||
public OkHttpCall(Call call) { | ||
this.call = call; | ||
} | ||
|
||
@Override | ||
public HttpResponse execute() { | ||
try (Response response = call.execute()) { | ||
return HttpResponse.builder() | ||
.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 | ||
) | ||
.build(); | ||
|
||
} catch (ConnectException | SocketTimeoutException | UnknownHostException | NoRouteToHostException fce) { | ||
throw new FailedConnectionException(fce); | ||
} catch (IOException ioe) { | ||
throw new RuntimeException(ioe); | ||
} | ||
|
||
} | ||
|
||
@Override | ||
public void cancel() { | ||
call.cancel(); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
network-client-okhttp/src/main/java/io/ably/lib/network/OkHttpEngine.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package io.ably.lib.network; | ||
|
||
import okhttp3.Call; | ||
import okhttp3.OkHttpClient; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
public class OkHttpEngine implements HttpEngine { | ||
|
||
private final OkHttpClient client; | ||
private final HttpEngineConfig config; | ||
|
||
public OkHttpEngine(OkHttpClient client, HttpEngineConfig config) { | ||
this.client = client; | ||
this.config = config; | ||
} | ||
|
||
@Override | ||
public HttpCall call(HttpRequest request) { | ||
Call call = client.newBuilder() | ||
.connectTimeout(request.getHttpOpenTimeout(), TimeUnit.MILLISECONDS) | ||
.readTimeout(request.getHttpReadTimeout(), TimeUnit.MILLISECONDS) | ||
.build() | ||
.newCall(OkHttpUtils.toOkhttpRequest(request)); | ||
return new OkHttpCall(call); | ||
} | ||
|
||
@Override | ||
public boolean isUsingProxy() { | ||
return config.getProxy() != null; | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
network-client-okhttp/src/main/java/io/ably/lib/network/OkHttpEngineFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package io.ably.lib.network; | ||
|
||
import okhttp3.OkHttpClient; | ||
|
||
public class OkHttpEngineFactory implements HttpEngineFactory { | ||
@Override | ||
public HttpEngine create(HttpEngineConfig config) { | ||
OkHttpClient.Builder connectionBuilder = new OkHttpClient.Builder(); | ||
OkHttpUtils.injectProxySetting(config.getProxy(), connectionBuilder); | ||
return new OkHttpEngine(connectionBuilder.build(), config); | ||
} | ||
|
||
@Override | ||
public EngineType getEngineType() { | ||
return EngineType.OKHTTP; | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
network-client-okhttp/src/main/java/io/ably/lib/network/OkHttpUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package io.ably.lib.network; | ||
|
||
import okhttp3.Credentials; | ||
import okhttp3.Headers; | ||
import okhttp3.MediaType; | ||
import okhttp3.OkHttpClient; | ||
import okhttp3.Request; | ||
import okhttp3.RequestBody; | ||
|
||
import java.net.InetSocketAddress; | ||
import java.net.Proxy; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class OkHttpUtils { | ||
public static void injectProxySetting(ProxyConfig proxyConfig, OkHttpClient.Builder connectionBuilder) { | ||
if (proxyConfig == null) return; | ||
connectionBuilder.proxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyConfig.getHost(), proxyConfig.getPort()))); | ||
if (proxyConfig.getUsername() == null || proxyConfig.getAuthType() != ProxyAuthType.BASIC) return; | ||
String username = proxyConfig.getUsername(); | ||
String password = proxyConfig.getPassword(); | ||
connectionBuilder.proxyAuthenticator((route, response) -> { | ||
String credential = Credentials.basic(username, password); | ||
return response.request().newBuilder() | ||
.header("Proxy-Authorization", credential) | ||
.build(); | ||
}); | ||
} | ||
|
||
public static Request toOkhttpRequest(HttpRequest request) { | ||
Request.Builder builder = new Request.Builder() | ||
.url(request.getUrl()); | ||
|
||
RequestBody body = null; | ||
|
||
if (request.getBody() != null) { | ||
body = RequestBody.create(request.getBody().getContent(), MediaType.parse(request.getBody().getContentType())); | ||
} | ||
|
||
builder.method(request.getMethod(), body); | ||
for (Map.Entry<String, List<String>> entry : request.getHeaders().entrySet()) { | ||
String headerName = entry.getKey(); | ||
List<String> values = entry.getValue(); | ||
for (String headerValue : values) { | ||
builder.addHeader(headerName, headerValue); | ||
} | ||
} | ||
|
||
return builder.build(); | ||
} | ||
} |
Oops, something went wrong.