Skip to content

Commit

Permalink
Declarative RestClient is implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
musab.bozkurt committed Feb 12, 2024
1 parent 35ce73b commit e380638
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.mb.livedataservice.client.jsonplaceholder;

import com.mb.livedataservice.client.jsonplaceholder.request.PostRequest;
import com.mb.livedataservice.client.jsonplaceholder.response.PostResponse;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.service.annotation.DeleteExchange;
import org.springframework.web.service.annotation.GetExchange;
import org.springframework.web.service.annotation.PostExchange;

import java.util.List;

public interface DeclarativeJSONPlaceholderRestClient {

@GetExchange("/posts")
List<PostResponse> getAllPosts();

@GetExchange("/posts/{id}")
PostResponse getPost(@PathVariable Integer id);

@DeleteExchange("/posts/{id}")
void deletePost(@PathVariable Integer id);

@PostExchange("/posts")
PostResponse createPost(@RequestBody PostRequest post);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.mb.livedataservice.config;

import com.mb.livedataservice.client.jsonplaceholder.DeclarativeJSONPlaceholderRestClient;
import com.mb.livedataservice.exception.BaseException;
import com.mb.livedataservice.exception.ErrorCode;
import lombok.SneakyThrows;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.client.JdkClientHttpRequestFactory;
import org.springframework.http.client.support.BasicAuthenticationInterceptor;
import org.springframework.web.client.RestClient;
import org.springframework.web.client.support.RestClientAdapter;
import org.springframework.web.service.invoker.HttpServiceProxyFactory;

import java.time.Duration;

@Configuration
public class DeclarativeHttpInterfaces {

@Bean
public DeclarativeJSONPlaceholderRestClient declarativeJSONPlaceholderRestClient(RestClient.Builder builder, JSONPlaceholderClientProperties JSONPlaceholderClientProperties) {
JdkClientHttpRequestFactory jdkClientHttpRequestFactory = new JdkClientHttpRequestFactory();
jdkClientHttpRequestFactory.setReadTimeout(Duration.ofSeconds(15));

RestClient restClient = builder
.baseUrl(JSONPlaceholderClientProperties.getUrl())
.requestFactory(jdkClientHttpRequestFactory)
.requestInterceptor(new BasicAuthenticationInterceptor(JSONPlaceholderClientProperties.getClientId(), JSONPlaceholderClientProperties.getClientSecret()))
.defaultStatusHandler(HttpStatusCode::isError, (request, response) -> {
throw new BaseException(new ErrorCode() {

@Override
@SneakyThrows
public HttpStatus getHttpStatus() {
return HttpStatus.resolve(response.getStatusCode().value());
}

@Override
@SneakyThrows
public String getCode() {
return response.getStatusText();
}
});
})
.build();

return HttpServiceProxyFactory
.builderFor(RestClientAdapter.create(restClient))
.build()
.createClient(DeclarativeJSONPlaceholderRestClient.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.mb.livedataservice.api.request.ApiTutorialUpdateRequest;
import com.mb.livedataservice.api.response.ApiTutorialResponse;
import com.mb.livedataservice.base.BaseUnitTest;
import com.mb.livedataservice.client.jsonplaceholder.DeclarativeJSONPlaceholderRestClient;
import com.mb.livedataservice.client.jsonplaceholder.JSONPlaceholderRestClient;
import com.mb.livedataservice.client.jsonplaceholder.request.PostRequest;
import com.mb.livedataservice.client.jsonplaceholder.response.PostResponse;
Expand Down Expand Up @@ -66,6 +67,9 @@ class TutorialControllerTest extends BaseUnitTest {
@Autowired
private JSONPlaceholderRestClient JSONPlaceholderRestClient;

@Autowired
private DeclarativeJSONPlaceholderRestClient declarativeJSONPlaceholderRestClient;

@BeforeAll
static void setup(@Autowired TestRestTemplate restTemplate) {
//Prepare some test data
Expand Down Expand Up @@ -215,4 +219,28 @@ void shouldGetPostById() {
PostResponse post = JSONPlaceholderRestClient.getPostById(5);
assertNotNull(post);
}

@Test
public void shouldGetAllPosts() {
List<PostResponse> posts = declarativeJSONPlaceholderRestClient.getAllPosts();
assertThat(posts.size()).isEqualTo(100);
}

@Test
public void shouldGetSinglePost() {
PostResponse post = declarativeJSONPlaceholderRestClient.getPost(5);
assertThat(post.id()).isEqualTo(5);
}

@Test
public void shouldDeleteSinglePost() {
declarativeJSONPlaceholderRestClient.deletePost(5);
}

@Test
public void shouldCreateNewPost() {
PostRequest newPost = new PostRequest(1, null, "new title", "new body");
PostResponse post = declarativeJSONPlaceholderRestClient.createPost(newPost);
assertThat("new title").isEqualTo(post.title());
}
}

0 comments on commit e380638

Please sign in to comment.