Skip to content

Commit

Permalink
removed unecessary method and refactored code
Browse files Browse the repository at this point in the history
  • Loading branch information
aress31 committed Dec 23, 2023
1 parent 81f4fb4 commit eb6a95a
Showing 1 changed file with 10 additions and 42 deletions.
52 changes: 10 additions & 42 deletions src/main/java/swurg/workers/Worker.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,8 @@ public List<RequestWithMetadata> parseOpenAPI(OpenAPI openAPI) {

for (Server server : openAPI.getServers()) {
String serverUrl = server.getUrl();
// Set a default protocol and host if they are missing
// which can occur when loading files locally
if (!serverUrl.startsWith("http://") && !serverUrl.startsWith("https://")) {
serverUrl = "https://example.com" + serverUrl; // Use the appropriate default protocol and host
serverUrl = "https://example.com" + serverUrl;
}
final String finalServerUrl = serverUrl;

Expand All @@ -83,23 +81,17 @@ public List<RequestWithMetadata> parseOpenAPI(OpenAPI openAPI) {

operationMap.forEach((method, operation) -> {
if (operation != null) {
StringJoiner stringJoiner = new StringJoiner(", ");
List<Parameter> parameters = operation.getParameters();

if (operation.getParameters() != null)
parameters.forEach(parameter -> stringJoiner.add(parameter.getName()));

RequestBody requestBody = operation.getRequestBody();

try {
URI fullUri = constructFullRequestUri(new URI(finalServerUrl), pathItem.getKey());
URI baseUrl = new URIBuilder(finalServerUrl).setPath(pathItem.getKey()).build();

HttpService httpService = HttpService.httpService(fullUri.getHost(), fullUri.getPort(),
fullUri.getPort() == 443);
List<HttpHeader> httpHeaders = constructHttp2RequestHeaders(method, fullUri, requestBody,
HttpService httpService = HttpService.httpService(baseUrl.toString());
List<HttpHeader> httpHeaders = buildHttp2RequestHeaders(method, baseUrl, requestBody,
operation.getResponses());
List<HttpParameter> httpParameters = constructHttpRequestParameters(parameters,
requestBody, openAPI.getComponents().getSchemas());
List<HttpParameter> httpParameters = buildHttpRequestParameters(parameters, requestBody,
openAPI.getComponents().getSchemas());

HttpRequest httpRequest = HttpRequest.http2Request(
httpService,
Expand All @@ -113,8 +105,7 @@ public List<RequestWithMetadata> parseOpenAPI(OpenAPI openAPI) {
.httpHeader("content-length", String.valueOf(contentLength)));

logEntries.add(
createLogEntry(httpRequest, stringJoiner.toString(),
operation.getDescription()));
createLogEntry(httpRequest, operation.getDescription()));
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
Expand All @@ -138,7 +129,7 @@ private String parseAccept(ApiResponses apiResponses) {
return stringJoiner.toString();
}

private List<HttpParameter> constructHttpRequestParameters(List<Parameter> parameters, RequestBody requestBody,
private List<HttpParameter> buildHttpRequestParameters(List<Parameter> parameters, RequestBody requestBody,
Map<String, Schema> schemas) {
List<HttpParameter> httpParameters = new ArrayList<>();

Expand All @@ -153,7 +144,6 @@ else if ("query".equals(in))
}
}

// Add request body parameters
if (requestBody != null) {
MediaType mediaType = requestBody.getContent().entrySet().stream().findFirst().get().getValue();

Expand Down Expand Up @@ -181,7 +171,7 @@ else if ("query".equals(in))
return httpParameters;
}

private List<HttpHeader> constructHttp2RequestHeaders(String method, URI uri, RequestBody requestBody,
private List<HttpHeader> buildHttp2RequestHeaders(String method, URI uri, RequestBody requestBody,
ApiResponses apiResponses) {
List<HttpHeader> httpHeaders = new ArrayList<>();

Expand All @@ -190,12 +180,10 @@ private List<HttpHeader> constructHttp2RequestHeaders(String method, URI uri, Re
httpHeaders.add(HttpHeader.httpHeader(":path", uri.getPath()));
httpHeaders.add(HttpHeader.httpHeader(":authority", uri.getHost()));

// Set Accept header
String acceptHeaderValue = parseAccept(apiResponses);
if (!acceptHeaderValue.isEmpty())
httpHeaders.add(HttpHeader.httpHeader("accept", acceptHeaderValue));

// Set Content-Type header
if (requestBody != null && requestBody.getContent() != null) {
Optional<String> contentType = requestBody.getContent().keySet().stream().findFirst();
contentType.ifPresent(value -> httpHeaders.add(HttpHeader.httpHeader("content-type", value)));
Expand All @@ -204,27 +192,7 @@ private List<HttpHeader> constructHttp2RequestHeaders(String method, URI uri, Re
return httpHeaders;
}

private URI constructFullRequestUri(URI baseUri, String path) throws URISyntaxException {
String basePath = baseUri.getPath();
if (!basePath.endsWith("/"))
basePath += "/";

String formattedPath = path.startsWith("/") ? path.substring(1) : path;
String scheme = baseUri.getScheme();
int defaultPort = scheme.equals("http") ? 80 : 443;
int port = baseUri.getPort() == -1 ? defaultPort : baseUri.getPort();

return new URIBuilder()
.setScheme(scheme)
.setHost(baseUri.getHost())
.setPort(port)
.setPath(basePath + formattedPath)
.build();
}

private RequestWithMetadata createLogEntry(HttpRequest httpRequest, String parameters,
String description) {

private RequestWithMetadata createLogEntry(HttpRequest httpRequest, String description) {
return new RequestWithMetadata(httpRequest, description);
}
}

0 comments on commit eb6a95a

Please sign in to comment.