Modern APIs with FastAPI and Python Transcripts
Chapter: Accepting inbound data
Lecture: Viewing all reports via the API
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
Well, we've written a way to get all the reports to get added to the
0:03
system. Let's go write
0:04
an API over here that will do that.
0:07
So let me just borrow a little bit from there.
0:10
What we're gonna do is gonna be "API
0:11
slash reports". Now, remember at the beginning,
0:15
we spoke about knowing your http verbs and what that means.
0:19
This becomes really relevant here. So what we're gonna do is I'm gonna call this
0:23
"reports" say get. Now If I go look in the documentation,
0:27
you'll see that this API is registered under "reports_get"
0:31
and that's not really how we want it.
0:33
Maybe we just call it all reports.
0:34
But I'll do it like this so you can see a feature here.
0:38
We can come over here and set the name to just be "all reports",
0:41
and that will actually control what shows up if you go looking like auto documentation and
0:46
whatnot. So rather than just saying "reports",
0:49
it'll say "all reports" or something.
0:51
So that allows you to control the name separately from what the external world sees.
0:56
Alright, so in order to write this code,
0:58
it turns out to be pretty simple.
1:00
There's nothing, you know, we're not asking for the location and reports near me,
1:03
this is a simple version. All we're going to do is return a list of
1:07
report, right? We don't have to put that type information there for FastAPI
1:10
but it can help us.
1:12
And how do we do this?
1:13
Well, we say "report service", and that we got it import, like that. And then
1:19
we'll just say "get reports".
1:21
Return that. Now this might look like it's gonna be OK,
1:24
but notice PyCharm is like,
1:25
"Yeah, not so much". You're returning a co-routine.
1:29
Remember, this is asynchronous.
1:31
So to get the actual value,
1:33
we have to await it. So we'll say await, like that. Now if we
1:37
await the co-routine, what we get back is a list of reports.
1:41
Let's go and just add,
1:42
really quick, a few add report really quick.
1:46
We'll add A and B and none. Put none
1:50
there for, that should be fine.
1:52
The type checking is complaining that it can't be none but should work.
1:58
If we go visit "API slash reports",
2:01
let's see what we get. There's that and nothing.
2:05
What's going on here? What have we missed?
2:08
What have we missed? Oh,
2:09
I think that's not what it's saying.
2:11
It's saying that we're not awaiting that call and the type checking is wrong.
2:16
That was important. Okay, try again.
2:19
Ah, error, Error, error.
2:20
None Value. Pydantic is good and checking this for us,
2:24
but, ok try one more time.
2:27
Alright. Location. We need city,
2:30
and we'll just say city equals Portland,
2:34
city equals NYC.
2:36
Alright. That should be enough to just add a little fake data and see it
2:39
come back. Yes, we have it.
2:41
Look at this. This is cool.
2:42
So we pretty print this. Got a list of reports.
2:46
Each report has a description, a created date and an embedded location.
2:52
Sweet, sweet, sweet. You see?
2:53
Yes. New York City and Portland.
2:55
And right now that is the date and time.
2:57
Cool, huh? So I just added this we could get actual data back.
3:01
We don't really want that there.
3:04
That's all we got to do to add this is we're gonna add another
3:06
get, rename it so it has better meaning,
3:09
but you'll see this is the, this is the URL, so we're going to do
3:11
a get against reports. That's why I gave it that name.
3:15
And then we just return a list of these right out of our fake async data server.