Python Cresco Client Library
Project description
Improved pycrescolib
This is an improved version of the pycrescolib Python client library for interacting with the Cresco distributed edge computing framework.
Improvements
This version includes the following improvements over the original:
- Thread Safety: Replaced the deprecated
threadmodule with proper thread handling and added locks where necessary. - Efficient JSON Handling: Optimized JSON serialization/deserialization with proper error handling.
- Proper Logging: Added comprehensive logging throughout the codebase.
- Base Classes: Created base classes to avoid code duplication.
- Context Managers: Added context managers for resource management.
- Retry Logic: Implemented retry mechanisms for handling connection failures.
- Modern Async: Used asyncio and the websockets library for better WebSocket handling.
- Comprehensive Documentation: Added docstrings and type hints throughout the code.
Dependencies
- Python 3.7+
- websockets>=10.0
- cryptography>=36.0.0
- backoff>=2.0.0
Installation
pip install -r requirements.txt
Usage Example
Synchronous Usage (Backward Compatible)
from pycrescolib.clientlib import clientlib
# Connect to Cresco server
client = clientlib("localhost", 8282, "your-service-key")
if client.connect():
# Get agent list
agent_list = client.globalcontroller.get_agent_list()
print(agent_list)
# Clean up
client.close()
Using Context Manager
from pycrescolib.clientlib import clientlib
# Connect to Cresco server using context manager
with clientlib("localhost", 8282, "your-service-key").connection() as client:
# Get agent list
agent_list = client.globalcontroller.get_agent_list()
print(agent_list)
# Context manager handles cleanup automatically
Using Dataplane
from pycrescolib.clientlib import clientlib
# Create client
client = clientlib("localhost", 8282, "your-service-key")
client.connect()
# Define message handler
def handle_message(message):
print(f"Received: {message}")
# Create dataplane
dp = client.get_dataplane("my-stream", handle_message)
dp.connect()
# Send data
dp.send(json.dumps({"data": "pycrescolib_test"}))
# Clean up
dp.close()
client.close()
Migration Notes
This version maintains backward compatibility with the original API, but includes additional async functionality for those who want to use it directly.
If you're using the async API directly, use the async/await syntax:
import asyncio
from pycrescolib.wc_interface import ws_interface
async def main():
ws = ws_interface()
await ws.connect("wss://localhost:8282/api/apisocket", "your-service-key")
# Your code here
await ws.close()
if __name__ == "__main__":
asyncio.run(main())
Configuration
You can configure the logging level:
from pycrescolib.clientlib import configure_logging
import logging
# Set logging level
configure_logging(level=logging.DEBUG)
SSL Verification
By default, SSL certificate verification is disabled. To enable it:
# Create client with SSL verification enabled
client = clientlib("localhost", 8282, "your-service-key", verify_ssl=True)
License
Same as the original pycrescolib license.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file pycrescolib-0.14.tar.gz.
File metadata
- Download URL: pycrescolib-0.14.tar.gz
- Upload date:
- Size: 25.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
803a2552de9ff13968dcd5ad938ba4c308613896ccca5801640a2a548aa6fdf8
|
|
| MD5 |
c660182d9b48b58d019a0eb521bfdf63
|
|
| BLAKE2b-256 |
1bc198d69bac900b7c5e4f2fdfd6bdbf1178937e77318314a96215acdb496b0f
|