Skip to main content

Python SDK for ContactsManager API authentication and token generation

Project description

ContactsManager Python SDK

PyPI version Build Status License: MIT Python Test Coverage

A Python SDK for the ContactsManager API that handles user management, authentication, and token generation for contactsmanager.io integration.

Overview

The ContactsManager SDK enables developers to easily integrate social features into their applications. It provides secure user management and token generation, helping you build features like activity feeds, follow/unfollow functionality, and contact management while ensuring user data privacy and security.

Installation

pip install contactsmanager

Quick Start

from contactsmanager import ContactsManagerClient
from contactsmanager.types import UserInfo, DeviceInfo

# Initialize the client
client = ContactsManagerClient(
    api_key="your_api_key",
    api_secret="your_api_secret",
    org_id="your_org_id"
)

# Create a user on the server and get a token
user_info = UserInfo(
    user_id="user123",
    full_name="John Doe",
    email="john@example.com",
    phone="+1234567890"  # Optional
)

device_info = DeviceInfo(
    device_type="mobile",
    os="iOS",
    app_version="1.0.0"
)

# Create user and get token in one call
response = client.create_user(user_info, device_info)

print(f"User created: {response.data.created}")
print(f"Token: {response.data.token.token}")
print(f"Expires at: {response.data.token.expires_at}")
print(f"User ID: {response.data.user.organization_user_id}")

Core Features

1. User Management

Create or update users on the ContactsManager server:

from contactsmanager.types import UserInfo, DeviceInfo

# Create user with email only
user_info = UserInfo(
    user_id="user123",
    full_name="John Doe",
    email="john@example.com"
)

response = client.create_user(user_info)

# Create user with phone only
user_info = UserInfo(
    user_id="user456",
    full_name="Jane Smith",
    phone="+1234567890"
)

response = client.create_user(user_info)

# Create user with both email and phone
user_info = UserInfo(
    user_id="user789",
    full_name="Bob Wilson",
    email="bob@example.com",
    phone="+1234567890",
    avatar_url="https://example.com/avatar.jpg",
    metadata={"role": "admin", "department": "engineering"}
)

response = client.create_user(user_info)

2. Delete Users

Remove users from the ContactsManager server:

# Delete a user
response = client.delete_user("user123")

print(f"Status: {response.status}")
print(f"Message: {response.message}")
print(f"Deleted contact ID: {response.data.deleted_contact_id}")

3. Token Generation Only

Generate tokens without creating users (for existing users):

# Generate a token for an existing user
token_response = client.generate_token(
    user_id="user123",
    device_info={
        "device_type": "mobile",
        "os": "iOS",
        "app_version": "1.0.0"
    }
)

print(f"Token: {token_response['token']}")
print(f"Expires at: {token_response['expires_at']}")

4. Custom Token Expiration

Control how long tokens remain valid:

# Create user with 1-hour token expiration
response = client.create_user(
    user_info,
    device_info,
    expiry_seconds=3600  # 1 hour instead of default 24 hours
)

# Generate token with custom expiration
token_response = client.generate_token(
    user_id="user123",
    expiration_seconds=7200  # 2 hours
)

Implementation Flow

Here's how to integrate ContactsManager into your application:

Server-Side Implementation

from contactsmanager import ContactsManagerClient
from contactsmanager.types import UserInfo, DeviceInfo

# 1. Initialize the client (do this once, typically in your app setup)
client = ContactsManagerClient(
    api_key="your_api_key",
    api_secret="your_api_secret",
    org_id="your_org_id"
)

# 2. When a user signs up or logs in, create/update them on ContactsManager
def handle_user_login(user_data):
    user_info = UserInfo(
        user_id=user_data["id"],  # Your internal user ID
        full_name=user_data["name"],
        email=user_data.get("email"),
        phone=user_data.get("phone")
    )

    device_info = DeviceInfo(
        device_type=user_data.get("device_type", "web"),
        os=user_data.get("os"),
        app_version=user_data.get("app_version")
    )

    # Create/update user and get token
    response = client.create_user(user_info, device_info)

    # Return the token to your client app
    return {
        "contactsmanager_token": response.data.token.token,
        "expires_at": response.data.token.expires_at,
        "user_created": response.data.created
    }

# 3. When a user deletes their account, remove them from ContactsManager
def handle_user_deletion(user_id):
    response = client.delete_user(user_id)
    return response.status == "success"

Client-Side Usage

Once you have the token from your server, use it in your client application:

// In your mobile app or web frontend
const contactsManagerToken = "token_from_your_server";

// Use this token with ContactsManager client SDKs
// to access social features, contact sync, etc.

Data Types

UserInfo

from contactsmanager.types import UserInfo

