Skip to content

Commit e2c276d

Browse files
feat(apiclient): review user agent usage
1 parent e79defd commit e2c276d

File tree

2 files changed

+69
-1
lines changed

2 files changed

+69
-1
lines changed

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

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,14 @@ public class APIClient {
4242
private SocketConfig socketConfig;
4343
/** debug mode flag */
4444
private boolean debugMode;
45+
/** user agent string */
46+
private String ua;
4547

4648
/**
4749
* Class constructor. Creates an API Client ready to use.
4850
*/
4951
public APIClient() {
52+
this.ua = "";
5053
this.debugMode = false;
5154
this.setURL("https://coreapi.1api.net/api/call.cgi");
5255
this.socketConfig = new SocketConfig();
@@ -129,6 +132,38 @@ public String getURL() {
129132
return this.socketURL;
130133
}
131134

135+
/**
136+
* Set a custom user agent header (useful for tools that use our SDK)
137+
*
138+
* @param str user agent label
139+
* @param rv user agent revision
140+
*/
141+
public void setUserAgent(String str, String rv) {
142+
String jv = System.getProperty("java.vm.name").toLowerCase().replaceAll(" .+", "");
143+
String jrv = System.getProperty("java.version");
144+
String arch = System.getProperty("os.arch");
145+
String os = System.getProperty("os.name");
146+
this.ua = (str + " (" + os + "; " + arch + "; rv:" + rv + ") java-sdk/" + this.getVersion()
147+
+ " " + jv + "/" + jrv);
148+
}
149+
150+
/**
151+
* Get the user agent string
152+
*
153+
* @return user agent string
154+
*/
155+
public String getUserAgent() {
156+
if (this.ua.length() == 0) {
157+
String jv = System.getProperty("java.vm.name").toLowerCase().replaceAll(" .+", "");
158+
String jrv = System.getProperty("java.version");
159+
String arch = System.getProperty("os.arch");
160+
String os = System.getProperty("os.name");
161+
this.ua = ("JAVA-SDK (" + os + "; " + arch + "; rv:" + this.getVersion() + ") " + jv
162+
+ "/" + jrv);
163+
}
164+
return this.ua;
165+
}
166+
132167
/**
133168
* Get the current module version
134169
*
@@ -334,7 +369,7 @@ public Response request(Map<String, String> cmd) {
334369
con.setRequestMethod("POST");
335370
con.setRequestProperty("Content-length", String.valueOf(data.length()));
336371
con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
337-
con.setRequestProperty("User-Agent", "JAVA-SDK::" + this.getVersion());
372+
con.setRequestProperty("User-Agent", this.getUserAgent());
338373
con.setRequestProperty("Expect", "");
339374
con.setConnectTimeout(APIClient.socketTimeout);
340375
con.setReadTimeout(APIClient.socketTimeout);

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,39 @@ public void getURL() {
9292
assertEquals("https://coreapi.1api.net/api/call.cgi", url);
9393
}
9494

95+
/**
96+
* Test getUserAgent method
97+
*/
98+
@Test
99+
public void getUserAgent() {
100+
APIClient cl = new APIClient();
101+
String jv = System.getProperty("java.vm.name").toLowerCase().replaceAll(" .+", "");
102+
String jrv = System.getProperty("java.version");
103+
String arch = System.getProperty("os.arch");
104+
String os = System.getProperty("os.name");
105+
String uaexpected = ("JAVA-SDK (" + os + "; " + arch + "; rv:" + cl.getVersion() + ") " + jv
106+
+ "/" + jrv);
107+
assertEquals(cl.getUserAgent(), uaexpected);
108+
}
109+
110+
/**
111+
* Test setUserAgent method
112+
*/
113+
@Test
114+
public void setUserAgent() {
115+
String pid = "WHMCS";
116+
String rv = "7.7.0";
117+
APIClient cl = new APIClient();
118+
cl.setUserAgent(pid, rv);
119+
String jv = System.getProperty("java.vm.name").toLowerCase().replaceAll(" .+", "");
120+
String jrv = System.getProperty("java.version");
121+
String arch = System.getProperty("os.arch");
122+
String os = System.getProperty("os.name");
123+
String uaexpected = (pid + " (" + os + "; " + arch + "; rv:" + rv + ") java-sdk/"
124+
+ cl.getVersion() + " " + jv + "/" + jrv);
125+
assertEquals(cl.getUserAgent(), uaexpected);
126+
}
127+
95128
/**
96129
* Test setURL method
97130
*/

0 commit comments

Comments
 (0)