Skip to content

Commit 46a25f9

Browse files
author
Scott Lyons
authored
Adding custom User-Agent to Java SDK (#29)
1 parent b734850 commit 46a25f9

4 files changed

Lines changed: 26 additions & 5 deletions

File tree

src/main/java/ai/spice/Config.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public static String getUserAgent() {
123123
osVersion = osVersion.split(" ")[0];
124124
}
125125

126-
return "spice-java " + Version.SPICE_JAVA_VERSION + " (" + osName + "/"
126+
return "spice-java/" + Version.SPICE_JAVA_VERSION + " (" + osName + "/"
127127
+ System.getProperty("os.version") + " "
128128
+ osArch + ")";
129129
}

src/main/java/ai/spice/SpiceClient.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public static SpiceClientBuilder builder() throws URISyntaxException {
9292
*
9393
* @param maxRetries the maximum number of connection retries for the client
9494
*/
95-
public SpiceClient(String appId, String apiKey, URI flightAddress, URI httpAddress, int maxRetries) {
95+
public SpiceClient(String appId, String apiKey, URI flightAddress, URI httpAddress, int maxRetries, String userAgent) {
9696
this.appId = appId;
9797
this.apiKey = apiKey;
9898
this.maxRetries = maxRetries;
@@ -117,7 +117,13 @@ public SpiceClient(String appId, String apiKey, URI flightAddress, URI httpAddre
117117

118118
// prepare additional headers to insert into Flight requests
119119
Map<String, String> headers = new HashMap<>();
120-
headers.put("X-Spice-User-Agent", Config.getUserAgent());
120+
String uaString;
121+
if (Strings.isNullOrEmpty(userAgent)) {
122+
uaString = Config.getUserAgent();
123+
} else {
124+
uaString = userAgent;
125+
}
126+
headers.put("User-Agent", uaString);
121127

122128
final ClientIncomingAuthHeaderMiddleware.Factory authFactory = new ClientIncomingAuthHeaderMiddleware.Factory(
123129
new ClientBearerHeaderHandler());

src/main/java/ai/spice/SpiceClientBuilder.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public class SpiceClientBuilder {
3434

3535
private String appId;
3636
private String apiKey;
37+
private String userAgent;
3738
private URI flightAddress;
3839
private URI httpAddress;
3940
private int maxRetries = 3;
@@ -98,6 +99,20 @@ public SpiceClientBuilder withApiKey(String apiKey) {
9899
return this;
99100
}
100101

102+
/**
103+
* Sets the client's custom User-Agent string
104+
*
105+
* @param userAgent The User-Agent string
106+
* @return The current instance of SpiceClientBuilder for method chaining.
107+
*/
108+
public SpiceClientBuilder withUserAgent(String userAgent) {
109+
if (Strings.isNullOrEmpty(userAgent)) {
110+
throw new IllegalArgumentException("userAgent can't be null or empty");
111+
}
112+
this.userAgent = userAgent;
113+
return this;
114+
}
115+
101116
/**
102117
* Sets the client's flight address to default Spice Cloud address.
103118
*
@@ -130,6 +145,6 @@ public SpiceClientBuilder withMaxRetries(int maxRetries) {
130145
* @return The SpiceClient instance
131146
*/
132147
public SpiceClient build() {
133-
return new SpiceClient(appId, apiKey, flightAddress, httpAddress, maxRetries);
148+
return new SpiceClient(appId, apiKey, flightAddress, httpAddress, maxRetries, userAgent);
134149
}
135150
}

src/test/java/ai/spice/UserAgentTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public class UserAgentTest
3333
extends TestCase {
3434
public void testUserAgent() throws ExecutionException, InterruptedException {
3535
// use a regex to match the expected user agent
36-
String regex = "spice-java \\d+\\.\\d+\\.\\d+ \\((Linux|Windows|Darwin)/[\\d\\w\\.\\-\\_]+ (x86_64|aarch64|i386)\\)";
36+
String regex = "spice-java/\\d+\\.\\d+\\.\\d+ \\((Linux|Windows|Darwin)/[\\d\\w\\.\\-\\_]+ (x86_64|aarch64|i386)\\)";
3737
Pattern pattern = Pattern.compile(regex);
3838

3939
String userAgent = Config.getUserAgent();

0 commit comments

Comments
 (0)