Skip to content
Open
Show file tree
Hide file tree
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
16 changes: 14 additions & 2 deletions openlibrary/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,19 +94,31 @@ def autologin(self, section=None):
return self.login(username, password)

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
Comment on lines 96 to +121
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.

def get(self, key, v=None):
response = self._request(key + '.json', params={'v': v} if v else {})
Expand Down
22 changes: 19 additions & 3 deletions scripts/copydocs.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,9 +365,25 @@ def main(
if isinstance(dest_ol, OpenLibrary):
section = "[%s]" % web.lstrips(dest, "http://").strip("/")
if section in read_lines(os.path.expanduser("~/.olrc")):
dest_ol.autologin()
else:
dest_ol.login("admin", "admin123")
if not dest_ol.autologin():
print(
f"Error: Failed to login using credentials from ~/.olrc "
f"for section {section}"
)
print("Please verify your ~/.olrc credentials have write permissions.")
sys.exit(1)
elif not dest_ol.login("admin", "admin123"):
print("Error: Failed to login with default credentials (admin/admin123)")
print(
"For local development, ensure the admin user exists "
"and has the correct password."
)
print("Alternatively, create a ~/.olrc file with your credentials:")
print(f" [{web.lstrips(dest, 'http://').strip('/')}]")
print(" username = your_username")
print(" password = your_password")
sys.exit(1)
print(f"Successfully logged in to {dest}")

for list_key in lists or []:
copy_list(src_ol, dest_ol, list_key, comment=comment)
Expand Down
Loading