API Clients¶
The SDK provides both synchronous and asynchronous API clients for interacting with the Upassist Cloud API. The clients automatically handle URL construction by prepending the base API URL to relative paths.
Features¶
- Synchronous client for simple use cases
- Asynchronous client for high-performance applications
- Automatic retry and error handling
- Type-safe API responses
- Automatic base URL handling
Examples¶
Synchronous Client¶
Use the synchronous client for simple operations
from upassist import SyncAPIClient
# Create a synchronous client
client = SyncAPIClient(api_key="your-api-key")
# Make API requests - base URL is automatically prepended
response = client.get("/heartbeats") # Calls https://api.upassist.cloud/v1/heartbeats
response = client.get("heartbeats") # Same as above
# Full URLs are used as-is
response = client.get("https://custom-api.upassist.cloud/v1/heartbeats")
Asynchronous Client¶
Use the asynchronous client for high-performance operations
from upassist import AsyncAPIClient
import asyncio
async def main():
# Create an asynchronous client
client = AsyncAPIClient(api_key="your-api-key")
# Make concurrent API requests - base URL is automatically prepended
responses = await asyncio.gather(
client.get("/heartbeats"), # Calls https://api.upassist.cloud/v1/heartbeats
client.get("logs"), # Calls https://api.upassist.cloud/v1/logs
client.get("https://custom-api.upassist.cloud/v1/metrics") # Uses full URL
)
# Process responses
heartbeats, logs, metrics = [r.json() for r in responses]
# Run the async code
asyncio.run(main())
Error Handling¶
Handle API errors gracefully
from upassist import SyncAPIClient
from upassist.exceptions import APIError
client = SyncAPIClient(api_key="your-api-key")
try:
response = client.get("/heartbeats")
except APIError as e:
print(f"API Error: {e}")
# Handle the error appropriately
API Reference¶
AbstractAPIClient
¶
Bases: ABC
Base class for API clients that provides common functionality for making HTTP requests.
This abstract class defines the interface and common functionality for API clients. It handles authentication, request formatting, and provides convenience methods for different HTTP methods.
The client can be initialized with an API key and version, or it will use the values from the config module if not provided.
Example
from upassist import config
from upassist.client import YourAPIClient
# Initialize with config values
client = YourAPIClient() # Uses config.API_KEY and config.API_VERSION
# Or override config values
client = YourAPIClient(
api_key="your-api-key",
api_version="v1"
)
# Make requests using convenience methods
response = client.get('users', params={'page': 1})
response = client.post('users', json={'name': 'John'})
response = client.delete('users/123')
# Or use the base request method
response = client.request(
method='GET',
url='users',
params={'page': 1}
)
Source code in upassist/client/abstract.py
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 | |
__init__(api_key=None, api_version=None)
¶
Initialize the API client.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
api_key
|
str | None
|
Optional API key. If not provided, uses config.API_KEY |
None
|
api_version
|
str | None
|
Optional API version. If not provided, uses config.API_VERSION |
None
|
Source code in upassist/client/abstract.py
For more information and to get started with monitoring your applications, visit Upassist Cloud