fix(hardcover): use injected restclient with expected timeouts (#2320)
This commit is contained in:
@@ -43,12 +43,14 @@ public class HardcoverSyncService {
|
|||||||
private final ThreadLocal<String> currentApiToken = new ThreadLocal<>();
|
private final ThreadLocal<String> currentApiToken = new ThreadLocal<>();
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
public HardcoverSyncService(HardcoverSyncSettingsService hardcoverSyncSettingsService, BookRepository bookRepository) {
|
public HardcoverSyncService(
|
||||||
|
HardcoverSyncSettingsService hardcoverSyncSettingsService,
|
||||||
|
BookRepository bookRepository,
|
||||||
|
RestClient restClient
|
||||||
|
) {
|
||||||
this.hardcoverSyncSettingsService = hardcoverSyncSettingsService;
|
this.hardcoverSyncSettingsService = hardcoverSyncSettingsService;
|
||||||
this.bookRepository = bookRepository;
|
this.bookRepository = bookRepository;
|
||||||
this.restClient = RestClient.builder()
|
this.restClient = restClient;
|
||||||
.baseUrl(HARDCOVER_API_URL)
|
|
||||||
.build();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -795,7 +797,7 @@ public class HardcoverSyncService {
|
|||||||
private Map<String, Object> executeGraphQL(GraphQLRequest request) {
|
private Map<String, Object> executeGraphQL(GraphQLRequest request) {
|
||||||
try {
|
try {
|
||||||
return restClient.post()
|
return restClient.post()
|
||||||
.uri("")
|
.uri(HARDCOVER_API_URL)
|
||||||
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
|
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
|
||||||
.header(HttpHeaders.AUTHORIZATION, "Bearer " + getApiToken())
|
.header(HttpHeaders.AUTHORIZATION, "Bearer " + getApiToken())
|
||||||
.body(request)
|
.body(request)
|
||||||
|
|||||||
+4
-6
@@ -16,7 +16,7 @@ import java.util.concurrent.atomic.AtomicLong;
|
|||||||
@Slf4j
|
@Slf4j
|
||||||
@Service
|
@Service
|
||||||
public class HardcoverBookSearchService {
|
public class HardcoverBookSearchService {
|
||||||
|
private static final String GRAPHQL_URI = "https://api.hardcover.app/v1/graphql";
|
||||||
public static final int DEFAULT_PER_PAGE = 10;
|
public static final int DEFAULT_PER_PAGE = 10;
|
||||||
private static final long INITIAL_DELAY_MS = 1000;
|
private static final long INITIAL_DELAY_MS = 1000;
|
||||||
private static final long MAX_DELAY_MS = 15000;
|
private static final long MAX_DELAY_MS = 15000;
|
||||||
@@ -27,11 +27,9 @@ public class HardcoverBookSearchService {
|
|||||||
private final AtomicLong lastRequestTime = new AtomicLong(0);
|
private final AtomicLong lastRequestTime = new AtomicLong(0);
|
||||||
private final AtomicLong successCount = new AtomicLong(0);
|
private final AtomicLong successCount = new AtomicLong(0);
|
||||||
|
|
||||||
public HardcoverBookSearchService(AppSettingService appSettingService) {
|
public HardcoverBookSearchService(AppSettingService appSettingService, RestClient restClient) {
|
||||||
this.appSettingService = appSettingService;
|
this.appSettingService = appSettingService;
|
||||||
this.restClient = RestClient.builder()
|
this.restClient = restClient;
|
||||||
.baseUrl("https://api.hardcover.app/v1/graphql")
|
|
||||||
.build();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<GraphQLResponse.BookWithEditions> searchBookByIsbn(List<String> isbn) {
|
public List<GraphQLResponse.BookWithEditions> searchBookByIsbn(List<String> isbn) {
|
||||||
@@ -169,7 +167,7 @@ public class HardcoverBookSearchService {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
T response = restClient.post()
|
T response = restClient.post()
|
||||||
.uri("")
|
.uri(GRAPHQL_URI)
|
||||||
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
|
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
|
||||||
.header(HttpHeaders.AUTHORIZATION, "Bearer " + apiToken)
|
.header(HttpHeaders.AUTHORIZATION, "Bearer " + apiToken)
|
||||||
.body(body)
|
.body(body)
|
||||||
|
|||||||
@@ -6,10 +6,10 @@ import org.booklore.model.entity.BookMetadataEntity;
|
|||||||
import org.booklore.repository.BookRepository;
|
import org.booklore.repository.BookRepository;
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.jupiter.api.DisplayName;
|
import org.junit.jupiter.api.DisplayName;
|
||||||
import org.junit.jupiter.api.Nested;
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.junit.jupiter.api.extension.ExtendWith;
|
import org.junit.jupiter.api.extension.ExtendWith;
|
||||||
import org.mockito.ArgumentMatchers;
|
import org.mockito.ArgumentMatchers;
|
||||||
|
import org.mockito.InjectMocks;
|
||||||
import org.mockito.Mock;
|
import org.mockito.Mock;
|
||||||
import org.mockito.junit.jupiter.MockitoExtension;
|
import org.mockito.junit.jupiter.MockitoExtension;
|
||||||
import org.mockito.junit.jupiter.MockitoSettings;
|
import org.mockito.junit.jupiter.MockitoSettings;
|
||||||
@@ -48,6 +48,7 @@ class HardcoverSyncServiceTest {
|
|||||||
@Mock
|
@Mock
|
||||||
private RestClient.ResponseSpec responseSpec;
|
private RestClient.ResponseSpec responseSpec;
|
||||||
|
|
||||||
|
@InjectMocks
|
||||||
private HardcoverSyncService service;
|
private HardcoverSyncService service;
|
||||||
|
|
||||||
private BookEntity testBook;
|
private BookEntity testBook;
|
||||||
@@ -60,12 +61,7 @@ class HardcoverSyncServiceTest {
|
|||||||
@BeforeEach
|
@BeforeEach
|
||||||
void setUp() throws Exception {
|
void setUp() throws Exception {
|
||||||
// Create service with mocked dependencies
|
// Create service with mocked dependencies
|
||||||
service = new HardcoverSyncService(hardcoverSyncSettingsService, bookRepository);
|
service = new HardcoverSyncService(hardcoverSyncSettingsService, bookRepository, restClient);
|
||||||
|
|
||||||
// Inject our mocked restClient using reflection
|
|
||||||
Field restClientField = HardcoverSyncService.class.getDeclaredField("restClient");
|
|
||||||
restClientField.setAccessible(true);
|
|
||||||
restClientField.set(service, restClient);
|
|
||||||
|
|
||||||
testBook = new BookEntity();
|
testBook = new BookEntity();
|
||||||
testBook.setId(TEST_BOOK_ID);
|
testBook.setId(TEST_BOOK_ID);
|
||||||
|
|||||||
+6
-1
@@ -7,8 +7,10 @@ import org.junit.jupiter.api.BeforeEach;
|
|||||||
import org.junit.jupiter.api.DisplayName;
|
import org.junit.jupiter.api.DisplayName;
|
||||||
import org.junit.jupiter.api.Nested;
|
import org.junit.jupiter.api.Nested;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.mockito.InjectMocks;
|
||||||
import org.mockito.Mock;
|
import org.mockito.Mock;
|
||||||
import org.mockito.MockitoAnnotations;
|
import org.mockito.MockitoAnnotations;
|
||||||
|
import org.springframework.web.client.RestClient;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@@ -20,12 +22,15 @@ class HardcoverBookSearchServiceTest {
|
|||||||
@Mock
|
@Mock
|
||||||
private AppSettingService appSettingService;
|
private AppSettingService appSettingService;
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private RestClient restClient;
|
||||||
|
|
||||||
|
@InjectMocks
|
||||||
private HardcoverBookSearchService searchService;
|
private HardcoverBookSearchService searchService;
|
||||||
|
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
void setUp() {
|
void setUp() {
|
||||||
MockitoAnnotations.openMocks(this);
|
MockitoAnnotations.openMocks(this);
|
||||||
searchService = new HardcoverBookSearchService(appSettingService);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setupAppSettingsWithToken(String token) {
|
private void setupAppSettingsWithToken(String token) {
|
||||||
|
|||||||
Reference in New Issue
Block a user