Skip to content

Commit

Permalink
fix: ut error
Browse files Browse the repository at this point in the history
  • Loading branch information
emptyOVO committed Nov 8, 2024
1 parent 3ff17a7 commit cdd387b
Showing 1 changed file with 44 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -456,51 +456,57 @@ private Map<String, String> buildPageParam(String searchVal) {
private JsonObject executeHttpRequest(String url, String method, Map<String, String> queryParams,
Map<String, String> headers, String requestBody) throws IOException {
// build param
HttpUrl.Builder urlBuilder = Objects.requireNonNull(HttpUrl.parse(url)).newBuilder();
for (Map.Entry<String, String> entry : queryParams.entrySet()) {
urlBuilder.addQueryParameter(entry.getKey(), entry.getValue());
}
HttpUrl httpUrl = urlBuilder.build();
if (url != null && !url.isEmpty()) {
HttpUrl.Builder urlBuilder = Objects.requireNonNull(HttpUrl.parse(url)).newBuilder();

// build request
Request.Builder requestBuilder = new Request.Builder()
.url(httpUrl);
for (Map.Entry<String, String> entry : queryParams.entrySet()) {
urlBuilder.addQueryParameter(entry.getKey(), entry.getValue());
}
HttpUrl httpUrl = urlBuilder.build();

// add header
for (Map.Entry<String, String> entry : headers.entrySet()) {
requestBuilder.addHeader(entry.getKey(), entry.getValue());
}
// build request
Request.Builder requestBuilder = new Request.Builder()
.url(httpUrl);

RequestBody body = RequestBody.create(MediaType.parse(CONTENT_TYPE), requestBody == null ? "" : requestBody);
// handle request method
switch (method.toUpperCase()) {
case HTTP_POST:
requestBuilder.post(body);
break;
case HTTP_GET:
requestBuilder.get();
break;
case HTTP_PUT:
requestBuilder.put(body);
break;
case HTTP_DELETE:
requestBuilder.delete(body);
break;
default:
throw new IllegalArgumentException("Unsupported request method: " + method);
}
// add header
for (Map.Entry<String, String> entry : headers.entrySet()) {
requestBuilder.addHeader(entry.getKey(), entry.getValue());
}

Request request = requestBuilder.build();
RequestBody body =
RequestBody.create(MediaType.parse(CONTENT_TYPE), requestBody == null ? "" : requestBody);
// handle request method
switch (method.toUpperCase()) {
case HTTP_POST:
requestBuilder.post(body);
break;
case HTTP_GET:
requestBuilder.get();
break;
case HTTP_PUT:
requestBuilder.put(body);
break;
case HTTP_DELETE:
requestBuilder.delete(body);
break;
default:
throw new IllegalArgumentException("Unsupported request method: " + method);
}

// get response
try (Response response = client.newCall(request).execute()) {
if (response.isSuccessful() && response.body() != null) {
String responseBody = response.body().string();
return JsonParser.parseString(responseBody).getAsJsonObject();
} else {
throw new IOException("Unexpected http response code " + response);
Request request = requestBuilder.build();

// get response
try (Response response = client.newCall(request).execute()) {
if (response.isSuccessful() && response.body() != null) {
String responseBody = response.body().string();
return JsonParser.parseString(responseBody).getAsJsonObject();
} else {
throw new IOException("Unexpected http response code " + response);
}
}
}
LOGGER.error("Fail to send http request, NP exception in url");
return null;
}

/**
Expand Down

0 comments on commit cdd387b

Please sign in to comment.