|
| 1 | +package cmf.commitField.domain.totalCommit.service; |
| 2 | + |
| 3 | +import cmf.commitField.domain.totalCommit.dto.TotalCommitGraphQLResponse; |
| 4 | +import cmf.commitField.domain.totalCommit.dto.TotalCommitResponseDto; |
| 5 | +import lombok.RequiredArgsConstructor; |
| 6 | +import org.springframework.beans.factory.annotation.Value; |
| 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.util.Map; |
| 13 | + |
| 14 | + |
| 15 | +@Service |
| 16 | +@RequiredArgsConstructor |
| 17 | +public class TotalCommitService { |
| 18 | + private static final String BASE_URL = "https://api.github.com/graphql"; |
| 19 | + |
| 20 | + @Value("${github.token}") |
| 21 | + private String PAT; |
| 22 | + |
| 23 | + |
| 24 | + private final WebClient webClient = WebClient.builder() |
| 25 | + .baseUrl(BASE_URL) |
| 26 | + .defaultHeader(HttpHeaders.CONTENT_TYPE , MediaType.APPLICATION_JSON_VALUE) |
| 27 | + .build(); |
| 28 | + |
| 29 | + public TotalCommitResponseDto getTotalCommitCount(String username) { |
| 30 | +// String query = String.format(""" |
| 31 | +// {"query": "query { user(login: \\\"%s\\\") { contributionsCollection { totalCommitContributions restrictedContributionsCount } } }"} |
| 32 | +// """, username); |
| 33 | + // GraphQL ์ฟผ๋ฆฌ๋ฅผ Map์ผ๋ก ๊ตฌ์ฑ |
| 34 | + Map<String, String> requestBody = Map.of( |
| 35 | + "query", String.format( |
| 36 | + "query { user(login: \"%s\") { contributionsCollection { totalCommitContributions restrictedContributionsCount } } }", |
| 37 | + username |
| 38 | + ) |
| 39 | + ); |
| 40 | + |
| 41 | + TotalCommitGraphQLResponse response = webClient.post() |
| 42 | + .header("Authorization", "bearer " + PAT) |
| 43 | + .bodyValue(requestBody) |
| 44 | + .retrieve() |
| 45 | + .bodyToMono(TotalCommitGraphQLResponse.class) |
| 46 | + .block(); |
| 47 | + |
| 48 | + TotalCommitGraphQLResponse.ContributionsCollection contributions = response.getData().getUser().getContributionsCollection(); |
| 49 | + |
| 50 | + return new TotalCommitResponseDto( |
| 51 | + contributions.getTotalCommitContributions(), |
| 52 | + contributions.getRestrictedContributionsCount() |
| 53 | + ); |
| 54 | + } |
| 55 | +} |
0 commit comments