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