alfred-python 0.4.0__tar.gz → 0.5.0__tar.gz

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.
Files changed (44) hide show
  1. {alfred_python-0.4.0 → alfred_python-0.5.0}/PKG-INFO +88 -3
  2. {alfred_python-0.4.0 → alfred_python-0.5.0}/README.md +85 -2
  3. {alfred_python-0.4.0/src → alfred_python-0.5.0}/alfred/base/config.py +3 -1
  4. alfred_python-0.5.0/alfred/base/constants.py +94 -0
  5. alfred_python-0.5.0/alfred/exceptions/__init__.py +7 -0
  6. alfred_python-0.5.0/alfred/realtime/__init__.py +124 -0
  7. {alfred_python-0.4.0/src → alfred_python-0.5.0}/alfred/rest/__init__.py +7 -7
  8. {alfred_python-0.4.0/src → alfred_python-0.5.0}/alfred/rest/data_points/v1.py +2 -2
  9. {alfred_python-0.4.0/src → alfred_python-0.5.0}/alfred/rest/files/v1.py +3 -4
  10. {alfred_python-0.4.0/src → alfred_python-0.5.0}/alfred/rest/jobs/base.py +1 -1
  11. {alfred_python-0.4.0/src → alfred_python-0.5.0}/alfred/rest/jobs/v1.py +3 -3
  12. {alfred_python-0.4.0/src → alfred_python-0.5.0}/alfred/rest/sessions/v1.py +2 -2
  13. alfred_python-0.5.0/alfred/typings/__init__.py +1 -0
  14. {alfred_python-0.4.0/src → alfred_python-0.5.0}/alfred/typings/misc.py +2 -2
  15. alfred_python-0.5.0/alfred/utils/__init__.py +1 -0
  16. {alfred_python-0.4.0/src → alfred_python-0.5.0}/alfred/utils/logging.py +1 -1
  17. {alfred_python-0.4.0/src → alfred_python-0.5.0}/alfred_python.egg-info/PKG-INFO +88 -3
  18. alfred_python-0.5.0/alfred_python.egg-info/SOURCES.txt +37 -0
  19. alfred_python-0.5.0/alfred_python.egg-info/dependency_links.txt +1 -0
  20. {alfred_python-0.4.0/src → alfred_python-0.5.0}/alfred_python.egg-info/requires.txt +2 -0
  21. {alfred_python-0.4.0 → alfred_python-0.5.0}/pyproject.toml +3 -2
  22. alfred_python-0.4.0/src/alfred/__init__.py +0 -1
  23. alfred_python-0.4.0/src/alfred/base/constants.py +0 -8
  24. alfred_python-0.4.0/src/alfred/typings/__init__.py +0 -1
  25. alfred_python-0.4.0/src/alfred/utils/__init__.py +0 -1
  26. alfred_python-0.4.0/src/alfred_python.egg-info/SOURCES.txt +0 -35
  27. {alfred_python-0.4.0 → alfred_python-0.5.0}/LICENSE +0 -0
  28. /alfred_python-0.4.0/src/alfred_python.egg-info/dependency_links.txt → /alfred_python-0.5.0/alfred/__init__.py +0 -0
  29. {alfred_python-0.4.0/src → alfred_python-0.5.0}/alfred/base/__init__.py +0 -0
  30. {alfred_python-0.4.0/src → alfred_python-0.5.0}/alfred/base/exceptions.py +0 -0
  31. {alfred_python-0.4.0/src → alfred_python-0.5.0}/alfred/http/__init__.py +0 -0
  32. {alfred_python-0.4.0/src → alfred_python-0.5.0}/alfred/http/http_client.py +0 -0
  33. {alfred_python-0.4.0/src → alfred_python-0.5.0}/alfred/http/typed.py +0 -0
  34. {alfred_python-0.4.0/src → alfred_python-0.5.0}/alfred/rest/data_points/__init__.py +0 -0
  35. {alfred_python-0.4.0/src → alfred_python-0.5.0}/alfred/rest/data_points/base.py +0 -0
  36. {alfred_python-0.4.0/src → alfred_python-0.5.0}/alfred/rest/files/__init__.py +0 -0
  37. {alfred_python-0.4.0/src → alfred_python-0.5.0}/alfred/rest/files/base.py +0 -0
  38. {alfred_python-0.4.0/src → alfred_python-0.5.0}/alfred/rest/files/typed.py +0 -0
  39. {alfred_python-0.4.0/src → alfred_python-0.5.0}/alfred/rest/jobs/__init__.py +0 -0
  40. {alfred_python-0.4.0/src → alfred_python-0.5.0}/alfred/rest/jobs/typed.py +0 -0
  41. {alfred_python-0.4.0/src → alfred_python-0.5.0}/alfred/rest/sessions/__init__.py +0 -0
  42. {alfred_python-0.4.0/src → alfred_python-0.5.0}/alfred/rest/sessions/base.py +0 -0
  43. {alfred_python-0.4.0/src → alfred_python-0.5.0}/alfred_python.egg-info/top_level.txt +0 -0
  44. {alfred_python-0.4.0 → alfred_python-0.5.0}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: alfred-python
3
- Version: 0.4.0
3
+ Version: 0.5.0
4
4
  Summary: Python library designed to simplify and streamline interactions with the Alfred API
5
5
  Author-email: Tagshelf LLC <support@tagshelf.com>
6
6
  License: Copyright (c) 2018 The Python Packaging Authority
@@ -32,6 +32,8 @@ License-File: LICENSE
32
32
  Requires-Dist: pydantic>=2.0
33
33
  Requires-Dist: pydantic-settings>=2.0
34
34
  Requires-Dist: requests>=2.30
