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
# Using poetry
poetry add bfloat
# Using pipenv
pipenv install bfloat
Quick Start
from bfloat 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 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.3.tar.gz
.
File metadata
- Download URL: bfloat-1.0.3.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 | 2a7a0a933540a324b310f91f51758c9bfe4e6a64002458228e7d5a1b97f55b11 |
|
MD5 | 569956ee7c3b22c3c0d1d320e54157e8 |
|
BLAKE2b-256 | cd7a550c0874aefb53867f2fb662ed75ede8284d52b81bcd27ada2e5ade1688b |
File details
Details for the file bfloat-1.0.3-py3-none-any.whl
.
File metadata
- Download URL: bfloat-1.0.3-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 | 6bf31cb9dfc48197f3dc05e9ecaabcddaa73c9272c15ee60c31955821c744ff8 |
|
MD5 | 094396955952f9d6605892b1d22d0883 |
|
BLAKE2b-256 | 2bb77b8d9cb011df086d5d1d21b24c9826884f9787ec61d7d9afe6be68b8ffa9 |