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

Talk Python's Mastodon Michael Kennedy's Mastodon