Skip to content

Commit fd57994

Browse files
committed
Allow user to set user_agent string
This should fix CV support in the short term since they have apparently whitelisted `Comictagger`.
1 parent e343c22 commit fd57994

File tree

2 files changed

+57
-2
lines changed

2 files changed

+57
-2
lines changed

simyan/comicvine.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ class Comicvine:
109109
api_key: User's API key to access the Comicvine API.
110110
timeout: Set how long requests will wait for a response (in seconds).
111111
cache: SQLiteCache to use if set.
112+
user_agent: Custom User-Agent string. If None, uses default Simyan User-Agent.
112113
"""
113114

114115
API_URL = "https://comicvine.gamespot.com/api"
@@ -121,10 +122,17 @@ class Comicvine:
121122
_limiter = Limiter(_bucket, raise_when_fail=False, max_delay=Duration.DAY)
122123
decorator = _limiter.as_decorator()
123124

124-
def __init__(self, api_key: str, timeout: int = 30, cache: SQLiteCache | None = None):
125+
def __init__(
126+
self,
127+
api_key: str,
128+
timeout: int = 30,
129+
cache: SQLiteCache | None = None,
130+
user_agent: str | None = None,
131+
):
125132
self.headers = {
126133
"Accept": "application/json",
127-
"User-Agent": f"Simyan/{__version__}/{platform.system()}: {platform.release()}",
134+
"User-Agent": user_agent
135+
or f"Simyan/{__version__}/{platform.system()}: {platform.release()}",
128136
}
129137
self.api_key = api_key
130138
self.timeout = timeout

tests/comicvine_test.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
"""The Comicvine test module.
2+
3+
This module contains tests for the Comicvine class initialization and configuration.
4+
"""
5+
6+
import platform
7+
8+
from simyan import __version__
9+
from simyan.comicvine import Comicvine
10+
11+
12+
def test_default_user_agent(comicvine_api_key: str) -> None:
13+
"""Test that the default User-Agent is set correctly."""
14+
session = Comicvine(api_key=comicvine_api_key)
15+
expected_user_agent = f"Simyan/{__version__}/{platform.system()}: {platform.release()}"
16+
assert session.headers["User-Agent"] == expected_user_agent
17+
18+
19+
def test_custom_user_agent(comicvine_api_key: str) -> None:
20+
"""Test that a custom User-Agent can be set."""
21+
custom_ua = "MyCustomApp/1.0"
22+
session = Comicvine(api_key=comicvine_api_key, user_agent=custom_ua)
23+
assert session.headers["User-Agent"] == custom_ua
24+
25+
26+
def test_custom_user_agent_with_all_params(comicvine_api_key: str) -> None:
27+
"""Test that custom User-Agent works with all initialization parameters."""
28+
custom_ua = "TestApp/2.0 (Custom)"
29+
session = Comicvine(api_key=comicvine_api_key, timeout=60, cache=None, user_agent=custom_ua)
30+
assert session.headers["User-Agent"] == custom_ua
31+
assert session.timeout == 60
32+
assert session.cache is None
33+
34+
35+
def test_empty_string_user_agent_uses_default(comicvine_api_key: str) -> None:
36+
"""Test that an empty string User-Agent falls back to default."""
37+
session = Comicvine(api_key=comicvine_api_key, user_agent="")
38+
expected_user_agent = f"Simyan/{__version__}/{platform.system()}: {platform.release()}"
39+
assert session.headers["User-Agent"] == expected_user_agent
40+
41+
42+
def test_headers_structure(comicvine_api_key: str) -> None:
43+
"""Test that headers are properly structured."""
44+
session = Comicvine(api_key=comicvine_api_key)
45+
assert "Accept" in session.headers
46+
assert "User-Agent" in session.headers
47+
assert session.headers["Accept"] == "application/json"

0 commit comments

Comments
 (0)