Deciding What Your App Can Do
00:00 Deciding What Your App Can Do. Before you add more code to your application, remember the endpoints and actions of your URL shortener, which is seen on-screen. When a user posts a target URL that they want to shorten, your app should send a message confirming that the action worked. To enable your user to manage the shortened URL, you are sending along a response with some additional information to the client.
00:28 On-screen is an example of how a response body can look.
00:35 The schema states what your API expects as a request body and what the client can expect in the response body. You’ll implement type hinting to verify that the request and the response match the data types that you define.
00:50
Start by creating the base models for your API request and response bodies in a schemas.py
file, as seen on-screen.
01:02
In this file, you are using pydantic’s BaseModel
to define the URLBase
class, so this is imported first.
01:11
The URLBase
class contains the field target_url
, which requires a string. You’ll use target_url
to store the URL that your shortened URL forwards to.
01:24
The URL
class inherits the target_url
field from URLBase
. You can add the Boolean field is_active
and the integer field clicks
to the class.
01:35
The is_active
field allows you to deactivate shortened URLs. With clicks
, you’ll later count how many times a shortened URL has been visited.
01:45
As in config.py
, you’ll use the Config
class in line 12 to provide configurations to pydantic. In this case, you’ll tell pydantic with orm_mode = True
to work with the database model. ORM stands for object-relational mapping, and it provides the convenience of interacting with your database using an object-oriented approach. In the next section, you’ll see how the ORM works and how the URL
class relates to the urls
database table. Here, you define URLInfo
.
02:20
This enhances URL
by requiring two additional strings, url
and admin_url
. You can also add the two strings url
and admin_url
to URL
.
02:34
But by adding a url
an admin_url
to the URLInfo
subclass, you can use the data in your API without storing it in your database.
02:46
Next head back to main.py
to use the URLBase
schema in a POST endpoint. First, you import validators
and HTTPException
from fastapi
.
03:01
You also import the schemas
module that you just created.
03:09
The raise_bad_request()
function takes messages and argument and raises an HTTPException
with status code 400. This will be used later when the provided URL isn’t valid. Later on in the course, you’ll reuse raise_bad_request()
, which is why you put HTTPException
into its own function.
03:31
Here, you define the create_url
endpoint, which expects a string as a POST request body. You define this endpoint as a POST request endpoint with the @app.post
decorator.
03:44
The URL for this endpoint is /url
. While pydantic makes sure the URL is a string, it doesn’t check if the string is a valid URL. That’s the job of the validators
package that you installed at the beginning of the course.
04:01
You check if target_url
is a valid URL. If the provided target_url
isn’t valid, then you call raise_bad_request()
. For now, you are only returning a message to yourself, but later you’ll create a database entry if target_url
is valid.
04:23
You now have functions in main.py
in place that work with the schema you defined in schemas.py
. You can send requests to your API endpoint and get responses in return, but nothing happens under the hood so far.
04:40 In the next section, you’ll breathe some life into your project by connecting your app with a database.
Become a Member to join the conversation.