Skip to content

Commit

Permalink
feat: spring boot update
Browse files Browse the repository at this point in the history
  • Loading branch information
Angular2Guy committed Nov 24, 2023
1 parent 0af0b63 commit 5d7dd91
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 26 deletions.
4 changes: 0 additions & 4 deletions backend/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,6 @@ http://www.apache.org/licenses/LICENSE-2.0
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@
package ch.xxx.moviemanager.adapter.client;

import java.net.URI;
import java.time.Duration;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.client.RestClient;

import com.fasterxml.jackson.core.JacksonException;
import com.fasterxml.jackson.databind.ObjectMapper;
Expand All @@ -41,18 +41,16 @@ public MovieDbRestClientBean(ObjectMapper objectMapper) {
}

public WrapperGenereDto fetchAllGeneres(String moviedbkey) {
WrapperGenereDto result = WebClient
WrapperGenereDto result = RestClient
.create().get().uri(URI.create(String
.format("https://api.themoviedb.org/3/genre/movie/list?api_key=%s&language=en-US", moviedbkey)))
.retrieve().bodyToMono(WrapperGenereDto.class).block(Duration.ofSeconds(10L));
.retrieve().body(WrapperGenereDto.class);
return result;
}

public MovieDto fetchMovie(String moviedbkey, long movieDbId) {
MovieDto wrMovie = WebClient.create().get()
.uri(URI.create(String.format("https://api.themoviedb.org/3/movie/%d?api_key=%s&language=en-US",
movieDbId, moviedbkey)))
.retrieve().bodyToMono(MovieDto.class).block(Duration.ofSeconds(10L));
MovieDto wrMovie = RestClient.create().get().uri(URI.create(String.format("https://api.themoviedb.org/3/movie/%d?api_key=%s&language=en-US",
movieDbId, moviedbkey))).retrieve().body(MovieDto.class);
return wrMovie;
}

Expand All @@ -65,24 +63,32 @@ private <T> T parseJsonToDto(String bodyStr, Class<T> result) {
}
}

public WrapperCastDto fetchCast(String moviedbkey, Long movieId) {
WrapperCastDto wrCast = WebClient.create().get()
public WrapperCastDto fetchCast(String moviedbkey, Long movieId, Long delay) {
waitFor(delay);
WrapperCastDto wrCast = RestClient.create().get()
.uri(URI.create(
String.format("https://api.themoviedb.org/3/movie/%d/credits?api_key=%s", movieId, moviedbkey)))
.retrieve().bodyToMono(WrapperCastDto.class).delayElement(Duration.ofMillis(300L))
.block(Duration.ofSeconds(10L));
.retrieve().body(WrapperCastDto.class);
return wrCast;
}

private void waitFor(long millis) {
try {
TimeUnit.MILLISECONDS.sleep(millis);
} catch (InterruptedException ie) {
Thread.currentThread().interrupt();
}
}

public ActorDto fetchActor(String moviedbkey, Integer castId, Long delay) {
ActorDto actor = WebClient.create().get().uri(URI.create(
this.waitFor(delay);
ActorDto actor = RestClient.create().get().uri(URI.create(
String.format("https://api.themoviedb.org/3/person/%d?api_key=%s&language=en-US", castId, moviedbkey)))
.retrieve().bodyToMono(ActorDto.class)
.retrieve().body(ActorDto.class);
// .bodyToMono(String.class)
// .map(bodyStr -> {
// return parseJsonToDto(bodyStr, ActorDto.class);
// })
.delayElement(Duration.ofMillis(delay)).block(Duration.ofSeconds(10L));
// })
return actor;
}

Expand All @@ -91,9 +97,9 @@ public ActorDto fetchActor(String moviedbkey, Integer castId) {
}

public WrapperMovieDto fetchImportMovie(String moviedbkey, String queryStr) {
WrapperMovieDto wrMovie = WebClient.create().get().uri(URI.create(String.format(
WrapperMovieDto wrMovie = RestClient.create().get().uri(URI.create(String.format(
"https://api.themoviedb.org/3/search/movie?api_key=%s&language=en-US&query=%s&page=1&include_adult=false",
moviedbkey, queryStr))).retrieve().bodyToMono(WrapperMovieDto.class).block(Duration.ofSeconds(10L));
moviedbkey, queryStr))).retrieve().body(WrapperMovieDto.class);
MovieDto[] movieArray = Arrays.stream(wrMovie.getResults()).map(movieDto -> {
movieDto.setMovieId(movieDto.getId());
movieDto.setId(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
public interface MovieDbRestClient {
MovieDto fetchMovie(String moviedbkey, long movieDbId);

WrapperCastDto fetchCast(String moviedbkey, Long movieId);
WrapperCastDto fetchCast(String moviedbkey, Long movieId, Long delay);

ActorDto fetchActor(String moviedbkey, Integer castId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import java.util.Base64;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;
Expand Down Expand Up @@ -220,7 +219,7 @@ public boolean importMovie(int movieDbId, String bearerStr) throws InterruptedEx
movieEntity.getUsers().add(user);
}
WrapperCastDto wrCast = this.movieDbRestClient.fetchCast(this.decrypt(user.getMoviedbkey(), user.getUuid()),
movieDto.getId());
movieDto.getId(), 300L);
if (movieEntity.getCast().isEmpty()) {
for (CastDto c : wrCast.getCast()) {
LOG.info("Creating new cast for movie");
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.1.4</version>
<version>3.2.0</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>

Expand Down

0 comments on commit 5d7dd91

Please sign in to comment.