campai-api 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
campai_api/__init__.py ADDED
@@ -0,0 +1,182 @@
1
+ # SPDX-FileCopyrightText: 2026 2026 Stephan Lukasczyk <stephan@dante.de>
2
+ #
3
+ # SPDX-License-Identifier: EUPL-1.2
4
+
5
+ """Campai REST API Python SDK."""
6
+
7
+ from __future__ import annotations
8
+
9
+ from campai_api.async_client import AsyncCampaiClient
10
+ from campai_api.auth import API_KEY_HEADER, APIKeyAuth
11
+ from campai_api.client import CampaiClient
12
+ from campai_api.exceptions import (
13
+ AuthenticationError,
14
+ BadGatewayError,
15
+ BadRequestError,
16
+ CampaiAPIError,
17
+ CampaiError,
18
+ CampaiJSONDecodeError,
19
+ CampaiNetworkError,
20
+ CampaiSerializationError,
21
+ ConflictError,
22
+ ForbiddenError,
23
+ GatewayTimeoutError,
24
+ InternalServerError,
25
+ NotFoundError,
26
+ RateLimitError,
27
+ ServerError,
28
+ ServiceUnavailableError,
29
+ UnprocessableEntityError,
30
+ create_api_error,
31
+ )
32
+ from campai_api.models import (
33
+ Address,
34
+ ApiKeyInfo,
35
+ Communication,
36
+ CommunicationCreateInput,
37
+ CommunicationUpdateInput,
38
+ Contact,
39
+ ContactCreateRequest,
40
+ ContactCreateResult,
41
+ ContactDepartment,
42
+ ContactDepartmentAssignment,
43
+ ContactsListResponse,
44
+ ContactUpdateRequest,
45
+ CostCenter,
46
+ FinanceDatevSettings,
47
+ FinanceSettings,
48
+ FinanceSettingsUpdateRequest,
49
+ Institution,
50
+ InvoiceCreateRequest,
51
+ InvoiceCreateResult,
52
+ InvoicePositionRequest,
53
+ InvoiceUpdateRequest,
54
+ MailingUnsubscriptionInput,
55
+ Mandate,
56
+ MandatesListResponse,
57
+ MemberCreateInput,
58
+ MemberData,
59
+ MemberUpdateInput,
60
+ Organization,
61
+ OrganizationAddress,
62
+ PermissionActions,
63
+ Person,
64
+ PersonCreateInput,
65
+ PersonUpdateInput,
66
+ Receipt,
67
+ ReceiptPosition,
68
+ ReceiptsListResponse,
69
+ WebhookTriggerEvent,
70
+ WebhookTriggerRequest,
71
+ )
72
+ from campai_api.resources import (
73
+ AsyncAuthResource,
74
+ AsyncBaseResource,
75
+ AsyncFinanceResource,
76
+ AsyncMembersResource,
77
+ AsyncWebhooksResource,
78
+ AuthResource,
79
+ BaseResource,
80
+ FinanceResource,
81
+ MembersResource,
82
+ WebhooksResource,
83
+ )
84
+ from campai_api.serialization import (
85
+ dump_json,
86
+ from_dict,
87
+ load_json,
88
+ to_camel_case,
89
+ to_jsonable,
90
+ )
91
+ from campai_api.transport import (
92
+ DEFAULT_BASE_URL,
93
+ DEFAULT_TIMEOUT,
94
+ AsyncTransport,
95
+ SyncTransport,
96
+ join_url,
97
+ quote_path_segment,
98
+ )
99
+
100
+ __all__ = [
101
+ "API_KEY_HEADER",
102
+ "DEFAULT_BASE_URL",
103
+ "DEFAULT_TIMEOUT",
104
+ "APIKeyAuth",
105
+ "Address",
106
+ "ApiKeyInfo",
107
+ "AsyncAuthResource",
108
+ "AsyncBaseResource",
109
+ "AsyncCampaiClient",
110
+ "AsyncFinanceResource",
111
+ "AsyncMembersResource",
112
+ "AsyncTransport",
113
+ "AsyncWebhooksResource",
114
+ "AuthResource",
115
+ "AuthenticationError",
116
+ "BadGatewayError",
117
+ "BadRequestError",
118
+ "BaseResource",
119
+ "CampaiAPIError",
120
+ "CampaiClient",
121
+ "CampaiError",
122
+ "CampaiJSONDecodeError",
123
+ "CampaiNetworkError",
124
+ "CampaiSerializationError",
125
+ "Communication",
126
+ "CommunicationCreateInput",
127
+ "CommunicationUpdateInput",
128
+ "ConflictError",
129
+ "Contact",
130
+ "ContactCreateRequest",
131
+ "ContactCreateResult",
132
+ "ContactDepartment",
133
+ "ContactDepartmentAssignment",
134
+ "ContactUpdateRequest",
135
+ "ContactsListResponse",
136
+ "CostCenter",
137
+ "FinanceDatevSettings",
138
+ "FinanceResource",
139
+ "FinanceSettings",
140
+ "FinanceSettingsUpdateRequest",
141
+ "ForbiddenError",
142
+ "GatewayTimeoutError",
143
+ "Institution",
144
+ "InternalServerError",
145
+ "InvoiceCreateRequest",
146
+ "InvoiceCreateResult",
147
+ "InvoicePositionRequest",
148
+ "InvoiceUpdateRequest",
149
+ "MailingUnsubscriptionInput",
150
+ "Mandate",
151
+ "MandatesListResponse",
152
+ "MemberCreateInput",
153
+ "MemberData",
154
+ "MemberUpdateInput",
155
+ "MembersResource",
156
+ "NotFoundError",
157
+ "Organization",
158
+ "OrganizationAddress",
159
+ "PermissionActions",
160
+ "Person",
161
+ "PersonCreateInput",
162
+ "PersonUpdateInput",
163
+ "RateLimitError",
164
+ "Receipt",
165
+ "ReceiptPosition",
166
+ "ReceiptsListResponse",
167
+ "ServerError",
168
+ "ServiceUnavailableError",
169
+ "SyncTransport",
170
+ "UnprocessableEntityError",
171
+ "WebhookTriggerEvent",
172
+ "WebhookTriggerRequest",
173
+ "WebhooksResource",
174
+ "create_api_error",
175
+ "dump_json",
176
+ "from_dict",
177
+ "join_url",
178
+ "load_json",
179
+ "quote_path_segment",
180
+ "to_camel_case",
181
+ "to_jsonable",
182
+ ]
@@ -0,0 +1,148 @@
1
+ # SPDX-FileCopyrightText: 2026 2026 Stephan Lukasczyk <stephan@dante.de>
2
+ #
3
+ # SPDX-License-Identifier: EUPL-1.2
4
+
5
+ """Asynchronous Campai API client."""
6
+
7
+ from __future__ import annotations
8
+
9
+ from typing import TYPE_CHECKING, Self, override
10
+
11
+ if TYPE_CHECKING:
12
+ from collections.abc import Mapping
13
+ from types import TracebackType
14
+
15
+ import httpx
16
+
17
+ from campai_api.resources.auth import AsyncAuthResource
18
+ from campai_api.resources.finance import AsyncFinanceResource
19
+ from campai_api.resources.members import AsyncMembersResource
20
+ from campai_api.resources.webhooks import AsyncWebhooksResource
21
+ from campai_api.transport import DEFAULT_BASE_URL, DEFAULT_TIMEOUT, AsyncTransport
22
+
23
+
24
+ class AsyncCampaiClient:
25
+ """Asynchronous client for the Campai REST API.
26
+
27
+ Provides domain-scoped sub-clients for authentication, members/CRM, finances,
28
+ and webhook triggers using async/await syntax.
29
+
30
+ Usage:
31
+ ```python
32
+ async with AsyncCampaiClient(api_key="your_api_key") as client:
33
+ info = await client.auth.get_info()
34
+ contacts = await client.members.list(
35
+ organization_id=info.organization,
36
+ mandate_id="mandate_id",
37
+ )
38
+ ```
39
+ """
40
+
41
+ def __init__(
42
+ self,
43
+ api_key: str | None = None,
44
+ *,
45
+ base_url: str | httpx.URL = DEFAULT_BASE_URL,
46
+ timeout: float | httpx.Timeout = DEFAULT_TIMEOUT,
47
+ headers: Mapping[str, str] | None = None,
48
+ client: httpx.AsyncClient | None = None,
49
+ auth: httpx.Auth | None = None,
50
+ transport: AsyncTransport | None = None,
51
+ ) -> None:
52
+ """Initialize a new asynchronous Campai API client.
53
+
54
+ Args:
55
+ api_key: Campai API key.
56
+ base_url: Base URL of the Campai REST API.
57
+ timeout: HTTP request timeout in seconds.
58
+ headers: Additional HTTP headers to include with requests.
59
+ client: Pre-configured httpx.AsyncClient instance.
60
+ auth: Custom httpx.Auth instance.
61
+ transport: Pre-configured AsyncTransport instance.
62
+
63
+ Raises:
64
+ ValueError: If invalid configuration options are provided.
65
+ """
66
+ if transport is not None:
67
+ has_extra_config = any((
68
+ api_key is not None,
69
+ auth is not None,
70
+ client is not None,
71
+ headers is not None,
72
+ base_url != DEFAULT_BASE_URL,
73
+ timeout != DEFAULT_TIMEOUT,
74
+ ))
75
+ if has_extra_config:
76
+ raise ValueError(
77
+ "Cannot specify configuration options (api_key, base_url, timeout, "
78
+ "headers, client, auth) when an explicit 'transport' is provided."
79
+ )
80
+ self._transport = transport
81
+ self._owns_transport = False
82
+ else:
83
+ if api_key is None and auth is None and client is None:
84
+ raise ValueError(
85
+ "An api_key must be provided, or an explicit auth/client/transport "
86
+ "instance must be supplied."
87
+ )
88
+ if api_key is not None and (not isinstance(api_key, str) or not api_key.strip()):
89
+ raise ValueError("API key must be a non-empty string.")
90
+
91
+ self._transport = AsyncTransport(
92
+ api_key=api_key,
93
+ base_url=base_url,
94
+ timeout=timeout,
95
+ headers=headers,
96
+ client=client,
97
+ auth=auth,
98
+ )
99
+ self._owns_transport = True
100
+
101
+ self.auth: AsyncAuthResource = AsyncAuthResource(self._transport)
102
+ self.members: AsyncMembersResource = AsyncMembersResource(self._transport)
103
+ self.finance: AsyncFinanceResource = AsyncFinanceResource(self._transport)
104
+ self.webhooks: AsyncWebhooksResource = AsyncWebhooksResource(self._transport)
105
+
106
+ @property
107
+ def is_closed(self) -> bool:
108
+ """Whether the underlying client session is closed."""
109
+ return self._transport.is_closed
110
+
111
+ @property
112
+ def transport(self) -> AsyncTransport:
113
+ """The underlying AsyncTransport engine."""
114
+ return self._transport
115
+
116
+ @property
117
+ def client(self) -> httpx.AsyncClient:
118
+ """The underlying httpx.AsyncClient."""
119
+ return self._transport.client
120
+
121
+ @property
122
+ def base_url(self) -> str:
123
+ """The base URL for API requests."""
124
+ return self._transport.base_url
125
+
126
+ async def aclose(self) -> None:
127
+ """Close the underlying HTTP client session asynchronously if owned."""
128
+ if self._owns_transport:
129
+ await self._transport.aclose()
130
+
131
+ async def close(self) -> None:
132
+ """Close the underlying HTTP client session asynchronously (alias for aclose)."""
133
+ await self.aclose()
134
+
135
+ async def __aenter__(self) -> Self:
136
+ return self
137
+
138
+ async def __aexit__(
139
+ self,
140
+ exc_type: type[BaseException] | None,
141
+ exc_val: BaseException | None,
142
+ exc_tb: TracebackType | None,
143
+ ) -> None:
144
+ await self.aclose()
145
+
146
+ @override
147
+ def __repr__(self) -> str:
148
+ return f"<AsyncCampaiClient base_url={self.base_url!r}>"
campai_api/auth.py ADDED
@@ -0,0 +1,49 @@
1
+ # SPDX-FileCopyrightText: 2026 2026 Stephan Lukasczyk <stephan@dante.de>
2
+ #
3
+ # SPDX-License-Identifier: EUPL-1.2
4
+
5
+ """Authentication mechanisms for the Campai REST API."""
6
+
7
+ from __future__ import annotations
8
+
9
+ from typing import TYPE_CHECKING, override
10
+
11
+ if TYPE_CHECKING:
12
+ from collections.abc import Generator
13
+
14
+ import httpx
15
+
16
+ API_KEY_HEADER = "X-API-Key"
17
+
18
+
19
+ class APIKeyAuth(httpx.Auth):
20
+ """Authentication handler injecting the Campai API key header."""
21
+
22
+ def __init__(self, api_key: str) -> None:
23
+ """Initialize APIKeyAuth with the given API key.
24
+
25
+ Args:
26
+ api_key: The Campai API key string.
27
+
28
+ Raises:
29
+ ValueError: If the API key is empty or invalid.
30
+ """
31
+ if not api_key or not isinstance(api_key, str) or not api_key.strip():
32
+ raise ValueError("API key must be a non-empty string.")
33
+ self._api_key = api_key.strip()
34
+
35
+ @property
36
+ def api_key(self) -> str:
37
+ """The configured API key."""
38
+ return self._api_key
39
+
40
+ @override
41
+ def auth_flow(self, request: httpx.Request) -> Generator[httpx.Request, httpx.Response, None]:
42
+ """Inject the X-API-Key header into outgoing requests."""
43
+ request.headers[API_KEY_HEADER] = self._api_key
44
+ yield request
45
+
46
+ @override
47
+ def __repr__(self) -> str:
48
+ masked = f"{self._api_key[:4]}...{self._api_key[-4:]}" if len(self._api_key) > 8 else "***"
49
+ return f"APIKeyAuth(api_key={masked!r})"
campai_api/client.py ADDED
@@ -0,0 +1,144 @@
1
+ # SPDX-FileCopyrightText: 2026 2026 Stephan Lukasczyk <stephan@dante.de>
2
+ #
3
+ # SPDX-License-Identifier: EUPL-1.2
4
+
5
+ """Synchronous Campai API client."""
6
+
7
+ from __future__ import annotations
8
+
9
+ from typing import TYPE_CHECKING, Self, override
10
+
11
+ if TYPE_CHECKING:
12
+ from collections.abc import Mapping
13
+ from types import TracebackType
14
+
15
+ import httpx
16
+
17
+ from campai_api.resources.auth import AuthResource
18
+ from campai_api.resources.finance import FinanceResource
19
+ from campai_api.resources.members import MembersResource
20
+ from campai_api.resources.webhooks import WebhooksResource
21
+ from campai_api.transport import DEFAULT_BASE_URL, DEFAULT_TIMEOUT, SyncTransport
22
+
23
+
24
+ class CampaiClient:
25
+ """Synchronous client for the Campai REST API.
26
+
27
+ Provides domain-scoped sub-clients for authentication, members/CRM, finances,
28
+ and webhook triggers.
29
+
30
+ Usage:
31
+ ```python
32
+ with CampaiClient(api_key="your_api_key") as client:
33
+ info = client.auth.get_info()
34
+ contacts = client.members.list(
35
+ organization_id=info.organization,
36
+ mandate_id="mandate_id",
37
+ )
38
+ ```
39
+ """
40
+
41
+ def __init__(
42
+ self,
43
+ api_key: str | None = None,
44
+ *,
45
+ base_url: str | httpx.URL = DEFAULT_BASE_URL,
46
+ timeout: float | httpx.Timeout = DEFAULT_TIMEOUT,
47
+ headers: Mapping[str, str] | None = None,
48
+ client: httpx.Client | None = None,
49
+ auth: httpx.Auth | None = None,
50
+ transport: SyncTransport | None = None,
51
+ ) -> None:
52
+ """Initialize a new synchronous Campai API client.
53
+
54
+ Args:
55
+ api_key: Campai API key.
56
+ base_url: Base URL of the Campai REST API.
57
+ timeout: HTTP request timeout in seconds.
58
+ headers: Additional HTTP headers to include with requests.
59
+ client: Pre-configured httpx.Client instance.
60
+ auth: Custom httpx.Auth instance.
61
+ transport: Pre-configured SyncTransport instance.
62
+
63
+ Raises:
64
+ ValueError: If invalid configuration options are provided.
65
+ """
66
+ if transport is not None:
67
+ has_extra_config = any((
68
+ api_key is not None,
69
+ auth is not None,
70
+ client is not None,
71
+ headers is not None,
72
+ base_url != DEFAULT_BASE_URL,
73
+ timeout != DEFAULT_TIMEOUT,
74
+ ))
75
+ if has_extra_config:
76
+ raise ValueError(
77
+ "Cannot specify configuration options (api_key, base_url, timeout, "
78
+ "headers, client, auth) when an explicit 'transport' is provided."
79
+ )
80
+ self._transport = transport
81
+ self._owns_transport = False
82
+ else:
83
+ if api_key is None and auth is None and client is None:
84
+ raise ValueError(
85
+ "An api_key must be provided, or an explicit auth/client/transport "
86
+ "instance must be supplied."
87
+ )
88
+ if api_key is not None and (not isinstance(api_key, str) or not api_key.strip()):
89
+ raise ValueError("API key must be a non-empty string.")
90
+
91
+ self._transport = SyncTransport(
92
+ api_key=api_key,
93
+ base_url=base_url,
94
+ timeout=timeout,
95
+ headers=headers,
96
+ client=client,
97
+ auth=auth,
98
+ )
99
+ self._owns_transport = True
100
+
101
+ self.auth: AuthResource = AuthResource(self._transport)
102
+ self.members: MembersResource = MembersResource(self._transport)
103
+ self.finance: FinanceResource = FinanceResource(self._transport)
104
+ self.webhooks: WebhooksResource = WebhooksResource(self._transport)
105
+
106
+ @property
107
+ def is_closed(self) -> bool:
108
+ """Whether the underlying client session is closed."""
109
+ return self._transport.is_closed
110
+
111
+ @property
112
+ def transport(self) -> SyncTransport:
113
+ """The underlying SyncTransport engine."""
114
+ return self._transport
115
+
116
+ @property
117
+ def client(self) -> httpx.Client:
118
+ """The underlying httpx.Client."""
119
+ return self._transport.client
120
+
121
+ @property
122
+ def base_url(self) -> str:
123
+ """The base URL for API requests."""
124
+ return self._transport.base_url
125
+
126
+ def close(self) -> None:
127
+ """Close the underlying HTTP client session if owned."""
128
+ if self._owns_transport:
129
+ self._transport.close()
130
+
131
+ def __enter__(self) -> Self:
132
+ return self
133
+
134
+ def __exit__(
135
+ self,
136
+ exc_type: type[BaseException] | None,
137
+ exc_val: BaseException | None,
138
+ exc_tb: TracebackType | None,
139
+ ) -> None:
140
+ self.close()
141
+
142
+ @override
143
+ def __repr__(self) -> str:
144
+ return f"<CampaiClient base_url={self.base_url!r}>"