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