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
12 changes: 12 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,18 @@
<build>
<finalName>flagsmith-java-client-${project.version}</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.3.0</version>
<configuration>
<archive>
<manifest>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.jsonschema2pojo</groupId>
<artifactId>jsonschema2pojo-maven-plugin</artifactId>
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/flagsmith/FlagsmithApiWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
public class FlagsmithApiWrapper implements FlagsmithSdk {

private static final String AUTH_HEADER = "X-Environment-Key";
private static final String USER_AGENT_HEADER = "User-Agent";
private static final String ACCEPT_HEADER = "Accept";
private static final Integer TIMEOUT = 15000;

Expand Down Expand Up @@ -300,6 +301,7 @@ public FlagsmithLogger getLogger() {
private Request.Builder newRequestBuilder() {
final Request.Builder builder = new Request.Builder()
.header(AUTH_HEADER, apiKey)
.header(USER_AGENT_HEADER, "flagsmith-java-sdk/" + Versions.getVersion())
.addHeader(ACCEPT_HEADER, "application/json");

if (this.customHeaders != null && !this.customHeaders.isEmpty()) {
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/com/flagsmith/Versions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.flagsmith;

public final class Versions {
private Versions() {}

public static String getVersion() {
String version = Versions.class.getPackage().getImplementationVersion();
return version != null ? version : "unknown";
}
}
5 changes: 5 additions & 0 deletions src/test/java/com/flagsmith/FlagsmithApiWrapperTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

import com.flagsmith.threads.AnalyticsProcessor;
import com.flagsmith.threads.RequestProcessor;
Expand Down Expand Up @@ -92,6 +93,8 @@ public void getFeatureFlags_noUser_success() throws JsonProcessingException {
// Arrange
interceptor.addRule()
.get(BASE_URL + "/flags/")
.headerMatches("X-Environment-Key", Pattern.compile(API_KEY))
.headerMatches("User-Agent", Pattern.compile("flagsmith-java-sdk/.*"))
.respond(mapper.writeValueAsString(Arrays.asList(getNewFlag())), MEDIATYPE_JSON);

// Act
Expand Down Expand Up @@ -128,6 +131,8 @@ public void identifyUserWithTraits_success() throws JsonProcessingException {
String responseBody = mapper.writeValueAsString(getFlagsAndTraitsResponse(Arrays.asList(getNewFlag()), Arrays.asList(new TraitModel())));
interceptor.addRule()
.post(BASE_URL + "/identities/")
.headerMatches("X-Environment-Key", Pattern.compile(API_KEY))
.headerMatches("User-Agent", Pattern.compile("flagsmith-java-sdk/.*"))
.respond(responseBody, MEDIATYPE_JSON);

// Act
Expand Down
24 changes: 23 additions & 1 deletion src/test/java/com/flagsmith/FlagsmithClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand Down Expand Up @@ -125,6 +126,8 @@ public void testClient_errorEnvironmentApi() {

interceptor.addRule()
.get(baseUrl + "/environment-document/")
.headerMatches("X-Environment-Key", Pattern.compile("api-key"))
.headerMatches("User-Agent", Pattern.compile("flagsmith-java-sdk/.*"))
.respond(
500,
ResponseBody.create("error", MEDIATYPE_JSON));
Expand Down Expand Up @@ -162,6 +165,8 @@ public void testClient_validateEnvironment()

interceptor.addRule()
.get(baseUrl + "/environment-document/")
.headerMatches("X-Environment-Key", Pattern.compile("api-key"))
.headerMatches("User-Agent", Pattern.compile("flagsmith-java-sdk/.*"))
.anyTimes()
.respond(
FlagsmithTestHelper.environmentString(),
Expand All @@ -188,6 +193,8 @@ public void testClient_flagsApiException()

interceptor.addRule()
.get(baseUrl + "/flags/")
.headerMatches("X-Environment-Key", Pattern.compile("api-key"))
.headerMatches("User-Agent", Pattern.compile("flagsmith-java-sdk/.*"))
.respond(
500,
ResponseBody.create("error", MEDIATYPE_JSON));
Expand All @@ -211,6 +218,8 @@ public void testClient_flagsApiEmpty()

interceptor.addRule()
.get(baseUrl + "/flags/")
.headerMatches("X-Environment-Key", Pattern.compile("api-key"))
.headerMatches("User-Agent", Pattern.compile("flagsmith-java-sdk/.*"))
.respond(
"[]",
MEDIATYPE_JSON);
Expand Down Expand Up @@ -238,6 +247,8 @@ public void testClient_flagsApi()

interceptor.addRule()
.get(baseUrl + "/flags/")
.headerMatches("X-Environment-Key", Pattern.compile("api-key"))
.headerMatches("User-Agent", Pattern.compile("flagsmith-java-sdk/.*"))
.respond(
MapperFactory.getMapper().writeValueAsString(featureStateModel),
MEDIATYPE_JSON);
Expand All @@ -264,6 +275,8 @@ public void testClient_identityFlagsApiNoTraitsException() throws FlagsmithClien

interceptor.addRule()
.post(baseUrl + "/identities/")
.headerMatches("X-Environment-Key", Pattern.compile("api-key"))
.headerMatches("User-Agent", Pattern.compile("flagsmith-java-sdk/.*"))
.respond(
500,
ResponseBody.create("error", MEDIATYPE_JSON));
Expand All @@ -289,6 +302,8 @@ public void testClient_identityFlagsApiNoTraits() throws FlagsmithClientError {

interceptor.addRule()
.post(baseUrl + "/identities/")
.headerMatches("X-Environment-Key", Pattern.compile("api-key"))
.headerMatches("User-Agent", Pattern.compile("flagsmith-java-sdk/.*"))
.respond(
json,
MEDIATYPE_JSON);
Expand Down Expand Up @@ -407,7 +422,8 @@ public void testClient_identityFlagsApiWithTraitsWithLocalEnvironment() {
.build();

interceptor.addRule()
.get(baseUrl + "/flags/").anyTimes()
.get(baseUrl + "/flags/")
.anyTimes()
.respond(500, ResponseBody.create("error", MEDIATYPE_JSON));

assertThrows(FlagsmithApiError.class,
Expand Down Expand Up @@ -443,6 +459,8 @@ public void testClient_defaultFlagWithNoEnvironment() throws FlagsmithClientErro

interceptor.addRule()
.get(baseUrl + "/flags/")
.headerMatches("X-Environment-Key", Pattern.compile("api-key"))
.headerMatches("User-Agent", Pattern.compile("flagsmith-java-sdk/.*"))
.respond(
"[]",
MEDIATYPE_JSON);
Expand Down Expand Up @@ -481,6 +499,8 @@ public void testGetIdentitySegmentsNoTraits() throws JsonProcessingException,
MockInterceptor interceptor = new MockInterceptor();
interceptor.addRule()
.get(baseUrl + "/environment-document/")
.headerMatches("X-Environment-Key", Pattern.compile("ser.abcdefg"))
.headerMatches("User-Agent", Pattern.compile("flagsmith-java-sdk/.*"))
.anyTimes()
.respond(
MapperFactory.getMapper().writeValueAsString(environmentModel),
Expand Down Expand Up @@ -514,6 +534,8 @@ public void testGetIdentitySegmentsWithValidTrait() throws JsonProcessingExcepti
MockInterceptor interceptor = new MockInterceptor();
interceptor.addRule()
.get(baseUrl + "/environment-document/")
.headerMatches("X-Environment-Key", Pattern.compile("ser.abcdefg"))
.headerMatches("User-Agent", Pattern.compile("flagsmith-java-sdk/.*"))
.anyTimes()
.respond(
MapperFactory.getMapper().writeValueAsString(environmentModel),
Expand Down