refactor(googbooks): move google books key to HTTP header (#2533)
This commit is contained in:
@@ -34,6 +34,7 @@ import java.util.stream.Stream;
|
|||||||
@Service
|
@Service
|
||||||
public class GoogleParser implements BookParser {
|
public class GoogleParser implements BookParser {
|
||||||
|
|
||||||
|
private static final String HEADER_API_KEY = "X-Goog-Api-Key"; // From https://docs.cloud.google.com/apis/docs/system-parameters
|
||||||
private static final Pattern FOUR_DIGIT_YEAR_PATTERN = Pattern.compile("^(\\d{4})$");
|
private static final Pattern FOUR_DIGIT_YEAR_PATTERN = Pattern.compile("^(\\d{4})$");
|
||||||
private static final Pattern YEAR_MONTH_PATTERN = Pattern.compile("^(\\d{4})-(\\d{2})$");
|
private static final Pattern YEAR_MONTH_PATTERN = Pattern.compile("^(\\d{4})-(\\d{2})$");
|
||||||
private static final Pattern WHITESPACE_PATTERN = Pattern.compile("\\s+");
|
private static final Pattern WHITESPACE_PATTERN = Pattern.compile("\\s+");
|
||||||
@@ -141,6 +142,9 @@ public class GoogleParser implements BookParser {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private List<BookMetadata> fetchFromApi(String query, boolean isIsbnSearch) {
|
private List<BookMetadata> fetchFromApi(String query, boolean isIsbnSearch) {
|
||||||
|
var googleSettings = getSettings();
|
||||||
|
String apiKey = googleSettings.getApiKey();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
waitForRateLimit();
|
waitForRateLimit();
|
||||||
|
|
||||||
@@ -154,12 +158,17 @@ public class GoogleParser implements BookParser {
|
|||||||
|
|
||||||
URI uri = uriBuilder.build().toUri();
|
URI uri = uriBuilder.build().toUri();
|
||||||
|
|
||||||
log.info("Google Books API URL: {}", uri);
|
log.debug("Google Books API URL: {}", uri);
|
||||||
|
|
||||||
HttpRequest request = HttpRequest.newBuilder()
|
var requestBuilder = HttpRequest.newBuilder()
|
||||||
.uri(uri)
|
.uri(uri)
|
||||||
.GET()
|
.GET();
|
||||||
.build();
|
|
||||||
|
if (apiKey != null && !apiKey.isBlank()) {
|
||||||
|
requestBuilder.setHeader(HEADER_API_KEY, apiKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
HttpRequest request = requestBuilder.build();
|
||||||
|
|
||||||
HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
|
HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
|
||||||
|
|
||||||
@@ -571,12 +580,24 @@ public class GoogleParser implements BookParser {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private MetadataProviderSettings.Google getSettings() {
|
||||||
|
var appSettings = appSettingService.getAppSettings();
|
||||||
|
|
||||||
|
if (
|
||||||
|
appSettings == null ||
|
||||||
|
appSettings.getMetadataProviderSettings() == null ||
|
||||||
|
appSettings.getMetadataProviderSettings().getGoogle() == null
|
||||||
|
) {
|
||||||
|
return new MetadataProviderSettings.Google();
|
||||||
|
}
|
||||||
|
|
||||||
|
return appSettings.getMetadataProviderSettings().getGoogle();
|
||||||
|
}
|
||||||
|
|
||||||
private String getApiUrl() {
|
private String getApiUrl() {
|
||||||
MetadataProviderSettings.Google googleSettings = appSettingService.getAppSettings()
|
var googleSettings = getSettings();
|
||||||
.getMetadataProviderSettings().getGoogle();
|
|
||||||
|
|
||||||
String language = googleSettings.getLanguage();
|
String language = googleSettings.getLanguage();
|
||||||
String apiKey = googleSettings.getApiKey();
|
|
||||||
|
|
||||||
UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(GOOGLE_BOOKS_API_URL);
|
UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(GOOGLE_BOOKS_API_URL);
|
||||||
|
|
||||||
@@ -584,10 +605,6 @@ public class GoogleParser implements BookParser {
|
|||||||
builder.queryParam("langRestrict", language);
|
builder.queryParam("langRestrict", language);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (apiKey != null && !apiKey.isBlank()) {
|
|
||||||
builder.queryParam("key", apiKey);
|
|
||||||
}
|
|
||||||
|
|
||||||
return builder.build().toUri().toString();
|
return builder.build().toUri().toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user