@@ -17,19 +17,19 @@ const (
17
17
DefaultAPIKey = "ckvsiSDsuqh7omh74ZZ6Q" // Read only API key kindly provided by LazyLibrarian
18
18
)
19
19
20
- var DefaultGoodreadsClient = & GoodreadsClient {
20
+ var DefaultClient = & Client {
21
21
client : http .DefaultClient ,
22
22
apiRootUrl : DefaultAPIRootUrl ,
23
23
apiKey : DefaultAPIKey ,
24
24
}
25
25
26
- type GoodreadsClient struct {
26
+ type Client struct {
27
27
client * http.Client
28
28
apiRootUrl string
29
29
apiKey string
30
30
}
31
31
32
- func (c * GoodreadsClient ) Get (
32
+ func (c * Client ) Get (
33
33
ctx context.Context ,
34
34
apiPath string ,
35
35
queryParams map [string ]string ,
@@ -82,7 +82,7 @@ func (c *GoodreadsClient) Get(
82
82
83
83
// GetBookById gets a book by its id.
84
84
// https://www.goodreads.com/api/index#book.show
85
- func (c * GoodreadsClient ) GetBookById (ctx context.Context , bookId string ) (Book , error ) {
85
+ func (c * Client ) GetBookById (ctx context.Context , bookId string ) (Book , error ) {
86
86
queryParams := map [string ]string {"id" : bookId }
87
87
88
88
var result struct {
@@ -96,9 +96,36 @@ func (c *GoodreadsClient) GetBookById(ctx context.Context, bookId string) (Book,
96
96
return result .Book , nil
97
97
}
98
98
99
+ func (c * Client ) GetBooksByIds (ctx context.Context , bookIds []string ) ([]Book , error ) {
100
+ books := make ([]Book , len (bookIds ))
101
+ var errs error
102
+ var wg sync.WaitGroup
103
+ for idx , bookId := range bookIds {
104
+ wg .Add (1 )
105
+ go func (bookId string , idx int ) {
106
+ defer wg .Done ()
107
+
108
+ book , err := c .GetBookById (ctx , bookId )
109
+ if err != nil {
110
+ errs = errors .Join (errs , err )
111
+ return
112
+ }
113
+ books [idx ] = book
114
+ }(bookId , idx )
115
+ }
116
+
117
+ wg .Wait ()
118
+
119
+ if errs != nil {
120
+ return nil , errs
121
+ }
122
+
123
+ return books , nil
124
+ }
125
+
99
126
// GetBookByTitle gets a book by its title and optionally an author (which can give a better match)
100
127
// https://www.goodreads.com/api/index#book.title
101
- func (c * GoodreadsClient ) GetBookByTitle (ctx context.Context , bookTitle string , bookAuthor * string ) (Book , error ) {
128
+ func (c * Client ) GetBookByTitle (ctx context.Context , bookTitle string , bookAuthor * string ) (Book , error ) {
102
129
queryParams := map [string ]string {"title" : bookTitle }
103
130
if bookAuthor != nil && * bookAuthor != "" {
104
131
queryParams ["author" ] = * bookAuthor
@@ -117,40 +144,26 @@ func (c *GoodreadsClient) GetBookByTitle(ctx context.Context, bookTitle string,
117
144
118
145
// SearchBooks search for a book by its title and optionally an author (which can give better results)
119
146
// https://www.goodreads.com/api/index#search.books
120
- func (c * GoodreadsClient ) SearchBooks (ctx context.Context , bookTitle string , bookAuthor * string ) ([]Book , error ) {
147
+ func (c * Client ) SearchBooks (ctx context.Context , bookTitle string , bookAuthor * string ) ([]Book , error ) {
121
148
query := bookTitle
122
149
if bookAuthor != nil && * bookAuthor != "" {
123
150
query = fmt .Sprintf ("%s %s" , query , * bookAuthor )
124
151
}
125
152
queryParams := map [string ]string {"q" : query }
126
153
127
- var result struct {
154
+ // Search for books, getting their ids
155
+ var unmarshaller struct {
128
156
BookIds []string `xml:"search>results>work>best_book>id"`
129
157
}
130
- err := c .Get (ctx , "search/index.xml" , queryParams , & result )
158
+ err := c .Get (ctx , "search/index.xml" , queryParams , & unmarshaller )
131
159
if err != nil {
132
160
return nil , err
133
161
}
134
162
135
- books := make ([]Book , len (result .BookIds ))
136
- var errs error
137
- var wg sync.WaitGroup
138
- for idx , bookId := range result .BookIds {
139
- wg .Add (1 )
140
- go func (bookId string , idx int ) {
141
- defer wg .Done ()
142
-
143
- book , err := c .GetBookById (ctx , bookId )
144
- if err != nil {
145
- errs = errors .Join (errs , err )
146
- return
147
- }
148
- books [idx ] = book
149
- }(bookId , idx )
150
- }
151
- wg .Wait ()
152
- if errs != nil {
153
- return nil , errs
163
+ // Get book details using their ids
164
+ books , err := c .GetBooksByIds (ctx , unmarshaller .BookIds )
165
+ if err != nil {
166
+ return nil , err
154
167
}
155
168
156
169
return books , nil
0 commit comments