Skip to content

Commit 8eab4ae

Browse files
authored
feat: Send a standard User-Agent: sdk-name/version header (#187)
1 parent ddc2662 commit 8eab4ae

File tree

5 files changed

+52
-1
lines changed

5 files changed

+52
-1
lines changed

pom.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,18 @@
188188
<build>
189189
<finalName>flagsmith-java-client-${project.version}</finalName>
190190
<plugins>
191+
<plugin>
192+
<groupId>org.apache.maven.plugins</groupId>
193+
<artifactId>maven-jar-plugin</artifactId>
194+
<version>3.3.0</version>
195+
<configuration>
196+
<archive>
197+
<manifest>
198+
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
199+
</manifest>
200+
</archive>
201+
</configuration>
202+
</plugin>
191203
<plugin>
192204
<groupId>org.jsonschema2pojo</groupId>
193205
<artifactId>jsonschema2pojo-maven-plugin</artifactId>

src/main/java/com/flagsmith/FlagsmithApiWrapper.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
public class FlagsmithApiWrapper implements FlagsmithSdk {
3434

3535
private static final String AUTH_HEADER = "X-Environment-Key";
36+
private static final String USER_AGENT_HEADER = "User-Agent";
3637
private static final String ACCEPT_HEADER = "Accept";
3738
private static final Integer TIMEOUT = 15000;
3839

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

305307
if (this.customHeaders != null && !this.customHeaders.isEmpty()) {
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.flagsmith;
2+
3+
public final class Versions {
4+
private Versions() {}
5+
6+
public static String getVersion() {
7+
String version = Versions.class.getPackage().getImplementationVersion();
8+
return version != null ? version : "unknown";
9+
}
10+
}

src/test/java/com/flagsmith/FlagsmithApiWrapperTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import java.util.Arrays;
3131
import java.util.List;
3232
import java.util.Map;
33+
import java.util.regex.Pattern;
3334

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

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

133138
// Act

src/test/java/com/flagsmith/FlagsmithClientTest.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import java.util.HashMap;
4040
import java.util.List;
4141
import java.util.Map;
42+
import java.util.regex.Pattern;
4243
import java.util.stream.Collectors;
4344
import java.util.stream.Stream;
4445

@@ -125,6 +126,8 @@ public void testClient_errorEnvironmentApi() {
125126

126127
interceptor.addRule()
127128
.get(baseUrl + "/environment-document/")
129+
.headerMatches("X-Environment-Key", Pattern.compile("api-key"))
130+
.headerMatches("User-Agent", Pattern.compile("flagsmith-java-sdk/.*"))
128131
.respond(
129132
500,
130133
ResponseBody.create("error", MEDIATYPE_JSON));
@@ -162,6 +165,8 @@ public void testClient_validateEnvironment()
162165

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

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

212219
interceptor.addRule()
213220
.get(baseUrl + "/flags/")
221+
.headerMatches("X-Environment-Key", Pattern.compile("api-key"))
222+
.headerMatches("User-Agent", Pattern.compile("flagsmith-java-sdk/.*"))
214223
.respond(
215224
"[]",
216225
MEDIATYPE_JSON);
@@ -238,6 +247,8 @@ public void testClient_flagsApi()
238247

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

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

290303
interceptor.addRule()
291304
.post(baseUrl + "/identities/")
305+
.headerMatches("X-Environment-Key", Pattern.compile("api-key"))
306+
.headerMatches("User-Agent", Pattern.compile("flagsmith-java-sdk/.*"))
292307
.respond(
293308
json,
294309
MEDIATYPE_JSON);
@@ -407,7 +422,8 @@ public void testClient_identityFlagsApiWithTraitsWithLocalEnvironment() {
407422
.build();
408423

409424
interceptor.addRule()
410-
.get(baseUrl + "/flags/").anyTimes()
425+
.get(baseUrl + "/flags/")
426+
.anyTimes()
411427
.respond(500, ResponseBody.create("error", MEDIATYPE_JSON));
412428

413429
assertThrows(FlagsmithApiError.class,
@@ -443,6 +459,8 @@ public void testClient_defaultFlagWithNoEnvironment() throws FlagsmithClientErro
443459

444460
interceptor.addRule()
445461
.get(baseUrl + "/flags/")
462+
.headerMatches("X-Environment-Key", Pattern.compile("api-key"))
463+
.headerMatches("User-Agent", Pattern.compile("flagsmith-java-sdk/.*"))
446464
.respond(
447465
"[]",
448466
MEDIATYPE_JSON);
@@ -481,6 +499,8 @@ public void testGetIdentitySegmentsNoTraits() throws JsonProcessingException,
481499
MockInterceptor interceptor = new MockInterceptor();
482500
interceptor.addRule()
483501
.get(baseUrl + "/environment-document/")
502+
.headerMatches("X-Environment-Key", Pattern.compile("ser.abcdefg"))
503+
.headerMatches("User-Agent", Pattern.compile("flagsmith-java-sdk/.*"))
484504
.anyTimes()
485505
.respond(
486506
MapperFactory.getMapper().writeValueAsString(environmentModel),
@@ -514,6 +534,8 @@ public void testGetIdentitySegmentsWithValidTrait() throws JsonProcessingExcepti
514534
MockInterceptor interceptor = new MockInterceptor();
515535
interceptor.addRule()
516536
.get(baseUrl + "/environment-document/")
537+
.headerMatches("X-Environment-Key", Pattern.compile("ser.abcdefg"))
538+
.headerMatches("User-Agent", Pattern.compile("flagsmith-java-sdk/.*"))
517539
.anyTimes()
518540
.respond(
519541
MapperFactory.getMapper().writeValueAsString(environmentModel),

0 commit comments

Comments
 (0)