1
+ package cmf .commitField .domain .commit .sinceCommit .service ;
2
+
3
+ import cmf .commitField .domain .commit .sinceCommit .dto .SinceCommitResponseDto ;
4
+ import lombok .extern .slf4j .Slf4j ;
5
+ import org .springframework .beans .factory .annotation .Value ;
6
+ import org .springframework .core .ParameterizedTypeReference ;
7
+ import org .springframework .http .HttpHeaders ;
8
+ import org .springframework .http .MediaType ;
9
+ import org .springframework .stereotype .Service ;
10
+ import org .springframework .web .reactive .function .client .WebClient ;
11
+
12
+ import java .time .LocalDateTime ;
13
+ import java .time .format .DateTimeFormatter ;
14
+ import java .util .List ;
15
+
16
+ @ Slf4j
17
+ @ Service
18
+ public class SinceCommitService {
19
+ private static final String BASE_URL = "https://api.github.com" ;
20
+ // ?since=2024-01-01T00:00:00Z&until=2025-02-1T23:59:59Z
21
+
22
+ @ Value ("${github.token}" )
23
+ private String PAT ;
24
+
25
+ private final WebClient webClient = WebClient .builder ()
26
+ .baseUrl (BASE_URL )
27
+ .defaultHeader (HttpHeaders .CONTENT_TYPE , MediaType .APPLICATION_JSON_VALUE )
28
+ .build ();
29
+
30
+ public List <SinceCommitResponseDto > getSinceCommits (String owner , String repo , LocalDateTime since , LocalDateTime until ) {
31
+ try {
32
+ return webClient .get ()
33
+ .uri (uriBuilder -> uriBuilder
34
+ .path ("/repos/{owner}/{repo}/commits" )
35
+ .queryParam ("since" , since .format (DateTimeFormatter .ISO_DATE_TIME ))
36
+ .queryParam ("until" , until .format (DateTimeFormatter .ISO_DATE_TIME ))
37
+ .build (owner , repo ))
38
+ .header ("Authorization" , "bearer " + PAT )
39
+ .retrieve ()
40
+ .bodyToMono (new ParameterizedTypeReference <List <SinceCommitResponseDto >>() {})
41
+ .block ();
42
+ } catch (Exception e ) {
43
+ log .error ("GitHub API 호출 중 오류 발생: {}" , e .getMessage ());
44
+ throw new RuntimeException ("GitHub API 호출 중 오류가 발생했습니다." , e );
45
+ }
46
+ }
47
+ }
48
+ /*
49
+ ParameterizedTypeReference를 사용하는 주요 이유
50
+ 1. 제네릭 타입 정보 보존
51
+ 2. 복잡한 타입 구조 처리 가능
52
+ 3. 타입 안전성 보장
53
+ 4. 런타임에 올바른 타입으로 역직렬화 가능
54
+
55
+ ParameterizedTypeReference를 사용하지 않으면
56
+ // 잘못된 예시
57
+ List<GitHubCommitResponse> commits = webClient.get()
58
+ .retrieve()
59
+ .bodyToMono(List.class)
60
+ .block();
61
+
62
+ // 런타임 에러 발생 가능
63
+ GitHubCommitResponse commit = commits.get(0); // ClassCastException 발생 위험
64
+ */
0 commit comments