Skip to main content

A small Python wrapper for RestApiMocker.

Project description

rest-api-mocker

A small Python wrapper for RestApiMocker.

Installation

pip install rest-api-mocker

Until it's published, install from a checkout:

pip install -e ".[test]"

Usage

from rest_api_mocker import RestApiMocker

mocker = RestApiMocker("http://localhost", 8080)
mocker.add_mock(
    method="GET",
    path_pattern="/users/.*",
    status=200,
    body={"id": 1, "name": "Ada"},
)

RestApiMocker can also be used as a context manager so the underlying HTTP session is closed for you:

with RestApiMocker("http://localhost", 8080) as mocker:
    mocker.add_mock("GET", "/health", 200, {"ok": True})

API

The client mirrors the server's /internal control plane:

Method Description
add_mock(method, path_pattern, status, body, conditions=None) Register a mock response.
get_mocks() -> list[MockConfig] List all configured mocks.
delete_mock(index) Delete a mock by its 0-based index.
delete_all_mocks() Delete every configured mock.
delete_mocks_by_pattern(path_pattern) Delete all mocks matching a path pattern.
get_config() -> ServerConfig Get the server's public/private ports.
get_history() -> list[RequestRecord] Get the recorded request history.
mocker.add_mock("GET", "/users/.*", 200, {"id": 1})

config = mocker.get_config()          # ServerConfig(public_port=9090, private_port=80)
mocks = mocker.get_mocks()            # [MockConfig(...)]
history = mocker.get_history()        # [RequestRecord(method=..., path=..., timestamp=...)]

mocker.delete_mocks_by_pattern("/users/.*")
mocker.delete_all_mocks()

The MockConfig, ServerConfig and RequestRecord dataclasses are importable from the top-level package.

Errors

A non-success response from the mocker server raises MockRequestError (a subclass of RestApiMockerError):

from rest_api_mocker import MockRequestError

try:
    mocker.add_mock("GET", "/x", 200, {})
except MockRequestError as exc:
    print(exc.status_code, exc.response_text)

Development

pip install -e ".[dev]"
pytest             # tests
ruff check .       # lint
ruff format .      # format
mypy src           # type-check

CI (.github/workflows/ci.yml) runs the tests on Python 3.8–3.13 plus lint, format, and type checks on every push and pull request.

Releasing to PyPI

Publishing is automated by .github/workflows/publish.yml, which runs when you publish a GitHub Release. It uses PyPI Trusted Publishing (OIDC), so there are no API tokens or secrets to store.

One-time setup:

  1. Create an account at https://pypi.org/account/register/.
  2. On PyPI, go to your account → PublishingAdd a pending publisher and register this repository as a trusted publisher:
    • PyPI Project Name: rest-api-mocker
    • Owner / Repository: your GitHub owner / RestApiMockerPythonAPI
    • Workflow name: publish.yml
    • Environment name: pypi
  3. (Recommended) In the GitHub repo settings, create an Environment named pypi to gate releases.

To cut a release:

  1. Bump version in pyproject.toml (and __version__ in src/rest_api_mocker/__init__.py).
  2. Tag and push, then publish a GitHub Release for that tag. The workflow builds the package and uploads it to PyPI.

Tip: to rehearse without affecting the real index, register the same trusted publisher on https://test.pypi.org and point the publish step at it with with: { repository-url: https://test.pypi.org/legacy/ }.

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

rest_api_mocker-0.1.0.tar.gz (9.0 kB view details)

Uploaded Source

Built Distribution

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

rest_api_mocker-0.1.0-py3-none-any.whl (7.8 kB view details)

Uploaded Python 3

File details

Details for the file rest_api_mocker-0.1.0.tar.gz.

File metadata

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

File hashes

Hashes for rest_api_mocker-0.1.0.tar.gz
Algorithm Hash digest
SHA256 55fc1645605b61b8c8be7baf81c3d4507c2209c0a6eb087c0d3ddd2fffd5028b
MD5 ba18112bd5b4408e7304faef381770b6
BLAKE2b-256 e11cb716c36d126eaef71cd56acf7a186d2db68c729ae64378262ae8669a0968

See more details on using hashes here.

Provenance

The following attestation bundles were made for rest_api_mocker-0.1.0.tar.gz:

Publisher: publish.yml on julienlopez/RestApiMockerPythonAPI

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

File details

Details for the file rest_api_mocker-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for rest_api_mocker-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4c6471900fc9a87be518ab25f73bf73428288eb93d55ced271d2ecdae2a6312a
MD5 e6b51a45ecea6b9423832c5342a839ed
BLAKE2b-256 23c9e389dc0b0f36f62192a36931d37d02b8501af51dee3b7024a04a9dbcfbc5

See more details on using hashes here.

Provenance

The following attestation bundles were made for rest_api_mocker-0.1.0-py3-none-any.whl:

Publisher: publish.yml on julienlopez/RestApiMockerPythonAPI

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