Temp Brijesh Consul
A comprehensive Python client library for HashiCorp Consul that provides both synchronous and asynchronous interfaces for service discovery, key-value store operations, and service registration.
Features
- Dual Interface: Both synchronous and asynchronous client implementations
- Service Discovery: Discover and query services registered in Consul
- Key-Value Store: Read and write data to Consul's distributed key-value store
- Service Registration: Register services with health checks
- Retry Logic: Built-in exponential backoff retry mechanism for resilient operations
- Default Client: Fallback client that reads from local JSON files when Consul is unavailable
- Type Hints: Full type annotation support for better IDE integration
Installation
From PyPI (when published)
pip install temp-brijesh-consul
From Source
git clone <repository-url>
cd temp_consul
pip install .
Development Installation
git clone <repository-url>
cd temp_consul
pip install -e ".[dev]"
Quick Start
Synchronous Client
from temp_brijesh_consul import ConsulClient
# Initialize the client
client = ConsulClient(host='localhost', port=8500)
client.start()
try:
# Service discovery
service = client.get_service('my-service')
print(f"Service details: {service}")
# Get all services
services = client.get_services()
print(f"All services: {services}")
# Key-value operations
data = client.get_kv('config/database')
print(f"Database config: {data}")
# Register a service
success = client.register_service(
name='my-service',
address='127.0.0.1',
port=8080,
tags=['api', 'v1'],
check={'http': 'http://127.0.0.1:8080/health', 'interval': '10s'}
)
print(f"Service registered: {success}")
finally:
client.close()
Asynchronous Client
import asyncio
from temp_brijesh_consul import AsyncConsulClient
async def main():
# Initialize the async client
client = AsyncConsulClient(host='localhost', port=8500)
client.start()
try:
# Service discovery
service = await client.get_service('my-service')
print(f"Service details: {service}")
# Get all services
services = await client.get_services()
print(f"All services: {services}")
# Key-value operations
data = await client.get_kv('config/database')
print(f"Database config: {data}")
# Get key-value tree
tree = await client.get_kv_tree('config/')
print(f"Config tree: {tree}")
# Register a service
success = await client.register_service(
name='my-async-service',
address='127.0.0.1',
port=8081,
tags=['async', 'api'],
check={'tcp': '127.0.0.1:8081', 'interval': '10s'}
)
print(f"Service registered: {success}")
finally:
await client.close()
# Run the async example
asyncio.run(main())
Default Client (Fallback Mode)
For development or testing when Consul is not available, you can use the default clients that read from a local JSON file:
from temp_brijesh_consul import DefaultConsulClient, AsyncDefaultConsulClient
# Create a default_consul_data.json file in the parent directory
# with the following structure:
{
"KV": {
"config/database": {"host": "localhost", "port": 5432},
"config/redis": {"host": "localhost", "port": 6379}
},
"Services": {
"my-service": {
"Service": "my-service",
"Address": "127.0.0.1",
"Port": 8080,
"Tags": ["api", "v1"]
}
}
}
# Synchronous default client
client = DefaultConsulClient()
data = client.get_kv('config/database')
print(f"Database config: {data}")
# Asynchronous default client
async def test_default():
client = AsyncDefaultConsulClient()
data = await client.get_kv('config/database')
print(f"Database config: {data}")
API Reference
ConsulClient / AsyncConsulClient
Constructor Parameters
host(str): Consul server host address (default: 'localhost')port(int): Consul server port (default: 8500)timeout(int): Request timeout in seconds (default: 3)max_retries(int): Maximum retry attempts (default: 3)retry_delay(float): Initial retry delay in seconds (default: 0.5)
Methods
Service Operations
get_service(service_name, index=None, tag=None): Get service instance detailsget_services(): Get all registered servicesregister_service(name, address, port, tags=None, check=None, service_id=None): Register a new service
Key-Value Operations
get_kv(key, index=None, recurse=False): Get a value from the KV storeget_kv_tree(prefix): Get all keys and values under a prefix
Connection Management
start(): Initialize the Consul connectionclose(): Close the connection (async version is awaitable)
Configuration
Environment Variables
You can configure the client using environment variables:
export CONSUL_HOST=consul.example.com
export CONSUL_PORT=8500
Health Checks
When registering services, you can specify health checks:
# HTTP health check
http_check = {
'http': 'http://127.0.0.1:8080/health',
'interval': '10s',
'timeout': '3s'
}
# TCP health check
tcp_check = {
'tcp': '127.0.0.1:8080',
'interval': '10s',
'timeout': '3s'
}
# TTL health check
ttl_check = {
'ttl': '30s'
}
Error Handling
The library includes built-in retry logic with exponential backoff. You can customize the retry behavior:
client = ConsulClient(
host='localhost',
port=8500,
max_retries=5,
retry_delay=1.0
)
Development
Setting up Development Environment
# Clone the repository
git clone <repository-url>
cd temp_consul
# Install in development mode
pip install -e ".[dev]"
# Run tests
pytest
# Run linting
flake8 temp_brijesh_consul/
black temp_brijesh_consul/
mypy temp_brijesh_consul/
Running Tests
# Run all tests
pytest
# Run with coverage
pytest --cov=temp_brijesh_consul --cov-report=html
# Run only async tests
pytest -k "async"
Dependencies
- python-consul: The underlying Consul client library
- Python 3.8+: Minimum Python version requirement
License
MIT License - see LICENSE file for details.
Contributing
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
Changelog
v0.1.0
- Initial release
- Synchronous and asynchronous Consul clients
- Service discovery and registration
- Key-value store operations
- Default client with JSON fallback
- Built-in retry logic with exponential backoff
Support
For questions, bug reports, or feature requests, please open an issue on the GitHub repository.
Metadata
Release files for temp-brijesh-consul 1.0.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| temp_brijesh_consul-1.0.0.tar.gz | 9.5 kB | Without provenance | Details for temp_brijesh_consul-1.0.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| temp_brijesh_consul-1.0.0-py3-none-any.whl | Python 3 | none | any | Details for temp_brijesh_consul-1.0.0-py3-none-any.whl |
Total release size: 17.5 kB
Release files / temp_brijesh_consul-1.0.0.tar.gz
| Download URL | temp_brijesh_consul-1.0.0.tar.gz |
|---|---|
| Size | 9.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
5840c07c185d90394c0739bc31923bdf44b07fe669a190aa1c8142782022102f
|
|
BLAKE2b-256 checksum How to use checksums |
5cfc1a6f69c0029f3ca1eaf7201a24ce5dc4bb4c6a100ac22918679208f4d890
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.10
|
Release files / temp_brijesh_consul-1.0.0-py3-none-any.whl
| Download URL | temp_brijesh_consul-1.0.0-py3-none-any.whl |
|---|---|
| Size | 8.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
b89a10e0c3120457e672736c6591c6037be25157a11716a5a5aa34ecc9b60b1f
|
|
BLAKE2b-256 checksum How to use checksums |
791cddab4540db91957cac290ca418623655c69c2b09c25585922f373cc65862
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.10
|
Security for temp-brijesh-consul 1.0.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.