Skip to content

Commit a953e69

Browse files
feat(responsetranslator): added initial version coming with rewrite/restructuring from scratch
As of known issues, we merged Response and ResponseTemplate classes. BREAKING CHANGE: Downward incompatible restructuring (Merge of Response/ResponseTemplate, Rewrite ResponseTemplateManager) and introducing ResponseTranslator
1 parent 742f3a6 commit a953e69

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+1621
-920
lines changed

.releaserc.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@
88
"changelogFile": "HISTORY.md"
99
}
1010
],
11+
[
12+
"@semantic-release/exec",
13+
{
14+
"prepareCmd": "./updateVersion.sh ${nextRelease.version}"
15+
}
16+
],
1117
"@conveyal/maven-semantic-release",
1218
[
1319
"@semantic-release/git",

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

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ public String getReferer() {
295295
* @return module version
296296
*/
297297
public String getVersion() {
298-
return "1.3.20";
298+
return "3.2.0";
299299
}
300300

301301
/**
@@ -495,10 +495,10 @@ public Response request(Map<String, Object> cmd) {
495495
Map<String, String> cfg = new HashMap<String, String>();
496496
cfg.put("CONNECTION_URL", this.socketURL);
497497

498-
StringBuilder response;
498+
StringBuilder response = new StringBuilder("");
499499
Response r;
500+
String err = null;
500501
try {
501-
response = new StringBuilder("");
502502
URL myurl = new URL(cfg.get("CONNECTION_URL"));
503503
HttpsURLConnection con = (HttpsURLConnection) myurl.openConnection();
504504
if (this.curlopts.containsKey("PROXY")) {
@@ -535,18 +535,14 @@ public Response request(Map<String, Object> cmd) {
535535
}
536536
}
537537
in.close();
538+
con.disconnect();
538539
} catch (Exception e) {
539-
ResponseTemplate tpl = ResponseTemplateManager.getInstance().getTemplate("httperror");
540-
response = new StringBuilder(tpl.getPlain());
541-
r = new Response(response.toString(), newcmd, cfg);
542-
if (this.debugMode) {
543-
this.logger.log(secured, r, e.getMessage());
544-
}
545-
return r;
540+
err = e.getMessage();
541+
response.append("httperror");
546542
}
547543
r = new Response(response.toString(), newcmd, cfg);
548544
if (this.debugMode) {
549-
this.logger.log(secured, r);
545+
this.logger.log(secured, r, err);
550546
}
551547
return r;
552548
}

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

Lines changed: 126 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
import java.util.HashMap;
55
import java.util.Iterator;
66
import java.util.Map;
7-
import java.util.regex.Pattern;
8-
import java.util.regex.Matcher;
97

108
/**
119
* Response covers all functionality to wrap a Backend API Response like accessing data
@@ -14,7 +12,12 @@
1412
* @version %I%, %G%
1513
* @since 2.0
1614
*/
17-
public class Response extends ResponseTemplate {
15+
public class Response {
16+
/** backend system plain response data */
17+
private String raw;
18+
/** backend system parsed response data (hash format) */
19+
private Map<String, Object> hash;
20+
1821
/**
1922
* The API Command used within this request
2023
*/
@@ -37,6 +40,15 @@ public class Response extends ResponseTemplate {
3740
*/
3841
private ArrayList<Record> records;
3942

43+
/**
44+
* Constructor
45+
*
46+
* @param raw API plain response
47+
*/
48+
public Response(String raw) {
49+
this(raw, Map.ofEntries(), Map.ofEntries());
50+
}
51+
4052
/**
4153
* Constructor
4254
*
@@ -56,22 +68,14 @@ public Response(String raw, Map<String, String> cmd) {
5668
*/
5769
@SuppressWarnings("unchecked") // not really a good way ...
5870
public Response(String raw, Map<String, String> cmd, Map<String, String> ph) {
59-
super(raw);
60-
61-
String regex = "\\{[^}]+\\}";
62-
Pattern pattern = Pattern.compile(regex);
63-
Matcher matcher = pattern.matcher(this.raw);
64-
if (matcher.find()) {
65-
for (Map.Entry<String, String> entry : ph.entrySet()) {
66-
this.raw = this.raw.replace("{" + entry.getKey() + "}", entry.getValue());
67-
}
68-
super.init(this.raw.replaceAll(regex, ""));
71+
// secure password for output
72+
if (cmd.containsKey("PASSWORD")) {
73+
cmd.replace("PASSWORD", "***");
6974
}
7075

76+
this.raw = ResponseTranslator.translate(raw, cmd, ph);
77+
this.hash = ResponseParser.parse(this.raw);
7178
this.command = new HashMap<String, String>(cmd);
72-
if (this.command.containsKey("PASSWORD")) { // make password no longer accessible
73-
this.command.replace("PASSWORD", "***");
74-
}
7579
this.columnkeys = new ArrayList<String>();
7680
this.columns = new ArrayList<Column>();
7781
this.recordIndex = 0;
@@ -106,6 +110,112 @@ public Response(String raw, Map<String, String> cmd, Map<String, String> ph) {
106110
}
107111
}
108112

113+
/**
114+
* Get API response code
115+
*
116+
* @return API response code
117+
*/
118+
public int getCode() {
119+
return Integer.parseInt((String) this.hash.get("CODE"));
120+
}
121+
122+
/**
123+
* Get API response description
124+
*
125+
* @return API response description
126+
*/
127+
public String getDescription() {
128+
return (String) this.hash.get("DESCRIPTION");
129+
}
130+
131+
/**
132+
* Get Plain API response
133+
*
134+
* @return Plain API response
135+
*/
136+
public String getPlain() {
137+
return this.raw;
138+
}
139+
140+
/**
141+
* Get Queuetime of API response
142+
*
143+
* @return Queuetime of API response
144+
*/
145+
public double getQueuetime() {
146+
String rt = (String) this.hash.get("QUEUETIME");
147+
if (rt != null) {
148+
return Double.parseDouble((String) this.hash.get("QUEUETIME"));
149+
}
150+
return 0.00;
151+
}
152+
153+
/**
154+
* Get API response as Hash
155+
*
156+
* @return API response hash
157+
*/
158+
public Map<String, Object> getHash() {
159+
return this.hash;
160+
}
161+
162+
/**
163+
* Get Runtime of API response
164+
*
165+
* @return Runtime of API response
166+
*/
167+
public double getRuntime() {
168+
String rt = (String) this.hash.get("RUNTIME");
169+
if (rt != null) {
170+
return Double.parseDouble((String) this.hash.get("RUNTIME"));
171+
}
172+
return 0.00;
173+
}
174+
175+
/**
176+
* Check if current API response represents an error case API response code is an 5xx code
177+
*
178+
* @return boolean result
179+
*/
180+
public boolean isError() {
181+
String code = (String) this.hash.get("CODE");
182+
return code.charAt(0) == '5';
183+
}
184+
185+
/**
186+
* Check if current API response represents a success case API response code is an 2xx code
187+
*
188+
* @return boolean result
189+
*/
190+
public boolean isSuccess() {
191+
String code = (String) this.hash.get("CODE");
192+
return code.charAt(0) == '2';
193+
}
194+
195+
/**
196+
* Check if current API response represents a temporary error case API response code is an 4xx
197+
* code
198+
*
199+
* @return boolean result
200+
*/
201+
public boolean isTmpError() {
202+
String code = (String) this.hash.get("CODE");
203+
return code.charAt(0) == '4';
204+
}
205+
206+
/**
207+
* Check if current operation is returned as pending
208+
*
209+
* @return boolean result
210+
*/
211+
public boolean isPending() {
212+
String pending = (String) this.hash.get("PENDING");
213+
if (pending != null) {
214+
return pending.equals("1");
215+
}
216+
return false;
217+
}
218+
109219
/**
110220
* Add a column to the column list
111221
*

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* @version %I%, %G%
1414
* @since 2.0
1515
*/
16-
public class ResponseParser {
16+
public final class ResponseParser {
1717
/**
1818
* Method to stringify a parsed api response
1919
*

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

Lines changed: 0 additions & 150 deletions
This file was deleted.

0 commit comments

Comments
 (0)