#100DaysOfCode in Python Transcripts
Chapter: Days 97-99: Building JSON APIs
Lecture: Your turn: Day 1

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Let's review just a few of the core concepts
0:02 that are new for this set of days.
0:06 We interacted with a lot of powerful, and honestly,
0:09 a little bit tricky bit of code, with SQLAlchemy,
0:12 and some of the other stuff, configuring Flask.
0:16 The stuff that was new was using these Flask view methods
0:20 as api methods, so, how do we do that?
0:23 Well we start out by defining the route,
0:25 this is standard Flask stuff.
0:27 We particularly focused on the methods
0:29 because this really strongly drives how RESTful API's work.
0:33 some of them are GET, some of them are POST, and so on.
0:35 So we set the methods there.
0:38 Then we'd write our standard implementation,
0:41 nothing really to do with Flask at all in this one.
0:44 We just go to the data base, convert our wins
0:47 into something that we'll be able to send back.
0:49 So, dictionaries, or dictionaries
0:51 of dictionaries, things like that.
0:53 Here's a list where each item is first, the player,
0:56 which is the dictionary and the scorer which is a number.
0:59 Okay. And then once we have our data to send back,
1:02 we just call, return flask.jsonify, the data we wanted.
1:06 Here we grab the top ten wins
1:08 sorted by the most winning players.
1:12 So this is super simple and super straightforward
1:14 for these GET methods.
1:16 The other type that we worked with was the POST.
1:19 Now this one's a little bit more interesting
1:22 because typically there is some kind
1:24 of body being sent to us.
1:26 A JSON body with lots of data in it.
1:29 We're going to come in here, have a route again.
1:31 This method is set to POST this time.
1:34 But now we're going to check
1:35 that the body was understood as JSON,
1:38 and if it's not, we're going to send a 400
1:40 which is a bad request.
1:44 You saw in the code that you can actually abort
1:46 within a Flask response, which is richer.
1:49 You could give 'em a description of what went wrong
1:51 and things like that.
1:53 Then we have to get the data.
1:55 Once we know that we have a JSON body,
1:57 we have to get the data out of it.
1:59 So here we're pulling the game id,
2:01 but in our play round example,
2:03 there's actually a bunch of stuff
2:04 we had to pull out and validate.
2:05 So a lot of validation, what ended up,
2:07 we actually moved that to a separate method,
2:10 and in the very end we just, again, returned
2:12 some jsonify result of whatever we wanted to tell them
2:16 We created, or was the outcome of this post.
2:19 So that's it, you also have PUT and DELETE,
2:21 but you can imagine they're really quite similar.
2:24 So you should be able to implement those.
2:26 It's just more of a conceptual thing for your API
2:29 than it is actually different in code.
2:32 So you now know how to make these APIs in Flask.
2:35 Hope you enjoy it, there's really cool
2:37 stuff you can do with it.