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.2.1.tar.gz (41.6 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.2.1-py3-none-any.whl (65.7 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for mockarty-0.2.1.tar.gz
Algorithm Hash digest
SHA256 08f00f0e47126398d8534705cb1a7174aef766f186b4fcbb8dc7adb95be0936a
MD5 9a93ca343f8b809c05a9373e6b11a5ff
BLAKE2b-256 302606845dcc6bb24512af8cfe348eb6eebc6c01ae2e1e012069d1dc5f88694a

See more details on using hashes here.

Provenance

The following attestation bundles were made for mockarty-0.2.1.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.2.1-py3-none-any.whl.

File metadata

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

File hashes

Hashes for mockarty-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 2834a35218f8dbf8c67f10fb3d7bb2000a9fc65af54a4cb758352e2a04b8bffa
MD5 961707213a6aef1714a96f6fac2975f3
BLAKE2b-256 cb77cad20d5d50fd70f7f7736ce5b43918d55bc822e0f1201e2251153b74f15d

See more details on using hashes here.

Provenance

The following attestation bundles were made for mockarty-0.2.1-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