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