Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 25 additions & 8 deletions src/main/java/se/michaelthelin/spotify/SpotifyHttpManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.client5.http.impl.io.BasicHttpClientConnectionManager;
import org.apache.hc.client5.http.io.HttpClientConnectionManager;
import org.apache.hc.core5.http.*;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.apache.hc.core5.util.Timeout;
Expand All @@ -38,6 +39,7 @@ public class SpotifyHttpManager implements IHttpManager {
private static final Gson GSON = new Gson();
private final CloseableHttpClient httpClient;
private final CloseableHttpClient httpClientCaching;
private final HttpClientConnectionManager connectionManager;
private final HttpHost proxy;
private final UsernamePasswordCredentials proxyCredentials;
private final Integer cacheMaxEntries;
Expand Down Expand Up @@ -75,14 +77,19 @@ public SpotifyHttpManager(Builder builder) {
);
}

ConnectionConfig connectionConfig = ConnectionConfig
.custom()
.setConnectTimeout(builder.connectTimeout != null
? Timeout.ofMilliseconds(builder.connectTimeout)
: ConnectionConfig.DEFAULT.getConnectTimeout())
.build();
BasicHttpClientConnectionManager connectionManager = new BasicHttpClientConnectionManager();
connectionManager.setConnectionConfig(connectionConfig);
if (builder.connectionManager != null) {
this.connectionManager = builder.connectionManager;
} else {
BasicHttpClientConnectionManager basicHttpClientConnectionManager = new BasicHttpClientConnectionManager();
basicHttpClientConnectionManager.setConnectionConfig(ConnectionConfig
.custom()
.setConnectTimeout(connectTimeout != null ?
Timeout.ofMilliseconds(connectTimeout) :
ConnectionConfig.DEFAULT.getConnectTimeout())
.build());
this.connectionManager = basicHttpClientConnectionManager;
}

RequestConfig requestConfig = RequestConfig
.custom()
.setCookieSpec(StandardCookieSpec.STRICT)
Expand Down Expand Up @@ -128,6 +135,10 @@ public static URI makeUri(String uriString) {
}
}

public HttpClientConnectionManager getConnectionManager() {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getConnectionManager is no longer necessary

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have those getters for all class variables right now, like getProxy, ... which we don't use in our code base either, so I just added this as well.

return connectionManager;
}

public HttpHost getProxy() {
return proxy;
}
Expand Down Expand Up @@ -359,6 +370,7 @@ private String getResponseBody(CloseableHttpResponse httpResponse) throws
}

public static class Builder {
private HttpClientConnectionManager connectionManager;
private HttpHost proxy;
private UsernamePasswordCredentials proxyCredentials;
private Integer cacheMaxEntries;
Expand All @@ -367,6 +379,11 @@ public static class Builder {
private Integer connectTimeout;
private Integer socketTimeout;

public Builder setConnectionManager(HttpClientConnectionManager connectionManager) {
this.connectionManager = connectionManager;
return this;
}

public Builder setProxy(HttpHost proxy) {
this.proxy = proxy;
return this;
Expand Down