Skip to content

Commit 58c7eb2

Browse files
feat(apiclient): allow to specify additional libraries via setUserAgent
1 parent 65860ab commit 58c7eb2

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

src/main/java/net/hexonet/apiconnector/APIClient.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,12 +166,31 @@ public String getURL() {
166166
* @return Current APIClient instance for method chaining
167167
*/
168168
public APIClient setUserAgent(String str, String rv) {
169+
return this.setUserAgent(str, rv, new ArrayList<String>());
170+
}
171+
172+
/**
173+
* Set a custom user agent header (useful for tools that use our SDK)
174+
*
175+
* @param str user agent label
176+
* @param rv user agent revision
177+
* @param modules further modules to add to user agent string ["<module>/<version>"]
178+
* @return Current APIClient instance for method chaining
179+
*/
180+
public APIClient setUserAgent(String str, String rv, ArrayList<String> modules) {
181+
StringBuilder mods = new StringBuilder(" ");
182+
if (modules.size() > 0) {
183+
for (int i = 0; i < modules.size(); i++) {
184+
mods.append(modules.get(i));
185+
mods.append(" ");
186+
}
187+
}
169188
String jv = System.getProperty("java.vm.name").toLowerCase().replaceAll(" .+", "");
170189
String jrv = System.getProperty("java.version");
171190
String arch = System.getProperty("os.arch");
172191
String os = System.getProperty("os.name");
173-
this.ua = (str + " (" + os + "; " + arch + "; rv:" + rv + ") java-sdk/" + this.getVersion()
174-
+ " " + jv + "/" + jrv);
192+
this.ua = (str + " (" + os + "; " + arch + "; rv:" + rv + ")" + mods + "java-sdk/"
193+
+ this.getVersion() + " " + jv + "/" + jrv);
175194
return this;
176195
}
177196

src/test/java/net/hexonet/apiconnector/APIClientTest.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,30 @@ public void setUserAgent() {
143143
assertEquals(cl.getUserAgent(), uaexpected);
144144
}
145145

146+
/**
147+
* Test setUserAgent method by including additional modules
148+
*/
149+
@Test
150+
public void setUserAgentModules() {
151+
String pid = "WHMCS";
152+
String rv = "7.7.0";
153+
ArrayList<String> mods = new ArrayList<String>();
154+
mods.add("reg/2.6.2");
155+
mods.add("ssl/7.2.2");
156+
mods.add("dc/8.2.2");
157+
158+
APIClient cl = new APIClient();
159+
cl.setUserAgent(pid, rv, mods);
160+
String jv = System.getProperty("java.vm.name").toLowerCase().replaceAll(" .+", "");
161+
String jrv = System.getProperty("java.version");
162+
String arch = System.getProperty("os.arch");
163+
String os = System.getProperty("os.name");
164+
String uaexpected = (pid + " (" + os + "; " + arch + "; rv:" + rv
165+
+ ") reg/2.6.2 ssl/7.2.2 dc/8.2.2 java-sdk/" + cl.getVersion() + " " + jv + "/"
166+
+ jrv);
167+
assertEquals(cl.getUserAgent(), uaexpected);
168+
}
169+
146170
/**
147171
* Test setURL method
148172
*/

0 commit comments

Comments
 (0)