Skip to main content

Official Python SDK for Mockarty — multi-protocol mock server for HTTP, gRPC, MCP, GraphQL, SOAP, SSE, Kafka, RabbitMQ, SMTP

Project description

Mockarty

Python SDK

Official Python client library for Mockarty — a multi-protocol mock server for HTTP, gRPC, MCP, GraphQL, SOAP, SSE, WebSocket, Kafka, RabbitMQ, and SMTP.

PyPI Python License

Installation

pip install mockarty

For async HTTP/2 support:

pip install mockarty[async]

Quick Start

Synchronous Client

from mockarty import MockartyClient, MockBuilder, AssertAction

# Create a client
client = MockartyClient(
    base_url="http://localhost:5770",
    api_key="your-api-key",
    namespace="sandbox",
)

# Create a mock using the builder
mock = (
    MockBuilder.http("/api/users/:id", "GET")
    .id("user-get")
    .respond(200, body={"id": "$.pathParam.id", "name": "$.fake.FirstName"})
    .ttl(3600)
    .build()
)

result = client.mocks.create(mock)
print(f"Created mock: {result.mock.id}")

# List mocks
page = client.mocks.list(namespace="sandbox", limit=10)
for m in page.items:
    print(f"  {m.id}")

# Health check
health = client.health.check()
print(f"Status: {health.status}")

client.close()

Async Client

import asyncio
from mockarty import AsyncMockartyClient, MockBuilder

async def main():
    async with AsyncMockartyClient(base_url="http://localhost:5770") as client:
        mock = MockBuilder.http("/api/hello", "GET").respond(200, body={"msg": "hello"}).build()
        result = await client.mocks.create(mock)
        print(f"Created: {result.mock.id}")

asyncio.run(main())

Context Manager

with MockartyClient() as client:
    client.mocks.create(mock)
    # client.close() is called automatically

Mock Builder

from mockarty import MockBuilder, AssertAction

# HTTP mock with conditions
mock = (
    MockBuilder.http("/api/orders", "POST")
    .id("create-order")
    .namespace("production")
    .tags("orders", "v2")
    .priority(10)
    .condition("$.body.amount", AssertAction.NOT_EMPTY)
    .header_condition("Authorization", AssertAction.NOT_EMPTY)
    .respond(201, body={
        "orderId": "$.fake.UUID",
        "amount": "$.req.amount",
        "status": "created",
    })
    .callback("https://webhook.site/test", method="POST", body={"event": "order.created"})
    .ttl(7200)
    .build()
)

# gRPC mock
grpc_mock = (
    MockBuilder.grpc("user.UserService", "GetUser")
    .id("grpc-get-user")
    .condition("$.id", AssertAction.NOT_EMPTY)
    .respond(200, body={"id": "$.req.id", "name": "$.fake.FirstName"})
    .build()
)

# MCP mock
mcp_mock = (
    MockBuilder.mcp("get_weather")
    .id("mcp-weather")
    .respond(200, body={"temperature": 22, "city": "$.req.city"})
    .build()
)

Working with Stores

with MockartyClient() as client:
    # Global store
    client.stores.global_set("counter", 0)
    data = client.stores.global_get()
    print(data)  # {"counter": 0}

    # Chain store
    client.stores.chain_set("order-flow", "orderId", "abc-123")
    chain_data = client.stores.chain_get("order-flow")
    print(chain_data)  # {"orderId": "abc-123"}

Error Handling

from mockarty import MockartyClient
from mockarty.errors import MockartyNotFoundError, MockartyAPIError

client = MockartyClient()
try:
    mock = client.mocks.get("non-existent")
except MockartyNotFoundError:
    print("Mock not found")
except MockartyAPIError as e:
    print(f"API error {e.status_code}: {e.message}")

Configuration

The client reads these environment variables as defaults:

Variable Description Default
MOCKARTY_BASE_URL Mockarty server URL http://localhost:5770
MOCKARTY_API_KEY API authentication key None

pytest Integration

Install the test extras:

pip install mockarty[test]

Use the provided fixtures in your tests:

# conftest.py
pytest_plugins = ["mockarty.testing.fixtures"]

# test_example.py
def test_create_mock(mock_cleanup):
    from mockarty import MockBuilder
    mock = MockBuilder.http("/test", "GET").respond(200, body="ok").build()
    created = mock_cleanup(mock)
    assert created.id is not None

License

MIT

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

mockarty-0.3.0.tar.gz (46.2 kB view details)

Uploaded Source

Built Distribution

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

mockarty-0.3.0-py3-none-any.whl (69.5 kB view details)

Uploaded Python 3

File details

Details for the file mockarty-0.3.0.tar.gz.

File metadata

  • Download URL: mockarty-0.3.0.tar.gz
  • Upload date:
  • Size: 46.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for mockarty-0.3.0.tar.gz
Algorithm Hash digest
SHA256 d33cf7a80f68fa6c9e0f9ec76b83f9d663ce69ef0fe1c1cabb44d0426d450f6e
MD5 681521bf86b4c45d7f70b2336423d4a6
BLAKE2b-256 fe06eba503978c9e09637da5c84d615ef2782c7cc63039a89f5dab92b8fe58c2

See more details on using hashes here.

Provenance

The following attestation bundles were made for mockarty-0.3.0.tar.gz:

Publisher: publish.yml on mockarty/py-sdk

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file mockarty-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: mockarty-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 69.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for mockarty-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 777e4ac5e4a89b92dd3df3a22955a2196c28473e77d0633f30af7f96a2523a9c
MD5 7d3e6f5e1d745d9010dbad6a17c1fea2
BLAKE2b-256 249cbe3b40f26e0bb3e55d432928448bdf784bddd1a3233efb5b6cfa9ba3e8d6

See more details on using hashes here.

Provenance

The following attestation bundles were made for mockarty-0.3.0-py3-none-any.whl:

Publisher: publish.yml on mockarty/py-sdk

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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