#100DaysOfCode in Python Transcripts
Chapter: Days 97-99: Building JSON APIs
Lecture: Implementing the create-user method

Login or purchase this course to watch this video and the rest of the course contents.
0:00 We were getting to a pretty interesting one here. Let's look at our create_user request that we expect to send. We're going to send over a PUT,
0:11 in JSON form, who's body is this JSON object. Now it's pretty simple here, but it could be way more interesting. The moves, or powers,
0:23 could be a list of power one, you know, jumping, running, whatever, hunting. All right, we're not actually doing that.
0:31 But we could send a rich document over, it just happens to be pretty straight forward. Want to do a PUT to that URL. So how do we deal with this,
0:39 how do we get that data? We saw, over here this really cool way of doing this little thing in the URL, and it would come out there,
0:48 but we don't have that here, so what do we do? Let's do a try/except, thing, just for a second. So let's do a little work in here,
1:01 because there's certain things that can go wrong. So what we can do to get to this body, is we can flask.request.json. But it might not be there if,
1:11 there was some sort of invalid input. So we got to test. We'll say if, so, if it's not there, or they didn't supply the user field,
1:27 or they supplied it but it was null, or somehow otherwise empty, any of those cases, this is a problem, so we'll raise an exception.
1:36 Then it will just kick us right down here. Okay, so this is our error handling. Here we go, so let's do that, this is going to be our username,
1:50 and now, what do you do with that? So let's say player = game_service.create_player, and it takes a name. What happens if that already exists?
2:01 It's going to raise an exception that the player already exists, which is another case that's going to kick us down here.
2:07 But if we made it through all these levels of tests here, we'll say return, Whew, okay, so test for this, make sure we get this.
2:17 This is going to do a little bit more testing in here, and then we'll just send this back. However, if there's a problem, we can do this.
2:26 Instead of kicking it over to our handler, we can be very specific and say we want a flask response, and the response is going to be,
2:44 and the status we'll set to be 400, which is typically bad request, like these are all bad requests.
2:49 This, it could be some other reason that causes it, but let's blame it on the client this time, how's that?
2:56 All right, you want to try to create the user Michael? Let's, before we do that, let's try to get the user Michael and just see that they do not exist.
3:02 Page was not found, page maybe that's the wrong word, but entity not found. So just create the user Michael. We got this richer object back,
3:13 which gave us the name again. But also when it was created, the id, everything out of the database, and now if we go and we say look for Michael,
3:21 hey there it is. We can run that as many times as we want. 'Course if we try to recreate him again, boom invalid request, player already exists, 400.
3:30 So literally if we go over here and we change the body, don't pass the user field, invalid request, no value for user.
3:37 So all of our error handling is really rocking, rocking on. All right, let's create one more. We'll create a Mark. Boom, now Mark exists as well.
3:48 That's it, so we're going to come in, we're going to try/except, just 'cause there's a lot of conditions here. Work with flask.request.json.
3:55 If it's invalid it can be parsed it would just be none, otherwise we're going to check the values within it, and then we're just going to do the logic,
4:03 and return the newly created player back to the user.

Talk Python's Mastodon Michael Kennedy's Mastodon