#100DaysOfCode in Python Transcripts
Chapter: Days 97-99: Building JSON APIs
Lecture: Implementing the client
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
Okay, let's come over here and close the story on the web and go to our client. And we're going to use Uplink,
0:07
so we're going to put that in our requirements on txt and let's add a game app, call it game_app. That's the thing that you're going to play.
0:16
And let's separate our API definition from our UI, which is kind of this. So let's do a quick main. Do a little bit of skeleton code here,
0:32
get that running. Okay, this is our little game_app. Let's add another one called api. And this over here is where we're going to use Uplink.
0:41
So we're going to import uplink and we have not installed those have we? So let's go over here to client and run the same code.
0:54
There PyCharm is less unhappy, let's say. So what we're going to do is we're going to define a class over here using Uplink
1:01
and remember the way it goes, this is going to be a GameService and the way it works is it's going to be uplink.consumer
1:10
and then we just define methods. Like def find_user. We put in the user name and then down here we just have nothing but we put in this at uplink.get
1:22
and then it's something like API game users username. Now they use curly braces which I prefer over here, where as Flask uses angle brackets
1:32
but whatever it's all the same. The other little trick that we'll have to do here is to define an init and set the base URL to wherever it is.
1:44
Something like 5000. Let's add another one here. This time for all rolls, remember that's doing a get against api/games/rolls.
1:54
It gets more interesting, code word for complicated. If we're going to do a create game, we're going to do a post up to API games game games
2:03
and we're going to pass a body of just a dictionary as a body. But let's just go over here and let's just see if we can get this
2:10
all rolls thing to work for a second. You've got to import that and we'll just say all_rolls. Now remember what we get back here
2:19
is we're going to get a response. It's going to be okay but not amazing. 200 response, 200 that's already good right?
2:27
And if that's true we could do a JSON here like this and we get boom, look at that. It's already working, that is so super cool.