#100DaysOfCode in Python Transcripts
Chapter: Days 55-57: Structured API clients with uplink
Lecture: Demo: Writing a new post

Login or purchase this course to watch this video and the rest of the course contents.
0:00 If we look at our service,
0:01 it tell us the way to create a post
0:04 is to add HTTP POST against that URL.
0:07 And what we need to do in our post,
0:10 is basically pass this information along.
0:13 A title, a content, a view count, and a publisher.
0:16 We don't need the id that's generated by the database.
0:19 We need to somehow do that, and it starts out looking pretty
0:25 obvious, but then it's not so obvious it's easy,
0:27 it's just not obvious.
0:29 So we know that we want to do HTTP POST against
0:34 /api/blog, and this will be create new entry.
0:40 And it's going to have some stuff that goes here.
0:43 This'll be create a new post, but what well it turns out
0:50 right at this level, I don't love the way that this works,
0:54 I would like to see something different, but here's how it
0:57 works. Basically you pass
0:59 **kwargs, you pass arbitrary keyword arguments.
1:02 And then what you say is this an uplink.body
1:06 So this document maps to the body.
1:08 Now this is actually not going to work quite at first,
1:11 it's going to give us the funky error, the fix is super easy
1:15 I want you to see me run into the error,
1:17 so that you can get around it.
1:20 It really depends on what the service is expecting, this
1:23 service expects a JSON body, if this was like a form
1:26 post type of thing, then what we actually have is perfect.
1:29 So let's go and try to use this.
1:33 I'll just pay some code that gets the input from the user.
1:36 Okay so, here's what we're going to do, we're going to ask
1:38 for the title, the body, and the view count from the user
1:42 again no error handling on the view count,
1:44 but you should add that.
1:45 And then we're just going to say it's published right now,
1:47 when you right it.
1:48 You can't change that.
1:49 And so we're going to call create new entry
1:51 and we're going to use keyword arguments here.
1:53 Now PyCharm is being a little more, helpful.
1:57 Basically the way they're using type annotations to
2:00 structure the behavior is conflicting with the actual
2:03 type checking that PyCharm is doing here.
2:06 So I'm not sure PyCharm really has this right, I'm not
2:09 sure who I should blame for this little funkiness here.
2:11 But let's just tell it chill out for a minute, so we can
2:14 suppress that for this statement and it will be okay.
2:16 Now this should fail with a 400 bad response, but it's
2:20 not obvious why.
2:22 Let's go and run that, and now notice there's
2:23 a bunch of these that are starting to pile up.
2:26 Let's close them off, and tell this to run a single instance
2:30 so it restarts.
2:31 Alright let's try this, I'm going try to right a post,
2:34 this is going to be just created this, and the contents are
2:39 going to be a new post, it's viewed 71 times, and boom 400.
2:43 Bad request. Why?
2:46 Well it's not at all obvious why,
2:49 you'd have to somehow look at the response from the server
2:52 or something to that effect maybe the server
2:54 could give you a better answer.
2:56 Could not parse the JSON body you sent,
2:58 it looks like this but I expected something else,
3:00 this service doesn't give us enough
3:02 information really to know.
3:03 But I'll tell you what is happening is,
3:05 it's actually doing form encoded post, rather than a JSON
3:09 post.
3:10 So we need to tell this little client,
3:13 hey when you post stuff to this server do that in JSON form
3:16 not standard web form.
3:17 So we can just say a uplink.json.
3:19 So now this applies to every method anytime it sees the body
3:23 that means JSON when we say that.
3:25 Let's run it again and this should work,
3:27 and let's do a quick read just to see.
3:29 Notice there are 4, let's do one,
3:31 now let's write a new post, this was done live,
3:36 and it should come into existence.
3:38 Should be fun to see it appear now, and it's very popular.
3:43 Boom, look at that it's created.
3:45 Well our program said it's created,
3:47 has an id that means is like probably did,
3:50 but let's just hit list again.
3:51 Look at that, number one this was done live,
3:54 and let's actually view the details of it.
3:56 This was done live, right now you can see
3:59 when it was recorded, and it should be fine
4:00 and appears right now, this is totally cool.
4:02 Let's do one little quick fix, up here let's put a comma.
4:05 Digit grouping in there, so over here
4:10 we can say colon comma, that will do Digit grouping.
4:14 Now we can read them, 1000 view perfect this was done live.
4:19 To review we figured out what the URL
4:23 is that we're going to work with.
4:24 For various API end points, we created functions
4:28 that map that information to behaviors we're doing here.
4:32 And then we just call them, and we don't even implement
4:35 anything, look there's literally no implementation to any of
4:38 these methods.
4:39 They're just documented which is pretty wild actually.