Skip to content

Commit 9488f0e

Browse files
committed
chore(example): migrate flask provider example
Following the changes to the FastAPI example, this migrates the Flask provider example to the new structure. The example relies on the consumer having published contracts, and the flask provider is verified against those contracts. Signed-off-by: JP-Ellis <josh@jpellis.me>
1 parent dd8827a commit 9488f0e

File tree

11 files changed

+188
-238
lines changed

11 files changed

+188
-238
lines changed

examples/flask_provider/.flake8

Lines changed: 0 additions & 4 deletions
This file was deleted.

examples/flask_provider/requirements.txt

Lines changed: 0 additions & 10 deletions
This file was deleted.

examples/flask_provider/run_pytest.sh

Lines changed: 0 additions & 20 deletions
This file was deleted.

examples/flask_provider/src/provider.py

Lines changed: 0 additions & 25 deletions
This file was deleted.

examples/flask_provider/tests/conftest.py

Lines changed: 0 additions & 8 deletions
This file was deleted.

examples/flask_provider/tests/pact_provider.py

Lines changed: 0 additions & 48 deletions
This file was deleted.

examples/flask_provider/tests/provider/test_provider.py

Lines changed: 0 additions & 83 deletions
This file was deleted.

examples/flask_provider/verify_pact.sh

Lines changed: 0 additions & 39 deletions
This file was deleted.

examples/src/flask.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
"""
2+
Flask provider example.
3+
4+
This modules defines a simple
5+
[provider](https://docs.pact.io/getting_started/terminology#service-provider)
6+
with Pact. As Pact is a consumer-driven framework, the consumer defines the
7+
contract which the provider must then satisfy.
8+
9+
The provider is the application which receives requests from another service
10+
(the consumer) and returns a response. In this example, we have a simple
11+
endpoint which returns a user's information from a (fake) database.
12+
13+
Note that the code in this module is agnostic of Pact. The `pact-python`
14+
dependency only appears in the tests. This is because the consumer is not
15+
concerned with Pact, only the tests are.
16+
"""
17+
18+
from __future__ import annotations
19+
20+
from typing import Any
21+
22+
from flask import Flask
23+
24+
app = Flask(__name__)
25+
26+
"""
27+
As this is a simple example, we'll use a simple dict to represent a database.
28+
This would be replaced with a real database in a real application.
29+
30+
When testing the provider in a real application, the calls to the database
31+
would be mocked out to avoid the need for a real database. An example of this
32+
can be found in the test suite.
33+
"""
34+
FAKE_DB: dict[int, dict[str, Any]] = {}
35+
36+
37+
@app.route("/users/<uid>")
38+
def get_user_by_id(uid: int) -> dict[str, Any] | tuple[dict[str, Any], int]:
39+
"""
40+
Fetch a user by their ID.
41+
42+
Args:
43+
uid: The ID of the user to fetch
44+
45+
Returns:
46+
The user data if found, HTTP 404 if not
47+
"""
48+
user = FAKE_DB.get(uid)
49+
if not user:
50+
return {"error": "User not found"}, 404
51+
return user

0 commit comments

Comments
 (0)