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
13 changes: 13 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
version: 2
updates:
- package-ecosystem: github-actions
directory: /
schedule:
interval: weekly
- package-ecosystem: pip
directory: /
schedule:
interval: weekly
groups:
dev-dependencies:
dependency-type: development
20 changes: 10 additions & 10 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ jobs:
strategy:
fail-fast: false
matrix:
python-version: [ "3.11", "3.12", "3.13" ]
python-version: [ "3.12", "3.13", "3.14", "3.15" ]

steps:
- name: Checkout code
uses: actions/checkout@v4
uses: actions/checkout@v7

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
uses: actions/setup-python@v7
with:
python-version: ${{ matrix.python-version }}
cache: 'pip'
Expand All @@ -44,10 +44,10 @@ jobs:

- name: Load cached venv
id: cached-poetry-dependencies
uses: actions/cache@v4
uses: actions/cache@v6
with:
path: .venv
key: venv-${{ runner.os }}-${{ hashFiles('**/poetry.lock') }}
key: venv-${{ runner.os }}-py${{ matrix.python-version }}-${{ hashFiles('**/poetry.lock') }}

- name: Install dependencies
if: steps.cached-poetry-dependencies.outputs.cache-hit != 'true'
Expand Down Expand Up @@ -77,7 +77,7 @@ jobs:
if: success() || failure()

- name: Surface failing tests
uses: pmeier/pytest-results-action@main
uses: pmeier/pytest-results-action@v0.9.0
with:
title: Test results (Python ${{ matrix.python-version }})
path: junit/test-results-${{ matrix.python-version }}.xml
Expand Down Expand Up @@ -108,14 +108,14 @@ jobs:

steps:
- name: Check out code from GitHub
uses: actions/checkout@v4
uses: actions/checkout@v7

- name: Set up Python
uses: actions/setup-python@v5
uses: actions/setup-python@v7
with:
python-version: '3.11'
python-version: '3.12'

- name: Build and publish to pypi
uses: JRubics/poetry-publish@v2.0
uses: JRubics/poetry-publish@v2.1
with:
pypi_token: ${{ secrets.PYPI_API_TOKEN }}
6 changes: 3 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ default_language_version:

repos:
- repo: https://github.com/python-poetry/poetry
rev: '2.1.3'
rev: '2.5.1'
hooks:
- id: poetry-check
- id: poetry-lock
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.12.0
rev: v0.17.1
hooks:
- id: ruff
args:
Expand All @@ -40,7 +40,7 @@ repos:
require_serial: true
exclude: ^tests/.+
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v5.0.0
rev: v6.0.0
hooks:
- id: check-yaml
- id: check-added-large-files
1,110 changes: 727 additions & 383 deletions poetry.lock

Large diffs are not rendered by default.

