From 4f675149ce87f8f9c43b0d1aff3fbf9560c8bd13 Mon Sep 17 00:00:00 2001 From: Sven Strittmatter Date: Fri, 15 Mar 2024 21:33:34 +0100 Subject: [PATCH] #121 Extract Request Entity Creation Signed-off-by: Sven Strittmatter --- .../service/GenericDefectDojoService.java | 21 ++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/src/main/java/io/securecodebox/persistence/defectdojo/service/GenericDefectDojoService.java b/src/main/java/io/securecodebox/persistence/defectdojo/service/GenericDefectDojoService.java index 7ddabfa..00d3902 100644 --- a/src/main/java/io/securecodebox/persistence/defectdojo/service/GenericDefectDojoService.java +++ b/src/main/java/io/securecodebox/persistence/defectdojo/service/GenericDefectDojoService.java @@ -8,9 +8,9 @@ import io.securecodebox.persistence.defectdojo.config.ClientConfig; import io.securecodebox.persistence.defectdojo.exception.TooManyResponsesException; import io.securecodebox.persistence.defectdojo.http.AuthHeaderFactory; -import io.securecodebox.persistence.defectdojo.http.RestTemplateFactory; import io.securecodebox.persistence.defectdojo.http.ProxyConfig; import io.securecodebox.persistence.defectdojo.http.ProxyConfigFactory; +import io.securecodebox.persistence.defectdojo.http.RestTemplateFactory; import io.securecodebox.persistence.defectdojo.model.Model; import io.securecodebox.persistence.defectdojo.model.PaginatedResult; import lombok.NonNull; @@ -67,7 +67,7 @@ public GenericDefectDojoService(@NonNull ClientConfig clientConfig, @NonNull Pro @Override public final T get(long id) { - final HttpEntity payload = new HttpEntity<>(createAuthorizationHeaders()); + final HttpEntity payload = createRequestEntity(createAuthorizationHeaders()); final var url = createBaseUrl().resolve(String.valueOf(id)); log.debug("Requesting URL: {}", url); @@ -133,7 +133,7 @@ public final Optional searchUnique(@NonNull Map queryParams) @Override public final T create(@NonNull T object) { - final HttpEntity payload = new HttpEntity<>(object, createAuthorizationHeaders()); + final HttpEntity payload = createRequestEntity(object, createAuthorizationHeaders()); final ResponseEntity response = restTemplate.exchange(createBaseUrl(), HttpMethod.POST, payload, getModelClass()); return response.getBody(); @@ -141,21 +141,28 @@ public final T create(@NonNull T object) { @Override public final void delete(long id) { - final HttpEntity payload = new HttpEntity<>(createAuthorizationHeaders()); - + final HttpEntity payload = createRequestEntity(createAuthorizationHeaders()); final var url = createBaseUrl().resolve(id + "/"); restTemplate.exchange(url, HttpMethod.DELETE, payload, String.class); } @Override public final T update(@NonNull T object, long id) { - final HttpEntity payload = new HttpEntity<>(object, createAuthorizationHeaders()); + final HttpEntity payload = createRequestEntity(object, createAuthorizationHeaders()); final var url = createBaseUrl().resolve(id + "/"); final ResponseEntity response = restTemplate.exchange(url, HttpMethod.PUT, payload, getModelClass()); return response.getBody(); } + private HttpEntity createRequestEntity(HttpHeaders headers) { + return createRequestEntity("", headers); + } + + private HttpEntity createRequestEntity(M body, HttpHeaders headers) { + return new HttpEntity<>(body, headers); + } + /** * Get the URL path for the REST endpoint relative to {@link ClientConfig#API_PREFIX} * @@ -213,7 +220,7 @@ private RestTemplate setupRestTemplate() { } protected PaginatedResult internalSearch(Map queryParams, long limit, long offset) { - final HttpEntity payload = new HttpEntity<>(createAuthorizationHeaders()); + final HttpEntity payload = createRequestEntity(createAuthorizationHeaders()); final var mutableQueryParams = new HashMap<>(queryParams); mutableQueryParams.put("limit", String.valueOf(limit));