user_info = UserInfo(
    user_id="string",        # Required: Your internal user ID
    full_name="string",      # Required: User's display name
    email="string",          # Optional: User's email
    phone="string",          # Optional: User's phone number
    avatar_url="string",     # Optional: URL to user's avatar image
    metadata={}              # Optional: Additional user data
)

DeviceInfo

from contactsmanager.types import DeviceInfo

device_info = DeviceInfo(
    device_type="string",    # Optional: "mobile", "web", "desktop"
    os="string",             # Optional: "iOS", "Android", "Windows"
    app_version="string",    # Optional: Your app version
    locale="string",         # Optional: User's locale
    timezone="string"        # Optional: User's timezone
)

Error Handling

from contactsmanager.server_api import ServerAPIError

try:
    response = client.create_user(user_info)
    print("User created successfully!")
except ServerAPIError as e:
    print(f"Server error: {e}")
    print(f"Status code: {e.status_code}")
except ValueError as e:
    print(f"Validation error: {e}")
except Exception as e:
    print(f"Unexpected error: {e}")

Webhook Verification

Verify webhooks from ContactsManager:

# Set your webhook secret (get this from ContactsManager dashboard)
client.set_webhook_secret("your_webhook_secret")

# In your webhook handler
def handle_webhook(request):
    payload = request.get_json()
    signature = request.headers.get('X-ContactsManager-Signature')

    if client.verify_webhook_signature(payload, signature):
        # Process the webhook
        print("Webhook verified!")
        return {"status": "success"}
    else:
        print("Invalid webhook signature")
        return {"error": "Invalid signature"}, 401

Requirements

  • Python 3.8+
  • PyJWT>=2.0.0
  • httpx>=0.24.0

Development

Setting up development environment

# Clone the repository
git clone https://github.com/arpwal/contactsmanager-py.git
cd contactsmanager-py

# Install development dependencies
pip install -e ".[dev]"

# Run tests
pytest

Running Integration Tests

Integration tests validate the SDK against real-world scenarios, including server-side API functionality.

Local Setup

  1. Create a .env file with your test configuration:

    # Copy the template file
    cp env.template .env
    
    # Edit the .env file with your credentials
    # The configuration is a JSON string in the TEST_CONFIG variable
    TEST_CONFIG='{"api_key":"your_api_key","api_secret":"your_api_secret","org_id":"your_org_id","api_base_url":"https://api.contactsmanager.io"}'
    
  2. Run the integration tests:

    ./run_integration_tests.sh
    

Releasing new versions

The SDK uses an automated process for releases:

  1. Update the version in contactsmanager/__init__.py using the provided script:

    ./bump_version.sh 0.1.1
    
  2. Commit and push the change to the main branch:

    git add contactsmanager/__init__.py
    git commit -m "Bump version to 0.1.1"
    git push origin main
    
  3. The GitHub Actions workflow will:

    • Run all tests across multiple Python versions
    • Run integration tests
    • Create a new GitHub release with the version tag
    • Build and publish the package to PyPI

License

MIT License

About ContactsManager.io

ContactsManager.io provides a platform for app developers to integrate social features into their applications. Our SDK ensures that contact information stays with users only, with multi-layer encryption and military-grade security to prevent spam and data misuse.

For more information and documentation, visit contactsmanager.io.

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

contactsmanager-1.2.5.tar.gz (17.4 kB view details)

Uploaded Source

Built Distribution

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

contactsmanager-1.2.5-py3-none-any.whl (17.4 kB view details)

Uploaded Python 3

File details

Details for the file contactsmanager-1.2.5.tar.gz.

File metadata

  • Download URL: contactsmanager-1.2.5.tar.gz
  • Upload date:
  • Size: 17.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.17

File hashes

Hashes for contactsmanager-1.2.5.tar.gz
Algorithm Hash digest
SHA256 a439cb90c958c9b3663155c19415b64851ef8704eef5dba7c957837ca830ab03
MD5 917125b72005a14a59255e572510e65a
BLAKE2b-256 4106d846d1cce78f03f74b142b5667e88809e997358d5a86bb259c0c8a14a316

See more details on using hashes here.

File details

Details for the file contactsmanager-1.2.5-py3-none-any.whl.

File metadata

File hashes

Hashes for contactsmanager-1.2.5-py3-none-any.whl
Algorithm Hash digest
SHA256 e17d1a15d72017fd8cb29cf8503a11cb0c1602ccdccf6a3798648f461cba31d3
MD5 6eca77f3b4709097c90a8aab04b1e14e
BLAKE2b-256 ba4a4f3c709fdc63bc3a9e38a1a46e0c091721162fb233d8ef85c0eb7f2353b3

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