#100DaysOfCode in Python Transcripts
Chapter: Days 97-99: Building JSON APIs
Lecture: Defining the API methods in Flask
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
Now, we're going to do something that makes
0:02
my skin crawl a little bit.
0:03
We're going to write a ton of code into this one,
0:06
ginormous, what's going to become
0:07
a ginormous single file web app, yuk.
0:11
Afterwards, I'll refactor it in a way
0:14
that I think is much cleaner and simpler.
0:17
If you don't like the refactored one I do,
0:19
you can just stop with what we're going to do
0:21
this time around and leave it organized this way.
0:23
I think you'll find it to be a lot nicer.
0:25
Okay, so what is the goal of this particular video?
0:29
We're going to go in here, and we're going to define
0:32
7 API methods, the 7 that we talked about
0:35
at the beginning, and recall down here,
0:37
we saw how to do this.
0:38
We have some kind of route, some kind of HTTP verb.
0:43
These are very important in RESTful services.
0:45
We get the data; this, we're going to get
0:47
from the database in the end.
0:49
And, we're going to return just the JSON response
0:54
of that dictionary.
0:57
So, just to keep things movin' along,
0:59
I'm going to go down here, and I'm going to,
1:01
let's put these at the very bottom.
1:05
I'm going to paste in some stuff, and we'll talk about it.
1:10
These are not implementations.
1:12
These are just getting started.
1:13
So, the first one is going to be: find a particular user.
1:17
So, we want to have api/game/users.
1:21
And then, you'll put the name here.
1:23
So, api/game/users/{name}, /computer /jeff or /jennifer.
1:29
You say, whatever you want, there at the end,
1:30
and that becomes this argument, right here.
1:34
So, let's just do something like: return would find,
1:40
just to make sure everything's working.
1:42
We'll see.
1:44
So, we still got to put the details of what that means.
1:46
It turns out this one's quite simple.
1:48
Some are really easy, some, not so much.
1:50
So, what is the next one?
1:53
So, down here, we're going to create a user.
1:56
And, we're going to do an HTTP PUT to the user's collection.
2:00
Why?
2:01
Well, there's only two reasonable options, here.
2:03
GET, get is out.
2:05
Delete is obviously out.
2:06
PATCH, obviously, they're out.
2:07
But, POST or PUT, and if you think you can do the operation
2:12
again and again and it shouldn't affect the state
2:14
of the system, well then,
2:15
PUT is probably what you want to do
2:17
if you know where it goes.
2:18
So, it's kind of, we're creating the /user,
2:21
whatever we paste in here.
2:23
So, this is going to create a user,
2:24
and it's going to be interesting.
2:25
I'll just say, we'd create_user.
2:26
It's going to be interesting how we get to that data,
2:29
and that's not too hard.
2:32
Next, we're going to create a new game.
2:37
So, over here, we have game.
2:38
And then, you're going to just do a POST to game.
2:40
You don't know where the ID or the link
2:43
of that game is going to go.
2:44
So, we're using POST as opposed to here,
2:47
we know the name of the user we're creating,
2:48
which drives the URL.
2:49
That's the reason PUT.
2:50
You don't have to be this nuanced with the rest,
2:52
but this is the way I'm doin' it.
2:55
Next one up, we're just going to be able to get all the rules.
2:57
Remember, we want to be able to change the rules,
2:59
potentially, in the server and have all the clients
3:01
immediately adapt.
3:03
And, this is a read operation, so we're going to do a GET,
3:06
very, very straightforward.
3:08
We'll implement that momentarily.
3:11
Next step, we want to get the game status.
3:13
This is a read operation, so it's GET.
3:15
We have api/game, and then here, in this part,
3:18
we're going to do the game ID.
3:20
So, API/gameS223/status.
3:25
And, that's going to be passed in here,
3:27
and then we'll just say, almost there, with our methods.
3:32
So, the next thing we want to do is just get the top scores.
3:34
Again, this is a read operation.
3:36
So, we'll just return.
3:39
And finally, comes our most complicated
3:41
but also most important method.
3:44
Those often go together, don't they?
3:45
Called play_round.
3:48
And, because this is modifying, it's doing a POST.
3:51
That's not where it goes, so this one is,
3:53
it's ready to POST.
3:55
Okay, so we have all of these methods, here,
3:57
if I can get my little green helper thing
3:59
to get out of the way.
4:00
You notice that somewhere along the way,
4:03
it was detecting changes, and it said,
4:05
"A view mapping over ID.
4:06
"Existing end point: find_user."
4:11
That just happened 'cause I duplicated something
4:13
before I typed it in.
4:14
That happens, sometimes, when Flask gets into bug mode,
4:16
is you can be messin' with it, and if you put, like,
4:18
junk and you accidentally save it, it tries to reload.
4:21
But then, it detects some kind of error,
4:23
and if I actually take that away and save again,
4:26
it's, the process is gone, got to restart it.
4:30
Okay, so let's just test one of these:
4:39
would find user jeff.
4:40
How cool, huh?
4:41
And, it's workin' pretty well.
4:43
This works pretty well because this is a get operation,
4:45
but how do you test this users by doing a PUT operation
4:50
in the browser? Now, it's super easy.
4:51
So, we're going to employ Postman to configure
4:54
all this stuff for us.