#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.
0:03 Probably the easiest way is actually to go on and run this.
0:06 And grab them off this little description page.
0:07 We're going to do a GET to search for movies.
0:10 Like this. Want to have that here.
0:13 That's something we're going to do.
0:14 And I'll put the others.
0:15 So this time we need to define three additional routes.
0:19 So, let's try it in the methods first.
0:20 And let's just call this search_by_keyword.
0:23 The name and the method is really just for us not for them.
0:26 Request and response, that's what we're going to
0:30 pass in and let's just do this for a moment.
0:34 And search_by_director then movie_by_imdb.
0:39 Great, so these are the things that we're going to build.
0:42 Now we need, see what we want here.
0:45 So, we want /api/search, and we want to
0:47 grab some data out of the route and we want to pass
0:50 it through. Well it turns out this is super nice.
0:52 We say api.route and we give it as a string of this bit
0:57 right here and then that actually gets converted
0:59 to a string and passed automatically.
1:01 So we just say that's a string right there.
1:03 And that's it. That's easy, right?
1:05 Now, how do we show or return some sort of json data?
1:09 Well, it's going to be pretty easy.
1:11 We'll say response and you're going to say the media
1:13 is equal to a dictionary. So let's just say 'searching'.
1:18 I'll just put the keyword here. So just have it echo it back.
1:20 Notice there's no return value.
1:21 This is mutable, these requests here or responses.
1:26 And you just set values like headers and media and
1:28 content and so on.
1:29 So we should be able to call this already.
1:30 Let's try. Go over and click this.
1:32 Look at that. Searching for run.
1:34 And if you actually view the raw data
1:36 that's the json value that was turned back.
1:38 Perfect! So this is pretty easy.
1:40 Let's do the other two.
1:42 The route for this one is going to be
1:43 api/director/{director_name}. So @api.route.
1:47 Put this into a string.
1:49 This is a variable as a string.
1:51 And so we get better intellisense or autocomplete
1:54 let's go ahead and decorate out our response object as well.
1:58 Okay and then this one is going to be somewhere.
2:01 We're going to pass in the variables from the route and
2:03 that's always going to be a string.
2:04 It'll clean up and it looks like we're pretty much
2:06 ready to go. We're going to do some kind of response.
2:09 Let's just echo back the values here.
2:11 We're going to have director and this one will have
2:13 imdb_number and let's just tell it that's spelled
2:16 correctly.
2:17 Alright, we can test the other two real quick now.
2:19 Searching for run, great.
2:21 Now we're searching for Cameron by the director bit.
2:25 And here we're searching by the IMDB code for api/movie.
2:28 Perfect!
2:29 So we have our stubs pretty much built.
2:31 And to be honest, this is kind of it for our API.
2:34 What's left?
2:35 Well what we have to do is we have to implement some
2:38 kind of data backend and some kind of searching
2:41 mechanism to actually search for the keyword or
2:44 the director or whatever.
2:45 It turns out that's pretty much pre-baked.
2:47 So we're going to do a little bit of work to fill out
2:49 these methods and then we'll be done.