Skip to content

Commit eb6a95a

Browse files
committed
removed unecessary method and refactored code
1 parent 81f4fb4 commit eb6a95a

File tree

1 file changed

+10
-42
lines changed

1 file changed

+10
-42
lines changed

src/main/java/swurg/workers/Worker.java

Lines changed: 10 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,8 @@ public List<RequestWithMetadata> parseOpenAPI(OpenAPI openAPI) {
6464

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

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

8482
operationMap.forEach((method, operation) -> {
8583
if (operation != null) {
86-
StringJoiner stringJoiner = new StringJoiner(", ");
8784
List<Parameter> parameters = operation.getParameters();
88-
89-
if (operation.getParameters() != null)
90-
parameters.forEach(parameter -> stringJoiner.add(parameter.getName()));
91-
9285
RequestBody requestBody = operation.getRequestBody();
9386

9487
try {
95-
URI fullUri = constructFullRequestUri(new URI(finalServerUrl), pathItem.getKey());
88+
URI baseUrl = new URIBuilder(finalServerUrl).setPath(pathItem.getKey()).build();
9689

97-
HttpService httpService = HttpService.httpService(fullUri.getHost(), fullUri.getPort(),
98-
fullUri.getPort() == 443);
99-
List<HttpHeader> httpHeaders = constructHttp2RequestHeaders(method, fullUri, requestBody,
90+
HttpService httpService = HttpService.httpService(baseUrl.toString());
91+
List<HttpHeader> httpHeaders = buildHttp2RequestHeaders(method, baseUrl, requestBody,
10092
operation.getResponses());
101-
List<HttpParameter> httpParameters = constructHttpRequestParameters(parameters,
102-
requestBody, openAPI.getComponents().getSchemas());
93+
List<HttpParameter> httpParameters = buildHttpRequestParameters(parameters, requestBody,
94+
openAPI.getComponents().getSchemas());
10395

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

115107
logEntries.add(
116-
createLogEntry(httpRequest, stringJoiner.toString(),
117-
operation.getDescription()));
108+
createLogEntry(httpRequest, operation.getDescription()));
118109
} catch (URISyntaxException e) {
119110
throw new RuntimeException(e);
120111
}
@@ -138,7 +129,7 @@ private String parseAccept(ApiResponses apiResponses) {
138129
return stringJoiner.toString();
139130
}
140131

141-
private List<HttpParameter> constructHttpRequestParameters(List<Parameter> parameters, RequestBody requestBody,
132+
private List<HttpParameter> buildHttpRequestParameters(List<Parameter> parameters, RequestBody requestBody,
142133
Map<String, Schema> schemas) {
143134
List<HttpParameter> httpParameters = new ArrayList<>();
144135

@@ -153,7 +144,6 @@ else if ("query".equals(in))
153144
}
154145
}
155146

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

@@ -181,7 +171,7 @@ else if ("query".equals(in))
181171
return httpParameters;
182172
}
183173

184-
private List<HttpHeader> constructHttp2RequestHeaders(String method, URI uri, RequestBody requestBody,
174+
private List<HttpHeader> buildHttp2RequestHeaders(String method, URI uri, RequestBody requestBody,
185175
ApiResponses apiResponses) {
186176
List<HttpHeader> httpHeaders = new ArrayList<>();
187177

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

193-
// Set Accept header
194183
String acceptHeaderValue = parseAccept(apiResponses);
195184
if (!acceptHeaderValue.isEmpty())
196185
httpHeaders.add(HttpHeader.httpHeader("accept", acceptHeaderValue));
197186

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

207-
private URI constructFullRequestUri(URI baseUri, String path) throws URISyntaxException {
208-
String basePath = baseUri.getPath();
209-
if (!basePath.endsWith("/"))
210-
basePath += "/";
211-
212-
String formattedPath = path.startsWith("/") ? path.substring(1) : path;
213-
String scheme = baseUri.getScheme();
214-
int defaultPort = scheme.equals("http") ? 80 : 443;
215-
int port = baseUri.getPort() == -1 ? defaultPort : baseUri.getPort();
216-
217-
return new URIBuilder()
218-
.setScheme(scheme)
219-
.setHost(baseUri.getHost())
220-
.setPort(port)
221-
.setPath(basePath + formattedPath)
222-
.build();
223-
}
224-
225-
private RequestWithMetadata createLogEntry(HttpRequest httpRequest, String parameters,
226-
String description) {
227-
195+
private RequestWithMetadata createLogEntry(HttpRequest httpRequest, String description) {
228196
return new RequestWithMetadata(httpRequest, description);
229197
}
230198
}

0 commit comments

Comments
 (0)