#100DaysOfWeb in Python Transcripts
Chapter: Day 50: Responder framework
Lecture: API view methods
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
The next thing to do is to define the API routes. Probably the easiest way is actually to go on and run this.
0:07
And grab them off this little description page. We're going to do a GET to search for movies. Like this. Want to have that here.
0:14
That's something we're going to do. And I'll put the others. So this time we need to define three additional routes.
0:20
So, let's try it in the methods first. And let's just call this search_by_keyword. The name and the method is really just for us not for them.
0:27
Request and response, that's what we're going to pass in and let's just do this for a moment. And search_by_director then movie_by_imdb.
0:40
Great, so these are the things that we're going to build. Now we need, see what we want here. So, we want /api/search, and we want to
0:48
grab some data out of the route and we want to pass it through. Well it turns out this is super nice.
0:53
We say api.route and we give it as a string of this bit right here and then that actually gets converted to a string and passed automatically.
1:02
So we just say that's a string right there. And that's it. That's easy, right? Now, how do we show or return some sort of json data?
1:10
Well, it's going to be pretty easy. We'll say response and you're going to say the media is equal to a dictionary. So let's just say 'searching'.
1:19
I'll just put the keyword here. So just have it echo it back. Notice there's no return value. This is mutable, these requests here or responses.
1:27
And you just set values like headers and media and content and so on. So we should be able to call this already. Let's try. Go over and click this.
1:33
Look at that. Searching for run. And if you actually view the raw data that's the json value that was turned back. Perfect! So this is pretty easy.
1:41
Let's do the other two. The route for this one is going to be api/director/{director_name}. So @api.route. Put this into a string.
1:50
This is a variable as a string. And so we get better intellisense or autocomplete let's go ahead and decorate out our response object as well.
1:59
Okay and then this one is going to be somewhere. We're going to pass in the variables from the route and that's always going to be a string.
2:05
It'll clean up and it looks like we're pretty much ready to go. We're going to do some kind of response. Let's just echo back the values here.
2:12
We're going to have director and this one will have imdb_number and let's just tell it that's spelled correctly.
2:18
Alright, we can test the other two real quick now. Searching for run, great. Now we're searching for Cameron by the director bit.
2:26
And here we're searching by the IMDB code for api/movie. Perfect! So we have our stubs pretty much built.
2:32
And to be honest, this is kind of it for our API. What's left? Well what we have to do is we have to implement some
2:39
kind of data backend and some kind of searching mechanism to actually search for the keyword or the director or whatever.
2:46
It turns out that's pretty much pre-baked. So we're going to do a little bit of work to fill out these methods and then we'll be done.