28 changes: 15 additions & 13 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "saic_ismart_client_ng"
version = "0.9.3"
version = "0.10.0"
description = "SAIC next gen client library (MG iSMART)"
authors = [
{ name = "Giovanni Condello", email = "saic-python-client@nanomad.net" },
Expand All @@ -13,7 +13,7 @@ classifiers = [
"Operating System :: OS Independent",
]

requires-python = ">=3.11"
requires-python = ">=3.12"

dependencies = [
"pycryptodome (>=3.20.0,<4.0.0)",
Expand All @@ -31,16 +31,16 @@ requires = ["poetry-core>=2.0.0,<3.0.0"]
build-backend = "poetry.core.masonry.api"

[tool.poetry.group.dev.dependencies]
pytest = "^8.2.2"
pytest = ">=9.0.3,<10.0.0"
mock = "^5.1.0"
coverage = "^7.5.4"
ruff = "^0.12.0"
pytest-cov = "^5.0.0"
pytest-asyncio = "^0.24.0"
pytest-mock = "^3.14.0"
mypy = "^1.15.0"
pylint = "^3.3.6"
pre-commit = "^4.2.0"
coverage = ">=7.16.0,<8.0.0"
ruff = ">=0.17.1,<0.18.0"
pytest-cov = ">=7.1.0,<8.0.0"
pytest-asyncio = ">=1.4.0,<2.0.0"
pytest-mock = ">=3.16.0,<4.0.0"
mypy = ">=2.4.0,<3.0.0"
pylint = ">=4.1.3,<5.0.0"
pre-commit = ">=4.7.0,<5.0.0"

[tool.pytest.ini_options]
norecursedirs = ".git build dist"
Expand Down Expand Up @@ -97,6 +97,8 @@ ignore = [
"D105", # Missing docstring in magic method
"D107", # Missing docstring in `__init__`
"E501", # line too long
"CPY001", # Missing copyright notice at top of file
"ICN001", # Import conventions, e.g. `datetime` as `dt`

"FBT", # flake8-boolean-trap

Expand Down Expand Up @@ -156,7 +158,7 @@ max-args = 10

[tool.mypy]
files = ["./src", "./tests"]
python_version = 3.11
python_version = 3.12
show_error_codes = true
strict_equality = true
warn_incomplete_stub = true
Expand All @@ -175,7 +177,7 @@ warn_unreachable = true
strict = true

[tool.pylint.MAIN]
py-version = "3.11"
py-version = "3.12"
ignore = ["tests"]
fail-on = ["I"]

Expand Down
17 changes: 9 additions & 8 deletions src/saic_ismart_client_ng/api/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
Any,
ClassVar,
Protocol,
TypeVar,
)

import dacite
Expand Down Expand Up @@ -38,7 +37,6 @@ class IsDataclass(Protocol):
# the most reliable way to ascertain that something is a dataclass
__dataclass_fields__: ClassVar[dict[str, Any]]

T = TypeVar("T", bound=IsDataclass)

logger = logging.getLogger(__name__)

Expand All @@ -53,6 +51,9 @@ def __init__(
self.__api_client = SaicApiClient(configuration, listener=listener)
self.__token_expiration: datetime.datetime | None = None

async def close(self) -> None:
await self.__api_client.close()

async def login(self) -> LoginResp:
headers = {
"Content-Type": "application/x-www-form-urlencoded",
Expand Down Expand Up @@ -108,7 +109,7 @@ async def login(self) -> LoginResp:
)
return result

async def execute_api_call(
async def execute_api_call[T: IsDataclass](
self,
method: str,
path: str,
Expand All @@ -134,7 +135,7 @@ async def execute_api_call(
raise SaicApiException(msg)
return result

async def execute_api_call_with_optional_result(
async def execute_api_call_with_optional_result[T: IsDataclass](
self,
method: str,
path: str,
Expand Down Expand Up @@ -176,7 +177,7 @@ async def execute_api_call_no_result(
allow_null_body=True,
)

async def __execute_api_call(
async def __execute_api_call[T: IsDataclass](
self,
method: str,
path: str,
Expand Down Expand Up @@ -207,7 +208,7 @@ async def __execute_api_call(
msg = f"API call {method} {path} failed unexpectedly"
raise SaicApiException(msg, return_code=500) from e

async def execute_api_call_with_event_id(
async def execute_api_call_with_event_id[T: IsDataclass](
self,
method: str,
path: str,
Expand Down Expand Up @@ -251,7 +252,7 @@ async def execute_api_call_with_event_id_no_result(
delay=delay,
)

async def __execute_api_call_with_event_id(
async def __execute_api_call_with_event_id[T: IsDataclass](
self,
method: str,
path: str,
Expand Down Expand Up @@ -283,7 +284,7 @@ async def execute_api_call_with_event_id_inner(*, event_id: str) -> T | None:

return await execute_api_call_with_event_id_inner(event_id="0")

async def __deserialize(
async def __deserialize[T: IsDataclass](
self,
request: httpx.Request,
response: httpx.Response,
Expand Down
4 changes: 4 additions & 0 deletions src/saic_ismart_client_ng/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ class SaicLogoutException(SaicApiException):
pass


class SaicApiClosedException(SaicApiException):
pass


class SaicApiRetryException(SaicApiException):
def __init__(
self, msg: str, *, event_id: str, return_code: int | None = None
Expand Down
9 changes: 9 additions & 0 deletions src/saic_ismart_client_ng/net/client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import httpx
from httpx import Request, Response, Timeout

from saic_ismart_client_ng.exceptions import SaicApiClosedException
from saic_ismart_client_ng.net.httpx import (
decrypt_httpx_response,
encrypt_httpx_request,
Expand All @@ -27,6 +28,7 @@ def __init__(
self.__listener = listener
self.__logger = logging.getLogger(__name__)
self.__user_token: str = ""
self.__closed = False
self.__client = httpx.AsyncClient(
timeout=Timeout(timeout=configuration.read_timeout),
event_hooks={
Expand All @@ -36,8 +38,15 @@ def __init__(
)

async def send(self, request: Request) -> Response:
if self.__closed:
msg = "The API client has been closed and cannot be reused"
raise SaicApiClosedException(msg)
return await self.__client.send(request)

async def close(self) -> None:
self.__closed = True
await self.__client.aclose()

@property
def user_token(self) -> str:
return self.__user_token
Expand Down
28 changes: 28 additions & 0 deletions tests/test_close.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from __future__ import annotations

import unittest

import pytest

from saic_ismart_client_ng import SaicApi
from saic_ismart_client_ng.exceptions import SaicApiClosedException
from saic_ismart_client_ng.model import SaicApiConfiguration


def _build_api() -> SaicApi:
return SaicApi(
SaicApiConfiguration(username="user", password="pass"), # noqa: S106
)


class TestClose(unittest.IsolatedAsyncioTestCase):
async def test_close_is_idempotent(self) -> None:
api = _build_api()
await api.close()
await api.close()

async def test_login_after_close_raises(self) -> None:
api = _build_api()
await api.close()
with pytest.raises(SaicApiClosedException):
await api.login()
Loading