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