Skip to content

Commit dfd6d4e

Browse files
committed
chore: migrate to uv
1 parent 04f938f commit dfd6d4e

10 files changed

Lines changed: 56 additions & 4 deletions

File tree

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,6 @@ __pycache__/
6262

6363
# VS Code
6464
.vscode/
65+
66+
# Local file I use for testing
67+
main.py

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,11 @@ warn_required_dynamic_aliases = true
108108
warn_untyped_fields = true
109109

110110
[tool.pytest.ini_options]
111-
addopts = "--color=yes --doctest-modules --exitfirst --failed-first --strict-config --strict-markers --typeguard-packages=ksef --verbosity=2 --junitxml=reports/pytest.xml"
111+
addopts = "--color=yes --doctest-modules --exitfirst --failed-first --strict-config --strict-markers --typeguard-packages=ksef --verbosity=2 --junitxml=reports/pytest.xml -m 'not integration'"
112112
filterwarnings = ["error", "ignore::DeprecationWarning"]
113113
testpaths = ["src", "tests"]
114114
xfail_strict = true
115+
markers = ["integration"]
115116

116117
[tool.ruff]
117118
fix = true

src/ksef/auth/token.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Simple token-based authorization implementation."""
22
import base64
33
import copy
4+
import logging
45
from datetime import datetime, timezone
56
from typing import Mapping, cast
67
from urllib.parse import urljoin
@@ -23,6 +24,8 @@
2324
from ksef.models.responses.authorization_token import AuthorizationToken
2425
from ksef.utils import response_to_exception
2526

27+
logger = logging.getLogger(__name__)
28+
2629

2730
class TokenAuthorization(Authorization):
2831
"""Simple token-based authorization."""
@@ -83,6 +86,9 @@ def get_authorization_challenge(self, nip: str) -> AuthorizationChallenge:
8386
},
8487
timeout=TIMEOUT,
8588
)
89+
logger.debug(
90+
"Authorization challenge response (%s): %s", response.status_code, response.text
91+
)
8692
challenge = response.json()
8793
error = response_to_exception(response)
8894
if error is not None:

src/ksef/client.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"""Base client for interacting with the KSEF API."""
2+
import logging
23
from typing import Dict, Mapping, Optional, Union, cast
34
from urllib.parse import urlencode, urljoin
45

@@ -8,6 +9,8 @@
89
from ksef.auth.base import Authorization
910
from ksef.constants import BASE_URL, URL_QUERY_INVOICES
1011

12+
logger = logging.getLogger(__name__)
13+
1114

1215
class Client:
1316
"""Base client for interacting with the KSEF API."""
@@ -47,5 +50,7 @@ def search_invoices(self, page_size: int = 100, page_offset: int = 0) -> Dict[st
4750
request = self.authorization.modify_request(request)
4851
prepared_request = request.prepare()
4952
response = self.session.send(prepared_request)
53+
logger.debug("Search invoices response (%s): %s", response.status_code, response.text)
54+
response.raise_for_status()
5055
data = cast(Dict[str, str], response.json())
5156
return data

src/ksef/constants.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
BASE_URL = "https://ksef-demo.mf.gov.pl/api/" # TODO: Change to prod
66
TIMEOUT = 30
77

8-
URL_AUTH_CHALLENGE = "online/Session/AuthorisationChallenge"
9-
URL_AUTH_INIT_TOKEN = "online/Session/InitToken" # noqa: S105
8+
URL_AUTH_CHALLENGE = "auth/challenge"
9+
URL_AUTH_INIT_TOKEN = "auth/ksef-token" # noqa: S105
10+
URL_AUTH_PUBLIC_KEYS = "security/public-key-certificates"
1011

11-
URL_QUERY_INVOICES = "online/Query/Invoice/Sync"
12+
URL_QUERY_INVOICES = "invoices/query/metadata"

tests/integration/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# python-ksef integration tests
2+
3+
These tests run against the demo instance of KSEF.
4+
They should not be run in CI as they require a live instance, real credentials and will modify the instance.

tests/integration/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"""Integration tests."""

tests/integration/conftest.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
"""Integration tests configuration file."""
2+
import pytest
3+
4+
5+
@pytest.fixture()
6+
def client() -> "ksef.client.Client": # type: ignore[name-defined] # noqa: F821
7+
"""KSEF Client with token authorization."""
8+
from ksef.auth.token import TokenAuthorization
9+
from ksef.client import Client
10+
11+
authorization = TokenAuthorization(
12+
token="", public_key="", base_url="https://ksef-test.mf.gov.pl/api/v2/"
13+
)
14+
authorization.authorize(nip="1234567890")
15+
return Client(
16+
authorization=authorization,
17+
)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"""Invoices-related integration tests."""
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
"""Tests for searching invoices."""
2+
3+
import pytest
4+
5+
from ksef.client import Client
6+
7+
8+
@pytest.mark.integration()
9+
@pytest.mark.withoutresponses()
10+
def test_search_invoices(client: Client) -> None:
11+
"""Integration test for searching invoices."""
12+
invoices = client.search_invoices()
13+
raise Exception(invoices)

0 commit comments

Comments
 (0)