pixxio-client 0.4.2__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.
@@ -0,0 +1,272 @@
1
+ """
2
+ PIXXIO API Client Library
3
+
4
+ This module provides the main client interface for interacting with the PIXXIO digital
5
+ asset management (DAM) REST API. It includes authentication handling and session
6
+ management for making API requests.
7
+
8
+ The client library follows a resource-based architecture where each API endpoint
9
+ is represented by a dedicated class inheriting from base classes with common
10
+ functionality.
11
+
12
+ Example:
13
+ Basic usage of the PIXXIO client:
14
+
15
+ ```python
16
+ from pixxio_client import Client, Authentication
17
+ from pixxio_client.files import File
18
+
19
+ # Create authenticated client
20
+ client = Client.create_client("your-api-key", "your-host.com")
21
+
22
+ # Use resource endpoints
23
+ files = File(client)
24
+ result = files.get() # List all files
25
+ file_detail = files.get(id=123) # Get specific file
26
+ ```
27
+
28
+ Classes:
29
+ Authentication: Handles Bearer token authentication for API requests
30
+ Client: Main client for managing API connections and session state
31
+ """
32
+
33
+ import logging
34
+ from typing import Optional
35
+ import requests
36
+ from requests import Session
37
+
38
+ from .pixxio_types import APIKey
39
+
40
+
41
+ class Authentication:
42
+ """
43
+ Handles Bearer token authentication for PIXXIO API requests.
44
+
45
+ This class manages API key authentication using the Bearer token method
46
+ required by the PIXXIO REST API. It automatically adds the proper
47
+ Authorization header to HTTP requests.
48
+
49
+ Attributes:
50
+ api_key (APIKey): The PIXXIO API key for authentication
51
+
52
+ Example:
53
+ ```python
54
+ auth = Authentication("your-api-key-here")
55
+ session = requests.Session()
56
+ auth.authenticate_client(session)
57
+ # Session now has Authorization: Bearer your-api-key-here
58
+ ```
59
+ """
60
+
61
+ def __init__(self, api_key: APIKey) -> None:
62
+ """
63
+ Initialize authentication with API key.
64
+
65
+ Args:
66
+ api_key: The PIXXIO API key obtained from your PIXXIO account
67
+ """
68
+ self.api_key = api_key
69
+
70
+ def authenticate_client(self, client_session: Session) -> None:
71
+ """
72
+ Add Bearer token authentication headers to the HTTP session.
73
+
74
+ This method updates the session headers to include the Authorization
75
+ header with the Bearer token format required by PIXXIO API.
76
+
77
+ Args:
78
+ client_session: The requests.Session object to authenticate
79
+
80
+ Note:
81
+ This method modifies the session in-place by updating its headers.
82
+ All subsequent requests using this session will include the
83
+ authentication header.
84
+ """
85
+ client_session.headers.update(
86
+ {
87
+ "Authorization": f"Bearer {self.api_key}",
88
+ "Accept": "application/json"
89
+ }
90
+ )
91
+
92
+
93
+ class Client:
94
+ """
95
+ Main client for interacting with the PIXXIO API.
96
+
97
+ This class manages the HTTP session, authentication, and base URL construction
98
+ for all API requests. It serves as the foundation for all resource-specific
99
+ classes and handles connection state management.
100
+
101
+ The client automatically constructs the proper API URLs and manages session
102
+ state including authentication headers. It supports both HTTP and HTTPS
103
+ connections and can be configured for different API versions.
104
+
105
+ Attributes:
106
+ authentication (Authentication): Authentication handler for API requests
107
+ location_base (str): Base URL for all API endpoints
108
+ logger (logging.Logger): Logger instance for debugging and monitoring
109
+
110
+ Example:
111
+ ```python
112
+ # Method 1: Direct instantiation
113
+ auth = Authentication("your-api-key")
114
+ client = Client(auth, "your-host.com")
115
+
116
+ # Method 2: Factory method (recommended)
117
+ client = Client.create_client("your-api-key", "your-host.com")
118
+
119
+ # Use with resource classes
120
+ files = File(client)
121
+ collections = Collection(client)
122
+ ```
123
+ """
124
+
125
+ def __init__(self, authentication: Authentication, host: str,
126
+ port: Optional[int] = None, version: str = 'v1',
127
+ logger: Optional[logging.Logger] = None) -> None:
128
+ """
129
+ Initialize the PIXXIO API client.
130
+
131
+ Args:
132
+ authentication: Authentication instance with API credentials
133
+ host: PIXXIO host domain (e.g., "your-company.pixxio.com")
134
+ port: Optional port number for API connections
135
+ version: API version to use (default: "v1")
136
+ logger: Optional custom logger instance
137
+
138
+ Note:
139
+ The host parameter should not include the protocol (http/https).
140
+ The client automatically uses HTTPS unless explicitly configured
141
+ otherwise. Port numbers are typically not needed for standard
142
+ PIXXIO installations.
143
+ """
144
+ self.authentication = authentication
145
+ self._host = host
146
+ self._port = port
147
+ self._version = version
148
+
149
+ if logger is None:
150
+ self.logger = logging.getLogger("pixx!o client")
151
+ else:
152
+ self.logger = logger
153
+
154
+ host = host.lower()
155
+ if not host.startswith('http'):
156
+ host = f'https://{host}'
157
+ if host.endswith('/'):
158
+ host = host[:-1]
159
+ if port is not None:
160
+ host = f'{host}:{port}'
161
+ self.location_base = f'{host}/api/{version}'
162
+ self._session: Optional[Session] = None
163
+
164
+ @staticmethod
165
+ def create_client(api_key: APIKey, host: str) -> 'Client':
166
+ """
167
+ Factory method to create a client instance with API key authentication.
168
+
169
+ This is the recommended way to create a client as it handles the
170
+ Authentication object creation automatically. It's particularly useful
171
+ when you only have an API key and don't need custom authentication
172
+ behavior.
173
+
174
+ Args:
175
+ api_key: Your PIXXIO API key (Bearer token)
176
+ host: PIXXIO host domain (e.g., "company.pixxio.com")
177
+
178
+ Returns:
179
+ Client: Fully configured client ready for API operations
180
+
181
+ Example:
182
+ ```python
183
+ # Simple client creation
184
+ client = Client.create_client("your-api-key", "company.pixxio.com")
185
+
186
+ # Use immediately with resources
187
+ from pixxio_client.files import File
188
+ files = File(client)
189
+ all_files = files.get()
190
+ ```
191
+ """
192
+ return Client(
193
+ host=host,
194
+ authentication=Authentication(api_key))
195
+
196
+ @property
197
+ def session(self) -> Session:
198
+ """
199
+ Get or create authenticated HTTP session.
200
+
201
+ This property lazily creates and configures the HTTP session used for
202
+ all API requests. The session includes proper authentication headers
203
+ and connection pooling for efficient request handling.
204
+
205
+ Returns:
206
+ Session: Configured requests.Session with authentication headers
207
+
208
+ Note:
209
+ The session is created once and reused for all subsequent requests
210
+ until reset() is called. This enables connection pooling and
211
+ improves performance for multiple API calls.
212
+ """
213
+ if self._session is None:
214
+ self._session = requests.Session()
215
+ self.authentication.authenticate_client(self._session)
216
+ return self._session
217
+
218
+ def reset(self) -> None:
219
+ """
220
+ Reset the HTTP session, forcing re-authentication on next use.
221
+
222
+ This method clears the current session, which will force creation
223
+ of a new session with fresh authentication headers on the next
224
+ API request. Useful for handling authentication token expiration
225
+ or connection issues.
226
+
227
+ Example:
228
+ ```python
229
+ client = Client.create_client("api-key", "company.pixxio.com")
230
+
231
+
232
+ # Make some requests...
233
+ files = File(client)
234
+
235
+ # Reset if authentication issues occur
236
+ client.reset()
237
+
238
+ # Next request will use new session
239
+ files.get() # Creates new authenticated session
240
+ ```
241
+ """
242
+ self._session = None
243
+
244
+
245
+ # Webhook parser exports (generalized approach - single EventData class handles all event types)
246
+ from .webhook_parser import (
247
+ WebhookParser,
248
+ WebhookEvent,
249
+ EventData,
250
+ parse_pixxio_webhook,
251
+ EventAction,
252
+ ResourceType,
253
+ EventKey,
254
+ )
255
+
256
+ __all__ = [
257
+ # Authentication and Client
258
+ "Authentication",
259
+ "Client",
260
+ # Webhook Parser - Generalized Approach
261
+ "WebhookParser",
262
+ "WebhookEvent",
263
+ "EventData",
264
+ "parse_pixxio_webhook",
265
+ "EventAction",
266
+ "ResourceType",
267
+ "EventKey",
268
+ ]
269
+
270
+
271
+
272
+