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


Talk Python's Mastodon Michael Kennedy's Mastodon