Skip to content

Commit 4a7d739

Browse files
olivier-hubautOlivier Hubaut
andauthored
Add support for selection of protocols (#154)
* fix: Retry logic was never applied. The implement of the Call object being stateful, the retry logic was never triggered as an IllegalStateException was bypassing the normal flow. Closes: #144 * feat: Allow specification of supported HTTP protocols. The flagsmith config builder now accepts a list of supported protocols that can be used by the underlying HTTP client. Closes: #153 --------- Co-authored-by: Olivier Hubaut <[email protected]>
1 parent 88b7e55 commit 4a7d739

File tree

2 files changed

+55
-5
lines changed

2 files changed

+55
-5
lines changed

src/main/java/com/flagsmith/config/FlagsmithConfig.java

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import java.util.ArrayList;
88
import java.util.List;
99
import java.util.concurrent.TimeUnit;
10+
import java.util.stream.Collectors;
1011
import javax.net.ssl.SSLSocketFactory;
1112
import javax.net.ssl.X509TrustManager;
1213
import lombok.Getter;
@@ -55,13 +56,19 @@ protected FlagsmithConfig(Builder builder) {
5556
.writeTimeout(builder.writeTimeoutMillis, TimeUnit.MILLISECONDS)
5657
.readTimeout(builder.readTimeoutMillis, TimeUnit.MILLISECONDS);
5758
if (builder.sslSocketFactory != null && builder.trustManager != null) {
58-
httpBuilder = httpBuilder.sslSocketFactory(builder.sslSocketFactory, builder.trustManager);
59+
httpBuilder.sslSocketFactory(builder.sslSocketFactory, builder.trustManager);
5960
}
6061
for (final Interceptor interceptor : builder.interceptors) {
61-
httpBuilder = httpBuilder.addInterceptor(interceptor);
62+
httpBuilder.addInterceptor(interceptor);
6263
}
6364
if (builder.proxy != null) {
64-
httpBuilder = httpBuilder.proxy(builder.proxy);
65+
httpBuilder.proxy(builder.proxy);
66+
}
67+
if (!builder.supportedProtocols.isEmpty()) {
68+
httpBuilder.protocols(
69+
builder.supportedProtocols.stream()
70+
.map(Protocol::internalProtocol)
71+
.collect(Collectors.toList()));
6572
}
6673
this.httpClient = httpBuilder.build();
6774

@@ -97,6 +104,7 @@ public static class Builder {
97104
private int connectTimeoutMillis = DEFAULT_CONNECT_TIMEOUT_MILLIS;
98105
private int writeTimeoutMillis = DEFAULT_WRITE_TIMEOUT_MILLIS;
99106
private int readTimeoutMillis = DEFAULT_READ_TIMEOUT_MILLIS;
107+
private List<Protocol> supportedProtocols = new ArrayList<>();
100108
private Retry retries = new Retry(3);
101109
private SSLSocketFactory sslSocketFactory;
102110
private X509TrustManager trustManager;
@@ -216,8 +224,8 @@ public Builder withLocalEvaluation(Boolean localEvaluation) {
216224
}
217225

218226
/**
219-
* set environment refresh rate with polling manager. Only needed when local
220-
* evaluation is true.
227+
* set environment refresh rate with polling manager. Only needed when local evaluation is
228+
* true.
221229
*
222230
* @param seconds seconds
223231
*/
@@ -267,8 +275,35 @@ public Builder withOfflineHandler(IOfflineHandler offlineHandler) {
267275
return this;
268276
}
269277

278+
/**
279+
* Specify the list of protocols supported for calls to the server.
280+
* @param supportedProtocols the list of supported protocols
281+
* @return
282+
*/
283+
public Builder withSupportedProtocols(List<Protocol> supportedProtocols) {
284+
this.supportedProtocols.clear();
285+
this.supportedProtocols.addAll(supportedProtocols);
286+
return this;
287+
}
288+
270289
public FlagsmithConfig build() {
271290
return new FlagsmithConfig(this);
272291
}
273292
}
293+
294+
// This enum prevents leakage of the underlying HTTP client implementation details.
295+
enum Protocol {
296+
HTTP_1_1(okhttp3.Protocol.HTTP_1_1),
297+
HTTP_2(okhttp3.Protocol.HTTP_2);
298+
299+
private final okhttp3.Protocol protocol;
300+
301+
Protocol(okhttp3.Protocol protocol) {
302+
this.protocol = protocol;
303+
}
304+
305+
okhttp3.Protocol internalProtocol() {
306+
return protocol;
307+
}
308+
}
274309
}

src/test/java/com/flagsmith/config/FlagsmithConfigTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
import static org.junit.jupiter.api.Assertions.assertEquals;
55
import static org.junit.jupiter.api.Assertions.assertTrue;
66

7+
import com.flagsmith.config.FlagsmithConfig.Protocol;
78
import java.net.InetSocketAddress;
89
import java.net.Proxy;
10+
import java.util.Collections;
911
import okhttp3.mock.MockInterceptor;
1012
import org.junit.jupiter.api.Test;
1113

@@ -50,4 +52,17 @@ public void configTest_multipleInterceptors() {
5052

5153
assertEquals(2, flagsmithConfig.getHttpClient().interceptors().size());
5254
}
55+
56+
@Test
57+
public void configTest_supportedProtocols() {
58+
final FlagsmithConfig defaultFlagsmithConfig = FlagsmithConfig.newBuilder().build();
59+
60+
assertEquals(2, defaultFlagsmithConfig.getHttpClient().protocols().size());
61+
62+
final FlagsmithConfig customFlagsmithConfig = FlagsmithConfig.newBuilder().withSupportedProtocols(
63+
Collections.singletonList(Protocol.HTTP_1_1)).build();
64+
65+
assertEquals(1, customFlagsmithConfig.getHttpClient().protocols().size());
66+
assertEquals(okhttp3.Protocol.HTTP_1_1, customFlagsmithConfig.getHttpClient().protocols().get(0));
67+
}
5368
}

0 commit comments

Comments
 (0)