This repository has been archived by the owner on Jul 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
1,059 additions
and
1,059 deletions.
There are no files selected for viewing
514 changes: 257 additions & 257 deletions
514
...end/src/main/java/org/donggle/backend/infrastructure/client/tistory/TistoryApiClient.java
Large diffs are not rendered by default.
Oops, something went wrong.
247 changes: 124 additions & 123 deletions
247
.../main/java/org/donggle/backend/infrastructure/client/tistory/TistoryConnectionClient.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,123 +1,124 @@ | ||
package org.donggle.backend.infrastructure.client.tistory; | ||
|
||
import org.donggle.backend.application.repository.MemberCredentialsRepository; | ||
import org.donggle.backend.application.repository.MemberRepository; | ||
import org.donggle.backend.application.service.request.OAuthAccessTokenRequest; | ||
import org.donggle.backend.domain.member.Member; | ||
import org.donggle.backend.domain.member.MemberCredentials; | ||
import org.donggle.backend.exception.notfound.MemberNotFoundException; | ||
import org.donggle.backend.infrastructure.client.exception.ClientException; | ||
import org.donggle.backend.infrastructure.client.tistory.dto.response.TistoryAccessTokenResponse; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.http.HttpStatusCode; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
import org.springframework.web.reactive.function.client.WebClient; | ||
import org.springframework.web.util.UriComponentsBuilder; | ||
|
||
import java.util.NoSuchElementException; | ||
|
||
@Service | ||
@Transactional | ||
public class TistoryConnectionClient { | ||
private static final String AUTHORIZE_URL = "https://www.tistory.com/oauth/authorize"; | ||
private static final String TOKEN_URL = "https://www.tistory.com/oauth"; | ||
private static final String PLATFORM_NAME = "Tistory"; | ||
|
||
private final String clientId; | ||
private final String clientSecret; | ||
private final WebClient webClient; | ||
private final MemberCredentialsRepository memberCredentialsRepository; | ||
private final MemberRepository memberRepository; | ||
private final TistoryApiClient tistoryApiService; | ||
|
||
@Autowired | ||
public TistoryConnectionClient(@Value("${tistory_client_id}") final String clientId, | ||
@Value("${tistory_client_secret}") final String clientSecret, | ||
final MemberCredentialsRepository memberCredentialsRepository, | ||
final MemberRepository memberRepository, | ||
final TistoryApiClient tistoryApiService) { | ||
this.clientId = clientId; | ||
this.clientSecret = clientSecret; | ||
this.memberCredentialsRepository = memberCredentialsRepository; | ||
this.memberRepository = memberRepository; | ||
this.tistoryApiService = tistoryApiService; | ||
this.webClient = WebClient.create(TOKEN_URL); | ||
} | ||
|
||
public TistoryConnectionClient(final String clientId, | ||
final String clientSecret, | ||
final MemberCredentialsRepository memberCredentialsRepository, | ||
final MemberRepository memberRepository, | ||
final TistoryApiClient tistoryApiService, | ||
final WebClient webClient) { | ||
this.clientId = clientId; | ||
this.clientSecret = clientSecret; | ||
this.memberCredentialsRepository = memberCredentialsRepository; | ||
this.memberRepository = memberRepository; | ||
this.tistoryApiService = tistoryApiService; | ||
this.webClient = webClient; | ||
} | ||
|
||
public String createAuthorizeRedirectUri(final String redirectUri) { | ||
return UriComponentsBuilder.fromUriString(AUTHORIZE_URL) | ||
.queryParam("client_id", clientId) | ||
.queryParam("redirect_uri", redirectUri) | ||
.queryParam("response_type", "code") | ||
.build() | ||
.toUriString(); | ||
} | ||
|
||
public void saveAccessToken(final Long memberId, final OAuthAccessTokenRequest oAuthAccessTokenRequest) { | ||
final Member member = findMember(memberId); | ||
final MemberCredentials memberCredentials = findMemberCredentials(member); | ||
|
||
final String accessToken = getAccessToken(oAuthAccessTokenRequest.code(), oAuthAccessTokenRequest.redirect_uri()); | ||
final String tistoryBlogName = tistoryApiService.findDefaultBlogName(accessToken); | ||
|
||
memberCredentials.updateTistory(accessToken, tistoryBlogName); | ||
} | ||
|
||
private String getAccessToken(final String code, final String redirectUri) { | ||
final String tokenUri = createTokenUri(redirectUri, code); | ||
|
||
return webClient.get() | ||
.uri(tokenUri) | ||
.accept(MediaType.APPLICATION_JSON) | ||
.retrieve() | ||
.onStatus(HttpStatusCode::is4xxClientError, clientResponse -> ClientException.handle4xxException(clientResponse.statusCode().value(), PLATFORM_NAME)) | ||
.onStatus(HttpStatusCode::is5xxServerError, clientResponse -> ClientException.handle5xxException(PLATFORM_NAME)) | ||
.bodyToMono(TistoryAccessTokenResponse.class) | ||
.block().access_token(); | ||
} | ||
|
||
public String createTokenUri(final String redirectUri, final String code) { | ||
return UriComponentsBuilder.fromUriString("/access_token") | ||
.queryParam("client_id", clientId) | ||
.queryParam("client_secret", clientSecret) | ||
.queryParam("redirect_uri", redirectUri) | ||
.queryParam("code", code) | ||
.queryParam("grant_type", "authorization_code") | ||
.build() | ||
.toUriString(); | ||
} | ||
|
||
public void deleteAccessToken(final Long memberId) { | ||
final Member member = findMember(memberId); | ||
final MemberCredentials memberCredentials = findMemberCredentials(member); | ||
|
||
memberCredentials.deleteTistoryConnection(); | ||
} | ||
|
||
private Member findMember(final Long memberId) { | ||
return memberRepository.findById(memberId) | ||
.orElseThrow(() -> new MemberNotFoundException(memberId)); | ||
} | ||
|
||
private MemberCredentials findMemberCredentials(final Member member) { | ||
return memberCredentialsRepository.findByMember(member) | ||
.orElseThrow(NoSuchElementException::new); | ||
} | ||
} | ||
//package org.donggle.backend.infrastructure.client.tistory; | ||
// | ||
//import org.donggle.backend.application.repository.MemberCredentialsRepository; | ||
//import org.donggle.backend.application.repository.MemberRepository; | ||
//import org.donggle.backend.application.service.request.OAuthAccessTokenRequest; | ||
//import org.donggle.backend.domain.member.Member; | ||
//import org.donggle.backend.domain.member.MemberCredentials; | ||
//import org.donggle.backend.exception.notfound.MemberNotFoundException; | ||
//import org.donggle.backend.infrastructure.client.exception.ClientException; | ||
//import org.donggle.backend.infrastructure.client.tistory.dto.response.TistoryAccessTokenResponse; | ||
//import org.springframework.beans.factory.annotation.Autowired; | ||
//import org.springframework.beans.factory.annotation.Value; | ||
//import org.springframework.http.HttpStatusCode; | ||
//import org.springframework.http.MediaType; | ||
//import org.springframework.stereotype.Service; | ||
//import org.springframework.transaction.annotation.Transactional; | ||
//import org.springframework.web.reactive.function.client.WebClient; | ||
//import org.springframework.web.util.UriComponentsBuilder; | ||
// | ||
//import java.util.NoSuchElementException; | ||
// | ||
//@Service | ||
//@Transactional | ||
//public class TistoryConnectionClient { | ||
// private static final String AUTHORIZE_URL = "https://www.tistory.com/oauth/authorize"; | ||
// private static final String TOKEN_URL = "https://www.tistory.com/oauth"; | ||
// private static final String PLATFORM_NAME = "Tistory"; | ||
// | ||
// private final String clientId; | ||
// private final String clientSecret; | ||
// private final WebClient webClient; | ||
// private final MemberCredentialsRepository memberCredentialsRepository; | ||
// private final MemberRepository memberRepository; | ||
// private final TistoryApiClient tistoryApiService; | ||
// | ||
// @Autowired | ||
// public TistoryConnectionClient(@Value("${tistory_client_id}") final String clientId, | ||
// @Value("${tistory_client_secret}") final String clientSecret, | ||
// final MemberCredentialsRepository memberCredentialsRepository, | ||
// final MemberRepository memberRepository | ||
// final TistoryApiClient tistoryApiService | ||
// ) { | ||
// this.clientId = clientId; | ||
// this.clientSecret = clientSecret; | ||
// this.memberCredentialsRepository = memberCredentialsRepository; | ||
// this.memberRepository = memberRepository; | ||
// this.tistoryApiService = tistoryApiService; | ||
// this.webClient = WebClient.create(TOKEN_URL); | ||
// } | ||
// | ||
// public TistoryConnectionClient(final String clientId, | ||
// final String clientSecret, | ||
// final MemberCredentialsRepository memberCredentialsRepository, | ||
// final MemberRepository memberRepository, | ||
// final TistoryApiClient tistoryApiService, | ||
// final WebClient webClient) { | ||
// this.clientId = clientId; | ||
// this.clientSecret = clientSecret; | ||
// this.memberCredentialsRepository = memberCredentialsRepository; | ||
// this.memberRepository = memberRepository; | ||
// this.tistoryApiService = tistoryApiService; | ||
// this.webClient = webClient; | ||
// } | ||
// | ||
// public String createAuthorizeRedirectUri(final String redirectUri) { | ||
// return UriComponentsBuilder.fromUriString(AUTHORIZE_URL) | ||
// .queryParam("client_id", clientId) | ||
// .queryParam("redirect_uri", redirectUri) | ||
// .queryParam("response_type", "code") | ||
// .build() | ||
// .toUriString(); | ||
// } | ||
// | ||
// public void saveAccessToken(final Long memberId, final OAuthAccessTokenRequest oAuthAccessTokenRequest) { | ||
// final Member member = findMember(memberId); | ||
// final MemberCredentials memberCredentials = findMemberCredentials(member); | ||
// | ||
// final String accessToken = getAccessToken(oAuthAccessTokenRequest.code(), oAuthAccessTokenRequest.redirect_uri()); | ||
// final String tistoryBlogName = tistoryApiService.findDefaultBlogName(accessToken); | ||
// | ||
// memberCredentials.updateTistory(accessToken, tistoryBlogName); | ||
// } | ||
// | ||
// private String getAccessToken(final String code, final String redirectUri) { | ||
// final String tokenUri = createTokenUri(redirectUri, code); | ||
// | ||
// return webClient.get() | ||
// .uri(tokenUri) | ||
// .accept(MediaType.APPLICATION_JSON) | ||
// .retrieve() | ||
// .onStatus(HttpStatusCode::is4xxClientError, clientResponse -> ClientException.handle4xxException(clientResponse.statusCode().value(), PLATFORM_NAME)) | ||
// .onStatus(HttpStatusCode::is5xxServerError, clientResponse -> ClientException.handle5xxException(PLATFORM_NAME)) | ||
// .bodyToMono(TistoryAccessTokenResponse.class) | ||
// .block().access_token(); | ||
// } | ||
// | ||
// public String createTokenUri(final String redirectUri, final String code) { | ||
// return UriComponentsBuilder.fromUriString("/access_token") | ||
// .queryParam("client_id", clientId) | ||
// .queryParam("client_secret", clientSecret) | ||
// .queryParam("redirect_uri", redirectUri) | ||
// .queryParam("code", code) | ||
// .queryParam("grant_type", "authorization_code") | ||
// .build() | ||
// .toUriString(); | ||
// } | ||
// | ||
// public void deleteAccessToken(final Long memberId) { | ||
// final Member member = findMember(memberId); | ||
// final MemberCredentials memberCredentials = findMemberCredentials(member); | ||
// | ||
// memberCredentials.deleteTistoryConnection(); | ||
// } | ||
// | ||
// private Member findMember(final Long memberId) { | ||
// return memberRepository.findById(memberId) | ||
// .orElseThrow(() -> new MemberNotFoundException(memberId)); | ||
// } | ||
// | ||
// private MemberCredentials findMemberCredentials(final Member member) { | ||
// return memberCredentialsRepository.findByMember(member) | ||
// .orElseThrow(NoSuchElementException::new); | ||
// } | ||
//} |
52 changes: 26 additions & 26 deletions
52
backend/src/main/java/org/donggle/backend/ui/BlogController.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,26 +1,26 @@ | ||
package org.donggle.backend.ui; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.donggle.backend.infrastructure.client.tistory.TistoryApiClient; | ||
import org.donggle.backend.ui.common.AuthenticationPrincipal; | ||
import org.donggle.backend.ui.response.TistoryCategoryListResponse; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("/blogs") | ||
public class BlogController { | ||
private final TistoryApiClient tistoryApiService; | ||
|
||
@GetMapping("/tistory/category") | ||
public ResponseEntity<TistoryCategoryListResponse> tistoryCategoryList( | ||
@AuthenticationPrincipal final Long memberId | ||
) { | ||
final TistoryCategoryListResponse response = tistoryApiService.findCategory(memberId); | ||
return ResponseEntity.ok(response); | ||
} | ||
} | ||
//package org.donggle.backend.ui; | ||
// | ||
//import lombok.RequiredArgsConstructor; | ||
//import org.donggle.backend.infrastructure.client.tistory.TistoryApiClient; | ||
//import org.donggle.backend.ui.common.AuthenticationPrincipal; | ||
//import org.donggle.backend.ui.response.TistoryCategoryListResponse; | ||
//import org.springframework.http.ResponseEntity; | ||
//import org.springframework.web.bind.annotation.GetMapping; | ||
//import org.springframework.web.bind.annotation.RequestMapping; | ||
//import org.springframework.web.bind.annotation.RestController; | ||
// | ||
// | ||
//@RestController | ||
//@RequiredArgsConstructor | ||
//@RequestMapping("/blogs") | ||
//public class BlogController { | ||
// private final TistoryApiClient tistoryApiService; | ||
// | ||
// @GetMapping("/tistory/category") | ||
// public ResponseEntity<TistoryCategoryListResponse> tistoryCategoryList( | ||
// @AuthenticationPrincipal final Long memberId | ||
// ) { | ||
// final TistoryCategoryListResponse response = tistoryApiService.findCategory(memberId); | ||
// return ResponseEntity.ok(response); | ||
// } | ||
//} |
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
Oops, something went wrong.