-
Notifications
You must be signed in to change notification settings - Fork 4
SpotifyPlayList-dev-009-be-fix-recommend-api-v3 #211
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
base: main
Are you sure you want to change the base?
Conversation
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.
Summary of Changes
Hello @yennanliu, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!
This pull request implements a significant architectural change for the Spotify recommendation API integration. The system transitions from using a Spotify Java SDK to a direct, custom implementation leveraging Spring's RestTemplate
for API calls. This change aims to provide more granular control over the recommendation request and response process, although it necessitates a re-evaluation and re-implementation of the playlist-based recommendation feature.
Highlights
- API Integration Strategy Shift: The backend's approach to fetching Spotify recommendations has been refactored. It now directly interacts with the Spotify Web API using Spring's
RestTemplate
instead of relying on a third-party Spotify Java SDK. - New Service Implementation: A new service,
CustomSpotifyRecommendationService
, has been introduced. This service is responsible for constructing Spotify API recommendation URLs, handlingBearer
token authentication, and executing direct HTTPGET
requests to the Spotify API. - Controller Adaptation: The
RecommendationsController
has been updated to utilize the newCustomSpotifyRecommendationService
. It now directly returns the raw JSON response received from the Spotify API, moving away from SDK-specific model objects. - Feature Status Update: The playlist-based recommendation endpoint (
/playlist/{playListId}
) is temporarily marked as 'not implemented' with aTODO
comment, indicating that this specific feature requires re-implementation with the new custom service. - Configuration Update: The
application.properties
file has been updated to include a configurablespotify.api.base-url
property, allowing the base URL for Spotify API calls to be easily managed.
Using Gemini Code Assist
The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.
Invoking Gemini
You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command>
or @gemini-code-assist <command>
. Below is a summary of the supported commands.
Feature | Command | Description |
---|---|---|
Code Review | /gemini review |
Performs a code review for the current pull request in its current state. |
Pull Request Summary | /gemini summary |
Provides a summary of the current pull request in its current state. |
Comment | @gemini-code-assist | Responds in comments when explicitly tagged, both in issue comments and review comments. |
Help | /gemini help |
Displays a list of available commands. |
Customization
To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/
folder in the base of the repository. Detailed instructions can be found here.
Limitations & Feedback
Gemini Code Assist is currently in preview and may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments to provide feedback.
You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.
Footnotes
-
Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩
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.
Code Review
This pull request refactors the recommendation feature to use a custom service that calls the Spotify API directly via RestTemplate, instead of using a third-party library. The changes introduce a new service and update the controller accordingly. My review focuses on improving robustness by configuring RestTemplate with timeouts, enhancing logging for better debuggability, simplifying controller logic, and preventing potential API errors by adding checks for optional parameters.
Recommendations recommendations = recommendationsService.getRecommendation(getRecommendationsDto); | ||
return ResponseEntity.status(HttpStatus.OK).body(recommendations); | ||
ResponseEntity<String> recommendations = recommendationsService.getRecommendations(getRecommendationsDto); | ||
return ResponseEntity.status(recommendations.getStatusCode()).body(recommendations.getBody()); | ||
} catch (Exception e) { | ||
log.error("getRecommendation error : " + e); |
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.
When logging exceptions, it's crucial to include the full stack trace. Logging just e.getMessage()
can hide the root cause and make debugging significantly harder. Pass the exception object itself to the logger.
log.error("getRecommendation error : " + e); | |
log.error("getRecommendation error", e); |
return response; | ||
|
||
} catch (Exception e) { | ||
log.error("Error getting recommendations: {}", e.getMessage()); |
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.
When logging exceptions, it's crucial to include the full stack trace for effective debugging. Logging only the message via e.getMessage()
loses valuable context about where and why the error occurred. Please pass the full exception object to the logger.
log.error("Error getting recommendations: {}", e.getMessage()); | |
log.error("Error getting recommendations", e); |
builder.queryParam("limit", request.getAmount()); | ||
builder.queryParam("market", request.getMarket()); | ||
builder.queryParam("min_popularity", request.getMinPopularity()); | ||
builder.queryParam("max_popularity", request.getMaxPopularity()); | ||
builder.queryParam("target_popularity", request.getTargetPopularity()); |
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.
These query parameters are being added to the URL unconditionally. If properties like amount
, market
, or the popularity fields in the DTO can be null
(which they should be if they are optional), this could result in sending parameters with null values, or default primitive values (e.g., 0 for an int
) which might be invalid for the Spotify API.
You should add these parameters only if they have a non-null value, similar to how you handle the seed parameters.
if (request.getAmount() != null) {
builder.queryParam("limit", request.getAmount());
}
if (request.getMarket() != null) {
builder.queryParam("market", request.getMarket());
}
if (request.getMinPopularity() != null) {
builder.queryParam("min_popularity", request.getMinPopularity());
}
if (request.getMaxPopularity() != null) {
builder.queryParam("max_popularity", request.getMaxPopularity());
}
if (request.getTargetPopularity() != null) {
builder.queryParam("target_popularity", request.getTargetPopularity());
}
@Bean | ||
public RestTemplate restTemplate() { | ||
return new RestTemplate(); | ||
} |
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.
Creating a RestTemplate
with new RestTemplate()
is not recommended as it comes with no timeouts configured. This can lead to your application threads being blocked indefinitely if the remote service is unresponsive, potentially causing resource exhaustion.
It's a best practice to configure connection and read timeouts. The recommended way to create RestTemplate
beans in Spring Boot is by using RestTemplateBuilder
, which allows for easy configuration.
|
||
@PostMapping("/") | ||
public ResponseEntity getRecommendation(@RequestBody GetRecommendationsDto getRecommendationsDto) { | ||
public ResponseEntity<?> getRecommendation(@RequestBody GetRecommendationsDto getRecommendationsDto) { | ||
try { | ||
log.info("(getRecommendation) getRecommendationsDto = " + getRecommendationsDto.toString()); |
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.
For logging, it's better to use parameterized messages instead of string concatenation. This improves performance because the string is only formatted if the log level is enabled, avoiding unnecessary toString()
calls.
log.info("(getRecommendation) getRecommendationsDto = " + getRecommendationsDto.toString()); | |
log.info("(getRecommendation) getRecommendationsDto = {}", getRecommendationsDto); |
ResponseEntity<String> recommendations = recommendationsService.getRecommendations(getRecommendationsDto); | ||
return ResponseEntity.status(recommendations.getStatusCode()).body(recommendations.getBody()); |
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.
The service method getRecommendations
already returns a ResponseEntity<String>
. You can return this directly from the controller method, which simplifies the code and avoids creating a new ResponseEntity
instance unnecessarily. The return type ResponseEntity<?>
is compatible.
ResponseEntity<String> recommendations = recommendationsService.getRecommendations(getRecommendationsDto); | |
return ResponseEntity.status(recommendations.getStatusCode()).body(recommendations.getBody()); | |
return recommendationsService.getRecommendations(getRecommendationsDto); |
@GetMapping("/playlist/{playListId}") | ||
public ResponseEntity getRecommendationWithPlayList(@PathVariable("playListId") String playListId) { | ||
public ResponseEntity<?> getRecommendationWithPlayList(@PathVariable("playListId") String playListId) { | ||
try { |
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.
This try-catch
block is unnecessary. The code within the try
block is unlikely to throw an exception that needs to be handled here. The catch
block is effectively dead code. Removing the try-catch
will make the code cleaner and simpler.
log.info("(getRecommendationWithPlayList) playListId = {}", playListId);
…IAuthService interface
No description provided.