Skip to content

Commit 79995ea

Browse files
committed
RR feedback
1 parent 762f99f commit 79995ea

File tree

1 file changed

+62
-54
lines changed

1 file changed

+62
-54
lines changed

source/integrations/fastapi-integration.txt

Lines changed: 62 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ Prerequisites
3939
Ensure you have the following components installed and set up before you start
4040
this tutorial:
4141

42-
- Python v3.9.0 or later
43-
- A MongoDB Atlas cluster
42+
- Python v3.9.0 or later.
43+
- A MongoDB Atlas cluster.
4444
To learn how to set up a cluster, see
45-
the :ref:`Getting Started <pymongo-get-started>`__ guide for more information.
45+
the :ref:`Getting Started <pymongo-get-started>` guide for more information.
4646

4747
Set-up
4848
~~~~~~
@@ -54,69 +54,74 @@ Install dependencies, connect to MongoDB and start your FastAPI server.
5454

5555
.. step:: Clone the example code example.
5656

57-
Run the following command in your terminal to clone the code from the `mongodb-with-fastapi <https://github.com/mongodb-developer/mongodb-with-fastapi>`__ GitHub repository:
57+
Run the following command in your terminal to clone the code from the
58+
:github:`mongodb-with-fastapi </mongodb-developer/mongodb-with-fastapi>`
59+
GitHub repository:
5860

59-
.. code-block:: shell
61+
.. code-block:: shell
6062

61-
git clone [email protected]:mongodb-developer/mongodb-with-fastapi.git
63+
git clone [email protected]:mongodb-developer/mongodb-with-fastapi.git
6264

6365
.. step:: Install the required dependencies.
6466

6567
.. tip:: Use a Virtual environment
6668

67-
Installing your Python dependencies in a `virtualenv
68-
<https://docs.python.org/3/tutorial/venv.html>`__ allows you to install
69-
versions of your libraries for individual
70-
projects. Before running any ``pip`` commands, ensure your ``virtualenv`` is active.
69+
Installing your Python dependencies in a `virtualenv
70+
<https://docs.python.org/3/tutorial/venv.html>`__ allows you to install
71+
versions of your libraries for individual projects. Before running any
72+
``pip`` commands, ensure your ``virtualenv`` is active.
7173

72-
Run the following command in your terminal to install the dependencies listed in the ``requirements.txt`` file:
74+
Run the following command in your terminal to install the dependencies listed in the ``requirements.txt`` file:
7375

74-
.. code-block:: shell
76+
.. code-block:: shell
7577

76-
cd mongodb-with-fastapi
77-
pip install -r requirements.txt
78+
cd mongodb-with-fastapi
79+
pip install -r requirements.txt
7880

79-
It might take a few moments to download and install your dependencies.
81+
It might take a few moments to download and install your dependencies.
8082

8183
.. step:: Retrieve your connection string.
82-
83-
Follow the :manual:`Find Your MongoDB Atlas Connection String guide </reference/connection-string/#find-your-mongodb-atlas-connection-string>` to retrieve your connection string.
84-
85-
Run the following code in your terminal to create an environment variable to store your connection string:
8684

87-
.. code-block:: shell
85+
Follow the :manual:`Find Your MongoDB Atlas Connection String guide
86+
</reference/connection-string/#find-your-mongodb-atlas-connection-string>`
87+
to retrieve your connection string.
8888

89-
export MONGODB_URL="mongodb+srv://<username>:<password>@<url>/<db>?retryWrites=true&w=majority"
90-
91-
.. tip:: Reset Environment Variables
89+
Run the following code in your terminal to create an environment variable
90+
to store your connection string:
91+
92+
.. code-block:: shell
9293

93-
Anytime you start a new terminal session, you will must reset this
94-
environment variable. You can use `direnv <https://direnv.net/>`__ to
95-
make this process easier.
94+
export MONGODB_URL="mongodb+srv://<username>:<password>@<url>/<db>?retryWrites=true&w=majority"
95+
96+
.. tip:: Reset Environment Variables
97+
98+
Anytime you start a new terminal session, you will must reset this
99+
environment variable. You can use `direnv <https://direnv.net/>`__ to
100+
make this process easier.
96101

97102
.. step:: Start your FastAPI server.
98103

99-
Run the following code in your terminal to start your FastAPI server:
104+
Run the following code in your terminal to start your FastAPI server:
100105

101-
.. code-block:: shell
106+
.. code-block:: shell
102107

103-
uvicorn app:app --reload
108+
uvicorn app:app --reload
104109

105-
After the application is successfully running, you can view it in your
106-
browse by navigating to http://127.0.0.1:8000/docs. The following image
107-
shows UI generated by the FastAPI server.
110+
After the application is successfully running, you can view it in your
111+
browse by navigating to `<http://127.0.0.1:8000/docs>`__. The following image
112+
shows interface generated by the FastAPI server.
108113

109-
.. image:: /includes/integrations/fastapi-browser.png
110-
:alt: Screenshot of browser and swagger UI
114+
.. image:: /includes/integrations/fastapi-browser.png
115+
:alt: Screenshot of browser and swagger UI
111116

112117
Connect Your Application to Your Cluster
113118
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
114119

115-
All the code for the example application is stored in the `app.py file in the mongodb-with-fastapi
116-
<https://github.com/mongodb-developer/mongodb-with-fastapi/blob/master/app.py>`__
117-
GitHub repository.
120+
All the code for the example application is stored in the :github:`app.py file
121+
in the mongodb-with-fastapi
122+
</mongodb-developer/mongodb-with-fastapi/blob/master/app.py>` GitHub repository.
118123

119-
Use the following code to:
124+
Use this code to perform the following actions:
120125

