Skip to content

Commit

Permalink
Migrate to httpclient 5.x
Browse files Browse the repository at this point in the history
  • Loading branch information
antas-marcin committed Oct 21, 2024
1 parent 0878142 commit 36d7649
Show file tree
Hide file tree
Showing 14 changed files with 86 additions and 108 deletions.
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
<maven.compiler.target>1.8</maven.compiler.target>
<lombok.version>1.18.34</lombok.version>
<gson.version>2.11.0</gson.version>
<httpclient.version>4.5.14</httpclient.version>
<httpclient.version>5.4</httpclient.version>
<lang3.version>3.17.0</lang3.version>
<junit.version>5.11.2</junit.version>
<testcontainers.version>1.20.2</testcontainers.version>
Expand Down Expand Up @@ -108,8 +108,8 @@
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<groupId>org.apache.httpcomponents.client5</groupId>
<artifactId>httpclient5</artifactId>
<version>${httpclient.version}</version>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
package io.weaviate.client.base.http.builder;

import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.impl.client.HttpClientBuilder;
import io.weaviate.client.Config;
import io.weaviate.client.base.http.impl.CommonsHttpClientImpl;
import java.util.concurrent.TimeUnit;
import org.apache.hc.client5.http.config.RequestConfig;
import org.apache.hc.client5.http.impl.classic.HttpClientBuilder;
import org.apache.hc.core5.http.HttpHost;
import org.apache.hc.core5.util.Timeout;

