Skip to content

Commit

Permalink
Rename
Browse files Browse the repository at this point in the history
  • Loading branch information
making committed Apr 8, 2024
1 parent a83c00e commit 0a9f733
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@
import am.ik.spring.http.client.RetryableClientHttpRequestInterceptor;

import org.springframework.core.ParameterizedTypeReference;
import org.springframework.stereotype.Service;
import org.springframework.stereotype.Component;
import org.springframework.util.backoff.FixedBackOff;
import org.springframework.web.client.RestClient;

@Service
public class PostService {
@Component
public class PostClient {

private final RestClient restClient;

public PostService(PostApiProps props, RestClient.Builder restClientBuilder) {
public PostClient(PostApiProps props, RestClient.Builder restClientBuilder) {
this.restClient = restClientBuilder.baseUrl(props.url())
.requestInterceptor(new RetryableClientHttpRequestInterceptor(
new FixedBackOff(1_000, 2), opts -> opts.sensitiveHeaders(Set.of("nel", "report-to",
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/com/example/post/PostController.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,20 @@
@RestController
public class PostController {

private final PostService postService;
private final PostClient postClient;

public PostController(PostService postService) {
this.postService = postService;
public PostController(PostClient postClient) {
this.postClient = postClient;
}

@GetMapping(path = "/api/posts")
public List<Post> getPosts() {
return this.postService.getPosts();
return this.postClient.getPosts();
}

@GetMapping(path = "/api/posts/{id}")
public Post getPost(@PathVariable int id) {
return this.postService.getPost(id);
return this.postClient.getPost(id);
}

}
12 changes: 6 additions & 6 deletions src/main/java/com/example/post/ssr/SsrController.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import java.util.Map;

import com.example.post.Post;
import com.example.post.PostService;
import com.example.post.PostClient;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
Expand All @@ -15,22 +15,22 @@ public class SsrController {

private final ReactRenderer reactRenderer;

private final PostService postService;
private final PostClient postClient;

public SsrController(ReactRenderer reactRenderer, PostService postService) {
public SsrController(ReactRenderer reactRenderer, PostClient postClient) {
this.reactRenderer = reactRenderer;
this.postService = postService;
this.postClient = postClient;
}

@GetMapping(path = { "/", "/posts" })
public String index() {
List<Post> posts = this.postService.getPosts();
List<Post> posts = this.postClient.getPosts();
return this.reactRenderer.render("/", Map.of("preLoadedPosts", posts));
}

@GetMapping(path = { "/posts/{id}" })
public String post(@PathVariable int id) {
Post post = this.postService.getPost(id);
Post post = this.postClient.getPost(id);
return this.reactRenderer.render("/posts/%d".formatted(id), Map.of("preLoadedPost", post));
}

Expand Down
8 changes: 4 additions & 4 deletions src/test/java/com/example/post/ssr/SsrControllerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import java.util.List;

import com.example.post.Post;
import com.example.post.PostService;
import com.example.post.PostClient;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.DomNode;
import com.gargoylesoftware.htmlunit.html.DomNodeList;
Expand Down Expand Up @@ -32,11 +32,11 @@ class SsrControllerTest {
WebClient webClient;

@MockBean
PostService postService;
PostClient postClient;

@Test
void index() throws Exception {
given(this.postService.getPosts()).willReturn(List.of(new Post(2, "SSR", "Server Side Rendering!", 1000),
given(this.postClient.getPosts()).willReturn(List.of(new Post(2, "SSR", "Server Side Rendering!", 1000),
new Post(1, "Hello", "Hello World!", 1000)));
HtmlPage page = this.webClient.getPage("/");
HtmlDivision root = page.getHtmlElementById("root");
Expand All @@ -49,7 +49,7 @@ void index() throws Exception {

@Test
void post() throws Exception {
given(this.postService.getPost(1)).willReturn(new Post(1, "Hello", "Hello World!", 1000));
given(this.postClient.getPost(1)).willReturn(new Post(1, "Hello", "Hello World!", 1000));
HtmlPage page = this.webClient.getPage("/posts/1");
HtmlDivision root = page.getHtmlElementById("root");
assertThat(root).isNotNull();
Expand Down

0 comments on commit 0a9f733

Please sign in to comment.