Skip to content

Commit a5f27ed

Browse files
committed
feat: improve __init__ function with docstring and validation
1 parent 16507a1 commit a5f27ed

1 file changed

Lines changed: 29 additions & 10 deletions

File tree

miniflux.py

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -112,19 +112,41 @@ def __init__(
112112
user_agent: str = DEFAULT_USER_AGENT,
113113
session: requests.Session = requests.Session(),
114114
):
115-
self._base_url = base_url
115+
"""
116+
Initializes the Miniflux API client.
117+
118+
Args:
119+
base_url (str): The base URL of the Miniflux API. Must start with "http://" or "https://".
120+
username (Optional[str]): The username for basic authentication. Required if `api_key` is not provided.
121+
password (Optional[str]): The password for basic authentication. Required if `api_key` is not provided.
122+
timeout (float): The timeout for API requests in seconds. Default is 30.0 seconds.
123+
api_key (Optional[str]): The API key for authentication. If provided, takes precedence over `username` and `password`.
124+
user_agent (str): The User-Agent string to use for API requests. Default is "Miniflux Python Client Library".
125+
session (requests.Session): A custom requests session to use for API requests. Default is a new requests.Session().
126+
127+
Raises:
128+
ValueError: If `base_url` is not a valid URL starting with "http://" or "https://".
129+
ValueError: If neither `api_key` nor both `username` and `password` are provided.
130+
"""
131+
if not base_url.startswith(("http://", "https://")):
132+
raise ValueError(
133+
"base_url must be a valid URL starting with http:// or https://"
134+
)
135+
136+
if not api_key and not (username and password):
137+
raise ValueError(
138+
"Either api_key or both username and password must be provided"
139+
)
140+
141+
self._base_url = base_url.rstrip("/")
116142
self._timeout = timeout
117143
self._session = session
118144

119-
auth: Optional[tuple] = (
120-
(username or "", password or "") if not api_key else None
121-
)
122-
123145
self._session.headers.update({"User-Agent": user_agent})
124146
if api_key:
125147
self._session.headers.update({"X-Auth-Token": api_key})
126-
if auth is not None:
127-
self._session.auth = auth
148+
elif username and password:
149+
self._session.auth = (username, password)
128150

129151
def __enter__(self):
130152
return self
@@ -133,9 +155,6 @@ def __exit__(self, *args):
133155
self.close()
134156

135157
def _get_endpoint(self, path: str) -> str:
136-
if len(self._base_url) > 0 and self._base_url[-1:] == "/":
137-
self._base_url = self._base_url[:-1]
138-
139158
return f"{self._base_url}/v{self.API_VERSION}{path}"
140159

141160
def _get_params(self, **kwargs) -> Optional[dict]:

0 commit comments

Comments
 (0)