Skip to content

Commit b884e4e

Browse files
committed
Merge branch 'master' into feat/update-dot-com-domain
2 parents 020608b + 01f6ca6 commit b884e4e

File tree

15 files changed

+350
-25
lines changed

15 files changed

+350
-25
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@ If you decide to go for the *Apache HttpClient* implementation, add the followin
2020
<dependency>
2121
<groupId>io.getstream.client</groupId>
2222
<artifactId>stream-repo-apache</artifactId>
23-
<version>1.3.2</version>
23+
<version>1.3.3</version>
2424
</dependency>
2525
```
2626

2727
or in your build.gradle:
2828

2929
```gradle
30-
compile 'io.getstream.client:stream-repo-apache:1.3.2'
30+
compile 'io.getstream.client:stream-repo-apache:1.3.3'
3131
```
3232

3333
Instead, if you opted for the *OkHttp* implementation please add it to your pom.xml
@@ -36,14 +36,14 @@ Instead, if you opted for the *OkHttp* implementation please add it to your pom.
3636
<dependency>
3737
<groupId>io.getstream.client</groupId>
3838
<artifactId>stream-repo-okhttp</artifactId>
39-
<version>1.3.2</version>
39+
<version>1.3.3</version>
4040
</dependency>
4141
```
4242

4343
or in your build.gradle:
4444

4545
```gradle
46-
compile 'io.getstream.client:stream-repo-okhttp:1.3.2'
46+
compile 'io.getstream.client:stream-repo-okhttp:1.3.3'
4747
```
4848

4949
In case you want to download the artifact and put it manually into your project,

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<groupId>io.getstream.client</groupId>
66
<artifactId>stream-java</artifactId>
77
<packaging>pom</packaging>
8-
<version>1.3.4-SNAPSHOT</version>
8+
<version>1.3.5-SNAPSHOT</version>
99

1010
<name>stream-java</name>
1111
<description>stream-java is a Java client for http://getstream.io.</description>

stream-core/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<parent>
44
<artifactId>stream-java</artifactId>
55
<groupId>io.getstream.client</groupId>
6-
<version>1.3.4-SNAPSHOT</version>
6+
<version>1.3.5-SNAPSHOT</version>
77
</parent>
88
<modelVersion>4.0.0</modelVersion>
99

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
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package io.getstream.client.exception;
2+
3+
/**
4+
* In case you have exceed your allowed calls per time interval (i.e. 1min, 15min or 1hr)
5+
*/
6+
public class RateLimitExceededException extends StreamClientException {
7+
8+
public RateLimitExceededException() {
9+
super();
10+
}
11+
12+
public RateLimitExceededException(String message) {
13+
super(message);
14+
}
15+
16+
public RateLimitExceededException(String message, Throwable cause) {
17+
super(message, cause);
18+
}
19+
20+
public RateLimitExceededException(Throwable cause) {
21+
super(cause);
22+
}
23+
24+
protected RateLimitExceededException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
25+
super(message, cause, enableSuppression, writableStackTrace);
26+
}
27+
}

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/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<parent>
44
<artifactId>stream-java</artifactId>
55
<groupId>io.getstream.client</groupId>
6-
<version>1.3.4-SNAPSHOT</version>
6+
<version>1.3.5-SNAPSHOT</version>
77
</parent>
88
<modelVersion>4.0.0</modelVersion>
99

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-apache/src/main/java/io/getstream/client/apache/repo/handlers/StreamExceptionHandler.java

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
import com.fasterxml.jackson.databind.ObjectMapper;
44
import io.getstream.client.exception.AuthenticationFailedException;
5-
import io.getstream.client.exception.InvalidOrMissingInputException;
6-
import io.getstream.client.exception.StreamClientException;
75
import io.getstream.client.exception.InternalServerException;
6+
import io.getstream.client.exception.InvalidOrMissingInputException;
7+
import io.getstream.client.exception.RateLimitExceededException;
88
import io.getstream.client.exception.ResourceNotFoundException;
9+
import io.getstream.client.exception.StreamClientException;
910
import io.getstream.client.model.beans.StreamErrorResponse;
1011
import org.apache.http.client.methods.CloseableHttpResponse;
11-
import org.apache.http.util.EntityUtils;
1212

1313
import java.io.IOException;
1414

@@ -38,7 +38,15 @@ public StreamExceptionHandler(ObjectMapper objectMapper) {
3838
* @throws StreamClientException in case of functional or server-side exception
3939
*/
4040
public void handleResponseCode(final CloseableHttpResponse response) throws IOException, StreamClientException {
41-
switch (response.getStatusLine().getStatusCode()) {
41+
int statusCode = response.getStatusLine().getStatusCode();
42+
if (statusCode < 200 || statusCode > 299) {
43+
parseException(response);
44+
}
45+
}
46+
47+
private void parseException(final CloseableHttpResponse response) throws IOException, StreamClientException {
48+
int statusCode = response.getStatusLine().getStatusCode();
49+
switch (statusCode) {
4250
case 400:
4351
throw buildException(new InvalidOrMissingInputException(), response);
4452
case 401:
@@ -47,19 +55,27 @@ public void handleResponseCode(final CloseableHttpResponse response) throws IOEx
4755
throw buildException(new AuthenticationFailedException(), response);
4856
case 404:
4957
throw buildException(new ResourceNotFoundException(), response);
58+
case 429:
59+
throw buildException(new RateLimitExceededException(), response);
5060
case 500:
5161
throw buildException(new InternalServerException(), response);
62+
default:
63+
StreamClientException e = new InternalServerException();
64+
e.setCode(statusCode);
65+
e.setHttpStatusCode(statusCode);
66+
throw e;
5267
}
5368
}
5469

5570
private StreamClientException buildException(StreamClientException exception,
5671
CloseableHttpResponse response) throws IOException {
57-
String responseMessage = EntityUtils.toString(response.getEntity());
58-
StreamErrorResponse error = objectMapper.readValue(responseMessage, StreamErrorResponse.class);
59-
exception.setCode(error.getCode());
60-
exception.setHttpStatusCode(error.getStatusCode());
61-
exception.setDetail(error.getDetail());
62-
exception.setExceptionField(error.getException());
72+
StreamErrorResponse error = objectMapper.readValue(response.getEntity().getContent(), StreamErrorResponse.class);
73+
if (null != error) {
74+
exception.setCode(error.getCode());
75+
exception.setHttpStatusCode(response.getStatusLine().getStatusCode());
76+
exception.setDetail(error.getDetail());
77+
exception.setExceptionField(error.getException());
78+
}
6379
return exception;
6480
}
6581
}

0 commit comments

Comments
 (0)