Skip to content

Commit cb3b7c2

Browse files
committed
Added a new configuration param to override the default GetStream.io regional endpoint
1 parent 1650dab commit cb3b7c2

File tree

5 files changed

+55
-2
lines changed

5 files changed

+55
-2
lines changed

stream-core/src/main/java/io/getstream/client/config/ClientConfiguration.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,14 @@ public class ClientConfiguration {
5252
*/
5353
private String personalizedFeedEndpoint;
5454

55+
/**
56+
* Set a custom endpoint.
57+
* If set the client will use the given endpoint instead of the default one associated with
58+
* the selected region.
59+
* It is usually not needed to provide an endpoint here.
60+
*/
61+
private String defaultEndpoint;
62+
5563

5664
private AuthenticationHandlerConfiguration authenticationHandlerConfiguration;
5765

@@ -155,4 +163,12 @@ public String getPersonalizedFeedEndpoint() {
155163
public void setPersonalizedFeedEndpoint(String personalizedFeedEndpoint) {
156164
this.personalizedFeedEndpoint = personalizedFeedEndpoint;
157165
}
166+
167+
public String getDefaultEndpoint() {
168+
return defaultEndpoint;
169+
}
170+
171+
public void setDefaultEndpoint(String defaultEndpoint) {
172+
this.defaultEndpoint = defaultEndpoint;
173+
}
158174
}

stream-core/src/main/java/io/getstream/client/util/EndpointUtil.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,21 @@ public static URI getPersonalizedEndpoint(ClientConfiguration streamClient) {
3838
throw new UriBuilderException("Malformed personalized feed's URL.");
3939
}
4040
}
41+
42+
/**
43+
* Build the base endpoint.
44+
* @param streamClient Configuration container
45+
* @return A valid URI endpoint
46+
* @throws UriBuilderException In case the URI is malformed
47+
*/
48+
public static URI getBaseEndpoint(final ClientConfiguration streamClient) throws UriBuilderException {
49+
if (null != streamClient.getDefaultEndpoint()) {
50+
try {
51+
return new URI(streamClient.getDefaultEndpoint());
52+
} catch (URISyntaxException e) {
53+
throw new UriBuilderException("Malformed GetStream.io base URL.");
54+
}
55+
}
56+
return streamClient.getRegion().getEndpoint();
57+
}
4158
}

stream-core/src/test/java/io/getstream/client/util/EndpointUtilTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.getstream.client.util;
22

33
import io.getstream.client.config.ClientConfiguration;
4+
import io.getstream.client.config.StreamRegion;
45
import org.junit.Test;
56

67
import java.net.URI;
@@ -28,6 +29,23 @@ public void shouldGetPersonalizedEndpointWithoutDoubleSlash() throws Exception {
2829
assertThat(personalizedEndpoint.toString(), is("http://yourcompany.getstream.io/yourcompany/"));
2930
}
3031

32+
@Test
33+
public void shouldGetCustomEndpoint() throws Exception {
34+
ClientConfiguration clientConfiguration = new ClientConfiguration();
35+
clientConfiguration.setDefaultEndpoint("http://www.example.com/v1");
36+
37+
URI personalizedEndpoint = EndpointUtil.getBaseEndpoint(clientConfiguration);
38+
assertThat(personalizedEndpoint.toString(), is("http://www.example.com/v1"));
39+
}
40+
41+
@Test
42+
public void shouldGetRegionDefaultEndpoint() throws Exception {
43+
ClientConfiguration clientConfiguration = new ClientConfiguration(StreamRegion.US_EAST);
44+
45+
URI personalizedEndpoint = EndpointUtil.getBaseEndpoint(clientConfiguration);
46+
assertThat(personalizedEndpoint.toString(), is(StreamRegion.US_EAST.getEndpoint().toString()));
47+
}
48+
3149
@Test(expected = NullPointerException.class)
3250
public void shouldFail() throws Exception {
3351
ClientConfiguration clientConfiguration = new ClientConfiguration();

stream-repo-apache/src/main/java/io/getstream/client/apache/repo/StreamRepositoryImpl.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import io.getstream.client.model.feeds.BaseFeed;
2121
import io.getstream.client.model.filters.FeedFilter;
2222
import io.getstream.client.repo.StreamRepository;
23+
import io.getstream.client.util.EndpointUtil;
2324
import io.getstream.client.util.JwtAuthenticationUtil;
2425
import org.apache.http.client.methods.CloseableHttpResponse;
2526
import org.apache.http.client.methods.HttpDelete;
@@ -67,7 +68,7 @@ public class StreamRepositoryImpl implements StreamRepository {
6768
*/
6869
public StreamRepositoryImpl(ObjectMapper objectMapper, ClientConfiguration streamClient, CloseableHttpClient closeableHttpClient) {
6970
this.objectMapper = objectMapper;
70-
this.baseEndpoint = streamClient.getRegion().getEndpoint();
71+
this.baseEndpoint = EndpointUtil.getBaseEndpoint(streamClient);
7172
this.apiKey = streamClient.getAuthenticationHandlerConfiguration().getApiKey();
7273
this.secretKey = streamClient.getAuthenticationHandlerConfiguration().getSecretKey();
7374
this.exceptionHandler = new StreamExceptionHandler(objectMapper);

stream-repo-okhttp/src/main/java/io/getstream/client/okhttp/repo/StreamRepositoryImpl.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import io.getstream.client.okhttp.repo.utils.StreamRepoUtils;
2727
import io.getstream.client.okhttp.repo.utils.UriBuilder;
2828
import io.getstream.client.repo.StreamRepository;
29+
import io.getstream.client.util.EndpointUtil;
2930
import io.getstream.client.util.HttpSignatureHandler;
3031
import io.getstream.client.util.JwtAuthenticationUtil;
3132
import org.slf4j.Logger;
@@ -64,7 +65,7 @@ public class StreamRepositoryImpl implements StreamRepository {
6465
* @param closeableHttpClient Actual instance of OkHTTP client
6566
*/
6667
public StreamRepositoryImpl(ObjectMapper objectMapper, ClientConfiguration streamClient, OkHttpClient closeableHttpClient) {
67-
this.baseEndpoint = streamClient.getRegion().getEndpoint();
68+
this.baseEndpoint = EndpointUtil.getBaseEndpoint(streamClient);
6869
this.apiKey = streamClient.getAuthenticationHandlerConfiguration().getApiKey();
6970
this.secretKey = streamClient.getAuthenticationHandlerConfiguration().getSecretKey();
7071
this.exceptionHandler = new StreamExceptionHandler(objectMapper);

0 commit comments

Comments
 (0)