public class HttpApacheClientBuilder {

private HttpApacheClientBuilder() {}

public static CommonsHttpClientImpl.CloseableHttpClientBuilder build(Config config) {
RequestConfig.Builder requestConfigBuilder = RequestConfig.custom()
.setConnectTimeout(config.getConnectionTimeout() * 1000)
.setConnectionRequestTimeout(config.getConnectionRequestTimeout() * 1000)
.setSocketTimeout(config.getSocketTimeout() * 1000);

.setConnectionRequestTimeout(Timeout.of(config.getConnectionRequestTimeout(), TimeUnit.SECONDS))
.setResponseTimeout(Timeout.of(config.getSocketTimeout(), TimeUnit.SECONDS));

if (config.getProxyHost() != null) {
requestConfigBuilder.setProxy(new HttpHost(config.getProxyHost(), config.getProxyPort(), config.getProxyScheme()));
requestConfigBuilder.setProxy(new HttpHost(config.getProxyScheme(), config.getProxyHost(), config.getProxyPort()));
}

RequestConfig requestConfig = requestConfigBuilder.build();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,26 @@
package io.weaviate.client.base.http.impl;

import java.io.Closeable;
import java.io.IOException;
import org.apache.http.HttpHeaders;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpHead;
import org.apache.http.client.methods.HttpPatch;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.util.EntityUtils;
import io.weaviate.client.base.http.HttpClient;
import io.weaviate.client.base.http.HttpResponse;

import io.weaviate.client.v1.auth.provider.AccessTokenProvider;
import java.io.Closeable;
import java.io.IOException;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import io.weaviate.client.v1.auth.provider.AccessTokenProvider;
import org.apache.hc.client5.http.classic.methods.HttpDelete;
import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.classic.methods.HttpHead;
import org.apache.hc.client5.http.classic.methods.HttpPatch;
import org.apache.hc.client5.http.classic.methods.HttpPost;
import org.apache.hc.client5.http.classic.methods.HttpPut;
import org.apache.hc.client5.http.classic.methods.HttpUriRequestBase;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.core5.http.HttpHeaders;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.apache.hc.core5.http.io.entity.StringEntity;
import org.apache.hc.core5.http.message.BasicClassicHttpRequest;

public class CommonsHttpClientImpl implements HttpClient, Closeable {
private final Map<String, String> headers;
Expand Down Expand Up @@ -72,19 +70,19 @@ public HttpResponse sendHeadRequest(String url) throws Exception {
return sendRequestWithoutPayload(new HttpHead(url));
}

private HttpResponse sendRequestWithoutPayload(HttpRequestBase request) throws Exception {
private HttpResponse sendRequestWithoutPayload(BasicClassicHttpRequest request) throws Exception {
request.setHeader(HttpHeaders.ACCEPT, "*/*");
return sendRequest(request);
}

private HttpResponse sendRequestWithPayload(HttpEntityEnclosingRequestBase request, String jsonString) throws Exception {
private HttpResponse sendRequestWithPayload(BasicClassicHttpRequest request, String jsonString) throws Exception {
request.setHeader(HttpHeaders.ACCEPT, "application/json");
request.setHeader(HttpHeaders.CONTENT_TYPE, "application/json");
request.setEntity(new StringEntity(jsonString, StandardCharsets.UTF_8));
return sendRequest(request);
}

private HttpResponse sendRequest(HttpUriRequest request) throws Exception {
private HttpResponse sendRequest(BasicClassicHttpRequest request) throws Exception {
if (headers != null && headers.size() > 0) {
headers.forEach(request::addHeader);
}
Expand All @@ -95,7 +93,7 @@ private HttpResponse sendRequest(HttpUriRequest request) throws Exception {
CloseableHttpClient client = clientBuilder.build();
CloseableHttpResponse response = client.execute(request);

int statusCode = response.getStatusLine().getStatusCode();
int statusCode = response.getCode();
String body = response.getEntity() != null
? EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8)
: "";
Expand All @@ -111,25 +109,12 @@ public void close() throws IOException {
}
}

private static class HttpDeleteWithBody extends HttpEntityEnclosingRequestBase {
public HttpDeleteWithBody() {
}

public HttpDeleteWithBody(URI uri) {
this.setURI(uri);
}

public HttpDeleteWithBody(String uri) {
this.setURI(URI.create(uri));
}

public String getMethod() {
return HttpDelete.METHOD_NAME;
private static class HttpDeleteWithBody extends HttpUriRequestBase {
public HttpDeleteWithBody(String url) {
super(HttpDelete.METHOD_NAME, URI.create(url));
}
}



public interface CloseableHttpClientBuilder {
CloseableHttpClient build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import io.weaviate.client.base.WeaviateErrorMessage;
import io.weaviate.client.base.WeaviateErrorResponse;
import io.weaviate.client.base.http.HttpClient;
import org.apache.http.HttpStatus;
import org.apache.hc.core5.http.HttpStatus;

public class ObjectsChecker extends BaseClient<String> implements ClientResult<Boolean> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import io.weaviate.client.base.WeaviateErrorResponse;
import io.weaviate.client.v1.schema.model.WeaviateClass;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpStatus;
import org.apache.hc.core5.http.HttpStatus;

import java.util.Collections;

Expand Down
12 changes: 6 additions & 6 deletions src/main/java/io/weaviate/client/v1/schema/api/ShardUpdater.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
package io.weaviate.client.v1.schema.api;

import io.weaviate.client.v1.schema.model.ShardStatus;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpStatus;
import io.weaviate.client.Config;
import io.weaviate.client.base.BaseClient;
import io.weaviate.client.base.ClientResult;
Expand All @@ -14,6 +8,12 @@
import io.weaviate.client.base.WeaviateErrorMessage;
import io.weaviate.client.base.WeaviateErrorResponse;
import io.weaviate.client.base.http.HttpClient;
import io.weaviate.client.v1.schema.model.ShardStatus;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.apache.hc.core5.http.HttpStatus;

public class ShardUpdater extends BaseClient<ShardStatus> implements ClientResult<ShardStatus> {
private String className;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package io.weaviate.client.v1.schema.api;

import io.weaviate.client.v1.schema.model.Shard;
import java.util.Collections;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpStatus;
import io.weaviate.client.Config;
import io.weaviate.client.base.BaseClient;
import io.weaviate.client.base.ClientResult;
Expand All @@ -12,6 +8,10 @@
import io.weaviate.client.base.WeaviateErrorMessage;
import io.weaviate.client.base.WeaviateErrorResponse;
import io.weaviate.client.base.http.HttpClient;
import io.weaviate.client.v1.schema.model.Shard;
import java.util.Collections;
import org.apache.commons.lang3.StringUtils;
import org.apache.hc.core5.http.HttpStatus;

public class ShardsGetter extends BaseClient<Shard[]> implements ClientResult<Shard[]> {
private String className;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import java.util.Collections;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpStatus;
import org.apache.hc.core5.http.HttpStatus;
import io.weaviate.client.Config;
import io.weaviate.client.base.ClientResult;
import io.weaviate.client.base.Result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import io.weaviate.client.base.util.DbVersionSupport;
import io.weaviate.client.base.util.UrlEncoder;
import io.weaviate.client.v1.schema.model.Tenant;
import org.apache.http.HttpStatus;
import org.apache.hc.core5.http.HttpStatus;

public class TenantsCreator extends BaseClient<Tenant[]> implements ClientResult<Boolean> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import io.weaviate.client.base.Result;
import io.weaviate.client.base.http.HttpClient;
import io.weaviate.client.base.util.UrlEncoder;
import org.apache.http.HttpStatus;
import org.apache.hc.core5.http.HttpStatus;

public class TenantsDeleter extends BaseClient<Object> implements ClientResult<Boolean> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import io.weaviate.client.base.Result;
import io.weaviate.client.base.http.HttpClient;
import io.weaviate.client.base.util.UrlEncoder;
import org.apache.http.HttpStatus;
import org.apache.hc.core5.http.HttpStatus;

public class TenantsExists extends BaseClient<Object> implements ClientResult<Boolean> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.apache.http.HttpStatus;
import org.apache.hc.core5.http.HttpStatus;

public class TenantsUpdater extends BaseClient<Tenant[]> implements ClientResult<Boolean> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public void test503Case() {
.verify(
request()
.withPath(OIDC_PATH),
VerificationTimes.exactly(3)
VerificationTimes.exactly(6)
);
}

Expand Down
Loading

0 comments on commit 36d7649

Please sign in to comment.