-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat : streamlining client request handling (#1202)
* feat : modularize httpClient Call * fix : use specific exception instead of Generic one * feat : add headers to final call
- Loading branch information
1 parent
cbf0864
commit 22a3e1a
Showing
5 changed files
with
136 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
...lient/src/main/java/com/example/restclient/bootrestclient/services/HttpClientService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package com.example.restclient.bootrestclient.services; | ||
|
||
import com.example.restclient.bootrestclient.exception.MyCustomClientException; | ||
import java.net.URI; | ||
import java.util.Map; | ||
import java.util.function.Function; | ||
import org.springframework.core.ParameterizedTypeReference; | ||
import org.springframework.http.HttpMethod; | ||
import org.springframework.http.HttpStatusCode; | ||
import org.springframework.lang.Nullable; | ||
import org.springframework.retry.annotation.Backoff; | ||
import org.springframework.retry.annotation.Retryable; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.util.CollectionUtils; | ||
import org.springframework.web.client.HttpServerErrorException; | ||
import org.springframework.web.client.RestClient; | ||
import org.springframework.web.util.UriBuilder; | ||
|
||
@Service | ||
@Retryable( | ||
retryFor = {HttpServerErrorException.class}, | ||
maxAttempts = 2, | ||
backoff = @Backoff(delay = 5000)) | ||
public class HttpClientService { | ||
|
||
private final RestClient restClient; | ||
|
||
public HttpClientService(RestClient restClient) { | ||
this.restClient = restClient; | ||
} | ||
|
||
<T> T callAndFetchResponse( | ||
Function<UriBuilder, URI> uriFunction, | ||
HttpMethod httpMethod, | ||
@Nullable Object body, | ||
Class<T> bodyType) { | ||
return callServer(uriFunction, httpMethod, null, body, bodyType, null); | ||
} | ||
|
||
<T> T callAndFetchResponse( | ||
Function<UriBuilder, URI> uriFunction, | ||
HttpMethod httpMethod, | ||
@Nullable Map<String, String> headers, | ||
Class<T> bodyType) { | ||
return callServer(uriFunction, httpMethod, headers, null, bodyType, null); | ||
} | ||
|
||
<T> T callAndFetchResponse( | ||
Function<UriBuilder, URI> uriFunction, | ||
HttpMethod httpMethod, | ||
@Nullable Object body, | ||
ParameterizedTypeReference<T> bodyType) { | ||
return callServer(uriFunction, httpMethod, null, body, null, bodyType); | ||
} | ||
|
||
private <T> T callServer( | ||
Function<UriBuilder, URI> uriFunction, | ||
HttpMethod httpMethod, | ||
Map<String, String> headers, | ||
Object body, | ||
Class<T> bodyType, | ||
ParameterizedTypeReference<T> typeReferenceBodyType) { | ||
RestClient.RequestBodySpec uri = restClient.method(httpMethod).uri(uriFunction); | ||
if (!CollectionUtils.isEmpty(headers)) { | ||
uri.headers( | ||
httpHeader -> | ||
headers.keySet().forEach(key -> httpHeader.add(key, headers.get(key)))); | ||
} | ||
if (body != null) { | ||
uri.body(body); | ||
} | ||
RestClient.ResponseSpec responseSpec = | ||
uri.retrieve() | ||
.onStatus( | ||
HttpStatusCode::is4xxClientError, | ||
(request, response) -> { | ||
throw new MyCustomClientException( | ||
response.getStatusCode(), response.getHeaders()); | ||
}); | ||
if (bodyType != null) { | ||
return responseSpec.body(bodyType); | ||
} else { | ||
return responseSpec.body(typeReferenceBodyType); | ||
} | ||
} | ||
} |
86 changes: 34 additions & 52 deletions
86
...-restclient/src/main/java/com/example/restclient/bootrestclient/services/PostService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,81 +1,63 @@ | ||
package com.example.restclient.bootrestclient.services; | ||
|
||
import com.example.restclient.bootrestclient.exception.MyCustomRuntimeException; | ||
import com.example.restclient.bootrestclient.model.response.PostDto; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import org.springframework.core.ParameterizedTypeReference; | ||
import org.springframework.http.HttpStatusCode; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.http.HttpMethod; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.web.client.RestClient; | ||
|
||
@Service | ||
public class PostService { | ||
|
||
private final RestClient restClient; | ||
private final HttpClientService httpClientService; | ||
|
||
public PostService(RestClient restClient) { | ||
this.restClient = restClient; | ||
public PostService(HttpClientService httpClientService) { | ||
this.httpClientService = httpClientService; | ||
} | ||
|
||
public List<PostDto> findAllPosts() { | ||
return restClient | ||
.get() | ||
.uri("/posts") | ||
.accept(MediaType.APPLICATION_JSON) | ||
.retrieve() | ||
.body(new ParameterizedTypeReference<List<PostDto>>() {}); | ||
return httpClientService.callAndFetchResponse( | ||
uriBuilder -> uriBuilder.path("/posts").build(), | ||
HttpMethod.GET, | ||
null, | ||
new ParameterizedTypeReference<List<PostDto>>() {}); | ||
} | ||
|
||
public Optional<PostDto> findPostById(Long id) { | ||
return Optional.ofNullable( | ||
restClient | ||
.get() | ||
.uri(uriBuilder -> uriBuilder.path("/posts/{postId}").build(id)) | ||
.accept(MediaType.APPLICATION_JSON) | ||
.retrieve() | ||
.onStatus( | ||
HttpStatusCode::is4xxClientError, | ||
(request, response) -> { | ||
throw new MyCustomRuntimeException( | ||
response.getStatusCode(), response.getHeaders()); | ||
}) | ||
.body(PostDto.class)); | ||
PostDto response = | ||
httpClientService.callAndFetchResponse( | ||
uriBuilder -> uriBuilder.path("/posts/{postId}").build(id), | ||
HttpMethod.GET, | ||
Map.of("apiKey", "123456"), | ||
PostDto.class); | ||
return Optional.ofNullable(response); | ||
} | ||
|
||
public PostDto savePost(PostDto post) { | ||
return restClient | ||
.post() | ||
.uri("/posts") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.body(post) | ||
.retrieve() | ||
.body(PostDto.class); | ||
return httpClientService.callAndFetchResponse( | ||
uriBuilder -> uriBuilder.path("/posts").build(), | ||
HttpMethod.POST, | ||
post, | ||
PostDto.class); | ||
} | ||
|
||
public Optional<PostDto> updatePostById(Long id, PostDto postDto) { | ||
return Optional.ofNullable( | ||
restClient | ||
.put() | ||
.uri(uriBuilder -> uriBuilder.path("/posts/{postId}").build(id)) | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.body(postDto) | ||
.retrieve() | ||
.onStatus( | ||
HttpStatusCode::is4xxClientError, | ||
(request, response) -> { | ||
throw new MyCustomRuntimeException( | ||
response.getStatusCode(), response.getHeaders()); | ||
}) | ||
.body(PostDto.class)); | ||
PostDto response = | ||
httpClientService.callAndFetchResponse( | ||
uriBuilder -> uriBuilder.path("/posts/{postId}").build(id), | ||
HttpMethod.PUT, | ||
postDto, | ||
PostDto.class); | ||
return Optional.ofNullable(response); | ||
} | ||
|
||
public String deletePostById(Long id) { | ||
return restClient | ||
.delete() | ||
.uri(uriBuilder -> uriBuilder.path("/posts/{postId}").build(id)) | ||
.retrieve() | ||
.body(String.class); | ||
return httpClientService.callAndFetchResponse( | ||
uriBuilder -> uriBuilder.path("/posts/{postId}").build(id), | ||
HttpMethod.DELETE, | ||
null, | ||
String.class); | ||
} | ||
} |