Flask¶
Flask provider example.
This modules defines a simple provider 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.
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.
Attributes¶
FAKE_DB: dict[int, User] = {}
module-attribute
¶
app = Flask(__name__)
module-attribute
¶
logger = logging.getLogger(__name__)
module-attribute
¶
Classes¶
User(id: int, name: str, created_on: datetime, email: str | None, ip_address: str | None, hobbies: list[str], admin: bool)
dataclass
¶
User data class.
Attributes¶
admin: bool
instance-attribute
¶
created_on: datetime
instance-attribute
¶
email: str | None
instance-attribute
¶
hobbies: list[str]
instance-attribute
¶
id: int
instance-attribute
¶
ip_address: str | None
instance-attribute
¶
name: str
instance-attribute
¶
Functions¶
dict() -> dict[str, Any]
¶
Return the user's data as a dict.
Source code in examples/src/flask.py
Functions¶
create_user() -> Response
¶
Source code in examples/src/flask.py
delete_user(uid: int) -> tuple[str | Response, int]
¶
get_user_by_id(uid: int) -> Response | tuple[Response, int]
¶
Fetch a user by their ID.
PARAMETER | DESCRIPTION |
---|---|
uid
|
The ID of the user to fetch
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Response | tuple[Response, int]
|
The user data if found, HTTP 404 if not |