Skip to content
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
2 changes: 1 addition & 1 deletion rpm/quads-lib.spec
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
%define name quads-lib
%define reponame python-quads-lib
%define branch development
%define version 0.1.12
%define version 0.1.13
%define build_timestamp %{lua: print(os.date("%Y%m%d"))}

Summary: Python client library for interacting with the QUADS API
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def read(*names, **kwargs):

setup(
name="quads-lib",
version="0.1.12",
version="0.1.13",
license="LGPL-3.0-only",
description="Python client library for interacting with the QUADS API",
long_description="{}\n{}".format(
Expand Down
2 changes: 1 addition & 1 deletion src/quads_lib/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = "0.1.12"
__version__ = "0.1.13"

from .quads import QuadsApi

Expand Down
13 changes: 11 additions & 2 deletions src/quads_lib/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def __init__(
password: str,
base_url: str,
verify: Union[bool, str] = False,
api_token: Optional[str] = None,
):
"""
Initialize QuadsBase.
Expand All @@ -36,6 +37,8 @@ def __init__(
backward compatibility)
- True: Enable verification using default CA bundle
- str: Path to a custom CA bundle file
api_token: Pre-generated API token (qat_-prefixed) for direct
bearer auth without username/password login.
"""
self.username = username
self.password = password
Expand All @@ -44,10 +47,16 @@ def __init__(
self.session = Session()
retries = Retry(total=5, backoff_factor=1, status_forcelist=[502, 503, 504])
self.session.mount("http://", HTTPAdapter(max_retries=retries))
self.auth = HTTPBasicAuth(self.username, self.password)
self.token = None
self.headers = {}

if api_token:
self.token = api_token
self.session.headers.update({"Authorization": f"Bearer {api_token}"})
self.auth = None
else:
self.auth = HTTPBasicAuth(self.username, self.password)
self.token = None

def __enter__(self):
self.login()
return self
Expand Down
10 changes: 10 additions & 0 deletions src/quads_lib/quads.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ def register(self) -> dict:
return json_response

def login(self) -> dict:
if self.token and self.token.startswith("qat_"):
Comment thread
sadsfae marked this conversation as resolved.
return {
"status_code": 201,
"status": "success",
"message": "Authenticated via API token",
"auth_token": self.token,
}
endpoint = urljoin(self.base_url, "login")
_response = self.session.post(endpoint, auth=self.auth, verify=self.verify)
json_response = _response.json()
Expand All @@ -30,6 +37,9 @@ def login(self) -> dict:
self.session.headers.update({"Authorization": f"Bearer {self.token}"})
return json_response

def get_current_user(self) -> dict:
return self.get("me")

def logout(self) -> dict:
json_response = self._make_request("POST", "logout")
if json_response.get("status_code") == 200:
Expand Down
Loading