Skip to content

Conversation

@akramcodez
Copy link
Contributor

@akramcodez akramcodez commented Dec 26, 2025

Closes #11628

Technical

The login() method in api.py was silently swallowing authentication failures, causing copydocs.py to proceed with unauthenticated requests that returned 403 errors.

Changes:

  • api.py: Modified login() to return True/False instead of silently failing. Re-raises non-auth errors.
  • copydocs.py: Now checks login result and exits with clear error messages on failure

Testing

  1. Run copydocs.py with invalid credentials - should exit with helpful error message
  2. Run with valid credentials - should successfully copy records

Stakeholders

@cdrini

Copilot AI review requested due to automatic review settings December 26, 2025 05:15
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR improves error handling for authentication failures in the Open Library API client by making login failures explicit rather than silent, preventing downstream 403 errors when operations are attempted with unauthenticated sessions.

Key changes:

  • Modified login() method in api.py to return boolean success/failure instead of silently swallowing authentication errors
  • Added login result validation and clear error messages in copydocs.py to guide users when authentication fails
  • Enhanced error messages provide actionable guidance for both development and production scenarios

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
openlibrary/api.py Modified login() to return True on success, False on auth failures (401/403), and re-raise non-auth errors; added comprehensive docstring documenting the new return value and exception behavior
scripts/copydocs.py Added login result checking for both autologin() and login() calls with clear error messages and actionable guidance; exits gracefully with status code 1 on authentication failures; includes helpful instructions for setting up credentials

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 96 to +121
def login(self, username, password):
"""Login to Open Library with given credentials."""
"""Login to Open Library with given credentials.

Returns:
bool: True if login was successful, False otherwise.

Raises:
OLError: If a non-authentication HTTP error occurs.
"""
headers = {'Content-Type': 'application/json'}
try:
data = json.dumps({"username": username, "password": password})
response = self._request(
'/account/login', method='POST', data=data, headers=headers
)
except OLError as e:
response = e
if e.code in (401, 403):
return False
raise

if 'Set-Cookie' in response.headers:
cookies = response.headers['Set-Cookie'].split(',')
self.cookie = ';'.join([c.split(';')[0] for c in cookies])
return True
logger.warning("Login request succeeded but no session cookie was received")
return False
Copy link

Copilot AI Dec 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The modified login() method that now returns a boolean value lacks test coverage. Tests should be added to verify the behavior when login succeeds (returns True), when authentication fails with 401/403 (returns False), and when other HTTP errors occur (re-raises OLError). The PR description mentions tests in openlibrary/tests/test_api.py, but this file was not included in the pull request.

Copilot uses AI. Check for mistakes.
@akramcodez
Copy link
Contributor Author

PTAL @cdrini

@github-actions github-actions bot added the Needs: Response Issues which require feedback from lead label Dec 26, 2025
@mekarpeles mekarpeles assigned mekarpeles and jimchamp and unassigned mekarpeles Dec 29, 2025
@jimchamp
Copy link
Collaborator

jimchamp commented Jan 8, 2026

Can you please wait to be assigned to issues before working on them? I've asked this of you at least once before...

Issues with the Needs: Triage label have not been viewed by staff, and could be closed at any moment.

Issues with the Needs: Breakdown label are missing important information about how to solve the issue, including but not limited to the general approach, related files, and possible pitfalls. We typically ask contributors to provide their own break-down for issues with this labels, if they are requesting to work on the issue.

Any change to the login handlers is risky. Had you asked to be assigned, I would have advised against changing this.

The OpenLibrary class is used by other scripts, and any changes to this API, especially the login methods, could cause regressions with these scripts.

I am closing this, and once again asking you to avoid working on issues unless you are assigned to them.

@jimchamp jimchamp closed this Jan 8, 2026
@akramcodez
Copy link
Contributor Author

sorry @jimchamp but recently I didn't created any PR these all are old PRs, really sorry again

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Needs: Response Issues which require feedback from lead

Projects

None yet

Development

Successfully merging this pull request may close these issues.

copydocs erroring with 403 forbiden

3 participants