Skip to content

Commit 35e500d

Browse files
feat(apiclient): request method now accepts HashMap<String, Object> as command (allow nested arrays)
BREAKING CHANGE: changed type for parameter cmd of APIClient method request
1 parent d2b5f2d commit 35e500d

File tree

2 files changed

+77
-28
lines changed

2 files changed

+77
-28
lines changed

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

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -76,17 +76,19 @@ public APIClient disableDebugMode() {
7676
return this;
7777
}
7878

79+
7980
/**
8081
* Method to use to encode data before sending it to the API server
8182
*
8283
* @param cmd The command to request
8384
* @return the ready to use, encoded request payload
8485
*/
85-
public String getPOSTData(Map<String, String> cmd) {
86+
public String getPOSTData(Map<String, Object> cmd) {
87+
Map<String, String> newcmd = this.flattenCommand(cmd);
8688
StringBuilder data = new StringBuilder(this.socketConfig.getPOSTData());
8789
try {
8890
StringBuilder tmp = new StringBuilder("");
89-
Iterator<Map.Entry<String, String>> it = cmd.entrySet().iterator();
91+
Iterator<Map.Entry<String, String>> it = newcmd.entrySet().iterator();
9092
boolean hasNext = it.hasNext();
9193
while (hasNext) {
9294
Map.Entry<String, String> pair = it.next();
@@ -290,7 +292,7 @@ public Response login(String otp) {
290292
* @return API Response
291293
*/
292294
public Response login() {
293-
Map<String, String> cmd = new HashMap<String, String>();
295+
Map<String, Object> cmd = new HashMap<String, Object>();
294296
cmd.put("COMMAND", "StartSession");
295297
Response rr = this.request(cmd);
296298
if (rr.isSuccess()) {
@@ -325,7 +327,7 @@ public Response loginExtended(Map<String, String> params, String otp) {
325327
* @return API Response
326328
*/
327329
public Response loginExtended(Map<String, String> params) {
328-
Map<String, String> cmd = new HashMap<String, String>(params);
330+
Map<String, Object> cmd = new HashMap<String, Object>(params);
329331
cmd.put("COMMAND", "StartSession");
330332
Response rr = this.request(cmd);
331333
if (rr.isSuccess()) {
@@ -345,7 +347,7 @@ public Response loginExtended(Map<String, String> params) {
345347
* @return API Response
346348
*/
347349
public Response logout() {
348-
Map<String, String> cmd = new HashMap<String, String>();
350+
Map<String, Object> cmd = new HashMap<String, Object>();
349351
cmd.put("Command", "EndSession");
350352
Response rr = this.request(cmd);
351353
if (rr.isSuccess()) {
@@ -360,7 +362,7 @@ public Response logout() {
360362
* @param cmd API command to request
361363
* @return API Response
362364
*/
363-
public Response request(Map<String, String> cmd) {
365+
public Response request(Map<String, Object> cmd) {
364366
String data = this.getPOSTData(cmd);
365367

366368
StringBuilder response;
@@ -398,7 +400,7 @@ public Response request(Map<String, String> cmd) {
398400
System.err.println(e);
399401
}
400402
}
401-
Response r = new Response(response.toString(), cmd);
403+
Response r = new Response(response.toString(), this.flattenCommand(cmd));
402404
if (this.debugMode) {
403405
System.out.println(data);
404406
System.out.println(cmd);
@@ -414,14 +416,15 @@ public Response request(Map<String, String> cmd) {
414416
* @return API Response or null in case there are no further list entries
415417
*/
416418
public Response requestNextResponsePage(Response rr) {
417-
Map<String, String> mycmd = this.toUpperCaseKeys(rr.getCommand());
419+
Map<String, Object> mycmd =
420+
new HashMap<String, Object>(this.toUpperCaseKeys(rr.getCommand()));
418421
if (mycmd.get("LAST") != null) {
419422
throw new Error(
420423
"Parameter LAST in use. Please remove it to avoid issues in requestNextPage.");
421424
}
422425
int first = 0;
423426
if (mycmd.get("FIRST") != null) {
424-
first = Integer.parseInt(mycmd.get("FIRST"));
427+
first = Integer.parseInt((String) mycmd.get("FIRST"));
425428
}
426429
int total = rr.getRecordsTotalCount();
427430
int limit = rr.getRecordsLimitation();
@@ -443,7 +446,7 @@ public Response requestNextResponsePage(Response rr) {
443446
*/
444447
public ArrayList<Response> requestAllResponsePages(Map<String, String> cmd) {
445448
ArrayList<Response> responses = new ArrayList<Response>();
446-
Map<String, String> mycmd = new HashMap<String, String>(cmd);
449+
Map<String, Object> mycmd = new HashMap<String, Object>(cmd);
447450
cmd.put("FIRST", "0");
448451
Response rr = this.request(mycmd);
449452
Response tmp = rr;
@@ -510,4 +513,34 @@ private Map<String, String> toUpperCaseKeys(Map<String, String> cmd) {
510513
}
511514
return newcmd;
512515
}
516+
517+
/**
518+
* flatten provided API command (possibly including parameters in array format) to Map<String,
519+
* String>
520+
*/
521+
private Map<String, String> flattenCommand(Map<String, Object> cmd) {
522+
Map<String, String> newcmd = new HashMap<String, String>();
523+
Iterator<Map.Entry<String, Object>> it = cmd.entrySet().iterator();
524+
while (it.hasNext()) {
525+
Map.Entry<String, Object> pair = it.next();
526+
Object val = pair.getValue();
527+
if (val != null) {
528+
String key = pair.getKey().toUpperCase();
529+
if (val.getClass().isArray()) {
530+
String[] param = (String[]) val;
531+
int a = 0;
532+
for (int i = 0; i < param.length; i++) {
533+
String entry = param[i].replaceAll("[\r\n]", "");
534+
if (!"".equals(entry)) {// null check included
535+
newcmd.put(key + a, entry);
536+
a++;
537+
}
538+
}
539+
} else {
540+
newcmd.put(key, val.toString());
541+
}
542+
}
543+
}
544+
return newcmd;
545+
}
513546
}

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

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public class APIClientTest {
1919
@Test
2020
public void getPOSTData1() {
2121
APIClient cl = new APIClient();
22-
Map<String, String> cmd = new HashMap<String, String>();
22+
Map<String, Object> cmd = new HashMap<String, Object>();
2323
cmd.put("COMMAND", "ModifyDomain");
2424
cmd.put("AUTH", "gwrgwqg%&\\44t3*");
2525
String validate =
@@ -34,14 +34,30 @@ public void getPOSTData1() {
3434
@Test
3535
public void getPOSTData2() {
3636
APIClient cl = new APIClient();
37-
Map<String, String> cmd = new HashMap<String, String>();
37+
Map<String, Object> cmd = new HashMap<String, Object>();
3838
cmd.put("COMMAND", "ModifyDomain");
3939
cmd.put("AUTH", null);
4040
String validate = "s_entity=54cd&s_command=COMMAND%3DModifyDomain";
4141
String enc = cl.getPOSTData(cmd);
4242
assertEquals(validate, enc);
4343
}
4444

45+
/**
46+
* Test getPOSTData method #3
47+
*/
48+
@Test
49+
public void getPOSTData3() {
50+
APIClient cl = new APIClient();
51+
Map<String, Object> cmd = new HashMap<String, Object>();
52+
cmd.put("COMMAND", "QueryDomainOptions");
53+
cmd.put("DOMAIN", new String[] {"example1.com", "example2.com"});
54+
String validate =
55+
"s_entity=54cd&s_command=COMMAND%3DQueryDomainOptions%0ADOMAIN0%3Dexample1.com%0ADOMAIN1%3Dexample2.com";
56+
String enc = cl.getPOSTData(cmd);
57+
System.out.println(enc);
58+
assertEquals(validate, enc);
59+
}
60+
4561
/**
4662
* Test enableDebugMode method
4763
*/
@@ -142,7 +158,7 @@ public void setURL() {
142158
public void setOTP1() {
143159
APIClient cl = new APIClient();
144160
cl.setOTP("12345678");
145-
Map<String, String> cmd = new HashMap<String, String>();
161+
Map<String, Object> cmd = new HashMap<String, Object>();
146162
cmd.put("COMMAND", "StatusAccount");
147163
String tmp = cl.getPOSTData(cmd);
148164
String validate = "s_entity=54cd&s_otp=12345678&s_command=COMMAND%3DStatusAccount";
@@ -156,7 +172,7 @@ public void setOTP1() {
156172
public void setOTP2() {
157173
APIClient cl = new APIClient();
158174
cl.setOTP("12345678").setOTP("");
159-
Map<String, String> cmd = new HashMap<String, String>();
175+
Map<String, Object> cmd = new HashMap<String, Object>();
160176
cmd.put("COMMAND", "StatusAccount");
161177
String tmp = cl.getPOSTData(cmd);
162178
String validate = "s_entity=54cd&s_command=COMMAND%3DStatusAccount";
@@ -170,7 +186,7 @@ public void setOTP2() {
170186
public void setSession1() {
171187
APIClient cl = new APIClient();
172188
cl.setSession("12345678");
173-
Map<String, String> cmd = new HashMap<String, String>();
189+
Map<String, Object> cmd = new HashMap<String, Object>();
174190
cmd.put("COMMAND", "StatusAccount");
175191
String tmp = cl.getPOSTData(cmd);
176192
String validate = "s_entity=54cd&s_session=12345678&s_command=COMMAND%3DStatusAccount";
@@ -186,7 +202,7 @@ public void setSession2() {
186202
// credentials and otp code have to be unset when session id is set
187203
cl.setRoleCredentials("myaccountid", "myrole", "mypassword").setOTP("12345678")
188204
.setSession("12345678");
189-
Map<String, String> cmd = new HashMap<String, String>();
205+
Map<String, Object> cmd = new HashMap<String, Object>();
190206
cmd.put("COMMAND", "StatusAccount");
191207
String tmp = cl.getPOSTData(cmd);
192208
String validate = "s_entity=54cd&s_session=12345678&s_command=COMMAND%3DStatusAccount";
@@ -200,7 +216,7 @@ public void setSession2() {
200216
public void setSession3() {
201217
APIClient cl = new APIClient();
202218
cl.setSession("12345678").setSession("");
203-
Map<String, String> cmd = new HashMap<String, String>();
219+
Map<String, Object> cmd = new HashMap<String, Object>();
204220
cmd.put("COMMAND", "StatusAccount");
205221
String tmp = cl.getPOSTData(cmd);
206222
String validate = "s_entity=54cd&s_command=COMMAND%3DStatusAccount";
@@ -217,7 +233,7 @@ public void saveANDreuseSession() {
217233
cl.setSession("12345678").saveSession(sessionobj);
218234
APIClient cl2 = new APIClient();
219235
cl2.reuseSession(sessionobj);
220-
Map<String, String> cmd = new HashMap<String, String>();
236+
Map<String, Object> cmd = new HashMap<String, Object>();
221237
cmd.put("COMMAND", "StatusAccount");
222238
String tmp = cl2.getPOSTData(cmd);
223239
String validate = "s_entity=54cd&s_session=12345678&s_command=COMMAND%3DStatusAccount";
@@ -231,7 +247,7 @@ public void saveANDreuseSession() {
231247
public void setRemoteIPAddress1() {
232248
APIClient cl = new APIClient();
233249
cl.setRemoteIPAddress("10.10.10.10");
234-
Map<String, String> cmd = new HashMap<String, String>();
250+
Map<String, Object> cmd = new HashMap<String, Object>();
235251
cmd.put("COMMAND", "StatusAccount");
236252
String tmp = cl.getPOSTData(cmd);
237253
String validate =
@@ -246,7 +262,7 @@ public void setRemoteIPAddress1() {
246262
public void setRemoteIPAddress2() {
247263
APIClient cl = new APIClient();
248264
cl.setRemoteIPAddress("10.10.10.10").setRemoteIPAddress("");
249-
Map<String, String> cmd = new HashMap<String, String>();
265+
Map<String, Object> cmd = new HashMap<String, Object>();
250266
cmd.put("COMMAND", "StatusAccount");
251267
String tmp = cl.getPOSTData(cmd);
252268
String validate = "s_entity=54cd&s_command=COMMAND%3DStatusAccount";
@@ -260,7 +276,7 @@ public void setRemoteIPAddress2() {
260276
public void setCredentials1() {
261277
APIClient cl = new APIClient();
262278
cl.setCredentials("myaccountid", "mypassword");
263-
Map<String, String> cmd = new HashMap<String, String>();
279+
Map<String, Object> cmd = new HashMap<String, Object>();
264280
cmd.put("COMMAND", "StatusAccount");
265281
String tmp = cl.getPOSTData(cmd);
266282
String validate =
@@ -275,7 +291,7 @@ public void setCredentials1() {
275291
public void setCredentials2() {
276292
APIClient cl = new APIClient();
277293
cl.setCredentials("myaccountid", "mypassword").setCredentials("", "");
278-
Map<String, String> cmd = new HashMap<String, String>();
294+
Map<String, Object> cmd = new HashMap<String, Object>();
279295
cmd.put("COMMAND", "StatusAccount");
280296
String tmp = cl.getPOSTData(cmd);
281297
String validate = "s_entity=54cd&s_command=COMMAND%3DStatusAccount";
@@ -289,7 +305,7 @@ public void setCredentials2() {
289305
public void setRoleCredentials1() {
290306
APIClient cl = new APIClient();
291307
cl.setRoleCredentials("myaccountid", "myroleid", "mypassword");
292-
Map<String, String> cmd = new HashMap<String, String>();
308+
Map<String, Object> cmd = new HashMap<String, Object>();
293309
cmd.put("COMMAND", "StatusAccount");
294310
String tmp = cl.getPOSTData(cmd);
295311
String validate =
@@ -305,7 +321,7 @@ public void setRoleCredentials2() {
305321
APIClient cl = new APIClient();
306322
cl.setRoleCredentials("myaccountid", "myroleid", "mypassword").setRoleCredentials("", "",
307323
"");
308-
Map<String, String> cmd = new HashMap<String, String>();
324+
Map<String, Object> cmd = new HashMap<String, Object>();
309325
cmd.put("COMMAND", "StatusAccount");
310326
String tmp = cl.getPOSTData(cmd);
311327
String validate = "s_entity=54cd&s_command=COMMAND%3DStatusAccount";
@@ -429,7 +445,7 @@ public void request1() {
429445
cl.enableDebugMode().setURL(cl.getURL().replace("api", "wrongcoreapi"))
430446
.setRemoteIPAddress("1.2.3.4").setCredentials("test.user", "test.passw0rd")
431447
.useOTESystem();
432-
Map<String, String> cmd = new HashMap<String, String>();
448+
Map<String, Object> cmd = new HashMap<String, Object>();
433449
cmd.put("COMMAND", "GetUserIndex");
434450
Response r = cl.request(cmd);
435451
assertTrue(r.isTmpError());
@@ -446,7 +462,7 @@ public void request2() {
446462
APIClient cl = new APIClient();
447463
cl.setURL(cl.getURL().replace("api", "wrongcoreapi")).setRemoteIPAddress("1.2.3.4")
448464
.setCredentials("test.user", "test.passw0rd").useOTESystem();
449-
Map<String, String> cmd = new HashMap<String, String>();
465+
Map<String, Object> cmd = new HashMap<String, Object>();
450466
cmd.put("COMMAND", "GetUserIndex");
451467
Response r = cl.request(cmd);
452468
assertTrue(r.isTmpError());
@@ -562,7 +578,7 @@ public void requestAllResponsePages() {
562578
*/
563579
@Test
564580
public void setUserView() {
565-
Map<String, String> cmd = new HashMap<String, String>();
581+
Map<String, Object> cmd = new HashMap<String, Object>();
566582
cmd.put("COMMAND", "GetUserIndex");
567583
APIClient cl = new APIClient();
568584
cl.setRemoteIPAddress("1.2.3.4").setCredentials("test.user", "test.passw0rd").useOTESystem()
@@ -576,7 +592,7 @@ public void setUserView() {
576592
*/
577593
@Test
578594
public void resetUserView() {
579-
Map<String, String> cmd = new HashMap<String, String>();
595+
Map<String, Object> cmd = new HashMap<String, Object>();
580596
cmd.put("COMMAND", "GetUserIndex");
581597
APIClient cl = new APIClient();
582598
cl.setRemoteIPAddress("1.2.3.4").setCredentials("test.user", "test.passw0rd").useOTESystem()

0 commit comments

Comments
 (0)