@@ -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
1817const openLibrarySearchURL = "https://openlibrary.org/search.json" // Open Library Search-Endpoint
1918const 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