Skip to content

Commit

Permalink
Merge pull request #61 from ActiveCampaign/http-client-updates
Browse files Browse the repository at this point in the history
Http client updates
  • Loading branch information
ibalosh authored Dec 19, 2023
2 parents dff32c1 + 9b4cd11 commit f8a3620
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 22 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 1.11.1

* small updated to http client connection settings

## 1.11.0

* Add data removal endpoints
Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
</developers>

<properties>
<postmark.version>1.11.0</postmark.version>
<postmark.version>1.11.1</postmark.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<jackson.minimum.version>2.9.7</jackson.minimum.version>
Expand Down Expand Up @@ -51,7 +51,7 @@
<dependency>
<groupId>org.apache.httpcomponents.client5</groupId>
<artifactId>httpclient5</artifactId>
<version>5.2.1</version>
<version>5.3</version>
</dependency>

<!-- Serialization to JSON -->
Expand Down
20 changes: 11 additions & 9 deletions src/main/java/com/postmarkapp/postmark/client/HttpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import org.apache.hc.client5.http.config.RequestConfig;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.HttpClientBuilder;
import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager;
import org.apache.hc.core5.http.ClassicHttpRequest;
import org.apache.hc.core5.http.ContentType;
import org.apache.hc.core5.http.HttpEntity;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.apache.hc.core5.http.io.support.ClassicRequestBuilder;
import org.apache.hc.core5.util.Timeout;
Expand Down Expand Up @@ -36,21 +36,17 @@ public enum DEFAULTS {
// client configuration options like timeouts, forwarding ..
private RequestConfig.Builder clientConfigBuilder;

private final PoolingHttpClientConnectionManager connectionManager;

private boolean secureConnection = true;

public HttpClient(Map<String,Object> headers, int connectTimeoutSeconds, int readTimeoutSeconds) {
this.headers = headers;
this.clientConfigBuilder = RequestConfig
.custom()
.setConnectTimeout(Timeout.ofSeconds(connectTimeoutSeconds))
.setConnectionRequestTimeout(Timeout.ofSeconds(connectTimeoutSeconds))
.setConnectionKeepAlive(Timeout.ofSeconds(connectTimeoutSeconds))
.setResponseTimeout(Timeout.ofSeconds(readTimeoutSeconds));

this.connectionManager = new PoolingHttpClientConnectionManager();
connectionManager.setMaxTotal(100);
connectionManager.setDefaultMaxPerRoute(25);

this.client = buildClient();
}

Expand Down Expand Up @@ -89,7 +85,14 @@ public ClientResponse execute(REQUEST_TYPES requestType, String url, String data

return client.execute(
request,
response -> new ClientResponse(response.getCode(), EntityUtils.toString(response.getEntity())));
response -> {
final HttpEntity entity = response.getEntity();
int code = response.getCode();
String body = EntityUtils.toString(response.getEntity());
EntityUtils.consume(entity);

return new ClientResponse(code, body);
});
}

/**
Expand Down Expand Up @@ -154,7 +157,6 @@ private CloseableHttpClient buildClient() {
return HttpClientBuilder
.create()
.setDefaultRequestConfig(clientConfigBuilder.build())
.setConnectionManager(connectionManager)
.build();
}

Expand Down
6 changes: 3 additions & 3 deletions src/test/java/integration/MessageStreamsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,16 @@ void edit() throws PostmarkException, IOException {

@Test
void archive() throws PostmarkException, IOException {
String streamId = "bulk-test-stream-1201";
String streamId = "test-bulk-test-stream-1201";
MessageStreams messages = client.getMessageStreams(Parameters.init().build("messageStreamType", "all")
.build("includeArchivedStreams", "true"));

MessageStream foundStream = findMessageStream(streamId);

if (foundStream == null) {
client.createMessageStream(new MessageStream(streamId, streamId,"Broadcasts"));
MessageStream messageStream = new MessageStream(streamId, streamId,"Broadcasts");
client.createMessageStream(messageStream);
}

else if (foundStream.getArchivedAt() != null) {
client.unarchiveMessageStream(streamId);
}
Expand Down
8 changes: 0 additions & 8 deletions src/test/java/integration/MessageTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,6 @@ void send() throws PostmarkException, IOException {
assertEquals(response.getMessageId().length(),36);
}

@Test
void invalidMessagetoSend() throws PostmarkException, IOException {
Message message = new Message("[email protected]", null, "Hello from Postmark!", "Hello body");

Throwable exception = assertThrows(InvalidMessageException.class, () -> client.deliverMessage(message));
assertEquals("Zero recipients specified", exception.getMessage());
}

@Test
void invalidApiToken() throws PostmarkException, IOException {
ApiClient client = Postmark.getApiClient("1991892", true);
Expand Down

0 comments on commit f8a3620

Please sign in to comment.