Provider
FastAPI XML provider example.
This module defines a simple
provider
implemented with fastapi which will be tested
with Pact in the provider test. As
Pact is a consumer-driven framework, the consumer defines the contract which the
provider must then satisfy.
The provider receives requests from the consumer and returns XML responses built
using the standard library xml.etree.ElementTree
module. Serialisation is handled manually rather than via FastAPI's built-in
JSON serialisation, since FastAPI does not natively support XML response bodies.
This also showcases how Pact tests differ from merely testing adherence to an
OpenAPI specification. The Pact tests are concerned with the practical use of
the API from the consumer's perspective. The User model here could contain
additional fields in a real application; if the provider later adds or removes
fields, Pact's consumer-driven testing will verify that the consumer remains
compatible with those changes.
Note that the code in this module is agnostic of Pact (i.e., this would be your
production code). The pact-python dependency only appears in the tests. This
is because the provider is not concerned with Pact, only the tests are.
Attributes¶
app = FastAPI()
module-attribute
¶
logger = logging.getLogger(__name__)
module-attribute
¶
Classes¶
User(id: int, name: str)
dataclass
¶
Represents a user in the provider system.
This class uses a plain dataclass rather than Pydantic to keep the focus
on the XML serialisation pattern. In a real FastAPI application you would
typically use a Pydantic model to benefit from automatic validation and
JSON serialisation; see the
requests_and_fastapi
example for that approach.
The provider's model may contain more fields than any single consumer requires. This demonstrates how provider-side data can be richer than what appears in the consumer contract.
UserDb
¶
A simple in-memory user database abstraction for demonstration purposes.
This class simulates a user database using a class-level dictionary. In a real application this would interface with a persistent database or external service. For testing, the state handlers in the provider test populate and clean up this database directly, ensuring each contract interaction runs against a predictable state.
Functions¶
create(user: User) -> None
classmethod
¶
Add a new user to the database.
| PARAMETER | DESCRIPTION |
|---|---|
user
|
The User instance to add.
TYPE:
|
delete(user_id: int) -> None
classmethod
¶
Delete a user from the database by their ID.
| PARAMETER | DESCRIPTION |
|---|---|
user_id
|
The ID of the user to delete.
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
KeyError
|
If the user does not exist. |
Source code in examples/http/xml_example/provider.py
Functions¶
get_user_by_id(uid: int) -> Response
async
¶
Retrieve a user by their ID, returning an XML response.
| PARAMETER | DESCRIPTION |
|---|---|
uid
|
The user ID to retrieve.
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
HTTPException
|
If the user is not found, a 404 error is raised. |