35
+ Requires-Dist: python-socketio>=5.11
36
+ Requires-Dist: websocket-client>=1.8
35
37
 
36
38
  # Overview
37
39
 
@@ -47,9 +49,9 @@ Check out this simple example to get up and running:
47
49
 
48
50
  ```python
49
51
  from alfred.rest import AlfredClient
50
- from alfred.base.config import Configuration
52
+ from alfred.base import Configuration
51
53
 
52
- config = Configuration.v1()
54
+ config = Configuration.default()
53
55
  auth_config = {"api_key": "AXXXXXXXXXXXXXXXXXXXXXX"}
54
56
 
55
57
  client = AlfredClient(config, auth_config)
@@ -206,6 +208,89 @@ In this SDK, we implement automatic retries to enhance the reliability of networ
206
208
 
207
209
  For non-idempotent methods like POST and PATCH, the SDK does not perform retries by default because doing so could potentially result in unwanted side effects or duplicate operations. If you need to enable retries for these methods under specific circumstances, please handle them cautiously in your application logic.
208
210
 
211
+ ## Real-time Events
212
+
213
+ The `alfred-python` library provides a way to listen to events emitted by Alfred IPA in real-time through a websockets implementation. This feature is particularly useful when you need to monitor the progress of a Job, File, or any other event that occurs within the Alfred platform. To see more information visit our [official documentation](https://docs.tagshelf.dev).
214
+
215
+ ### Getting started
216
+
217
+ To get started, you need to create an instance of the `AlfredRealTimeClient` class.
218
+
219
+ ```python
220
+ from alfred import AlfredRealTimeClient
221
+ from alfred.base import Configuration
222
+ from alfred import AuthConfiguration
223
+
224
+ config = Configuration.default()
225
+
226
+ auth_config = AuthConfiguration({
227
+ "api_key": "AXXXXXXXXXXXXXXXXXXXXXX"
228
+ })
229
+
230
+ client = AlfredRealTimeClient(config, auth_config, verbose=True)
231
+ ```
232
+
233
+ ### File Events
234
+ These events are specifically designed to respond to a variety of actions or status changes related to Files. To see more details about File events, visit our [official documentation](https://docs.tagshelf.dev/event-api/fileevents).
235
+ ```python
236
+ # Listen to all File events
237
+ client.on_file_event(lambda data: print(data))
238
+ ```
239
+
240
+ ### Job Events
241
+ Alfred performs asynchronous document classification, extraction, and indexing on a variety of file types. The events detailed here offer insights into how a Job progresses, fails, retries, or completes its tasks. To see more details about Job events, visit our [official documentation](https://docs.tagshelf.dev/event-api/jobevents).
242
+
243
+ ```python
244
+ # Listen to all Job events
245
+ client.on_job_event(lambda data: print(data))
246
+ ```
247
+
248
+ ### Specific Events
249
+
250
+ This enables you to select a specific event you wish to monitor from the list of supported events.
251
+ It's particularly useful when you want to listen to a specific event instead of all events of a particular type.
252
+
253
+ Here's an example of how to listen to a specific event:
254
+
255
+ ```python
256
+ from alfred.base import FileEvent, JobEvent
257
+
258
+ # Listen to the specific File Done event
259
+ client.on(FileEvent.FILE_DONE_EVENT.value, lambda data: print(data))
260
+
261
+ # Listen to the specific Job Finished event
262
+ client.on(JobEvent.JOB_FINISHED_EVENT.value, lambda data: print(data))
263
+ ```
264
+
265
+ Here is a list of all supported events:
266
+
267
+ | Event Type | Event Name | Description |
268
+ | --- | --- | --- |
269
+ | FileEvent | `FILE_ADD_TO_JOB_EVENT` | Triggered when a file is added to a job for processing. |
270
+ | FileEvent | `FILE_CATEGORY_CREATE_EVENT` | Occurs when a new category is created for a file. |
271
+ | FileEvent | `FILE_CATEGORY_DELETE_EVENT` | Signals the deletion of a file's category. |
272
+ | FileEvent | `FILE_CHANGE_TAG_EVENT` | Indicates a change in the tag associated with a file. |
273
+ | FileEvent | `FILE_DONE_EVENT` | Marks the completion of file processing. |
274
+ | FileEvent | `FILE_EXTRACTED_DATA_CREATE_EVENT` | Triggered when new data is extracted from a file. |
275
+ | FileEvent | `FILE_EXTRACTED_DATA_DELETE_EVENT` | Occurs when extracted data from a file is deleted. |
276
+ | FileEvent | `FILE_FAILED_EVENT` | Indicates a failure in file processing. |
277
+ | FileEvent | `FILE_MOVE_EVENT` | Signals the movement of a file within the system. |
278
+ | FileEvent | `FILE_MOVE_TO_PENDING_EVENT` | Triggered when a file is moved to a pending state. |
279
+ | FileEvent | `FILE_MOVE_TO_RECYCLE_BIN_EVENT` | Indicates movement of a file to the recycle bin. |
280
+ | FileEvent | `FILE_PROPERTY_CREATE_EVENT` | Reflects the creation of a file property. |
281
+ | FileEvent | `FILE_PROPERTY_DELETE_EVENT` | Signals the deletion of a file property. |
282
+ | FileEvent | `FILE_REMOVE_TAG_EVENT` | Signals the removal of a tag from a file. |
283
+ | FileEvent | `FILE_STATUS_UPDATE_EVENT` | Indicates an update in the file's status. |
284
+ | FileEvent | `FILE_UPDATE_EVENT` | Triggered when a file is updated in any manner. |
285
+ | JobEvent | `JOB_CREATE_EVENT` | Triggered when a new job is instantiated for file operations. |
286
+ | JobEvent | `JOB_EXCEEDED_RETRIES_EVENT` | Fires when job exceeds maximum retry attempts for a stage. |
287
+ | JobEvent | `JOB_FAILED_EVENT` | Occurs when a job halts due to an unrecoverable error. |
288
+ | JobEvent | `JOB_FINISHED_EVENT` | Triggered when job successfully completes all workflow stages. |
289
+ | JobEvent | `JOB_INVALID_EVENT` | Fires when job fails initial validation of input files or parameters. |
290
+ | JobEvent | `JOB_RETRY_EVENT` | Triggered when job retries a stage after a recoverable failure. |
291
+ | JobEvent | `JOB_STAGE_UPDATE_EVENT` | Occurs when job transitions from one workflow stage to another. |
292
+ | JobEvent | `JOB_START_EVENT` | Triggered when job begins its workflow and state machine. |
293
+
209
294
  ## Development Setup
210
295
 
211
296
  ### Setting up the development environment
@@ -12,9 +12,9 @@ Check out this simple example to get up and running:
12
12
 
13
13
  ```python
