Skip to main content

A Python client library for the Validiz Email Validation API

Project description

Validiz Python Library

A Python client library for the Validiz Email Validation API. This library provides both synchronous and asynchronous clients for interacting with the API endpoints.

Installation

pip install validiz

Requirements

Python 3.9 or higher is required. All dependencies will be installed automatically when installing the package.

Alternatively, you can install the dependencies directly from the requirements.txt file:

pip install -r requirements.txt

Features

  • Both synchronous and asynchronous clients
  • Email validation API
  • File upload and processing
  • Comprehensive error handling
  • Type annotations for IDE completion

Quick Start

Synchronous Client

from validiz import Validiz, ValidizError

# Create a client with API key
client = Validiz(api_key="your_api_key_here")

# Validate an email
try:
    results = client.validate_email("user@example.com")
    for result in results:
        print(f"Email: {result['email']}")
        print(f"Is valid: {result['is_valid']}")
        print(f"Status: {result['status']}")
except ValidizError as e:
    print(f"API error: {e.message}")

Asynchronous Client

import asyncio
from validiz import AsyncValidiz, ValidizError

async def validate_email():
    # Create a client with API key
    async with AsyncValidiz(api_key="your_api_key_here") as client:
        # Validate an email
        try:
            results = await client.validate_email("user@example.com")
            for result in results:
                print(f"Email: {result['email']}")
                print(f"Is valid: {result['is_valid']}")
                print(f"Status: {result['status']}")
        except ValidizError as e:
            print(f"API error: {e.message}")

# Run the async function
asyncio.run(validate_email())

API Reference

Client Architecture

The library uses a Template Method pattern for shared functionality:

  • BaseClient implements common methods like poll_file_until_complete
  • Synchronous and asynchronous clients inherit from BaseClient and implement client-specific operations
  • This architecture ensures code reuse while handling the differences between synchronous and asynchronous operations

Initialization

# Synchronous client
from validiz import Validiz

client = Validiz(
    api_key="your_api_key",  # Required
    api_base_url="https://api.validiz.com/v1",  # Optional
    timeout=30  # Optional (seconds)
)

# Asynchronous client
from validiz import AsyncValidiz

client = AsyncValidiz(
    api_key="your_api_key",  # Required
    api_base_url="https://api.validiz.com/v1",  # Optional
    timeout=30  # Optional (seconds)
)

Validation Methods

Email Validation

# Synchronous
results = client.validate_email("user@example.com")
# or
results = client.validate_email(["user1@example.com", "user2@example.com"])

# Asynchronous
results = await client.validate_email("user@example.com")
# or
results = await client.validate_email(["user1@example.com", "user2@example.com"])

File Operations

# Synchronous
# Upload a file
upload_result = client.upload_file("emails.csv")
file_id = upload_result["file_id"]

# Check status
status = client.get_file_status(file_id)

# Download results when complete
if status["status"] == "completed":
    output_file = client.download_file(file_id, "results.csv")

# Or use the polling method to wait for completion and get results as DataFrame
import pandas as pd

# Get results as DataFrame without saving to disk
df = client.poll_file_until_complete(
    file_id=file_id, 
    interval=5, 
    max_retries=60,
    output_path=None,  # No file will be saved
    return_dataframe=True
)

# Or save the file and get the DataFrame
df = client.poll_file_until_complete(
    file_id=file_id, 
    interval=5, 
    max_retries=60,
    output_path="results.csv",  # File will be saved here
    return_dataframe=True
)

# Or get the file path
file_path = client.poll_file_until_complete(
    file_id=file_id, 
    interval=5, 
    max_retries=60,
    output_path="results.csv",
    return_dataframe=False
)

# Or get the raw content as bytes
content = client.poll_file_until_complete(
    file_id=file_id, 
    interval=5, 
    max_retries=60,
    output_path=None,
    return_dataframe=False
)

# Process results
print(f"Number of validated emails: {len(df)}")
print(df.head())

# Asynchronous
# Upload a file
upload_result = await client.upload_file("emails.csv")
file_id = upload_result["file_id"]

# Check status
status = await client.get_file_status(file_id)

# Download results when complete
if status["status"] == "completed":
    output_file = await client.download_file(file_id, "results.csv")

# Or use the polling method to wait for completion and get results as DataFrame
import pandas as pd

