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.3.tar.gz (44.3 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.3-py3-none-any.whl (67.4 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for mockarty-0.2.3.tar.gz
Algorithm Hash digest
SHA256 1b3bac9f42a8e8959ce872afb6eff822fdd53b6941aacde000c39752009d7c1d
MD5 b94b56187b31c77214014f27204090d8
BLAKE2b-256 8f4491d3c45c4d002363995784f77d0d3796e2ab5be00fca49f36818ec286722

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: mockarty-0.2.3-py3-none-any.whl
  • Upload date:
  • Size: 67.4 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.2.3-py3-none-any.whl
Algorithm Hash digest
SHA256 4baab654d0f2e3795593227fbdad2357b7e7246f93d215709b2376eff647d94a
MD5 49b3f7ddcefe94210ea091167ef00c51
BLAKE2b-256 7fe5a2229c86c9381b4002818ec571f066c3e03afb584bdf2075928029c2acc7

See more details on using hashes here.

Provenance

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