14
14
  from alfred.rest import AlfredClient
15
- from alfred.base.config import Configuration
15
+ from alfred.base import Configuration
16
16
 
17
- config = Configuration.v1()
17
+ config = Configuration.default()
18
18
  auth_config = {"api_key": "AXXXXXXXXXXXXXXXXXXXXXX"}
19
19
 
20
20
  client = AlfredClient(config, auth_config)
@@ -171,6 +171,89 @@ In this SDK, we implement automatic retries to enhance the reliability of networ
171
171
 
172
172
  For non-idempotent methods like POST and PATCH, the SDK does not perform retries by default because doing so could potentially result in unwanted side effects or duplicate operations. If you need to enable retries for these methods under specific circumstances, please handle them cautiously in your application logic.
173
173
 
174
+ ## Real-time Events
175
+
176
+ The `alfred-python` library provides a way to listen to events emitted by Alfred IPA in real-time through a websockets implementation. This feature is particularly useful when you need to monitor the progress of a Job, File, or any other event that occurs within the Alfred platform. To see more information visit our [official documentation](https://docs.tagshelf.dev).
177
+
178
+ ### Getting started
179
+
180
+ To get started, you need to create an instance of the `AlfredRealTimeClient` class.
181
+
182
+ ```python
183
+ from alfred import AlfredRealTimeClient
184
+ from alfred.base import Configuration
185
+ from alfred import AuthConfiguration
186
+
187
+ config = Configuration.default()
188
+
189
+ auth_config = AuthConfiguration({
190
+ "api_key": "AXXXXXXXXXXXXXXXXXXXXXX"
191
+ })
192
+
193
+ client = AlfredRealTimeClient(config, auth_config, verbose=True)
194
+ ```
195
+
196
+ ### File Events
197
+ These events are specifically designed to respond to a variety of actions or status changes related to Files. To see more details about File events, visit our [official documentation](https://docs.tagshelf.dev/event-api/fileevents).
198
+ ```python
199
+ # Listen to all File events
200
+ client.on_file_event(lambda data: print(data))
201
+ ```
202
+
203
+ ### Job Events
204
+ Alfred performs asynchronous document classification, extraction, and indexing on a variety of file types. The events detailed here offer insights into how a Job progresses, fails, retries, or completes its tasks. To see more details about Job events, visit our [official documentation](https://docs.tagshelf.dev/event-api/jobevents).
205
+
206
+ ```python
207
+ # Listen to all Job events
208
+ client.on_job_event(lambda data: print(data))
209
+ ```
210
+
211
+ ### Specific Events
212
+
213
+ This enables you to select a specific event you wish to monitor from the list of supported events.
214
+ It's particularly useful when you want to listen to a specific event instead of all events of a particular type.
215
+
216
+ Here's an example of how to listen to a specific event:
217
+
218
+ ```python
219
+ from alfred.base import FileEvent, JobEvent
220
+
221
+ # Listen to the specific File Done event
222
+ client.on(FileEvent.FILE_DONE_EVENT.value, lambda data: print(data))
223
+
224
+ # Listen to the specific Job Finished event
225
+ client.on(JobEvent.JOB_FINISHED_EVENT.value, lambda data: print(data))
226
+ ```
227
+
228
+ Here is a list of all supported events:
229
+
230
+ | Event Type | Event Name | Description |
231
+ | --- | --- | --- |
232
+ | FileEvent | `FILE_ADD_TO_JOB_EVENT` | Triggered when a file is added to a job for processing. |
233
+ | FileEvent | `FILE_CATEGORY_CREATE_EVENT` | Occurs when a new category is created for a file. |
234
+ | FileEvent | `FILE_CATEGORY_DELETE_EVENT` | Signals the deletion of a file's category. |
235
+ | FileEvent | `FILE_CHANGE_TAG_EVENT` | Indicates a change in the tag associated with a file. |
236
+ | FileEvent | `FILE_DONE_EVENT` | Marks the completion of file processing. |
237
+ | FileEvent | `FILE_EXTRACTED_DATA_CREATE_EVENT` | Triggered when new data is extracted from a file. |
238
+ | FileEvent | `FILE_EXTRACTED_DATA_DELETE_EVENT` | Occurs when extracted data from a file is deleted. |
239
+ | FileEvent | `FILE_FAILED_EVENT` | Indicates a failure in file processing. |
240
+ | FileEvent | `FILE_MOVE_EVENT` | Signals the movement of a file within the system. |
241
+ | FileEvent | `FILE_MOVE_TO_PENDING_EVENT` | Triggered when a file is moved to a pending state. |
242
+ | FileEvent | `FILE_MOVE_TO_RECYCLE_BIN_EVENT` | Indicates movement of a file to the recycle bin. |
243
+ | FileEvent | `FILE_PROPERTY_CREATE_EVENT` | Reflects the creation of a file property. |
244
+ | FileEvent | `FILE_PROPERTY_DELETE_EVENT` | Signals the deletion of a file property. |
245
+ | FileEvent | `FILE_REMOVE_TAG_EVENT` | Signals the removal of a tag from a file. |
246
+ | FileEvent | `FILE_STATUS_UPDATE_EVENT` | Indicates an update in the file's status. |
247
+ | FileEvent | `FILE_UPDATE_EVENT` | Triggered when a file is updated in any manner. |
248
+ | JobEvent | `JOB_CREATE_EVENT` | Triggered when a new job is instantiated for file operations. |
249
+ | JobEvent | `JOB_EXCEEDED_RETRIES_EVENT` | Fires when job exceeds maximum retry attempts for a stage. |
250
+ | JobEvent | `JOB_FAILED_EVENT` | Occurs when a job halts due to an unrecoverable error. |
251
+ | JobEvent | `JOB_FINISHED_EVENT` | Triggered when job successfully completes all workflow stages. |
252
+ | JobEvent | `JOB_INVALID_EVENT` | Fires when job fails initial validation of input files or parameters. |
253
+ | JobEvent | `JOB_RETRY_EVENT` | Triggered when job retries a stage after a recoverable failure. |
254
+ | JobEvent | `JOB_STAGE_UPDATE_EVENT` | Occurs when job transitions from one workflow stage to another. |
255
+ | JobEvent | `JOB_START_EVENT` | Triggered when job begins its workflow and state machine. |
256
+
174
257
  ## Development Setup
175
258
 
176
259
  ### Setting up the development environment
@@ -4,6 +4,7 @@ from typing import TypedDict, Optional, Text
4
4
 
5
5
  class ConfigurationDict(TypedDict):
6
6
  base_url: Text
7
+ realtime_url: Text
7
8
  version: int
8
9
 
9
10
 
@@ -15,7 +16,7 @@ class Configuration:
15
16
  @staticmethod
16
17
  def default() -> ConfigurationDict:
17
18
  """
18
- Returns default client configuration. Currently targets Alfred V1.
19
+ Returns default client configuration. Currently, targets Alfred V1.
19
20
  """
20
21
  return Configuration.v1()
21
22
 
@@ -28,4 +29,5 @@ class Configuration:
28
29
  return {
29
30
  "version": 1,
30
31
  "base_url": overrides.get("base_url", "https://app.tagshelf.com"),
32
+ "realtime_url": overrides.get("realtime_url", "https://sockets.tagshelf.io"),
31
33
  }
@@ -0,0 +1,94 @@
1
+ from enum import Enum
2
+
3
+ from alfred.http.typed import ResponseType
4
+
5
+ # Response type/header mapping
6
+ RESPONSE_TYPE_HEADER_MAPPING = {
7
+ ResponseType.JSON: "application/json",
8
+ ResponseType.TEXT: "text/plain",
9
+ ResponseType.XML: "application/xml",
10
+ }
11
+
12
+
13
+ class EventType(Enum):
14
+ """
15
+ Enumeration of event types.
16
+ """
17
+ JOB_EVENT = "job_event"
18
+ FILE_EVENT = "file_event"
19
+
20
+
21
+ class FileEvent(Enum):
22
+ # Triggered when a file is added to a job for processing.
23
+ FILE_ADD_TO_JOB_EVENT = "file_add_to_job_event"
24
+
25
+ # Occurs when a new category is created for a file.
26
+ FILE_CATEGORY_CREATE_EVENT = "file_category_create_event"
27
+
28
+ # Signals the deletion of a file's category.
29
+ FILE_CATEGORY_DELETE_EVENT = "file_category_delete_event"
30
+
31
+ # Indicates a change in the tag associated with a file.
32
+ FILE_CHANGE_TAG_EVENT = "file_change_tag_event"
33
+
34
+ # Marks the completion of file processing.
35
+ FILE_DONE_EVENT = "file_done_event"
36
+
37
+ # Triggered when new data is extracted from a file.
38
+ FILE_EXTRACTED_DATA_CREATE_EVENT = "file_extracted_data_create_event"
39
+
40
+ # Occurs when extracted data from a file is deleted.
41
+ FILE_EXTRACTED_DATA_DELETE_EVENT = "file_extracted_data_delete_event"
42
+
43
+ # Indicates a failure in file processing.
44
+ FILE_FAILED_EVENT = "file_failed_event"
45
+
46
+ # Signals the movement of a file within the system.
47
+ FILE_MOVE_EVENT = "file_move_event"
48
+
49
+ # Triggered when a file is moved to a pending state.
50
+ FILE_MOVE_TO_PENDING_EVENT = "file_move_to_pending_event"
51
+
52
+ # Indicates movement of a file to the recycle bin.
53
+ FILE_MOVE_TO_RECYCLE_BIN_EVENT = "file_move_to_recycle_bin_event"
54
+
55
+ # Reflects the creation of a file property.
56
+ FILE_PROPERTY_CREATE_EVENT = "file_property_create_event"
57
+
58
+ # Signals the deletion of a file property.
59
+ FILE_PROPERTY_DELETE_EVENT = "file_property_delete_event"
60
+
61
+ # Signals the removal of a tag from a file.
62
+ FILE_REMOVE_TAG_EVENT = "file_remove_tag_event"
63
+
64
+ # Indicates an update in the file's status.
65
+ FILE_STATUS_UPDATE_EVENT = "file_status_update_event"
66
+
67
+ # Triggered when a file is updated in any manner.
68
+ FILE_UPDATE_EVENT = "file_update_event"
69
+
70
+
71
+ class JobEvent(Enum):
72
+ # Triggered when a new job is instantiated for file operations.
73
+ JOB_CREATE_EVENT = "job_create_event"
74
+
75
+ # Fires when job exceeds maximum retry attempts for a stage.
76
+ JOB_EXCEEDED_RETRIES_EVENT = "job_exceeded_retries_event"
77
+
78
+ # Occurs when a job halts due to an unrecoverable error.
79
+ JOB_FAILED_EVENT = "job_failed_event"
80
+
81
+ # Triggered when job successfully completes all workflow stages.
82
+ JOB_FINISHED_EVENT = "job_finished_event"
83
+
84
+ # Fires when job fails initial validation of input files or parameters.
85
+ JOB_INVALID_EVENT = "job_invalid_event"
86
+
87
+ # Triggered when job retries a stage after a recoverable failure.
88
+ JOB_RETRY_EVENT = "job_retry_event"
89
+
90
+ # Occurs when job transitions from one workflow stage to another.
91
+ JOB_STAGE_UPDATE_EVENT = "job_stage_update_event"
92
+
93
+ # Triggered when job begins its workflow and state machine.
94
+ JOB_START_EVENT = "job_start_event"
@@ -0,0 +1,7 @@
1
+ class ConnectionError(Exception):
2
+ """
3
+ Raised when a connection error occurs.
4
+ """
5
+ def __init__(self, message="A connection error occurred"):
6
+ self.message = message
7
+ super().__init__(self.message)
@@ -0,0 +1,124 @@
1
+ # Native Imports
2
+ from typing import Union
3
+
4
+ # 3rd Party Imports
5
+ import socketio
6
+
7
+ # Project Imports
8
+ import alfred.exceptions
9
+ from alfred.base.config import ConfigurationDict
10
+ from alfred.base.constants import EventType, FileEvent, JobEvent
11
+ from alfred.http.typed import AuthConfiguration
12
+ from alfred.utils import logging, setup_logger
13
+
14
+
15
+ class AlfredRealTimeClient:
16
+ def __init__(self, config: ConfigurationDict, auth_config: AuthConfiguration, verbose=False):
17
+ """
18
+ Initializes the AlfredRealTimeClient class.
19
+
20
+ Args:
21
+ config (ConfigurationDict): The configuration dictionary.
22
+ auth_config (AuthConfiguration): The authentication configuration.
23
+ verbose (bool, optional): Whether to print verbose output. Defaults to False.
24
+ """
25
+ self.socket = socketio.Client()
26
+ self.verbose = verbose
27
+ self.config = config
28
+ self.auth_config = auth_config
29
+ self.base_url = config.get("realtime_url")
30
+
31
+ # Initialize logger
32
+ self.logger = logging.getLogger("alfred-python")
33
+ if self.logger.level == logging.NOTSET:
34
+ setup_logger({
35
+ "level": "DEBUG" if verbose else "INFO",
36
+ "name": "alfred-python"
37
+ })
38
+ print(self.logger.level)
39
+
40
+ # Subscribe to connection life-cycle events.
41
+ self.socket.on('connect', self.__on_connect)
42
+ self.socket.on('disconnect', self.__on_disconnect)
43
+ self.socket.on('connect_error', self.__on_connect_error)
44
+
45
+ # Establish connection with verbose output if enabled
46
+ if self.verbose:
47
+ self.logger.debug("Attempting to establish a connection...")
48
+ try:
49
+ self.socket.connect(f"{self.base_url}?apiKey={auth_config.get('api_key')}")
50
+ except Exception as err:
51
+ raise alfred.exceptions.ConnectionError(f"Could not establish connection with server: {err}")
52
+
53
+ def __on_connect(self):
54
+ """
55
+ Handles the 'connect' event.
56
+ """
57
+ self.logger.info(f"Successfully connected to: {self.base_url}")
58
+
59
+ def __on_disconnect(self):
60
+ """
61
+ Handles the 'disconnect' event.
62
+ """
63
+ self.logger.info("Disconnected from the server.")
64
+
65
+ def __on_connect_error(self, err):
66
+ """
67
+ Handles the 'connect_error' event.
68
+
69
+ Args:
70
+ err (str): The error message.
71
+ """
72
+ self.logger.info("Connection error: %s", err)
73
+ self.disconnect()
74
+ raise Exception(f"Failed to connect to {self.base_url}: {err}")
75
+
76
+ def __callback(self, event: Union[FileEvent, JobEvent, EventType], callback):
77
+ """
78
+ Wrapper function to subscribe a specific event.
79
+
80
+ Args:
81
+ event (str): The event name.
82
+ callback (function): The callback function to handle the event.
83
+ """
84
+ def handle_event(data):
85
+ if self.verbose:
86
+ self.logger.debug(f"Event {event} received: %s", data)
87
+ callback(data)
88
+
89
+ self.socket.on(event, handle_event)
90
+
91
+ def on_file_event(self, callback):
92
+ """
93
+ Listens to all file-related events.
94
+
95
+ Args:
96
+ callback (function): The callback function to handle the event.
97
+ """
98
+ self.__callback(EventType.FILE_EVENT.value, callback)
99
+
100
+ def on_job_event(self, callback):
101
+ """
102
+ Listens to all job-related events.
103
+
104
+ Args:
105
+ callback (function): The callback function to handle the event.
106
+ """
107
+ self.__callback(EventType.JOB_EVENT.value, callback)
108
+
109
+ def on(self, event: Union[FileEvent, JobEvent], callback):
110
+ """
111
+ Listens to a specific event.
112
+
113
+ Args:
114
+ event (str): The event name.
115
+ callback (function): The callback function to handle the event.
116
+ """
117
+ self.__callback(event, callback)
118
+
119
+ def disconnect(self):
120
+ """
121
+ Disconnects client from the server.
122
+ """
123
+ self.logger.info("Closing connection...")
124
+ self.socket.disconnect()
@@ -2,13 +2,13 @@
2
2
  from typing import Optional
3
3
 
4
4
  # Project imports
5
- from src.alfred.base.config import ConfigurationDict
6
- from src.alfred.http.http_client import HttpClient
7
- from src.alfred.http.typed import AuthConfiguration, HttpConfiguration
8
- from src.alfred.rest.data_points import DataPointsBase, DataPointsFactory
9
- from src.alfred.rest.sessions import SessionsBase, SessionsFactory
10
- from src.alfred.rest.jobs import JobsBase, JobsFactory
11
- from src.alfred.rest.files import FilesBase, FilesFactory
5
+ from alfred.base.config import ConfigurationDict
6
+ from alfred.http.http_client import HttpClient
7
+ from alfred.http.typed import AuthConfiguration, HttpConfiguration
8
+ from alfred.rest.data_points import DataPointsBase, DataPointsFactory
9
+ from alfred.rest.sessions import SessionsBase, SessionsFactory
10
+ from alfred.rest.jobs import JobsBase, JobsFactory
11
+ from alfred.rest.files import FilesBase, FilesFactory
12
12
 
13
13
 
14
14
  class AlfredClient:
@@ -2,8 +2,8 @@
2
2
  from typing import Text
3
3
 
4
4
  # Project imports
5
- from src.alfred.http.http_client import HttpClient
6
- from src.alfred.rest.data_points.base import DataPointsBase
5
+ from alfred.http.http_client import HttpClient
6
+ from alfred.rest.data_points.base import DataPointsBase
7
7
 
8
8
 
9
9
  class DataPoints(DataPointsBase):
@@ -2,13 +2,12 @@
2
2
  import os
3
3
  import json
4
4
  from typing import Text
5
- from io import BufferedReader
6
5
  from urllib.parse import unquote
7
6
 
8
7
  # Project imports
9
- from src.alfred.rest.files.typed import * # pylint: disable=W0401, W0614
10
- from src.alfred.http.http_client import HttpClient
11
- from src.alfred.base.exceptions import AlfredMissingArgument
8
+ from alfred.rest.files.typed import * # pylint: disable=W0401, W0614
9
+ from alfred.http.http_client import HttpClient
10
+ from alfred.base.exceptions import AlfredMissingArgument
12
11
  from .base import FilesBase
13
12
  from .typed import FileDetailsResponse
14
13
 
@@ -1,6 +1,6 @@
1
1
  # Native imports
2
2
  from typing import Text, Any
3
- from src.alfred.rest.jobs.typed import CreateJobDict
3
+ from alfred.rest.jobs.typed import CreateJobDict
4
4
  from abc import ABC, abstractmethod
5
5
 
6
6
 
@@ -2,9 +2,9 @@
2
2
  from typing import Text
3
3
 
4
4
  # Project imports
5
- from src.alfred.rest.jobs.typed import CreateJobDict
6
- from src.alfred.http.http_client import HttpClient
7
- from src.alfred.rest.jobs.base import JobsBase
5
+ from alfred.rest.jobs.typed import CreateJobDict
6
+ from alfred.http.http_client import HttpClient
7
+ from alfred.rest.jobs.base import JobsBase
8
8
 
9
9
 
10
10
  class Jobs(JobsBase):
@@ -2,8 +2,8 @@
2
2
  from typing import Text
3
3
 
4
4
  # Project imports
5
- from src.alfred.http.http_client import HttpClient
6
- from src.alfred.rest.sessions.base import SessionsBase
5
+ from alfred.http.http_client import HttpClient
6
+ from alfred.rest.sessions.base import SessionsBase
7
7
 
8
8
 
9
9
  class Sessions(SessionsBase):
@@ -0,0 +1 @@
1
+ from alfred.typings.misc import *
@@ -1,10 +1,10 @@
1
1
  # Native imports
2
- from typing import TypedDict
2
+ from typing import TypedDict, Union
3
3
 
4
4
 
5
5
  # Typed dictionaries
6
6
  class LoggingOptions(TypedDict):
7
- level: int
7
+ level: Union[str, int]
8
8
  name: str
9
9
  format: str
10
10
  papertrail_host: str
@@ -0,0 +1 @@
1
+ from alfred.utils.logging import *
@@ -6,7 +6,7 @@ from logging.handlers import SysLogHandler
6
6
  from typing import Union
7
7
 
8
8
  # Project imports
9
- from src.alfred.typings import LoggingOptions
9
+ from alfred.typings import LoggingOptions
10
10
 
11
11
 
12
12
  def setup_logger(options: LoggingOptions):
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: alfred-python
3
- Version: 0.4.0
3
+ Version: 0.5.0
4
4
  Summary: Python library designed to simplify and streamline interactions with the Alfred API
5
5
  Author-email: Tagshelf LLC <support@tagshelf.com>
6
6
  License: Copyright (c) 2018 The Python Packaging Authority
@@ -32,6 +32,8 @@ License-File: LICENSE
32
32
  Requires-Dist: pydantic>=2.0
33
33
  Requires-Dist: pydantic-settings>=2.0
34
34
  Requires-Dist: requests>=2.30
35
+ Requires-Dist: python-socketio>=5.11
36
+ Requires-Dist: websocket-client>=1.8
35
37
 
36
38
  # Overview
37
39
 
@@ -47,9 +49,9 @@ Check out this simple example to get up and running:
47
49
 
48
50
  ```python
49
51
  from alfred.rest import AlfredClient
50
- from alfred.base.config import Configuration
52
+ from alfred.base import Configuration
51
53
 
52
- config = Configuration.v1()
54
+ config = Configuration.default()
53
55
  auth_config = {"api_key": "AXXXXXXXXXXXXXXXXXXXXXX"}
54
56
 
55
57
  client = AlfredClient(config, auth_config)
@@ -206,6 +208,89 @@ In this SDK, we implement automatic retries to enhance the reliability of networ
206
208
 
207
209
  For non-idempotent methods like POST and PATCH, the SDK does not perform retries by default because doing so could potentially result in unwanted side effects or duplicate operations. If you need to enable retries for these methods under specific circumstances, please handle them cautiously in your application logic.
208
210
 
211
+ ## Real-time Events
212
+
213
+ The `alfred-python` library provides a way to listen to events emitted by Alfred IPA in real-time through a websockets implementation. This feature is particularly useful when you need to monitor the progress of a Job, File, or any other event that occurs within the Alfred platform. To see more information visit our [official documentation](https://docs.tagshelf.dev).
214
+
215
+ ### Getting started
216
+
217
+ To get started, you need to create an instance of the `AlfredRealTimeClient` class.
218
+
219
+ ```python
220
+ from alfred import AlfredRealTimeClient
221
+ from alfred.base import Configuration
222
+ from alfred import AuthConfiguration
223
+
224
+ config = Configuration.default()
225
+
226
+ auth_config = AuthConfiguration({
227
+ "api_key": "AXXXXXXXXXXXXXXXXXXXXXX"
228
+ })
229
+
230
+ client = AlfredRealTimeClient(config, auth_config, verbose=True)
231
+ ```
232
+
233
+ ### File Events
234
+ These events are specifically designed to respond to a variety of actions or status changes related to Files. To see more details about File events, visit our [official documentation](https://docs.tagshelf.dev/event-api/fileevents).
235
+ ```python
236
+ # Listen to all File events
237
+ client.on_file_event(lambda data: print(data))
238
+ ```
239
+
240
+ ### Job Events
241
+ Alfred performs asynchronous document classification, extraction, and indexing on a variety of file types. The events detailed here offer insights into how a Job progresses, fails, retries, or completes its tasks. To see more details about Job events, visit our [official documentation](https://docs.tagshelf.dev/event-api/jobevents).
242
+
243
+ ```python
244
+ # Listen to all Job events
245
+ client.on_job_event(lambda data: print(data))
246
+ ```
247
+
248
+ ### Specific Events
249
+
250
+ This enables you to select a specific event you wish to monitor from the list of supported events.
251
+ It's particularly useful when you want to listen to a specific event instead of all events of a particular type.
252
+
253
+ Here's an example of how to listen to a specific event:
254
+
255
+ ```python
256
+ from alfred.base import FileEvent, JobEvent
257
+
258
+ # Listen to the specific File Done event
259
+ client.on(FileEvent.FILE_DONE_EVENT.value, lambda data: print(data))
260
+
261
+ # Listen to the specific Job Finished event
262
+ client.on(JobEvent.JOB_FINISHED_EVENT.value, lambda data: print(data))
263
+ ```
264
+
265
+ Here is a list of all supported events:
266
+
267
+ | Event Type | Event Name | Description |
268
+ | --- | --- | --- |
269
+ | FileEvent | `FILE_ADD_TO_JOB_EVENT` | Triggered when a file is added to a job for processing. |
270
+ | FileEvent | `FILE_CATEGORY_CREATE_EVENT` | Occurs when a new category is created for a file. |
271
+ | FileEvent | `FILE_CATEGORY_DELETE_EVENT` | Signals the deletion of a file's category. |
272
+ | FileEvent | `FILE_CHANGE_TAG_EVENT` | Indicates a change in the tag associated with a file. |
273
+ | FileEvent | `FILE_DONE_EVENT` | Marks the completion of file processing. |
274
+ | FileEvent | `FILE_EXTRACTED_DATA_CREATE_EVENT` | Triggered when new data is extracted from a file. |
275
+ | FileEvent | `FILE_EXTRACTED_DATA_DELETE_EVENT` | Occurs when extracted data from a file is deleted. |
276
+ | FileEvent | `FILE_FAILED_EVENT` | Indicates a failure in file processing. |
277
+ | FileEvent | `FILE_MOVE_EVENT` | Signals the movement of a file within the system. |
278
+ | FileEvent | `FILE_MOVE_TO_PENDING_EVENT` | Triggered when a file is moved to a pending state. |
279
+ | FileEvent | `FILE_MOVE_TO_RECYCLE_BIN_EVENT` | Indicates movement of a file to the recycle bin. |
280
+ | FileEvent | `FILE_PROPERTY_CREATE_EVENT` | Reflects the creation of a file property. |
281
+ | FileEvent | `FILE_PROPERTY_DELETE_EVENT` | Signals the deletion of a file property. |
282
+ | FileEvent | `FILE_REMOVE_TAG_EVENT` | Signals the removal of a tag from a file. |
283
+ | FileEvent | `FILE_STATUS_UPDATE_EVENT` | Indicates an update in the file's status. |
284
+ | FileEvent | `FILE_UPDATE_EVENT` | Triggered when a file is updated in any manner. |
285
+ | JobEvent | `JOB_CREATE_EVENT` | Triggered when a new job is instantiated for file operations. |
286
+ | JobEvent | `JOB_EXCEEDED_RETRIES_EVENT` | Fires when job exceeds maximum retry attempts for a stage. |
287
+ | JobEvent | `JOB_FAILED_EVENT` | Occurs when a job halts due to an unrecoverable error. |
288
+ | JobEvent | `JOB_FINISHED_EVENT` | Triggered when job successfully completes all workflow stages. |
289
+ | JobEvent | `JOB_INVALID_EVENT` | Fires when job fails initial validation of input files or parameters. |
290
+ | JobEvent | `JOB_RETRY_EVENT` | Triggered when job retries a stage after a recoverable failure. |
291
+ | JobEvent | `JOB_STAGE_UPDATE_EVENT` | Occurs when job transitions from one workflow stage to another. |
292
+ | JobEvent | `JOB_START_EVENT` | Triggered when job begins its workflow and state machine. |
293
+
209
294
  ## Development Setup
210
295
 
211
296
  ### Setting up the development environment
@@ -0,0 +1,37 @@
1
+ LICENSE
2
+ README.md
3
+ pyproject.toml
4
+ alfred/__init__.py
5
+ alfred/base/__init__.py
6
+ alfred/base/config.py
7
+ alfred/base/constants.py
8
+ alfred/base/exceptions.py
9
+ alfred/exceptions/__init__.py
10
+ alfred/http/__init__.py
11
+ alfred/http/http_client.py
12
+ alfred/http/typed.py
13
+ alfred/realtime/__init__.py
14
+ alfred/rest/__init__.py
15
+ alfred/rest/data_points/__init__.py
16
+ alfred/rest/data_points/base.py
17
+ alfred/rest/data_points/v1.py
18
+ alfred/rest/files/__init__.py
19
+ alfred/rest/files/base.py
20
+ alfred/rest/files/typed.py
21
+ alfred/rest/files/v1.py
22
+ alfred/rest/jobs/__init__.py
23
+ alfred/rest/jobs/base.py
24
+ alfred/rest/jobs/typed.py
25
+ alfred/rest/jobs/v1.py
26
+ alfred/rest/sessions/__init__.py
27
+ alfred/rest/sessions/base.py
28
+ alfred/rest/sessions/v1.py
29
+ alfred/typings/__init__.py
30
+ alfred/typings/misc.py
31
+ alfred/utils/__init__.py
32
+ alfred/utils/logging.py
33
+ alfred_python.egg-info/PKG-INFO
34
+ alfred_python.egg-info/SOURCES.txt
35
+ alfred_python.egg-info/dependency_links.txt
36
+ alfred_python.egg-info/requires.txt
37
+ alfred_python.egg-info/top_level.txt
@@ -1,3 +1,5 @@
1
1
  pydantic>=2.0
2
2
  pydantic-settings>=2.0
3
3
  requests>=2.30
4
+ python-socketio>=5.11
5
+ websocket-client>=1.8
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "alfred-python"
7
- version = "0.4.0"
7
+ version = "0.5.0"
8
8
  authors = [{ name = "Tagshelf LLC", email = "support@tagshelf.com" }]
9
9
  description = "Python library designed to simplify and streamline interactions with the Alfred API"
10
10
  readme = "README.md"
@@ -19,11 +19,12 @@ dependencies = [
19
19
  "pydantic >= 2.0",
20
20
  "pydantic-settings >= 2.0",
21
21
  "requests >= 2.30",
22
+ "python-socketio >= 5.11",
23
+ "websocket-client >= 1.8"
22
24
  ]
23
25
 
24
26
  [project.urls]
25
27
  homepage = "https://github.com/tagshelfsrl/alfred-python"
26
28
 
27
29
  [tool.setuptools.packages.find]
28
- where = ["src"]
29
30
  include = ["alfred", "alfred.*"]
@@ -1 +0,0 @@
1
- __version__ = "0.0.1"
@@ -1,8 +0,0 @@
1
- from src.alfred.http.typed import ResponseType
2
-
3
- # Response type/header mapping
4
- RESPONSE_TYPE_HEADER_MAPPING = {
5
- ResponseType.JSON: "application/json",
6
- ResponseType.TEXT: "text/plain",
7
- ResponseType.XML: "application/xml",
8
- }
@@ -1 +0,0 @@
1
- from src.alfred.typings.misc import *
@@ -1 +0,0 @@
1
- from src.alfred.utils.logging import *
@@ -1,35 +0,0 @@
1
- LICENSE
2
- README.md
3
- pyproject.toml
4
- src/alfred/__init__.py
5
- src/alfred/base/__init__.py
6
- src/alfred/base/config.py
7
- src/alfred/base/constants.py
8
- src/alfred/base/exceptions.py
9
- src/alfred/http/__init__.py
10
- src/alfred/http/http_client.py
11
- src/alfred/http/typed.py
12
- src/alfred/rest/__init__.py
13
- src/alfred/rest/data_points/__init__.py
14
- src/alfred/rest/data_points/base.py
15
- src/alfred/rest/data_points/v1.py
16
- src/alfred/rest/files/__init__.py
17
- src/alfred/rest/files/base.py
18
- src/alfred/rest/files/typed.py
19
- src/alfred/rest/files/v1.py
20
- src/alfred/rest/jobs/__init__.py
21
- src/alfred/rest/jobs/base.py
22
- src/alfred/rest/jobs/typed.py
23
- src/alfred/rest/jobs/v1.py
24
- src/alfred/rest/sessions/__init__.py
25
- src/alfred/rest/sessions/base.py
26
- src/alfred/rest/sessions/v1.py
27
- src/alfred/typings/__init__.py
28
- src/alfred/typings/misc.py
29
- src/alfred/utils/__init__.py
30
- src/alfred/utils/logging.py
31
- src/alfred_python.egg-info/PKG-INFO
32
- src/alfred_python.egg-info/SOURCES.txt
33
- src/alfred_python.egg-info/dependency_links.txt
34
- src/alfred_python.egg-info/requires.txt
35
- src/alfred_python.egg-info/top_level.txt
File without changes
File without changes