Skip to content
This repository was archived by the owner on Dec 6, 2024. It is now read-only.

Commit 6cf5081

Browse files
SynapticloopSynapticloop
authored andcommitted
refactored adding request parameters
1 parent adc6de1 commit 6cf5081

File tree

1 file changed

+25
-23
lines changed

1 file changed

+25
-23
lines changed

src/main/java/synapticloop/b2/request/BaseB2Request.java

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -121,15 +121,9 @@ protected CloseableHttpResponse executeHead() throws B2ApiException {
121121
CloseableHttpClient closeableHttpClient = HttpClients.createDefault();
122122

123123
try {
124-
URI uri = new URIBuilder(url).build();
125-
LOG.debug("HEAD request to URL '{}'", url);
126-
127-
Iterator<String> iterator = parameters.keySet().iterator();
128-
while (iterator.hasNext()) {
129-
String key = (String) iterator.next();
130-
// @TODO - this is kind of wasteful - refactor
131-
uri = new URIBuilder(uri).addParameter(key, parameters.get(key)).build();
132-
}
124+
URI uri = getUri();
125+
126+
LOG.debug("HEAD request to URL '{}'", uri.toString());
133127

134128
HttpHead httpHead = new HttpHead(uri);
135129
setHeaders(httpHead);
@@ -151,12 +145,15 @@ protected CloseableHttpResponse executeHead() throws B2ApiException {
151145

152146
protected String executeGet() throws B2ApiException {
153147
CloseableHttpClient closeableHttpClient = HttpClients.createDefault();
154-
HttpGet httpGet = new HttpGet(url);
155-
setHeaders(httpGet);
156-
157-
LOG.debug("GET request to URL '{}'", url);
158148

159149
try {
150+
URI uri = getUri();
151+
152+
HttpGet httpGet = new HttpGet(uri);
153+
setHeaders(httpGet);
154+
155+
LOG.debug("GET request to URL '{}'", url);
156+
160157

161158
CloseableHttpResponse httpResponse = closeableHttpClient.execute(httpGet);
162159
int statusCode = httpResponse.getStatusLine().getStatusCode();
@@ -169,7 +166,7 @@ protected String executeGet() throws B2ApiException {
169166
} else {
170167
return(response);
171168
}
172-
} catch (IOException ex) {
169+
} catch (IOException | URISyntaxException ex) {
173170
throw new B2ApiException(ex);
174171
}
175172
}
@@ -179,15 +176,9 @@ protected CloseableHttpResponse executeGetWithData() throws B2ApiException {
179176
CloseableHttpClient closeableHttpClient = HttpClients.createDefault();
180177

181178
try {
182-
URI uri = new URIBuilder(url).build();
183-
LOG.debug("GET request to URL '{}'", url);
179+
URI uri = getUri();
184180

185-
Iterator<String> iterator = parameters.keySet().iterator();
186-
while (iterator.hasNext()) {
187-
String key = (String) iterator.next();
188-
// @TODO - this is kind of wasteful - refactor
189-
uri = new URIBuilder(uri).addParameter(key,parameters.get(key)).build();
190-
}
181+
LOG.debug("GET request to URL '{}'", uri.toString());
191182

192183
HttpGet httpGet = new HttpGet(uri);
193184
setHeaders(httpGet);
@@ -266,7 +257,18 @@ protected String executePost(File file) throws B2ApiException {
266257
}
267258
}
268259

269-
/**
260+
private URI getUri() throws URISyntaxException {
261+
URIBuilder uriBuilder = new URIBuilder(url);
262+
263+
Iterator<String> iterator = parameters.keySet().iterator();
264+
while (iterator.hasNext()) {
265+
String key = (String) iterator.next();
266+
uriBuilder.addParameter(key, parameters.get(key));
267+
}
268+
269+
return(uriBuilder.build());
270+
}
271+
/**
270272
* Set the headers safely, go through the headers Map and add them to the http
271273
* request. If they already exist on the http request, it will be ignored.
272274
* To over-ride what headers are set, this should be done in the constructor

0 commit comments

Comments
 (0)