Skip to content

Commit

Permalink
Merge pull request #4 from ajbosco/show_all_books_on_shelf
Browse files Browse the repository at this point in the history
return all books from shelves
  • Loading branch information
ajbosco authored Nov 5, 2018
2 parents 4d6c21e + 26cfc71 commit f0e6396
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 11 deletions.
2 changes: 1 addition & 1 deletion VERSION.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v0.1.0
v0.1.1
21 changes: 16 additions & 5 deletions goodreads/shelves.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package goodreads
import (
"fmt"
"net/http"
"strconv"
)

// ListShelves for current user.
Expand All @@ -19,16 +20,26 @@ func (c *Client) ListShelves(userID string) ([]UserShelf, error) {

// ListShelfBooks returns books by from a shelf.
func (c *Client) ListShelfBooks(shelf string, userID string) ([]Book, error) {
var books []Book
args := make(map[string]string)
args["shelf"] = shelf
args["v"] = "2"
listShelfEndpoint := fmt.Sprintf("%s/%s.xml", ListShelfEndpoint, userID)
data, err := c.doRequest(http.MethodGet, listShelfEndpoint, args)
if err != nil {
return nil, err
args["per_page"] = "200"

for page := 1; ; page++ {
args["page"] = strconv.Itoa(page)
listShelfEndpoint := fmt.Sprintf("%s/%s.xml", ListShelfEndpoint, userID)
data, err := c.doRequest(http.MethodGet, listShelfEndpoint, args)
if err != nil {
return nil, err
}
books = append(books, data.Reviews.Books...)
if data.Reviews.AttrEnd == data.Reviews.AttrTotal {
break
}
}

return data.Reviews.Books, nil
return books, nil
}

// AddToShelf adds a book to shelf.
Expand Down
15 changes: 10 additions & 5 deletions goodreads/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,18 @@ type User struct {

// Reviews defines the struct for the reviews object
type Reviews struct {
Books []Book `xml:"review>book,omitempty"`
AttrEnd string `xml:"end,attr"`
AttrStart string `xml:"start,attr"`
AttrTotal string `xml:"total,attr"`
Books []Book `xml:"review,omitempty"`
}

// Book defines the struct for the book object
type Book struct {
ID string `xml:"id,omitempty"`
Title string `xml:"title,omitempty"`
AvgRating string `xml:"average_rating,omitempty"`
Link string `xml:"link,omitempty"`
ID string `xml:"book>id,omitempty"`
Title string `xml:"book>title,omitempty"`
AvgRating string `xml:"book>average_rating,omitempty"`
Link string `xml:"book>link,omitempty"`
Pages string `xml:"book>num_pages,omitempty"`
ReadAt string `xml:"read_at,omitempty"`
}

0 comments on commit f0e6396

Please sign in to comment.