A Python SDK for interacting with the bfloat.ai API
Project description
BFloat.ai Python SDK
A powerful and intuitive Python SDK for interacting with the bfloat.ai API. Manage browser automation sessions with ease using modern async/await patterns.
Features
- 🚀 Full type hinting support with dataclasses
- 💪 Async/await support using aiohttp
- 🛡️ Robust error handling
- 🔄 Complete session lifecycle management
- ⚙️ Flexible browser configuration
- 📘 Extensive documentation and examples
Installation
# Using pip
pip install bfloat-sdk
# Using poetry
poetry add bfloat-sdk
# Using pipenv
pipenv install bfloat-sdk
Quick Start
from bfloat_sdk import BFloatSDK, DEFAULT_BROWSER_CONFIG
import asyncio
# Initialize the SDK
bfloat = BFloatSDK('your-api-key')
# Create and manage a session
async def example():
try:
# Create a new session
session = await bfloat.create_session({
'browser': DEFAULT_BROWSER_CONFIG,
'lifetime': 3600,
'keep_alive': True
})
print(f'Session created: {session.id}')
print(f'Debug URL: {session.debug_info.ws_debugger_url}')
# Stop the session when done
await bfloat.stop_session(session.id)
except Exception as error:
print(f'Error: {error}')
finally:
await bfloat.close()
# Run the example
asyncio.run(example())
API Reference
Initialization
bfloat = BFloatSDK(api_key: str, base_url: str = "https://api.bfloat.ai/v1")
Methods
List Sessions
sessions = await bfloat.list_sessions()
Get Session Details
session = await bfloat.get_session(session_id: str)
Create Session
session = await bfloat.create_session(config: SessionConfig)
Stop Session
result = await bfloat.stop_session(session_id: str)
Configuration
Browser Configuration
@dataclass
class BrowserConfig:
type: str
settings: Optional[BrowserSettings] = None
block_ads: Optional[bool] = None
proxy: Optional[bool] = None
@dataclass
class BrowserSettings:
os: Optional[List[str]] = None
devices: Optional[List[str]] = None
screen: Optional[Dict[str, int]] = None
locales: Optional[List[str]] = None
Session Configuration
@dataclass
class SessionConfig:
browser: BrowserConfig
lifetime: Optional[int] = None
keep_alive: Optional[bool] = None
Error Handling
The SDK provides a custom BFloatError
class for error handling:
try:
session = await bfloat.create_session(config)
except BFloatError as error:
print(f'Status: {error.status}')
print(f'Message: {error.message}')
print(f'Response: {error.response}')
Complete Example
from bfloat_sdk import BFloatSDK, BFloatError, DEFAULT_BROWSER_CONFIG, SessionConfig
import asyncio
async def run_browser_session():
bfloat = BFloatSDK('your-api-key')
try:
# List existing sessions
sessions = await bfloat.list_sessions()
print(f'Active sessions: {len(sessions)}')
# Create a new session
config = SessionConfig(
browser=DEFAULT_BROWSER_CONFIG,
lifetime=3600
)
session = await bfloat.create_session(config)
print(f'New session: {session.id}')
print(f'Debug URL: {session.debug_info.ws_debugger_url}')
# Get session details
details = await bfloat.get_session(session.id)
print(f'Session status: {details.status}')
# Stop session
await bfloat.stop_session(session.id)
print('Session stopped successfully')
except BFloatError as error:
print(f'BFloat API error: {error}')
print(f'Status: {error.status}')
except Exception as error:
print(f'Unexpected error: {error}')
finally:
await bfloat.close()
# Run the example
if __name__ == "__main__":
asyncio.run(run_browser_session())
Development
# Install dependencies
poetry install
# Run tests
pytest
# Build
poetry build
# Generate documentation
pdoc --html bfloat_sdk
Contributing
We welcome contributions! Please see our Contributing Guidelines for details.
- Fork the repository
- Create your feature branch (
git checkout -b feature/AmazingFeature
) - Commit your changes (
git commit -m 'Add some AmazingFeature'
) - Push to the branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
License
This project is licensed under the MIT License - see the LICENSE file for details.
Support
- Documentation: https://docs.bfloat.ai
- Issues: GitHub Issues
- Email: support@bfloat.ai
Security
If you discover a security vulnerability, please send an email to security@bfloat.ai. All security vulnerabilities will be promptly addressed.
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
Built Distribution
File details
Details for the file bfloat-1.0.0.tar.gz
.
File metadata
- Download URL: bfloat-1.0.0.tar.gz
- Upload date:
- Size: 7.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 94afa1391128531080372f8c73e877bf2575fa24a31b3d4efa2aee83cb106689 |
|
MD5 | 14585292372f595f8ab0347b18a7c83a |
|
BLAKE2b-256 | e479fd9875b3606a1a8c76f97936ba5c2466d3fbdc98bbffda48b41d80ed1370 |
File details
Details for the file bfloat-1.0.0-py3-none-any.whl
.
File metadata
- Download URL: bfloat-1.0.0-py3-none-any.whl
- Upload date:
- Size: 6.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5613e5d1b188496a5f2b4ad198a9b2e35c464e3c93fab1ca1689a08a6ff5d385 |
|
MD5 | 8f0edf977513aa2bb8bf2cfc9ed97e49 |
|
BLAKE2b-256 | 19d7e124f692e6a21a8301f9cfbd877123174df6cbb754df4e8981e73f1fcc40 |