-
Notifications
You must be signed in to change notification settings - Fork 0
[feat] google API 연동 #23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
38 changes: 38 additions & 0 deletions
38
src/main/java/dmu/dasom/api/domain/google/controller/GoogleController.java
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| package dmu.dasom.api.domain.google.controller; | ||
|
|
||
| import dmu.dasom.api.domain.google.service.GoogleApiService; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.beans.factory.annotation.Value; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.web.bind.annotation.PostMapping; | ||
| import org.springframework.web.bind.annotation.RequestMapping; | ||
| import org.springframework.web.bind.annotation.RequestParam; | ||
| import org.springframework.web.bind.annotation.RestController; | ||
|
|
||
| import java.util.Collections; | ||
| import java.util.List; | ||
|
|
||
| @RestController | ||
| @RequestMapping("/google") | ||
| @RequiredArgsConstructor | ||
| public class GoogleController { | ||
|
|
||
| private final GoogleApiService googleApiService; | ||
| @Value("${google.spreadsheet.id}") | ||
| private String spreadsheetId; | ||
|
|
||
| private static final String RANGE = "A1"; | ||
|
|
||
| @PostMapping("/write") | ||
| public ResponseEntity<String> writeToSheet(@RequestParam String word){ | ||
| try{ | ||
| List<List<Object>> values = List.of(Collections.singletonList(word)); | ||
|
|
||
| googleApiService.writeToSheet(spreadsheetId, RANGE, values); | ||
| return ResponseEntity.ok("Data written successfully to the spreadsheet" + word); | ||
| } catch (Exception e){ | ||
| e.printStackTrace(); | ||
| return ResponseEntity.internalServerError().body("Failed to write data to the spreadsheet" + e.getMessage()); | ||
| } | ||
| } | ||
| } | ||
66 changes: 66 additions & 0 deletions
66
src/main/java/dmu/dasom/api/domain/google/service/GoogleApiService.java
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| package dmu.dasom.api.domain.google.service; | ||
|
|
||
| import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; | ||
| import com.google.api.client.json.JsonFactory; | ||
| import com.google.api.client.json.jackson2.JacksonFactory; | ||
|
|
||
| import com.google.api.services.sheets.v4.Sheets; | ||
| import com.google.api.services.sheets.v4.model.UpdateValuesResponse; | ||
| import com.google.api.services.sheets.v4.model.ValueRange; | ||
| import com.google.auth.http.HttpCredentialsAdapter; | ||
| import com.google.auth.oauth2.GoogleCredentials; | ||
| import dmu.dasom.api.domain.common.exception.CustomException; | ||
| import dmu.dasom.api.domain.common.exception.ErrorCode; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
| import org.springframework.beans.factory.annotation.Value; | ||
| import org.springframework.core.io.ClassPathResource; | ||
| import org.springframework.stereotype.Service; | ||
|
|
||
| import java.io.IOException; | ||
| import java.security.GeneralSecurityException; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
|
|
||
| @Service | ||
| public class GoogleApiService { | ||
|
|
||
| private static final Logger logger = LoggerFactory.getLogger(GoogleApiService.class); | ||
| private static final String APPLICATION_NAME = "Recruit Form"; | ||
| private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance(); | ||
| @Value("${google.credentials.file.path}") | ||
| private String credentialsFilePath; | ||
| private Sheets sheetsService; | ||
|
|
||
| // 해당 메소드는 sheets의 인스턴스를 얻는데 사용 | ||
| private Sheets getSheetsService() throws IOException, GeneralSecurityException{ | ||
| if(sheetsService == null){ | ||
| GoogleCredentials credentials = GoogleCredentials | ||
| .fromStream(new ClassPathResource(credentialsFilePath).getInputStream()) | ||
| .createScoped(Collections.singletonList("https://www.googleapis.com/auth/spreadsheets")); | ||
| sheetsService = new Sheets.Builder(GoogleNetHttpTransport.newTrustedTransport(), JSON_FACTORY, new HttpCredentialsAdapter(credentials)) | ||
| .setApplicationName(APPLICATION_NAME) | ||
| .build(); | ||
| } | ||
| return sheetsService; | ||
| } | ||
|
|
||
| public void writeToSheet(String spreadsheetId, String range, List<List<Object>> values) { | ||
| try { | ||
| Sheets service = getSheetsService(); | ||
| ValueRange body = new ValueRange().setValues(values); | ||
| UpdateValuesResponse result = service.spreadsheets().values() | ||
| .update(spreadsheetId, range, body) | ||
| .setValueInputOption("USER_ENTERED") | ||
| .execute(); | ||
| logger.info("Updated rows: {}", result.getUpdatedRows()); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Logger는 |
||
| } catch (IOException e) { | ||
| logger.error("Failed to write data to the spreadsheet", e); | ||
| throw new CustomException(ErrorCode.WRITE_FAIL); | ||
| } catch (GeneralSecurityException e) { | ||
| logger.error("Failed to write data to the spreadsheet", e); | ||
| throw new CustomException(ErrorCode.INTERNAL_SERVER_ERROR); | ||
| } | ||
| } | ||
|
|
||
| } | ||
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이 컨트롤러 클래스는 어디에 사용되는 건가요?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
아직 지원자 컨트롤러가 없어서 만들었습니다. 추후 recruit 컨트롤러 만들어지면 그쪽으로 옮길예정입니다.