Skip to content

Commit

Permalink
fix: some api mistakes
Browse files Browse the repository at this point in the history
  • Loading branch information
rwxd committed Mar 11, 2023
1 parent 6263efd commit 5600407
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 7 deletions.
10 changes: 6 additions & 4 deletions readwise/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ def _get_pagination(
)
data = response.json()
yield data
if not data['next']:
if type(data) == list or not data.get('next'):
break
page += 1

Expand All @@ -231,7 +231,9 @@ def get_books(
category=book['category'],
source=book['source'],
num_highlights=book['num_highlights'],
last_highlight_at=datetime.fromisoformat(book['last_highlight_at']),
last_highlight_at=datetime.fromisoformat(book['last_highlight_at'])
if book['last_highlight_at']
else None,
updated=datetime.fromisoformat(book['updated']),
cover_image_url=book['cover_image_url'],
highlights_url=book['highlights_url'],
Expand All @@ -257,7 +259,7 @@ def get_book_highlights(
A generator of ReadwiseHighlight objects
'''
for data in self.get_pagination_limit_20(
f'/books/{book_id}/highlights/', params={'book_id': book_id}
'/highlights/', params={'book_id': book_id}
):
for highlight in data['results']:
yield ReadwiseHighlight(
Expand Down Expand Up @@ -323,7 +325,7 @@ def get_book_tags(self, book_id: str) -> Generator[ReadwiseTag, None, None]:
for data in self.get_pagination_limit_20(
f'/books/{book_id}/tags/', params={'book_id': book_id}
):
for tag in data['results']:
for tag in data:
yield ReadwiseTag(id=tag['id'], name=tag['name'])

def add_tag(self, book_id: str, tag: str):
Expand Down
2 changes: 1 addition & 1 deletion readwise/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class ReadwiseBook:
category: str
source: str
num_highlights: int
last_highlight_at: datetime
last_highlight_at: datetime | None
updated: datetime
cover_image_url: str
highlights_url: str
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def test_get_books(mock_get):
'category': 'article',
'source': 'Test Source',
'num_highlights': 1,
'last_highlight_at': '2020-01-01T00:00:00Z',
'last_highlight_at': '2022-12-27T11:33:09Z',
'updated': '2020-01-01T00:00:00Z',
'cover_image_url': 'https://example.com/image.jpg',
'highlights_url': 'https://example.com/highlights',
Expand All @@ -88,7 +88,7 @@ def test_get_books(mock_get):
assert books[0].category == 'article'
assert books[0].source == 'Test Source'
assert books[0].num_highlights == 1
assert books[0].last_highlight_at == datetime.fromisoformat('2020-01-01T00:00:00Z')
assert books[0].last_highlight_at == datetime.fromisoformat('2022-12-27T11:33:09Z')
assert books[0].updated == datetime.fromisoformat('2020-01-01T00:00:00Z')
assert books[0].cover_image_url == 'https://example.com/image.jpg'
assert books[0].highlights_url == 'https://example.com/highlights'
Expand Down

0 comments on commit 5600407

Please sign in to comment.