Managing URLs
00:00 Managing URLs. In the last section of the course, you cleaned up your code by creating new files and functions. Now you’ll build upon these improvements. At the end of this part of the course, you’ll be able to manage your URLs by accessing secured endpoints of your API.
00:20
When you create a shortened URL, you receive the information in the response body that looks as seen on-screen. Next, you’ll create an admin
endpoint so that you can see this information about your URL later.
00:34
This endpoint will only be accessible to users who know the secret_key
. Start by creating get_db_url_by_secret_key()
in your crud.py
file.
00:59
This function checks the database for an active entry with the provided secret_key
. If a database entry is found, then you return the entry.
01:08
Otherwise, you return None
. You work with the returned data in get_url_info()
in main.py
. First, a new API endpoint at the /admin/{secret_key}
URL is defined. You also give this endpoint the name "administration info"
, making it easier to refer to later.
01:36
As response_model
, you expect a URLInfo
schema.
01:51
After you get the database entry of crud.get_db_url_by_secret_key()
, you assign it to db_url
and check it right away. Here, you are using an assignment expression for the if
statement of this line.
02:11
Do you have the feeling that these two lines look familiar? Those are exactly the same lines of code that you wrote in create_url()
, so you have a chance for a refactoring session in the same file.
02:28
In get_admin_info()
, you’ll go one step further than just receiving the .url
and .admin_url
attributes. You’ll leverage the URL
class and the starlette
package that comes with FastAPI, so that is imported first.
02:50
You’ll also need to import get_settings
from config
, as you’ll need it to use the value of base_url
.
03:12
base_url
is created by passing in base_url
from the settings to the URL
class. After this, the URLs are created by using the .replace()
method to construct the full URLs.
03:35
Previously, you only returned key
and secret_key
on their own. If you wanted to visit one of the endpoints, then you had to add it to your base URL yourself.
03:47
URL app is now much more user-friendly, as URLInfo
returns the complete URLs for both the forwarding url
and the admin_url
.
03:56
With this function in place, you can update both create_url()
and get_url_info()
. Remove the lines where you set the .url
and .admin_url
attributes.
04:11
Instead of returning db_url
, you now return the URLInfo
schema from get_admin_info()
. Make the same changes to get_url_info()
, as seen on-screen.
04:40
You cleaned up create_url()
, and you have an endpoint to see information about your URL. Try it out in the browser.
05:04 In the response, you now receive the complete URLs of your forwarding URL and the administration URL. This looks good so far. You may have shared the shortened URL in your network.
05:19
But although the URL has been clicked multiple times, the clicks
value is still 0
. In the next section of the course, you’ll implement the functionality to see how often your URL has been visited.
Become a Member to join the conversation.