Skip to main content

A Python SDK for interacting with the bfloat.ai API

Project description

BFloat.ai Python SDK

PyPI version Python Async Support

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.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

License

This project is licensed under the MIT License - see the LICENSE file for details.

Support

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 Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

bfloat-1.0.2-py3-none-any.whl (6.3 kB view details)

Uploaded Python 3

File details

Details for the file bfloat-1.0.2-py3-none-any.whl.

File metadata

  • Download URL: bfloat-1.0.2-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

Hashes for bfloat-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 394447a16d51b2c4bc66c2f1f1e6041fc98ce3b287d58c57d9bb512e3f02da95
MD5 9140cde4b2e50b7db75599bda3dbb503
BLAKE2b-256 eaa57b6b380a87bee885990f71039eb0bf14cd39945f3c1eb935c487095dfd4d

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page