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:
- Create an account at https://pypi.org/account/register/.
- On PyPI, go to your account → Publishing → Add 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
- PyPI Project Name:
- (Recommended) In the GitHub repo settings, create an Environment named
pypito gate releases.
To cut a release:
- Bump
versioninpyproject.toml(and__version__insrc/rest_api_mocker/__init__.py). - 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
55fc1645605b61b8c8be7baf81c3d4507c2209c0a6eb087c0d3ddd2fffd5028b
|
|
| MD5 |
ba18112bd5b4408e7304faef381770b6
|
|
| BLAKE2b-256 |
e11cb716c36d126eaef71cd56acf7a186d2db68c729ae64378262ae8669a0968
|
Provenance
The following attestation bundles were made for rest_api_mocker-0.1.0.tar.gz:
Publisher:
publish.yml on julienlopez/RestApiMockerPythonAPI
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
rest_api_mocker-0.1.0.tar.gz -
Subject digest:
55fc1645605b61b8c8be7baf81c3d4507c2209c0a6eb087c0d3ddd2fffd5028b - Sigstore transparency entry: 1836350685
- Sigstore integration time:
-
Permalink:
julienlopez/RestApiMockerPythonAPI@23776802d78752186627af0a9c12f6685ac46cfd -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/julienlopez
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@23776802d78752186627af0a9c12f6685ac46cfd -
Trigger Event:
release
-
Statement type:
File details
Details for the file rest_api_mocker-0.1.0-py3-none-any.whl.
File metadata
- Download URL: rest_api_mocker-0.1.0-py3-none-any.whl
- Upload date:
- Size: 7.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4c6471900fc9a87be518ab25f73bf73428288eb93d55ced271d2ecdae2a6312a
|
|
| MD5 |
e6b51a45ecea6b9423832c5342a839ed
|
|
| BLAKE2b-256 |
23c9e389dc0b0f36f62192a36931d37d02b8501af51dee3b7024a04a9dbcfbc5
|
Provenance
The following attestation bundles were made for rest_api_mocker-0.1.0-py3-none-any.whl:
Publisher:
publish.yml on julienlopez/RestApiMockerPythonAPI
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
rest_api_mocker-0.1.0-py3-none-any.whl -
Subject digest:
4c6471900fc9a87be518ab25f73bf73428288eb93d55ced271d2ecdae2a6312a - Sigstore transparency entry: 1836350911
- Sigstore integration time:
-
Permalink:
julienlopez/RestApiMockerPythonAPI@23776802d78752186627af0a9c12f6685ac46cfd -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/julienlopez
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@23776802d78752186627af0a9c12f6685ac46cfd -
Trigger Event:
release
-
Statement type: