Skip to content

Commit

Permalink
Merge pull request #52 from brenoepics/dev
Browse files Browse the repository at this point in the history
Remove OkHTTP
  • Loading branch information
brenoepics authored Jan 19, 2024
2 parents 53b77f1 + f85ca6d commit 49445f4
Show file tree
Hide file tree
Showing 14 changed files with 342 additions and 517 deletions.
20 changes: 5 additions & 15 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>io.github.brenoepics</groupId>
<artifactId>at4j</artifactId>
<version>0.0.6</version>
<version>0.0.7</version>
<name>Azure Translator For Java</name>
<description>
A simple Java library to translate text using Azure AI Cognitive Services.
Expand Down Expand Up @@ -44,8 +44,8 @@
</distributionManagement>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>9</maven.compiler.source>
<maven.compiler.target>9</maven.compiler.target>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<sonar.organization>brenoepic</sonar.organization>
<sonar.host.url>https://sonarcloud.io</sonar.host.url>
</properties>
Expand Down Expand Up @@ -83,18 +83,6 @@
</profile>
</profiles>
<dependencies>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.12.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>logging-interceptor</artifactId>
<version>4.12.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
Expand Down Expand Up @@ -143,6 +131,8 @@
<artifactId>maven-compiler-plugin</artifactId>
<version>3.12.1</version>
<configuration>
<source>11</source>
<target>11</target>
</configuration>
</plugin>
<!-- Maven JAR Plugin -->
Expand Down
107 changes: 89 additions & 18 deletions src/main/java/io/github/brenoepics/at4j/AzureApiBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

import io.github.brenoepics.at4j.azure.BaseURL;
import io.github.brenoepics.at4j.core.AzureApiImpl;
import io.github.brenoepics.at4j.util.logging.LoggerUtil;
import io.github.brenoepics.at4j.util.logging.PrivacyProtectionLogger;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;

import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLParameters;
import java.net.ProxySelector;
import java.net.http.HttpClient;
import java.time.Duration;

