Consumer
Requests consumer example.
This module defines a simple
consumer
using the synchronous requests
library which will be tested with
Pact in the consumer test.
As Pact is a consumer-driven framework, the consumer defines the interactions
which the provider must then satisfy.
The consumer is the application which makes requests to another service (the
provider) and receives a response to process. In this example, we have a simple
User
class and the consumer fetches a user's information from a HTTP endpoint.
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. So you will see below that as far as this consumer is concerned, the only information needed from the provider is the user's ID, name, and creation date. This is despite the provider having additional fields in the response.
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¶
logger = logging.getLogger(__name__)
module-attribute
¶
Classes¶
User(id: int, name: str, created_on: datetime)
dataclass
¶
Represents a user as seen by the consumer.
This class is intentionally minimal, including only the fields the consumer actually uses. It may differ from the provider's user model, which could have additional fields. This demonstrates the consumer-driven nature of contract testing: the consumer defines what it needs, not what the provider exposes.
UserClient(hostname: str, base_path: str | None = None)
¶
HTTP client for interacting with a user provider service.
This class is a simple consumer that fetches user data from a provider over HTTP. It demonstrates how to structure consumer code for use in contract testing, keeping it independent of Pact or any contract testing framework.
PARAMETER | DESCRIPTION |
---|---|
hostname
|
The base URL of the provider (must include scheme, e.g.,
TYPE:
|
base_path
|
The base path for the provider's API endpoints. Defaults to
TYPE:
|
RAISES | DESCRIPTION |
---|---|
ValueError
|
If the hostname does not start with 'http://' or |
Source code in examples/http/requests_and_fastapi/consumer.py
Attributes¶
base_path: str
property
¶
The base path as a string.
base_url: str
property
¶
The base URL as a string.
hostname: str
property
¶
The hostname as a string.
This includes the scheme.
Functions¶
create_user(*, name: str) -> User
¶
Create a new user on the provider.
PARAMETER | DESCRIPTION |
---|---|
name
|
The name of the user to create.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
User
|
A |
RAISES | DESCRIPTION |
---|---|
HTTPError
|
If the server returns a non-2xx response or the request fails. |
Source code in examples/http/requests_and_fastapi/consumer.py
delete_user(uid: int | User) -> None
¶
Delete a user by ID from the provider.
PARAMETER | DESCRIPTION |
---|---|
uid
|
The user ID (int) or a |
RAISES | DESCRIPTION |
---|---|
HTTPError
|
If the server returns a non-2xx response or the request fails. |
Source code in examples/http/requests_and_fastapi/consumer.py
get_user(user_id: int) -> User
¶
Fetch a user by ID from the provider.
This method demonstrates how a consumer fetches only the data it needs from a provider, regardless of what else the provider may return.
PARAMETER | DESCRIPTION |
---|---|
user_id
|
The ID of the user to fetch.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
User
|
A |
RAISES | DESCRIPTION |
---|---|
HTTPError
|
If the server returns a non-2xx response or the request fails. |