#100DaysOfCode in Python Transcripts
Chapter: Days 55-57: Structured API clients with uplink
Lecture: Demo: Better wrappers and helpers

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Now when we called service.create new entry, let's just type it again. And we saw Python kind of freaked out, and what could we put in here?
0:09 Well, this **kwargs just means, you can put anything and so much for the help here. You're just kind of on your own.
0:16 You have to know this is exactly how it goes, but it doesn't have to be this way. Let's go over here and fix this.
0:22 Let's rename this to double underscore, under here it will say create new underscores. A, no that's fine. And then we'll define, __create_new_entry.
0:33 So why the double underscore? This hides it from being used from the outside. When we say service., we'll never see this function. Right.
0:41 Almost private, almost. So here we can just say return self.__create_new_entry and then we need to put some stuff in here.
0:52 Let's go borrow this real quick. And up here, we don't have to have this unpleasant **kwargs. We can have real values like title, which is a string,
1:02 content which is a string, view count. Let's just call them views, which isn't it? And published which is a string.
1:16 And we could even put some values like this. I'll say if published is None, then we'll go and set it to be the current value right here, like that.
1:32 Of course we're going to have to import that here, and this was views and so on. Now this one still is kind of doing this
1:39 free count thing so we can suppress it internally, but nobody outside of here will have to know. You could specify tighter on content
1:47 or if you want to override the views, you can do so here and then this is actually going to go to the service.
1:53 And we might as well go ahead and tell it. Then it also returns this. Okay, so now if we come back over here, let's write this again.
2:02 So svc.__create_new_entry, oh look, did we get any help with visitor? Of course we do. We put title, we put content, we could put the view count.
2:11 I'll go ahead and do that. We'll leave the published off like this. Don't need that warning. This is much cleaner.
2:18 Now we know how to work with the API. We put reasonable defaults and have it work really well. So let's see what's here. Let's do one more post.
2:26 Let's write it. Of course, there's that lack of error handling. Let's write a post. This will be the final post. It was Final Frontier and say 7,001.
2:41 Okay, created just like before. If we go and read them, it should be there. You want to see details about it, it's exactly what we wrote.
2:46 But we have this much nicer way to work with it. Okay so that's uplink. If you're going to work with this well
2:53 structured API, it really lets you control the way you interact with it. Let's just look over here really quickly at a few more variations.
3:01 We talked about how you can pass at the query strings. You can do things like specify the headers. You can pass in an authorization,
3:10 which then gets stored in the header. You could even run synchronous and asynchronous versions. Okay, so there's a lot more to go,
3:18 but I think we're going to leave that exploration up to you.


Talk Python's Mastodon Michael Kennedy's Mastodon