Skip to content
This repository has been archived by the owner on Jul 29, 2024. It is now read-only.

Commit

Permalink
fix: Tistory API 연결 종료 (#616)
Browse files Browse the repository at this point in the history
  • Loading branch information
echo724 authored Mar 11, 2024
1 parent a6fbf46 commit f22df6c
Show file tree
Hide file tree
Showing 11 changed files with 1,059 additions and 1,059 deletions.

Large diffs are not rendered by default.

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 backend/src/main/java/org/donggle/backend/ui/BlogController.java
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);
// }
//}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import org.donggle.backend.application.service.request.TokenAddRequest;
import org.donggle.backend.infrastructure.client.medium.MediumConnectionClient;
import org.donggle.backend.infrastructure.client.notion.NotionConnectionClient;
import org.donggle.backend.infrastructure.client.tistory.TistoryConnectionClient;
import org.donggle.backend.ui.common.AuthenticationPrincipal;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
Expand All @@ -21,38 +20,38 @@
@RequiredArgsConstructor
@RequestMapping("/connections")
public class ConnectionController {
private final TistoryConnectionClient tistoryConnectService;
// private final TistoryConnectionClient tistoryConnectService;
private final NotionConnectionClient notionConnectionService;
private final MediumConnectionClient mediumConnectionClient;

@GetMapping("/tistory/redirect")
public ResponseEntity<Void> connectionsRedirectTistory(
@RequestParam final String redirect_uri
) {
final String redirectUri = tistoryConnectService.createAuthorizeRedirectUri(redirect_uri);
return ResponseEntity
.status(HttpStatus.FOUND)
.header(HttpHeaders.LOCATION, redirectUri)
.build();
}
// @GetMapping("/tistory/redirect")
// public ResponseEntity<Void> connectionsRedirectTistory(
// @RequestParam final String redirect_uri
// ) {
// final String redirectUri = tistoryConnectService.createAuthorizeRedirectUri(redirect_uri);
// return ResponseEntity
// .status(HttpStatus.FOUND)
// .header(HttpHeaders.LOCATION, redirectUri)
// .build();
// }

@PostMapping("/tistory")
public ResponseEntity<Void> connectionsAddTistory(
@AuthenticationPrincipal final Long memberId,
@RequestBody final OAuthAccessTokenRequest oAuthAccessTokenRequest
) {
tistoryConnectService.saveAccessToken(memberId, oAuthAccessTokenRequest);
return ResponseEntity.ok().build();
}

@PostMapping("/tistory/disconnect")
public ResponseEntity<Void> connectionsDisconnectTistory(
@AuthenticationPrincipal final Long memberId
) {
tistoryConnectService.deleteAccessToken(memberId);
return ResponseEntity.ok().build();
}
// @PostMapping("/tistory")
// public ResponseEntity<Void> connectionsAddTistory(
// @AuthenticationPrincipal final Long memberId,
// @RequestBody final OAuthAccessTokenRequest oAuthAccessTokenRequest
// ) {
// tistoryConnectService.saveAccessToken(memberId, oAuthAccessTokenRequest);
// return ResponseEntity.ok().build();
// }

// @PostMapping("/tistory/disconnect")
// public ResponseEntity<Void> connectionsDisconnectTistory(
// @AuthenticationPrincipal final Long memberId
// ) {
// tistoryConnectService.deleteAccessToken(memberId);
// return ResponseEntity.ok().build();
// }
//
@GetMapping("/notion/redirect")
public ResponseEntity<Void> connectionsRedirectNotion(
@RequestParam final String redirect_uri
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@
public class PublishController {
private final PublishFacadeService blogService;

@PostMapping("/tistory")
public ResponseEntity<Void> publishToTistory(
@AuthenticationPrincipal final Long memberId,
@PathVariable final Long writingId,
@Valid @RequestBody final PublishRequest request
) {
blogService.publishWriting(memberId, writingId, BlogType.TISTORY, PublishRequest.tistory(request));
return ResponseEntity.ok().build();
}
// @PostMapping("/tistory")
// public ResponseEntity<Void> publishToTistory(
// @AuthenticationPrincipal final Long memberId,
// @PathVariable final Long writingId,
// @Valid @RequestBody final PublishRequest request
// ) {
// blogService.publishWriting(memberId, writingId, BlogType.TISTORY, PublishRequest.tistory(request));
// return ResponseEntity.ok().build();
// }

@PostMapping("/medium")
public ResponseEntity<Void> publishToMedium(
Expand Down
Loading

0 comments on commit f22df6c

Please sign in to comment.