Provider
FastAPI provider example.
This modules 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 is the application which receives requests from another service (the consumer) and returns a response. In this example, we have a simple endpoint which returns a user's information from a (fake) database.
This also showcases how Pact tests differ from merely testing adherence to an OpenAPI specification. The Pact tests are more concerned with the practical use of the API, rather than the formally defined specification. The User class defined here has additional fields which are not used by the consumer. Should the provider later decide to add or remove fields, Pact's consumer-driven testing will provide feedback on whether the consumer is compatible with the provider's 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 consumer is not concerned with Pact, only the tests are.
Attributes¶
app = FastAPI()
module-attribute
¶
logger = logging.getLogger(__name__)
module-attribute
¶
Classes¶
User
¶
Bases: BaseModel
Represents a user in the provider system.
This class models user data as it might exist in a real application. In a provider context, the data model may contain more fields than are required by any single consumer. This example demonstrates how a provider can serve multiple consumers with different data needs, and how consumer-driven contract testing (such as with Pact) helps ensure compatibility as the provider evolves.
Attributes¶
admin: bool = False
class-attribute
instance-attribute
¶
created_on: datetime = Field(default_factory=(lambda: datetime.now(tz=(timezone.utc))))
class-attribute
instance-attribute
¶
email: Optional[str] = None
class-attribute
instance-attribute
¶
hobbies: list[str] = Field(default_factory=list)
class-attribute
instance-attribute
¶
id: int
instance-attribute
¶
ip_address: Optional[str] = None
class-attribute
instance-attribute
¶
name: str
instance-attribute
¶
Functions¶
validate_id(value: int) -> int
classmethod
¶
Ensure the ID is a positive integer.
Source code in examples/http/requests_and_fastapi/provider.py
validate_name(value: str) -> str
classmethod
¶
Ensure the name is not empty.
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 user service. For testing, calls to this class can be mocked to avoid the need for a real database. See the test suite for an example.
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/requests_and_fastapi/provider.py
get(user_id: int) -> User | None
classmethod
¶
new_user_id() -> int
classmethod
¶
update(user: User) -> None
classmethod
¶
Update an existing user in the database.
PARAMETER | DESCRIPTION |
---|---|
user
|
The User instance with updated data.
TYPE:
|
RAISES | DESCRIPTION |
---|---|
KeyError
|
If the user does not exist. |
Source code in examples/http/requests_and_fastapi/provider.py
Functions¶
create_user(data: dict[str, Any]) -> User
async
¶
Create a new user in the system.
Source code in examples/http/requests_and_fastapi/provider.py
delete_user(uid: int)
async
¶
Delete a user by their ID.
PARAMETER | DESCRIPTION |
---|---|
uid
|
The user ID to delete.
TYPE:
|
RAISES | DESCRIPTION |
---|---|
HTTPException
|
If the user is not found, a 404 error is raised. |
Source code in examples/http/requests_and_fastapi/provider.py
get_user_by_id(uid: int) -> User
async
¶
Retrieve a user by their ID.
PARAMETER | DESCRIPTION |
---|---|
uid
|
The user ID to retrieve.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
User
|
A User instance representing the user with the given ID. |
RAISES | DESCRIPTION |
---|---|
HTTPException
|
If the user is not found, a 404 error is raised. |