Skip to main content

Python gRPC client for Your Service

Project description

Messaging Core Python SDK

PyPI version Python Version License: MIT

Python gRPC client for the Messaging Core service, providing easy access to messaging functionality including user management, conversations, and real-time messaging.

Installation

Prerequisites

  • Python 3.8 or higher
  • gRPC and Protocol Buffers support

Install from PyPI

pip install messaging-core

Quick Start

Basic Usage

from messaging_core import MessagingCoreClient

# Initialize the client with your server address and API key
client = MessagingCoreClient(
    server_address="your-server-address:50051",  # e.g., "localhost:50051"
    api_key="your-api-key-here",
    use_ssl=False  # Set to True in production
)

# Login with user credentials
try:
    auth_response = client.login(
        username="user@example.com",
        password="your-secure-password"
    )
    print(f"Successfully logged in as {auth_response.user.email}")
    
    # Get user conversations
    conversations = client.list_conversations()
    print(f"Found {len(conversations)} conversations")
    
    # Send a message
    message = client.send_message(
        conversation_id=conversations[0].id,
        content="Hello from Python SDK!"
    )
    print(f"Message sent with ID: {message.id}")
    
except Exception as e:
    print(f"Error: {e}")

Features

  • Authentication: Secure login with JWT tokens
  • Conversation Management: Create, list, and manage conversations
  • Messaging: Send and receive messages in real-time
  • User Management: User profiles and presence
  • Attachments: Send and receive files and media

API Reference

Initialization

from messaging_core import MessagingCoreClient

# For development (without SSL)
client = MessagingCoreClient(
    server_address="localhost:50051",
    api_key="your-api-key",
    use_ssl=False
)

# For production (with SSL)
client = MessagingCoreClient(
    server_address="api.yourdomain.com:443",
    api_key="your-api-key",
    use_ssl=True
)

Authentication

# Login with username/password
auth_response = client.login(
    username="user@example.com",
    password="your-password"
)

# The client automatically handles token refresh
# You can also manually refresh the token
new_tokens = client.refresh_token(
    refresh_token=auth_response.refresh_token
)

# Logout
client.logout()

Conversations

# List conversations with pagination
conversations = client.list_conversations(
    page=1,
    page_size=20
)

# Create a new conversation
conversation = client.create_conversation(
    title="Team Chat",
    participant_ids=["user1", "user2"],
    type="GROUP"
)

# Get conversation details
conversation = client.get_conversation(conversation_id="conv123")

# Delete a conversation
client.delete_conversation(conversation_id="conv123")

Messages

# Send a text message
message = client.send_message(
    conversation_id="conv123",
    content="Hello, world!"
)

# Send a message with metadata
message = client.send_message(
    conversation_id="conv123",
    content="Check this out!",
    metadata={"type": "announcement", "priority": "high"}
)

# Get message history
messages = client.get_messages(
    conversation_id="conv123",
    limit=50,
    before=datetime.utcnow()
)

Users

# Get current user profile
profile = client.get_my_profile()

# Update profile
updated = client.update_profile(
    first_name="John",
    last_name="Doe",
    avatar_url="https://example.com/avatar.jpg"
)

# Search users
users = client.search_users(query="john")

Error Handling

All API calls raise grpc.RpcError for gRPC-related errors. The client also provides custom exceptions for business logic errors.

try:
    client.login(username="user@example.com", password="wrong-password")
except Exception as e:
    if hasattr(e, 'code') and e.code() == grpc.StatusCode.UNAUTHENTICATED:
        print("Authentication failed: Invalid credentials")
    else:
        print(f"Error: {e}")

Development

For development and contributing, you'll need:

Setup

  1. Clone the repository:

    git clone https://github.com/alijkdkar/Messaging-Core.git
    cd Messaging-Core/docs/sdk/python
    
  2. Install dependencies:

    poetry install
    

Generating gRPC Code

To regenerate the gRPC code from the .proto files:

./scripts/generate_sdk.sh

Running Tests

pytest tests/

Building the Package

python -m build

Publishing to PyPI

  1. Build the package:

    python -m build
    
  2. Upload to PyPI:

    twine upload dist/*
    

License

This project is licensed under the MIT License - see the 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

messaging_core-0.1.2.tar.gz (28.6 kB view details)

Uploaded Source

Built Distribution

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

messaging_core-0.1.2-py3-none-any.whl (32.8 kB view details)

Uploaded Python 3

File details

Details for the file messaging_core-0.1.2.tar.gz.

File metadata

  • Download URL: messaging_core-0.1.2.tar.gz
  • Upload date:
  • Size: 28.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.9

File hashes

Hashes for messaging_core-0.1.2.tar.gz
Algorithm Hash digest
SHA256 d93f44c781e01b6e7772a653dc46445046e678ac2df042f680dc9873f8f57fa9
MD5 599c2fe82a11114e1c772d47ba9a5ea8
BLAKE2b-256 da7605c210d41ad5afa3bd0eef17d1e7296cbaecb779f7cdcb4e3a0c80d22311

See more details on using hashes here.

File details

Details for the file messaging_core-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: messaging_core-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 32.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.9

File hashes

Hashes for messaging_core-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 5424f42d14dbe17290ec2cea5276782ede98c5a0fdcdb3448d19c7efa09cb613
MD5 4901457ec19c4fa223230e498dfae8f8
BLAKE2b-256 6f8c818bba924b4e564020e2198a5ef872b886021c05d034b7c18d391e35878b

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