Responder Web Framework Mini-course Transcripts
Chapter: Building a RESTful API with Responder
Lecture: Implementing the search APIs

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Let's implement search by keyword here.
0:02 We saw that it's pretty easy if we have our database.
0:05 We just got to make sure everything is
0:06 a dictionary, and off it goes.
0:07 One other item I want to be careful about here
0:10 is I want to make sure we don't have too many responses.
0:13 Right? If you search for the letter A
0:15 you might get basically the database back.
0:17 So up here I want to set some kind of max response count
0:20 like to 10 or something along those lines.
0:23 So a little bit of a complicating detail
0:25 but at the same time you don't want to
0:26 dump your database back if they search for nothing
0:29 or space or, you know, some weird thing like that.
0:32 Alright, so let's say we're going to get our movies
0:33 and again we're going to go back to our database
0:35 and say search by keyword this time.
0:38 And the keyword is already coming in.
0:40 That's cool, right?
0:41 So right away we get a list of movies back
0:43 from the database, and then we'll say limited.
0:47 We want to know and report to the user
0:48 whether or not there's more responses.
0:51 So we'll say is the len of movies greater
0:53 than the max response count there.
0:56 I'll say, if it is limited
0:58 like there's too many movies here
0:59 what we're going to do is we're going to trim it down.
1:01 So we'll set the movies to just the first 10 using slicing.
1:06 Remember, this is just in memory.
1:07 It's kind of silly, but it works.
1:09 Now, what happens if we try to return a movie?
1:11 Well, we already saw down here is goes
1:13 this is not JSON serializable. It's not going to work.
1:16 So we have to do a little bit of magice right here.
1:19 We'll say movie dicts, because this is the list
1:22 but the things in the list are not serializable.
1:24 So we're going to go and create a new list where the items in
1:27 the list are, so we'll say db.movie_to_dict(m)
1:31 for m in movies, and now here we're going to just return
1:35 our movie dict along with a few other things.
1:38 So we want to say what the keyword was, we'll say hits
1:41 that's what it says in the other api so it's going to say here
1:43 it's going to be movie dict and then truncated results is
1:49 going to be indicated by whether it was limited yes or no.
1:52 Alright so here's our search and we can even do a print
1:55 statement searching, we'll do a print, get the results out
2:00 scale one more time, click over here on search.
2:02 Hmm, look at that!
2:04 Our search for run returned The Rundown, The Runaway Bride
2:08 Run All Night, Chicken Run, all sorts of stuff.
2:11 Cool, so what if we search for Superman.
2:15 Boom, Batman versus Superman, Superman Returns, just straight
2:18 up Superman, Superman 2
2:20 Yeah, so it's working really well, it's pretty straight forward right.
2:23 We're going to do something super super similar with the other one.
2:26 So I'm just going to copy this down here, and I'm going to use
2:30 director name and, instead of searching by keyword
2:33 we'll search by director.
2:34 A little database thing, knows all about that.
2:36 So director will pass along the director name and
2:40 the keyword is going to be director name.
2:44 Alright, let's run it again.
2:45 This time if we click, lets clean this up a little.
2:47 If we click right here on director, it shows us just
2:51 Cameron, Avatar, Titanic and so on.
2:54 If we search for someone else like Berg, Peter Berg.
2:59 We get Battleship, Indiana Jones all these. Great right.
3:03 Hancock, so we can search by whatever we want
3:06 Cameron, here we go.
3:07 So I think that's it, we have them all implemented
3:10 lets just double check, keyword, seems like that works.
3:13 Director, seems like that works.
3:15 And IMDB code where we get one back, well that one's been
3:18 working for a while.
3:19 It was pretty quick right and look, this entire thing is
3:22 implemented in PEP 8 style, 58 lines plus the static HTML
3:27 and CSS, and I guess maybe the db as well.
3:31 But the API side of thing is really really quite simple
3:34 one thing you might want to do, like in PyCharm saying
3:37 this is not used in python, a way to say, I have to pass
3:41 a thing here but I don't want to use it, is put an _
3:45 So that might make you feel better if you want
3:46 to put underscores, but then you also maybe don't remember
3:49 what that's for.
3:50 So it's up to you, but I wouldn't put those there so it
3:52 doesn't look like they're errors.
3:53 Here we go, so it's totally working.
3:55 Is it totally beautiful?
3:57 No, I hate having all this stuff crammed in this one file
4:00 but these Flask like api's, because they allocate this thing
4:04 and use it in, sort of nudge people down that path.
4:07 We're going to clean this up but I think this is working
4:09 really really well.