Skip to content

Commit b522fa1

Browse files
Merge pull request #126 from bestreads/add-email-to-open-lib-requests
Add email to open lib requests
2 parents 9a32045 + 3e5d7f1 commit b522fa1

3 files changed

Lines changed: 17 additions & 12 deletions

File tree

internal/config/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ type Config struct {
3131
TokenRefreshPath string `mapstructure:"TOKEN_REFRESH_PATH"`
3232
TokenSecureFlag bool `mapstructure:"TOKEN_SECURE_FLAG"`
3333
PaginationSteps int `mapstructure:"PAGINATION_STEPS"`
34+
OpenLibraryRequestEmail string `mapstructure:"OPEN_LIBRARY_REQUEST_EMAIL"`
3435
}
3536

3637
func Load() *Config {

internal/handlers/book_search.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ func BookSearch(c *fiber.Ctx) error {
1414
ctx := c.UserContext()
1515
log := middlewares.Logger(ctx)
1616
cfg := middlewares.Config(ctx)
17-
httpClient := middlewares.HttpClient(ctx)
1817

1918
// Get offset from optional query param
2019
offset := c.Query("offset")
@@ -66,7 +65,7 @@ func BookSearch(c *fiber.Ctx) error {
6665
log.Debug().Int("localResults", len(books)).Int("limit", cfg.PaginationSteps).Msg("Not enough local results, searching Open Library API")
6766

6867
// Re-query DB to get all books including newly added ones
69-
if err := services.SearchOpenLibrary(httpClient, ctx, query, cfg.PaginationSteps, author); err != nil {
68+
if err := services.SearchOpenLibrary(ctx, query, cfg.PaginationSteps, author); err != nil {
7069
log.Error().Err(err).Msg("Error searching in Open Library")
7170
return c.Status(fiber.StatusInternalServerError).
7271
JSON(dtos.GenericRestErrorResponse{

internal/services/openlibrary.go

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,15 @@ import (
1212
"github.com/bestreads/Backend/internal/middlewares"
1313
"gorm.io/gorm"
1414
"gorm.io/gorm/clause"
15-
"resty.dev/v3"
1615
)
1716

1817
const openLibrarySearchURL = "https://openlibrary.org/search.json" // Open Library Search-Endpoint
1918
const openLibraryBaseURL = "https://openlibrary.org" // Basis-URL für Work-Details und Beschreibungen
20-
const maxConcurrentRequests = 7 // Rate limiting: maximal 7 gleichzeitige Requests
2119

2220
// SearchOpenLibrary führt eine OpenLibrary-Suche aus, lädt parallel zugehörige Work-Descriptions
2321
// und speichert die gefundenen Bücher inklusive Beschreibung und Cover-URL in der Datenbank.
24-
func SearchOpenLibrary(httpClient *resty.Client, ctx context.Context, query string, limit int, searchAuthors bool) error {
25-
response, err := searchBooks(ctx, httpClient, query, limit, searchAuthors)
22+
func SearchOpenLibrary(ctx context.Context, query string, limit int, searchAuthors bool) error {
23+
response, err := searchBooks(ctx, query, limit, searchAuthors)
2624
if err != nil {
2725
return err
2826
}
@@ -38,7 +36,7 @@ func SearchOpenLibrary(httpClient *resty.Client, ctx context.Context, query stri
3836
defer wg.Done()
3937

4038
// hier holen wir die daten, seperat vom sperren
41-
single, err := metadataSingle(httpClient, ctx, book)
39+
single, err := metadataSingle(ctx, book)
4240
if err != nil {
4341
log := middlewares.Logger(ctx)
4442
log.Err(err).Msg(fmt.Sprintf("worker %d returned an error", i))
@@ -67,13 +65,16 @@ func SearchOpenLibrary(httpClient *resty.Client, ctx context.Context, query stri
6765
return nil
6866
}
6967

70-
func searchBooks(ctx context.Context, c *resty.Client, query string, limit int, author bool) (dtos.OpenLibraryResponse, error) {
68+
func searchBooks(ctx context.Context, query string, limit int, author bool) (dtos.OpenLibraryResponse, error) {
69+
httpClient := middlewares.HttpClient(ctx)
70+
cfg := middlewares.Config(ctx)
7171

7272
var response dtos.OpenLibraryResponse
7373

74-
_, err := c.R().
74+
_, err := httpClient.R().
7575
SetContext(ctx).
7676
SetQueryParams(buildQuery(query, strconv.Itoa(limit), author)).
77+
SetHeader("User-Agent", fmt.Sprintf("BestReads/1.0 (%s)", cfg.OpenLibraryRequestEmail)).
7778
SetResult(&response).
7879
Get(openLibrarySearchURL)
7980
if err != nil {
@@ -112,7 +113,7 @@ func insertNewBooks(ctx context.Context, books []database.Book) error {
112113
return nil
113114
}
114115

115-
func metadataSingle(client *resty.Client, ctx context.Context, book dtos.OpenLibraryBook) (dtos.OlibFullData, error) {
116+
func metadataSingle(ctx context.Context, book dtos.OpenLibraryBook) (dtos.OlibFullData, error) {
116117
isbn, err := dtos.UnwrapFirst(book.ISBN)
117118
if err != nil {
118119
return dtos.OlibFullData{}, err
@@ -123,7 +124,7 @@ func metadataSingle(client *resty.Client, ctx context.Context, book dtos.OpenLib
123124
return dtos.OlibFullData{}, err
124125
}
125126

126-
desc := fetchWorkDetails(client, ctx, book.Key)
127+
desc := fetchWorkDetails(ctx, book.Key)
127128

128129
cacheId, err := database.CacheMedia(coverUrlfmt(book.CoverID))
129130
if err != nil {
@@ -147,7 +148,10 @@ func coverUrlfmt(id int) string {
147148
}
148149

149150
// fetchWorkDetails ruft die Work-JSON von Open Library ab und extrahiert die Description
150-
func fetchWorkDetails(httpClient *resty.Client, ctx context.Context, workKey string) string {
151+
func fetchWorkDetails(ctx context.Context, workKey string) string {
152+
httpClient := middlewares.HttpClient(ctx)
153+
cfg := middlewares.Config(ctx)
154+
151155
if workKey == "" {
152156
return "Es gibt keine Beschreibung für dieses Buch."
153157
}
@@ -158,6 +162,7 @@ func fetchWorkDetails(httpClient *resty.Client, ctx context.Context, workKey str
158162

159163
_, err := httpClient.R().
160164
SetContext(ctx).
165+
SetHeader("User-Agent", fmt.Sprintf("BestReads/1.0 (%s)", cfg.OpenLibraryRequestEmail)).
161166
SetResult(&workResponse).
162167
Get(url)
163168

0 commit comments

Comments
 (0)