Consumer¶
Simple Consumer Implementation.
This modules defines a simple consumer 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. The pact-python
dependency only appears in the tests. This is because the consumer is not
concerned with Pact, only the tests are.
Classes¶
User(id: int, name: str, created_on: datetime)
dataclass
¶
UserConsumer(base_uri: str)
¶
Example consumer.
This class defines a simple consumer which will interact with a provider
over HTTP to fetch a user's information, and then return an instance of the
User
class.
PARAMETER | DESCRIPTION |
---|---|
base_uri
|
The uri of the provider
TYPE:
|
Source code in examples/src/consumer.py
Attributes¶
base_uri = base_uri
instance-attribute
¶
Functions¶
create_user(*, name: str) -> User
¶
Create a new user on the server.
PARAMETER | DESCRIPTION |
---|---|
name
|
The name of the user to create.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
User
|
The user, if successfully created. |
RAISES | DESCRIPTION |
---|---|
HTTPError
|
If the server returns a non-200 response. |
Source code in examples/src/consumer.py
delete_user(uid: int | User) -> None
¶
Delete a user by ID from the server.
PARAMETER | DESCRIPTION |
---|---|
uid
|
The user ID or user object to delete. |
RAISES | DESCRIPTION |
---|---|
HTTPError
|
If the server returns a non-200 response. |
Source code in examples/src/consumer.py
get_user(user_id: int) -> User
¶
Fetch a user by ID from the server.
PARAMETER | DESCRIPTION |
---|---|
user_id
|
The ID of the user to fetch.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
User
|
The user if found. |
User
|
In all other cases, an error dictionary is returned with the key |
User
|
|
RAISES | DESCRIPTION |
---|---|
HTTPError
|
If the server returns a non-200 response. |