Skip to content
Merged
Show file tree
Hide file tree
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
16 changes: 13 additions & 3 deletions src/main/java/se/michaelthelin/spotify/SpotifyApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ public class SpotifyApi {
public static final String DEFAULT_HOST = "api.spotify.com";

/**
* A HttpManager configured with default settings.
* A HttpManager Builder ready to build SpotifyHttpManager
*/
public static final IHttpManager DEFAULT_HTTP_MANAGER = new SpotifyHttpManager.Builder().build();
public static final SpotifyHttpManager.Builder shmb = new SpotifyHttpManager.Builder();
Copy link
Member

Choose a reason for hiding this comment

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

Please try to name variables without abbreviations.


/**
* The default port of Spotify API calls.
Expand Down Expand Up @@ -1885,7 +1885,8 @@ public GetUsersProfileRequest.Builder getUsersProfile(String user_id) {
*/
public static class Builder {

private IHttpManager httpManager = DEFAULT_HTTP_MANAGER;
private SpotifyHttpManager.Builder spotifyHttpManagerBuilder = new SpotifyHttpManager.Builder();
private IHttpManager httpManager = null;
private String scheme = DEFAULT_SCHEME;
private String host = DEFAULT_HOST;
private Integer port = DEFAULT_PORT;
Expand All @@ -1898,6 +1899,7 @@ public static class Builder {
private URI redirectUri;
private String accessToken;
private String refreshToken;
private boolean usePoolingConnectionManager = false;

/**
* The HttpManager setter.
Expand Down Expand Up @@ -2042,12 +2044,20 @@ public Builder setRefreshToken(String refreshToken) {
return this;
}

public Builder setPoolingConnectionManager() {
this.usePoolingConnectionManager = true;
return this;
}

/**
* Build a {@link SpotifyApi} instance with the information given to the builder.
*
* @return A {@link SpotifyApi} instance.
*/
public SpotifyApi build() {
if (usePoolingConnectionManager)
spotifyHttpManagerBuilder.setPoolingConnectionManager();
httpManager = spotifyHttpManagerBuilder.build();
return new SpotifyApi(this);
}
}
Expand Down
27 changes: 25 additions & 2 deletions src/main/java/se/michaelthelin/spotify/SpotifyHttpManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
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.impl.io.PoolingHttpClientConnectionManager;
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 @@ -45,6 +47,7 @@ public class SpotifyHttpManager implements IHttpManager {
private final Integer connectionRequestTimeout;
private final Integer connectTimeout;
private final Integer socketTimeout;
private final boolean usePooledConnectionManager;

/**
* Construct a new SpotifyHttpManager instance.
Expand All @@ -59,6 +62,8 @@ public SpotifyHttpManager(Builder builder) {
this.connectionRequestTimeout = builder.connectionRequestTimeout;
this.connectTimeout = builder.connectTimeout;
this.socketTimeout = builder.socketTimeout;
this.usePooledConnectionManager = builder.usePoolingConnectionManager;
HttpClientConnectionManager connectionManager = null;

CacheConfig cacheConfig = CacheConfig.custom()
.setMaxCacheEntries(cacheMaxEntries != null ? cacheMaxEntries : DEFAULT_CACHE_MAX_ENTRIES)
Expand All @@ -81,8 +86,20 @@ public SpotifyHttpManager(Builder builder) {
? Timeout.ofMilliseconds(builder.connectTimeout)
: ConnectionConfig.DEFAULT.getConnectTimeout())
.build();
BasicHttpClientConnectionManager connectionManager = new BasicHttpClientConnectionManager();
connectionManager.setConnectionConfig(connectionConfig);

if (usePooledConnectionManager) {
connectionManager = new PoolingHttpClientConnectionManager();
SpotifyApi.LOGGER.log(
Level.FINE,
"Using PoolingHttpClientConnectionManager.");
} else {
connectionManager = new BasicHttpClientConnectionManager();
((BasicHttpClientConnectionManager) connectionManager).setConnectionConfig(connectionConfig);
SpotifyApi.LOGGER.log(
Level.FINE,
"Using BasicHttpClientConnectionManager.");
}

RequestConfig requestConfig = RequestConfig
.custom()
.setCookieSpec(StandardCookieSpec.STRICT)
Expand Down Expand Up @@ -366,6 +383,7 @@ public static class Builder {
private Integer connectionRequestTimeout;
private Integer connectTimeout;
private Integer socketTimeout;
private boolean usePoolingConnectionManager = false;

public Builder setProxy(HttpHost proxy) {
this.proxy = proxy;
Expand Down Expand Up @@ -402,6 +420,11 @@ public Builder setSocketTimeout(Integer socketTimeout) {
return this;
}

public Builder setPoolingConnectionManager() {
this.usePoolingConnectionManager = true;
return this;
}

public SpotifyHttpManager build() {
return new SpotifyHttpManager(this);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ public static abstract class Builder<T, BT extends Builder<T, ?>> implements IRe
private final List<NameValuePair> queryParameters = new ArrayList<>();
private final List<Header> headers = new ArrayList<>();
private final List<NameValuePair> bodyParameters = new ArrayList<>();
private IHttpManager httpManager = SpotifyApi.DEFAULT_HTTP_MANAGER;
private IHttpManager httpManager = null;
private String scheme = SpotifyApi.DEFAULT_SCHEME;
private String host = SpotifyApi.DEFAULT_HOST;
private Integer port = SpotifyApi.DEFAULT_PORT;
Expand Down