# Get results as DataFrame without saving to disk
df = await client.poll_file_until_complete(
    file_id=file_id, 
    interval=5, 
    max_retries=60,
    output_path=None,  # No file will be saved
    return_dataframe=True
)

# Process results
print(f"Number of validated emails: {len(df)}")
print(df.head())

Batch Processing with Async Client

One of the major advantages of the async client is the ability to process multiple requests in parallel:

import asyncio
from validiz import AsyncValidiz

async def batch_validate():
    emails = [
        "user1@example.com",
        "user2@example.com",
        "user3@example.com",
        "user4@example.com",
    ]
    
    async with AsyncValidiz(api_key="your_api_key") as client:
        # Create tasks for each email
        tasks = [client.validate_email(email) for email in emails]
        
        # Process all emails in parallel
        results = await asyncio.gather(*tasks)
        
        # Process results
        for i, result in enumerate(results):
            print(f"Results for {emails[i]}:")
            print(result)

# Run the async function
asyncio.run(batch_validate())

Batch File Processing with Polling

Process multiple files in parallel and wait for them to complete:

import asyncio
import pandas as pd
from validiz import AsyncValidiz

async def process_multiple_files():
    files = ["file1.csv", "file2.csv", "file3.csv"]
    
    async with AsyncValidiz(api_key="your_api_key") as client:
        # Upload all files
        upload_tasks = [client.upload_file(file) for file in files]
        upload_results = await asyncio.gather(*upload_tasks)
        
        # Get file IDs
        file_ids = [result["file_id"] for result in upload_results]
        
        # Poll for completion and get results without saving to disk
        poll_tasks = [client.poll_file_until_complete(file_id, output_path=None) for file_id in file_ids]
        dataframes = await asyncio.gather(*poll_tasks)
        
        # Process results
        for i, df in enumerate(dataframes):
            print(f"Results for {files[i]}:")
            print(f"Number of emails: {len(df)}")
            print(f"Valid emails: {df[df['is_valid'] == True].shape[0]}")
            print(f"Invalid emails: {df[df['is_valid'] == False].shape[0]}")

# Run the async function
asyncio.run(process_multiple_files())

Error Handling

The library provides several exception classes for different types of errors:

from validiz import (
    ValidizError,  # Base exception
    ValidizAuthError,  # Authentication errors
    ValidizRateLimitError,  # Rate limit exceeded
    ValidizValidationError,  # Validation errors
    ValidizNotFoundError,  # Resource not found
    ValidizConnectionError  # Connection errors
)

try:
    # Make API call
    results = client.validate_email("user@example.com")
except ValidizAuthError as e:
    print(f"Authentication error: {e.message}")
except ValidizRateLimitError as e:
    print(f"Rate limit exceeded: {e.message}")
except ValidizConnectionError as e:
    print(f"Connection error: {e.message}")
except ValidizError as e:
    print(f"API error (status {e.status_code}): {e.message}")

Examples

Check the examples directory for more detailed examples:

  • sync_example.py - Examples for the synchronous client
  • async_example.py - Examples for the asynchronous client

License

This library is released under the MIT License. See the LICENSE file for more 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

validiz-1.0.0.tar.gz (10.9 kB view details)

Uploaded Source

Built Distribution

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

validiz-1.0.0-py3-none-any.whl (12.4 kB view details)

Uploaded Python 3

File details

Details for the file validiz-1.0.0.tar.gz.

File metadata

  • Download URL: validiz-1.0.0.tar.gz
  • Upload date:
  • Size: 10.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.16

File hashes

Hashes for validiz-1.0.0.tar.gz
Algorithm Hash digest
SHA256 d6dfb6ec849b54e30dbdb275678a4eb9c5c4fae294f480dc0f917ce3a6b75364
MD5 7d902127d3074d1e49ccb9342b76cdaa
BLAKE2b-256 5b436c8999db53ac5825aefb0292904a1eb0c18f1722ca568442de98f6db0e3c

See more details on using hashes here.

File details

Details for the file validiz-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: validiz-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 12.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.16

File hashes

Hashes for validiz-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 40f5076e1b2a590f66929e5837b01d7196c003570dd640a31298b014b7d92cff
MD5 8ec8f351e16ad2f6a3e2d1b9c58c8ea6
BLAKE2b-256 0dd041b0091142f6906f4fdc24697e21d6f4167b4e2619685f9f6add835a5aab

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