Skip to content

Commit 2d462f2

Browse files
refactor query params
1 parent 768a6a8 commit 2d462f2

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

src/main/java/com/cucumber/tutorial/context/services/api/HttpService.java

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import org.apache.hc.core5.http.Method;
2222
import org.apache.hc.core5.http.ParseException;
2323
import org.apache.hc.core5.http.io.entity.EntityUtils;
24-
import org.apache.hc.core5.http.message.BasicNameValuePair;
2524
import org.apache.hc.core5.net.URIBuilder;
2625
import org.awaitility.core.ConditionTimeoutException;
2726
import org.awaitility.pollinterval.FixedPollInterval;
@@ -41,11 +40,11 @@
4140
import java.security.SecureRandom;
4241
import java.security.cert.X509Certificate;
4342
import java.time.Duration;
43+
import java.util.List;
4444
import java.util.Map;
4545
import java.util.concurrent.TimeUnit;
4646
import java.util.concurrent.atomic.AtomicReference;
4747
import java.util.function.Consumer;
48-
import java.util.stream.Collectors;
4948

5049
import static com.cucumber.tutorial.util.PlainHttpResponseUtils.from;
5150
import static org.awaitility.Awaitility.await;
@@ -81,32 +80,38 @@ protected URI uri(String path) {
8180
}
8281

8382
protected URI uri(String path, Map<String, Object> pathParams) {
84-
return uri(path, pathParams, null);
83+
return uri(path, pathParams, (Map<String, String>) null);
8584
}
8685

8786
protected URI uri(String path, Map<String, Object> pathParams, Map<String, String> nonEmptyQueryParams) {
8887
return uri(path, pathParams, nonEmptyQueryParams, null);
8988
}
9089

9190
protected URI uri(String path, Map<String, Object> pathParams, Map<String, String> nonEmptyQueryParams, Map<String, String> rawQueryParams) {
92-
return uri(address(), path, pathParams, nonEmptyQueryParams, rawQueryParams);
91+
return uri(address(), path, pathParams,
92+
nonEmptyQueryParams != null ? List.of(nonEmptyQueryParams) : null, rawQueryParams != null ? List.of(rawQueryParams) : null);
9393
}
9494

95-
public static URI uri(String address, String path, Map<String, Object> pathParams, Map<String, String> nonEmptyQueryParams, Map<String, String> rawQueryParams) {
95+
protected URI uri(String path, Map<String, Object> pathParams, List<Map<String, String>> rawQueryParams) {
96+
return uri(address(), path, pathParams, null, rawQueryParams);
97+
}
98+
99+
public static URI uri(String address, String path, Map<String, Object> pathParams,
100+
List<Map<String, String>> nonEmptyQueryParamsList, List<Map<String, String>> rawQueryParamsList) {
96101
URIBuilder uriBuilder;
97102
try {
98103
uriBuilder = new URIBuilder(address);
99104
if (path != null) {
100105
uriBuilder.appendPath(StringFormat.replaceProps(path, pathParams));
101106
}
102-
if (nonEmptyQueryParams != null) {
103-
uriBuilder.addParameters(nonEmptyQueryParams.entrySet().stream()
104-
.filter(e -> e.getValue() != null && !e.getValue().isEmpty())
105-
.map(param -> new BasicNameValuePair(param.getKey(), param.getValue())).collect(Collectors.toList()));
107+
if (nonEmptyQueryParamsList != null) {
108+
nonEmptyQueryParamsList
109+
.forEach(queryParam ->
110+
queryParam.entrySet().stream().filter(e -> e.getValue() != null && !e.getValue().isEmpty())
111+
.forEach(entry -> uriBuilder.addParameter(entry.getKey(), entry.getValue())));
106112
}
107-
if (rawQueryParams != null) {
108-
uriBuilder.addParameters(rawQueryParams.entrySet().stream()
109-
.map(param -> new BasicNameValuePair(param.getKey(), param.getValue())).collect(Collectors.toList()));
113+
if (rawQueryParamsList != null) {
114+
rawQueryParamsList.forEach(queryParam -> queryParam.forEach(uriBuilder::addParameter));
110115
}
111116
return uriBuilder.build();
112117
} catch (URISyntaxException e) {

0 commit comments

Comments
 (0)