/**
* Builder class for constructing instances of AzureApi.
Expand All @@ -23,6 +26,14 @@ public class AzureApiBuilder {
// The subscription region for the Azure API.
private String subscriptionRegion;

private ProxySelector proxySelector = null;

private SSLContext sslContext = null;

private SSLParameters sslParameters = null;

private Duration connectTimeout = null;

/** Default constructor initializes the base URL to the global endpoint. */
public AzureApiBuilder() {
this.baseURL = BaseURL.GLOBAL;
Expand Down Expand Up @@ -68,6 +79,62 @@ public AzureApiBuilder region(String subscriptionRegion) {
return this;
}

/**
* Sets the proxy selector for the Azure API.
*
* @param proxySelector The proxy selector for the Azure API.
* @return The current instance of AzureApiBuilder for method chaining.
* @see <a
* href="https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/net/ProxySelector.html"
* >ProxySelector</a>
*/
public AzureApiBuilder proxy(ProxySelector proxySelector) {
this.proxySelector = proxySelector;
return this;
}

/**
* Sets the connect timeout for the Azure API.
*
* @param connectTimeout The connect timeout for the Azure API.
* @return The current instance of AzureApiBuilder for method chaining.
* @see <a
* href="https://docs.oracle.com/en/java/javase/11/docs/api/java.net.http/java/net/http/HttpClient.Builder.html#connectTimeout(java.time.Duration)"
* >Connection Timeout</a>
*/
public AzureApiBuilder connectTimeout(Duration connectTimeout) {
this.connectTimeout = connectTimeout;
return this;
}

/**
* Sets the SSL context for the Azure API.
*
* @param sslContext The SSL context for the Azure API.
* @return The current instance of AzureApiBuilder for method chaining.
* @see <a
* href="https://docs.oracle.com/en/java/javase/11/docs/api/java.base/javax/net/ssl/SSLContext.html"
* >SSLContext</a>
*/
public AzureApiBuilder sslContext(SSLContext sslContext) {
this.sslContext = sslContext;
return this;
}

/**
* Sets the SSL parameters for the Azure API.
*
* @param sslParameters The SSL parameters for the Azure API.
* @return The current instance of AzureApiBuilder for method chaining.
* @see <a
* href="https://docs.oracle.com/en/java/javase/11/docs/api/java.base/javax/net/ssl/SSLParameters.html"
* >SSLParameters</a>
*/
public AzureApiBuilder sslParameters(SSLParameters sslParameters) {
this.sslParameters = sslParameters;
return this;
}

/**
* Builds and returns an instance of AzureApi with the configured parameters.
*
Expand All @@ -81,20 +148,24 @@ public AzureApi build() {
}

// The HTTP client used by the Azure API.
OkHttpClient httpClient =
new OkHttpClient.Builder()
.addInterceptor(
chain ->
chain.proceed(
chain
.request()
.newBuilder()
.addHeader("User-Agent", AT4J.USER_AGENT)
.build()))
.addInterceptor(
new HttpLoggingInterceptor(LoggerUtil.getLogger(OkHttpClient.class)::trace)
.setLevel(HttpLoggingInterceptor.Level.BODY))
.build();
return new AzureApiImpl<>(httpClient, baseURL, subscriptionKey, subscriptionRegion);
HttpClient.Builder httpClient = HttpClient.newBuilder().version(HttpClient.Version.HTTP_1_1);

if (proxySelector != null) {
httpClient.proxy(proxySelector);
}

if (sslContext != null) {
httpClient.sslContext(sslContext);
}

if (sslParameters != null) {
httpClient.sslParameters(sslParameters);
}

if (connectTimeout != null) {
httpClient.connectTimeout(connectTimeout);
}

return new AzureApiImpl<>(httpClient.build(), baseURL, subscriptionKey, subscriptionRegion);
}
}
12 changes: 5 additions & 7 deletions src/main/java/io/github/brenoepics/at4j/core/AzureApiImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,20 @@
import io.github.brenoepics.at4j.util.rest.RestMethod;
import io.github.brenoepics.at4j.util.rest.RestRequest;

import java.net.http.HttpClient;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;

import okhttp3.OkHttpClient;

/**
* This class is an implementation of the AzureApi interface. It provides methods to interact with
* Azure's translation API.
*/
public class AzureApiImpl<T> implements AzureApi {

/** The Http Client for this instance. */
private final OkHttpClient httpClient;
private final HttpClient httpClient;

/** The BaseURL for this instance. */
private final BaseURL baseURL;
Expand Down Expand Up @@ -62,7 +61,7 @@ public class AzureApiImpl<T> implements AzureApi {
* @param subscriptionRegion The subscription region for this instance.
*/
public AzureApiImpl(
OkHttpClient httpClient, BaseURL baseURL, String subscriptionKey, String subscriptionRegion) {
HttpClient httpClient, BaseURL baseURL, String subscriptionKey, String subscriptionRegion) {
this.httpClient = httpClient;
this.baseURL = baseURL;
this.subscriptionKey = subscriptionKey;
Expand Down Expand Up @@ -183,15 +182,14 @@ public CompletableFuture<Optional<Collection<Language>>> getAvailableLanguages(
@Override
public void disconnect() {
this.threadPool.getExecutorService().shutdown();
this.httpClient.dispatcher().executorService().shutdown();
}

/**
* Gets the used OkHttpClient.
*
* @return OkHttpClient - The used OkHttpClient.
* @return HttpClient - The used HttpClient.
*/
public OkHttpClient getHttpClient() {
public HttpClient getHttpClient() {
return this.httpClient;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ public class RateLimitBucket<T> {
private final ConcurrentLinkedQueue<RestRequest<T>> requestQueue = new ConcurrentLinkedQueue<>();

private final RestEndpoint endpoint;
private final String majorUrlParameter;

private volatile long rateLimitResetTimestamp = 0;
private volatile int rateLimitRemaining = 1;
Expand All @@ -22,11 +21,9 @@ public class RateLimitBucket<T> {
* Creates a RateLimitBucket for the given endpoint / parameter combination.
*
* @param endpoint The REST endpoint the rate-limit is tracked for.
* @param majorUrlParameter The url parameter this bucket is specific for. Maybe null.
*/
public RateLimitBucket(RestEndpoint endpoint, String majorUrlParameter) {
public RateLimitBucket(RestEndpoint endpoint) {
this.endpoint = endpoint;
this.majorUrlParameter = majorUrlParameter;
}

/**
Expand Down Expand Up @@ -88,42 +85,31 @@ public int getTimeTillSpaceGetsAvailable() {
* Checks if a bucket created with the given parameters would equal this bucket.
*
* @param endpoint The endpoint.
* @param majorUrlParameter The major url parameter.
* @return Whether a bucket created with the given parameters would equal this bucket or not.
*/
public boolean equals(RestEndpoint endpoint, String majorUrlParameter) {
boolean endpointSame = this.endpoint == endpoint;
boolean majorUrlParameterBothNull = this.majorUrlParameter == null && majorUrlParameter == null;
boolean majorUrlParameterEqual =
this.majorUrlParameter != null && this.majorUrlParameter.equals(majorUrlParameter);

return endpointSame && (majorUrlParameterBothNull || majorUrlParameterEqual);
public boolean endpointMatches(RestEndpoint endpoint) {
return this.endpoint == endpoint;
}

@Override
public boolean equals(Object obj) {
if (obj instanceof RateLimitBucket) {
RateLimitBucket<T> otherBucket = (RateLimitBucket<T>) obj;
return equals(otherBucket.endpoint, otherBucket.majorUrlParameter);
return endpointMatches(otherBucket.endpoint);
}
return false;
}

@Override
public int hashCode() {
int hash = 42;
int urlParamHash = majorUrlParameter == null ? 0 : majorUrlParameter.hashCode();
int endpointHash = endpoint == null ? 0 : endpoint.hashCode();

hash = hash * 11 + urlParamHash;
hash = hash * 17 + endpointHash;
return hash;
}

@Override
public String toString() {
String str = "Endpoint: " + (endpoint == null ? "global" : endpoint.getEndpointUrl());
str += ", Major url parameter:" + (majorUrlParameter == null ? "none" : majorUrlParameter);
return str;
return "Endpoint: " + (endpoint == null ? "global" : endpoint.getEndpointUrl());
}
}
Loading

0 comments on commit 49445f4

Please sign in to comment.