Skip to content

Commit

Permalink
Pass X-request-id
Browse files Browse the repository at this point in the history
  • Loading branch information
3750 committed May 28, 2024
1 parent d2d9c6c commit 757326b
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package pl.allegro.tech.workshops.testsparallelexecution.support

final class Headers {

private Headers() {}

public static final String REQUEST_ID = 'X-request-id'

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,49 @@ package pl.allegro.tech.workshops.testsparallelexecution.support

import org.springframework.boot.test.web.client.TestRestTemplate
import org.springframework.http.HttpEntity
import org.springframework.http.HttpMethod
import org.springframework.http.HttpHeaders
import org.springframework.http.ResponseEntity

import static org.springframework.http.HttpMethod.DELETE
import static org.springframework.http.HttpMethod.GET
import static org.springframework.http.HttpMethod.POST
import static org.springframework.http.HttpMethod.PUT

class RestClient {
String url
TestRestTemplate restTemplate

<T> ResponseEntity<T> get(String path, Class<T> responseType) {
restTemplate.getForEntity("$url$path", responseType)
<T> ResponseEntity<T> get(String path, Class<T> responseType, Map<String, String> headers = [:]) {
restTemplate.exchange("$url$path", GET, createHttpEntity(headers), responseType)
}

<T> ResponseEntity<T> post(String path, Object request, Class<T> responseType) {
restTemplate.postForEntity("$url$path", request, responseType)
<T> ResponseEntity<T> post(String path, Object request, Class<T> responseType, Map<String, String> headers = [:]) {
restTemplate.exchange("$url$path", POST, createHttpEntity(request, headers), responseType)
}

<T> ResponseEntity<T> put(String path, Object request, Class<T> responseType) {
restTemplate.exchange("$url$path", PUT, new HttpEntity(request), responseType)
<T> ResponseEntity<T> put(String path, Object request, Class<T> responseType, Map<String, String> headers = [:]) {
restTemplate.exchange("$url$path", PUT, createHttpEntity(request, headers), responseType)
}

ResponseEntity<Void> delete(String path) {
restTemplate.exchange("$url$path", HttpMethod.DELETE, HttpEntity.EMPTY, Void)
ResponseEntity<Void> delete(String path, Map<String, String> headers = [:]) {
restTemplate.exchange("$url$path", DELETE, createHttpEntity(headers), Void)
}

private static HttpEntity createHttpEntity(Object request, Map<String, String> headers) {
new HttpEntity(request, createHeaders(headers))
}

private static HttpEntity createHttpEntity(Map<String, String> headers = [:]) {
new HttpEntity(createHeaders(headers))
}

private static HttpHeaders createHeaders(Map<String, String> headers) {
new HttpHeaders().tap {
headers.each { header ->
set(header.key, header.value)
}
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,20 @@

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.retry.policy.MaxAttemptsRetryPolicy;
import org.springframework.retry.support.RetryTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.io.IOException;
import java.time.Duration;

@Component
Expand All @@ -21,6 +30,7 @@ public ExternalEmailClient(RestTemplateBuilder builder, @Value("${application.se
.rootUri(serviceUrl)
.setConnectTimeout(Duration.ofMillis(300))
.setReadTimeout(Duration.ofMillis(500))
.additionalInterceptors(new PassRequestIdHeaderRequestInterceptor())
.build();
this.retryTemplate = new RetryTemplate();
this.retryTemplate.setRetryPolicy(new MaxAttemptsRetryPolicy(2));
Expand All @@ -36,4 +46,16 @@ public Email read(String id) {
return retryTemplate.execute(context -> restTemplate.getForEntity("/external-api-service/emails/" + id, Email.class).getBody());
}

private static class PassRequestIdHeaderRequestInterceptor implements ClientHttpRequestInterceptor {
@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
if (requestAttributes != null && requestAttributes instanceof ServletRequestAttributes) {
String requestId = ((ServletRequestAttributes) requestAttributes).getRequest().getHeader("X-request-id");
HttpHeaders headers = request.getHeaders();
headers.set("X-request-id", requestId);
}
return execution.execute(request, body);
}
}
}

0 comments on commit 757326b

Please sign in to comment.