121126
1. :ref:`Connect to your MongoDB Atlas cluster <pymongo_connect_atlas>` by using
122127
the ``AsyncMongoClient()`` method with the ``MONGODB_URL`` environment
@@ -134,7 +139,8 @@ Create Your Database Models
134139
~~~~~~~~~~~~~~~~~~~~~~~~~~~
135140

136141
Your application has three models, the ``StudentModel``, the
137-
``UpdateStudentModel``, and the ``StudentCollection``.
142+
``UpdateStudentModel``, and the ``StudentCollection``. All of these models are
143+
defined in the ``app.py`` file.
138144

139145
.. procedure::
140146
:style: connected
@@ -151,8 +157,8 @@ Your application has three models, the ``StudentModel``, the
151157
private, meaning you cannot assign it a value. Therefore, you can name the
152158
field ``id`` but give it an alias of ``_id`` and set ``populate_by_name``
153159
to ``True`` in the model's ``model_config``. You can also set this ``id``
154-
value automatically to ``None``, so that you can create a new student with
155-
out specifying it.
160+
value automatically to ``None``, so that you can create a new student
161+
without specifying it.
156162

157163

158164
.. note:: BSON to JSON Mapping
@@ -164,10 +170,11 @@ Your application has three models, the ``StudentModel``, the
164170
you must convert ``ObjectId`` objects to strings before storing them in
165171
the ``_id`` field.
166172

167-
For more information about how BSON compares to JSON, see this `JSON
173+
For more information about how BSON compares to JSON, see the `JSON
168174
and BSON <https://www.mongodb.com/json-and-bson>`__ MongoDB article.
169175

170-
Define the ``StudentModel`` class by adding the following code:
176+
Define the ``StudentModel`` class by adding the following code to your
177+
``app.py`` file:
171178

172179
.. code-block:: python
173180

@@ -238,18 +245,17 @@ Your application has three models, the ``StudentModel``, the
238245

239246
The ``StudentCollection`` class is defined to encapsulate a list of
240247
``StudentModel`` instances. In theory, the endpoint could return a
241-
top-level list of ``StudentModel`` objects, but there are some
242-
vulnerabilities associated with returning JSON responses with top-level lists.
248+
top-level list of ``StudentModel`` objects, but there are some lists. For
249+
more information, you can read this article about `JSON Hijacking
250+
<https://haacked.com/archive/2009/06/25/json-hijacking.aspx/>`__.
243251

244252
Define the ``StudentCollection`` class by adding the following code:
245253

246254
.. code-block:: python
247255

248256
class StudentCollection(BaseModel):
249257
"""
250-
A container holding a list of `StudentModel` instances.
251-
252-
This exists because providing a top-level array in a JSON response can be a `vulnerability <https://haacked.com/archive/2009/06/25/json-hijacking.aspx/>`__
258+
A container holding a list of `StudentModel` instances
253259
"""
254260

255261
students: List[StudentModel]
@@ -283,6 +289,8 @@ with your data:
283289
* - ``DELETE /students/{id}``
284290
- Delete a student
285291

292+
All of these routes are defined in the ``app.py`` file.
293+
286294
.. procedure::
287295
:style: connected
288296

@@ -328,8 +336,8 @@ with your data:
328336

329337
.. step:: Create the Read routes.
330338

331-
This application has two read routes: one for viewing all students, and one
332-
for viewing an individual student specified by their ``id``.
339+
This application one route for viewing all students, and one
340+
for viewing an individual student, specified by their ``id``.
333341

334342
Define the ``list_students`` route to view all students by adding the following code:
335343

@@ -354,7 +362,7 @@ with your data:
354362

355363
This example uses the ``to_list()`` method; but in a real application,
356364
we recommend using the `skip and limit parameters
357-
<https://pymongo.readthedocs.io/en/stable/api/pymongo/asynchronous/collection.html#pymongo.asynchronous.collection.AsyncCollection.find>`__
365+
<{+api-root+}pymongo/asynchronous/collection.html#pymongo.asynchronous.collection.AsyncCollection.find>`
358366
in ``find`` to paginate your results.
359367

360368
The student detail route has a path parameter of ``id``, which FastAPI
@@ -387,7 +395,7 @@ with your data:
387395

388396
.. step:: Create the Update route.
389397

390-
The ``update_student`` route is like a combination of the
398+
The ``update_student`` route functions similar to a combination of the
391399
``create_student`` and the ``show_student`` routes. It receives the ``id``
392400
of the student to update, and the new data in the JSON body.
393401

@@ -472,9 +480,9 @@ For more information about FastAPI integration, see the following resources:
472480

473481
- MongoDB's Full Stack FastAPI App Generator
474482
- `Blog post: Introducing the Full Stack FastAPI App Generator for Python Developers <https://www.mongodb.com/blog/post/introducing-full-stack-fast-api-app-generator-for-python-developers>`__
475-
- `Github repository: full-stack-fastapi-mongodb <https://github.com/mongodb-labs/full-stack-fastapi-mongodb>`__
483+
- :github:`Github repository: full-stack-fastapi-mongodb </mongodb-labs/full-stack-fastapi-mongodb>`
476484
- `Introducing the FARM stack (FastAPI, React and MongoDB) blog post <https://developer.mongodb.com/how-to/FARM-Stack-FastAPI-React-MongoDB>`__
477485
- `FastAPI documentation <https://fastapi.tiangolo.com/>`__
478-
- `Third-party FastAPI Integration Options <https://github.com/mjhea0/awesome-fastapi>`__
486+
- :github:`Third-party FastAPI Integration Options </mjhea0/awesome-fastapi>`
479487

480488
For support or to contribute to the MongoDB Community, see the `MongoDB Developer Community <https://community.mongodb.com/>`__.

0 commit comments

Comments
 (0)