Skip to main content

Async Python client library for pararam.io platform

Project description

Pararamio AIO

Async Python API client for pararam.io platform.

Features

  • Async/Await: Modern asynchronous interface with aiohttp
  • 🚀 Explicit Loading: Predictable API calls with explicit load() methods
  • 🍪 Cookie Persistence: Automatic session management
  • 🔐 Two-Factor Authentication: Built-in 2FA support
  • 🐍 Type Hints: Full typing support for better IDE experience

Installation

pip install pararamio-aio

Quick Start

import asyncio
from pararamio_aio import AsyncPararamio
from pararamio_aio import AsyncFileCookieManager

async def main():
    # Initialize cookie manager for persistent authentication
    cookie_manager = AsyncFileCookieManager("session.cookie")

    # Initialize client
    async with AsyncPararamio(
        login="your_login",
        password="your_password",
        key="your_2fa_key",
        cookie_manager=cookie_manager
    ) as client:
        # Authenticate
        await client.authenticate()

        # Search for users - returns User objects (clean names!)
        users = await client.search_users("John")
        for user in users:
            print(f"{user.name}")

        # Get chat messages - returns Chat and Post objects
        chat = await client.get_chat_by_id(12345)
        posts = await chat.get_posts(limit=10)
        for post in posts:
            await post.load()  # Explicit loading
            print(f"{post.author.name}: {post.text}")

asyncio.run(main())

Explicit Loading

Unlike the sync version, pararamio-aio uses explicit loading for predictable async behavior:

from pararamio_aio import AsyncPararamio

async def main():
    # Get user object
    client = AsyncPararamio(
        login="user",
        password="pass",
        key="key",
    )
    await client.authenticate()
    user = await client.get_user_by_id(123)
    print(user.name)  # Basic data is already loaded

    # Load full profile data explicitly
    await user.load()
    print(user.bio)  # Now additional data is available

    # Load specific relations
    posts = await user.get_posts()
    for post in posts:
        await post.load()  # Load each post's content

# Run the async function
import asyncio
asyncio.run(main())

Cookie Management

The async client supports multiple cookie storage options:

Default (In-Memory)

from pararamio_aio import AsyncPararamio


async def main():
    # By default, uses AsyncInMemoryCookieManager (no persistence)
    async with AsyncPararamio(
            login="user",
            password="pass",
            key="key"
    ) as client:
        await client.authenticate()
        # Cookies are stored in memory only during the session


# Run the async function
import asyncio

asyncio.run(main())

File-based Persistence

from pararamio_aio import AsyncFileCookieManager, AsyncPararamio

async def main():
    # Create a cookie manager for persistent storage
    cookie_manager = AsyncFileCookieManager("session.cookie")

    # First run - authenticates with credentials
    async with AsyncPararamio(
        login="user",
        password="pass",
        key="key",
        cookie_manager=cookie_manager
    ) as client:
        await client.authenticate()

    # Later runs - uses saved cookie
    cookie_manager2 = AsyncFileCookieManager("session.cookie")
    async with AsyncPararamio(cookie_manager=cookie_manager2) as client:
        # Already authenticated!
        profile = await client.get_profile()

# Run the async function
import asyncio
asyncio.run(main())

Concurrent Operations

Take advantage of async for concurrent operations:

import asyncio
async def get_multiple_users(client, user_ids):
    # Fetch all users concurrently
    tasks = [client.get_user_by_id(uid) for uid in user_ids]
    users = await asyncio.gather(*tasks)

    # Load all profiles concurrently
    await asyncio.gather(*[user.load() for user in users])

    return users

API Reference

Client Methods

All methods are async and must be awaited:

  • authenticate() - Authenticate with the API
  • search_users(query) - Search for users
  • get_user_by_id(user_id) - Get user by ID
  • get_users_by_ids(ids) - Get multiple users
  • get_chat_by_id(chat_id) - Get a chat by ID
  • search_groups(query) - Search for groups
  • create_chat(title, description) - Create new chat

Model Objects

