Skip to content

Commit

Permalink
secureCodeBox#121 Extract Request Entity Creation
Browse files Browse the repository at this point in the history
Signed-off-by: Sven Strittmatter <[email protected]>
  • Loading branch information
Weltraumschaf committed Jul 3, 2024
1 parent 2d58c31 commit 4f67514
Showing 1 changed file with 14 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -67,7 +67,7 @@ public GenericDefectDojoService(@NonNull ClientConfig clientConfig, @NonNull Pro

@Override
public final T get(long id) {
final HttpEntity<String> payload = new HttpEntity<>(createAuthorizationHeaders());
final HttpEntity<String> payload = createRequestEntity(createAuthorizationHeaders());

final var url = createBaseUrl().resolve(String.valueOf(id));
log.debug("Requesting URL: {}", url);
Expand Down Expand Up @@ -133,29 +133,36 @@ public final Optional<T> searchUnique(@NonNull Map<String, Object> queryParams)

@Override
public final T create(@NonNull T object) {
final HttpEntity<T> payload = new HttpEntity<>(object, createAuthorizationHeaders());
final HttpEntity<T> payload = createRequestEntity(object, createAuthorizationHeaders());
final ResponseEntity<T> response = restTemplate.exchange(createBaseUrl(), HttpMethod.POST, payload, getModelClass());

return response.getBody();
}

@Override
public final void delete(long id) {
final HttpEntity<String> payload = new HttpEntity<>(createAuthorizationHeaders());

final HttpEntity<String> 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<T> payload = new HttpEntity<>(object, createAuthorizationHeaders());
final HttpEntity<T> payload = createRequestEntity(object, createAuthorizationHeaders());
final var url = createBaseUrl().resolve(id + "/");
final ResponseEntity<T> response = restTemplate.exchange(url, HttpMethod.PUT, payload, getModelClass());

return response.getBody();
}

private HttpEntity<String> createRequestEntity(HttpHeaders headers) {
return createRequestEntity("", headers);
}

private <M> HttpEntity<M> createRequestEntity(M body, HttpHeaders headers) {
return new HttpEntity<>(body, headers);
}

/**
* Get the URL path for the REST endpoint relative to {@link ClientConfig#API_PREFIX}
*
Expand Down Expand Up @@ -213,7 +220,7 @@ private RestTemplate setupRestTemplate() {
}

protected PaginatedResult<T> internalSearch(Map<String, Object> queryParams, long limit, long offset) {
final HttpEntity<String> payload = new HttpEntity<>(createAuthorizationHeaders());
final HttpEntity<String> payload = createRequestEntity(createAuthorizationHeaders());

final var mutableQueryParams = new HashMap<>(queryParams);
mutableQueryParams.put("limit", String.valueOf(limit));
Expand Down

0 comments on commit 4f67514

Please sign in to comment.