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
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