internetarchive / internetarchive/openlibrary

Refactor get_loans_of_user to use Archive.org's /loans/loan/ API (user_bookshelf)

Open
#11,232 2 comments 0 reactions 1 assignee Claimed by @cdrini View on GitHub
Lead: @mekarpeles Module: Borrowing / Lending Needs: Staff / Internal Priority: 2 Theme: Book Availability
Dominant language
Python
Stars
6.7k
Forks
2k
Avg merge
2d 19h
Merged PRs (30d)
138

Description

#### Background
The current `get_loans_of_user` function in `openlibrary/core/lending.py` uses Open Library's local store and an outdated bridge to archive.org for loan tracking. We need to migrate this logic to use Archive.org's `/loans/loan/` API endpoint (`action=user_bookshelf`) via the `s3_loan_api` method.

#### Task Requirements
- **Replace loan retrieval logic** in `get_loans_of_user` with a call to `s3_loan_api` using `action=user_bookshelf`.
- Parse the API response (see example in context) and convert each item in `publications` into a usable loan model for Open Library.
- **Cache loans for 10 minutes**. Invalidate/refresh cache whenever a loan or return is performed.
- **Remove/rewrite legacy code** that queries Open Library's local loan records.
- **Maintain compatibility** with existing callers and code structure as much as possible.

#### Acceptance Criteria
- `get_loans_of_user` only uses the S3 bookshelf API, not local OL loan records.
- Loans are cached and invalidated as required.
- Unit/integration tests demonstrate correct bookshelf retrieval and cache behavior.

#### References
- [lending.py: get_loans_of_user](https://github.com/internetarchive/openlibrary/blob/c0a7f0b1c2017de573b88588c0d869c251a86720/openlibrary/core/lending.py#L662-L680)
- [Archive.org bookshelf API response example](https://archive.org/services/loans/loan/?action=user_bookshelf)
- [s3_loan_api usage](https://github.com/internetarchive/openlibrary/blob/c0a7f0b1c2017de573b88588c0d869c251a86720/openlibrary/core/lending.py#L129-L151)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.