All models have async methods:

  • User - User profile

    • load() - Load full profile
    • get_posts() - Get user's posts
    • get_groups() - Get user's groups
  • Chat - Chat/conversation

    • load() - Load chat details
    • get_posts(limit, offset) - Get messages
    • send_message(text) - Send a message
  • Post - Message/post

    • load() - Load post content
    • delete() - Delete post
  • Group - Community group

    • load() - Load group details
    • members - Get member list (property)

Error Handling

from pararamio_aio import AsyncPararamio
from pararamio_aio.exceptions import PararamioAuthenticationError, PararamioHTTPRequestError



async def main():
    async with AsyncPararamio(login="user", password="pass", key="key") as client:
        try:
            await client.authenticate()
        except PararamioAuthenticationError as e:
            print(f"Authentication failed: {e}")
        except PararamioHTTPRequestError as e:
            print(f"HTTP error {e.code}: {e.message}")


# Run the async function
import asyncio

asyncio.run(main())

Advanced Usage

Custom Session

import httpx
from pararamio_aio import AsyncPararamio


async def main():
    # Create custom httpx client with specific timeout and limits
    timeout = httpx.Timeout(timeout=60.0, connect=10.0)
    limits = httpx.Limits(max_keepalive_connections=5, max_connections=100)

    async with httpx.AsyncClient(timeout=timeout, limits=limits) as session:
        async with AsyncPararamio(session=session, login="user", password="pass", key="key") as client:
            # Client will use your custom session
            await client.authenticate()

            # Perform operations
            user = await client.get_user_by_id(123)
            await user.load()


# Run the async function
import asyncio

asyncio.run(main())

Rate Limiting

The client automatically handles rate limiting:

from pararamio_aio import AsyncPararamio

client = AsyncPararamio(
    login="user",
    password="pass",
    key="key",
    wait_auth_limit=True,  # Wait instead of failing on rate limit
)

Migration from Sync Version

If you're migrating from the synchronous pararamio package:

  1. Add async/await keywords
  2. Use async context manager (async with)
  3. Call load() explicitly when needed
  4. Use asyncio.gather() for concurrent operations

Example migration:

from pararamio import Pararamio
from pararamio_aio import AsyncPararamio


async def main():
    # Sync version
    client = Pararamio()
    user = client.get_user_by_id(123)
    print(user.bio)  # Lazy loaded

    # Async version
    async with AsyncPararamio(login="user", password="pass", key="key") as client:
        user = await client.get_user_by_id(123)
        await user.load()  # Explicit load
        print(user.bio)


# Run the async function
import asyncio

asyncio.run(main())

License

MIT License—see LICENSE file for details.

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

pararamio_aio-3.0.5.tar.gz (106.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pararamio_aio-3.0.5-py3-none-any.whl (146.2 kB view details)

Uploaded Python 3

File details

Details for the file pararamio_aio-3.0.5.tar.gz.

File metadata

  • Download URL: pararamio_aio-3.0.5.tar.gz
  • Upload date:
  • Size: 106.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.13

File hashes

Hashes for pararamio_aio-3.0.5.tar.gz
Algorithm Hash digest
SHA256 2d412d99516821538e58c2d549a1577f1c90d6465f7e3e84abea82120619affb
MD5 678fb7630417099ce59f6e82b4d5ef37
BLAKE2b-256 10d2f87622c255afd4aa451e8369138e04f47feecce3c558e5f9979dd52d29cd

See more details on using hashes here.

File details

Details for the file pararamio_aio-3.0.5-py3-none-any.whl.

File metadata

  • Download URL: pararamio_aio-3.0.5-py3-none-any.whl
  • Upload date:
  • Size: 146.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.13

File hashes

Hashes for pararamio_aio-3.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 4f5027a4386e777e80d9da1406f1eb46a0aa35254cd39d73d1815e1550a7efaa
MD5 f1f821254da0e32f07eff03aef763efd
BLAKE2b-256 14df52540a18e1235bc2e0c05d6ad725219287ab93ce9785687377c4cfc86bcb

See more details on using hashes here.

Supported by

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