Skip to content

Commit

Permalink
Add support for selection of protocols (#154)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
olivier-hubaut and Olivier Hubaut authored Aug 19, 2024
1 parent 88b7e55 commit 4a7d739
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 5 deletions.
45 changes: 40 additions & 5 deletions src/main/java/com/flagsmith/config/FlagsmithConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.X509TrustManager;
import lombok.Getter;
Expand Down Expand Up @@ -55,13 +56,19 @@ protected FlagsmithConfig(Builder builder) {
.writeTimeout(builder.writeTimeoutMillis, TimeUnit.MILLISECONDS)
.readTimeout(builder.readTimeoutMillis, TimeUnit.MILLISECONDS);
if (builder.sslSocketFactory != null && builder.trustManager != null) {
httpBuilder = httpBuilder.sslSocketFactory(builder.sslSocketFactory, builder.trustManager);
httpBuilder.sslSocketFactory(builder.sslSocketFactory, builder.trustManager);
}
for (final Interceptor interceptor : builder.interceptors) {
httpBuilder = httpBuilder.addInterceptor(interceptor);
httpBuilder.addInterceptor(interceptor);
}
if (builder.proxy != null) {
httpBuilder = httpBuilder.proxy(builder.proxy);
httpBuilder.proxy(builder.proxy);
}
if (!builder.supportedProtocols.isEmpty()) {
httpBuilder.protocols(
builder.supportedProtocols.stream()
.map(Protocol::internalProtocol)
.collect(Collectors.toList()));
}
this.httpClient = httpBuilder.build();

Expand Down Expand Up @@ -97,6 +104,7 @@ public static class Builder {
private int connectTimeoutMillis = DEFAULT_CONNECT_TIMEOUT_MILLIS;
private int writeTimeoutMillis = DEFAULT_WRITE_TIMEOUT_MILLIS;
private int readTimeoutMillis = DEFAULT_READ_TIMEOUT_MILLIS;
private List<Protocol> supportedProtocols = new ArrayList<>();
private Retry retries = new Retry(3);
private SSLSocketFactory sslSocketFactory;
private X509TrustManager trustManager;
Expand Down Expand Up @@ -216,8 +224,8 @@ public Builder withLocalEvaluation(Boolean localEvaluation) {
}

/**
* set environment refresh rate with polling manager. Only needed when local
* evaluation is true.
* set environment refresh rate with polling manager. Only needed when local evaluation is
* true.
*
* @param seconds seconds
*/
Expand Down Expand Up @@ -267,8 +275,35 @@ public Builder withOfflineHandler(IOfflineHandler offlineHandler) {
return this;
}

/**
* Specify the list of protocols supported for calls to the server.
* @param supportedProtocols the list of supported protocols
* @return
*/
public Builder withSupportedProtocols(List<Protocol> supportedProtocols) {
this.supportedProtocols.clear();
this.supportedProtocols.addAll(supportedProtocols);
return this;
}

public FlagsmithConfig build() {
return new FlagsmithConfig(this);
}
}

// This enum prevents leakage of the underlying HTTP client implementation details.
enum Protocol {
HTTP_1_1(okhttp3.Protocol.HTTP_1_1),
HTTP_2(okhttp3.Protocol.HTTP_2);

private final okhttp3.Protocol protocol;

Protocol(okhttp3.Protocol protocol) {
this.protocol = protocol;
}

okhttp3.Protocol internalProtocol() {
return protocol;
}
}
}
15 changes: 15 additions & 0 deletions src/test/java/com/flagsmith/config/FlagsmithConfigTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import com.flagsmith.config.FlagsmithConfig.Protocol;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.util.Collections;
import okhttp3.mock.MockInterceptor;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -50,4 +52,17 @@ public void configTest_multipleInterceptors() {

assertEquals(2, flagsmithConfig.getHttpClient().interceptors().size());
}

@Test
public void configTest_supportedProtocols() {
final FlagsmithConfig defaultFlagsmithConfig = FlagsmithConfig.newBuilder().build();

assertEquals(2, defaultFlagsmithConfig.getHttpClient().protocols().size());

final FlagsmithConfig customFlagsmithConfig = FlagsmithConfig.newBuilder().withSupportedProtocols(
Collections.singletonList(Protocol.HTTP_1_1)).build();

assertEquals(1, customFlagsmithConfig.getHttpClient().protocols().size());
assertEquals(okhttp3.Protocol.HTTP_1_1, customFlagsmithConfig.getHttpClient().protocols().get(0));
}
}

0 comments on commit 4a7d739

Please sign in to comment.