Skip to content

WIP: Add support for saving to Reader #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions lib/readwise/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,26 @@ def export(updated_after: nil, book_ids: [])
results.sort_by(&:highlighted_at_time)
end

def save(params)
uri = URI("https://readwise.io/api/v3/save")
headers = {
"Authorization": "Token #{@token}",
"Content-Type": "application/json"
}
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
response = http.post(uri.path, params.to_json, headers)
response_code = Integer(response.code)
if response_code == 429
retry_after = Integer(response.fetch("Retry-After"))
raise Error, "Rate limited exceeded, retry after #{retry_after} seconds"
elsif response_code >= 400
raise Error, "Save request failed"
else
response.body
end
end

private

def export_page(page_cursor: nil, updated_after: nil, book_ids: [])
Expand Down