Skip to content
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

Send Authorized API Requests #10

Merged
merged 4 commits into from
Jan 10, 2025
Merged
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 grayven/grand_comics_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,23 @@ class GrandComicsDatabase:
"""Class with functionality to request GCD API endpoints.

Args:
email: The user's GCD email address, which is used for authentication.
password: The user's GCD password, which is used for authentication.
timeout: Set how long requests will wait for a response (in seconds).
cache: SQLiteCache to use if set.
"""

API_URL = "https://www.comics.org/api"

def __init__(self, timeout: int = 30, cache: Optional[SQLiteCache] = None):
def __init__(
self, email: str, password: str, timeout: int = 30, cache: Optional[SQLiteCache] = None
):
self.headers = {
"Accept": "application/json",
"User-Agent": f"Grayven/{__version__}/{platform.system()}: {platform.release()}",
}
self.email = email
self.password = password
self.timeout = timeout
self.cache = cache

Expand All @@ -64,7 +70,13 @@ def _perform_get_request(
params = {}

try:
response = get(url, params=params, headers=self.headers, timeout=self.timeout)
response = get(
url,
params=params,
headers=self.headers,
auth=(self.email, self.password),
timeout=self.timeout,
)
response.raise_for_status()
return response.json()
except RequestError as err:
Expand Down
20 changes: 18 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
This module contains pytest fixtures.
"""

import os
from pathlib import Path

import pytest
Expand All @@ -12,8 +13,23 @@


@pytest.fixture(scope="session")
def session() -> GrandComicsDatabase:
def gcd_email() -> str:
"""Email address for testing."""
return os.getenv("GCD_EMAIL", "<EMAIL>")


@pytest.fixture(scope="session")
def gcd_password() -> str:
"""Password for testing."""
return os.getenv("GCD_PASSWORD", "passwrd")


@pytest.fixture(scope="session")
def session(gcd_email: str, gcd_password: str) -> GrandComicsDatabase:
"""Set the GrandComicsDatabase session fixture."""
return GrandComicsDatabase(
cache=SQLiteCache(path=Path("tests/cache.sqlite"), expiry=None), timeout=5
email=gcd_email,
password=gcd_password,
cache=SQLiteCache(path=Path("tests/cache.sqlite"), expiry=None),
timeout=5,
)
4 changes: 2 additions & 2 deletions tests/exceptions_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ def test_not_found(session: GrandComicsDatabase) -> None:
session._get_request(endpoint="/invalid") # noqa: SLF001


def test_timeout() -> None:
def test_timeout(gcd_email: str, gcd_password: str) -> None:
"""Test a TimeoutError for slow responses."""
session = GrandComicsDatabase(timeout=1, cache=None)
session = GrandComicsDatabase(gcd_email, gcd_password, timeout=1, cache=None)
with pytest.raises(ServiceError):
session.get_publisher(id=1)
15 changes: 10 additions & 5 deletions tests/issue_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,13 @@ def issue_no_page_json() -> dict[str, any]:
}


def test_issue_no_page(httpx_mock: HTTPXMock, issue_no_page_json: dict[str, any]) -> None:
def test_issue_no_page(
gcd_email: str, gcd_password: str, httpx_mock: HTTPXMock, issue_no_page_json: dict[str, any]
) -> None:
"""Test issue with no page count."""
session = GrandComicsDatabase() # We don't want to cache these results
session = GrandComicsDatabase(
email=gcd_email, password=gcd_password
) # We don't want to cache these results
httpx_mock.add_response(json=issue_no_page_json)
result = session.get_issue(2698986)
assert isinstance(result, Issue)
Expand All @@ -63,9 +67,10 @@ def test_issue(session: GrandComicsDatabase) -> None:
assert result.publication_date == "July 2005"
assert result.price == "3.50 USD; 4.75 CAD"
assert result.page_count == Decimal("48")
assert (
result.editing
== "Peter J. Tomasi (credited as Peter Tomasi) (editor); Harvey Richards (credited) (assistant editor); Dan DiDio (credited) (executive editor); Paul Levitz (credited) (publisher)" # noqa: E501
assert result.editing == (
"Peter J. Tomasi (credited as Peter Tomasi) (editor); Harvey Richards "
"(credited) (assistant editor); Dan DiDio (credited) (executive editor); "
"Paul Levitz (credited) (publisher)"
)
assert result.indicia_publisher == "DC Comics"
assert result.brand == "DC [bullet]"
Expand Down
Loading