Brought to you by Michael and Brian - take a Talk Python course or get Brian's pytest book


Transcript #52: Call your APIs with uplink and test them in the tavern

Return to episode page view on github
Recorded on Wednesday, Nov 15, 2017.

00:00 Hello and welcome to Python Bytes, where we deliver Python news and headlines directly to your earbuds.

00:05 This is episode 52, recorded November 15th, 2017. I'm Michael Kennedy.

00:11 And I'm Brian Okken.

00:12 And we got some awesome news for you. But hey, Brian, just want to say happy birthday, man.

00:16 Oh, yeah. Happy birthday.

00:17 Yeah. So this is the 52nd episode of Python Bytes. And if I recall, I don't think we skipped.

00:22 I'm pretty sure we did not skip a single episode. We shipped the entire first year.

00:28 We shipped an episode every week. And I think that's pretty awesome.

00:30 That is cool. Even around Christmas and stuff?

00:32 Yeah. I think we somehow recorded ahead. Or maybe we missed one. But I think we did it.

00:37 Okay.

00:37 So pretty exciting. And I just want to say thank you to everybody out there who listens to the show on a weekly basis.

00:42 That's why we do it. We do it for you guys. And we wouldn't do it if you weren't interested and excited.

00:46 So thank you for appreciating this, giving us all the ideas and keeping us going.

00:50 Yeah, definitely. Thank you. And we had a whole bunch backed up. So this episode is two hours long and it has 52 topics.

00:56 That's right. So you guys, just pause it now, get a coffee, settle in, or whiskey if it's late.

01:01 No. Actually, we're going to keep to the same format. All right. Cool.

01:05 So before we get to our topics, though, I just want to say thank you again to Digital Ocean as well.

01:09 They're another reason that this show is going strong.

01:13 Digital Ocean, Rollbar, and a few of the other folks that continuously support this show.

01:18 Thank you. Thank you.

01:19 They just launched Spaces. Check it out at do.co slash Python.

01:23 Tell you more about it later. Right now, I want to spend a while on APIs, Brian.

01:27 Yeah, I've got APIs on the brain right now. So we'll start with a new project. I don't know if it's new, actually. A project called Tavern.

01:35 It's like a drinking game or something?

01:38 No, it's testing RESTful APIs. So I don't know why it's called Tavern. I'll have to ask him about that.

01:45 It's a really cool project, though. I checked it out. I like it a lot, actually.

01:48 Yeah, so it's at tavern testing dot GitHub dot IO. Like I said, it's a RESTful API testing. And what it reminds me of the most is PyREST test, because it uses a YAML format to describe the tests and describe what sequences to go through.

02:06 So it does have like one off tests where you could just either post or gets from a URL and then determines whether or not. And you can specify what you want out of it.

02:17 But you can also do sequences. And one of my favorite things about this is it comes with a pytest plugin and they say it works best with integrated with pytest.

02:27 That's really awesome. So you basically describe, I want you to call this URL. It's going to be a get and you expect to get kind of this thing back and then you can just assert against it. Is that how it works?

02:37 With the YAML syntax, you don't even have to specify asserts. You just specify what you expect to get back. And it just automatically tests for all that.

02:47 For things like this, I actually I really like, even though it takes up a lot of space, the YAML takes a lot more space than like a little test function.

02:55 But it's very readable, especially if you have if you have an editor that colorizes your YAML files. And I think it's good for especially you can discuss it with non programmer people. So that's one of the benefits of that.

03:09 OK, yeah, that's really cool. Yeah, that's true. You can give a YAML file to a non technical person who is like sort of a requirements gatherer, business analyst or something or domain expert.

03:19 They can say, yeah, OK, these are the things that let's slot in here. Right. Or to the person who built the API.

03:24 Like, for instance, the sequences, you can say, OK, here's the kind of the test is called this. And this is sort of the sequence we're going to go through.

03:30 First, you log in and then you have to do this. And there is also with a lot of sequences, you have to collect get information like tokens or something from from the server.

03:42 And Tavern allows you to save those tokens as variable names to use later in later tests, which is nice.

03:50 Yeah, that's really sweet. That's Tavern. Tavern sounds really cool. Definitely worth checking out.

03:53 The thing I want to talk about is not for testing APIs, but consuming APIs, calling APIs.

03:59 What if you weren't looking at this document or sharing, just like thinking of like, hey, I'm going to call an API from Python.

04:05 What library do you think you would use? Oh, requests.

04:08 Obviously, everyone uses requests and requests is one of the absolute most popular libraries since downloaded an insane number of times.

04:14 What I find myself doing a lot when I know this is like a proper API I'm going to consume.

04:20 It's part of an application or I'm going to fold it in and make it really important is I'll create like a class or some module that will model all the actions that you take against that API.

04:30 Right. Like log in or, you know, get courses or whatever your API is about.

04:36 Right. And you sort of put use requests to implement it.

04:39 But deep down, you know, you kind of bury requests and hopefully you've got some like facade sort of class or module in front of it.

04:47 So I want to talk about this sort of up and coming project that does that all at once for you, which is really sweet.

04:55 And it uses it with decorators called Uplink. Have you heard of Uplink?

04:59 Not until you listed it today, but it looks really cool.

05:02 It's super cool. Right. So let me just describe like real quick how you use this.

05:04 So imagine I want to call the GitHub API.

05:06 I need to have a header on all my requests that says I'm using this particular format or schema for my JSON.

05:13 I'm going to call the get users function.

05:16 I might update a user and so on.

05:18 So what I do is I create a class, call it whatever you want, derived from a certain base class that comes from Uplink.

05:23 I had to add headers decorator to the class.

05:26 I say accept the right funky content type.

05:29 And that just applies to all the functions you call on this class.

05:32 If I want to get the users, I'd say create a function called get user.

05:36 And I'd say at get slash users slash curly username.

05:41 And that curly username there maps to the argument.

05:44 So when I call it, I say GitHub dot get user.

05:46 Yeah, Mike C. Kennedy is my username there.

05:49 And it actually directly pulls that into the little URL in the decorator and passes it.

05:54 Oh, this is cool.

05:55 That is cool, right?

05:55 And they have another example for updating a user.

05:58 That's a patch call.

05:59 So you say at JSON at patch.

06:01 And then the arguments to the method, you can pass in just like a body of basically KW args.

06:09 And that becomes the body of the patch submission.

06:12 You can also say access token colon query and use the type decorator in Python 3 to decorate as a query.

06:19 So then I'll go question mark access token equals what you pass as that argument.

06:23 This is so smooth.

06:25 Wow.

06:25 I really like it.

06:26 So if I'm building like a super structured API that's got really strict, restful requirements like this, I'm definitely going to check out Uplink.

06:33 I'm going to definitely watch this.

06:34 This is neat.

06:35 They have a little warning in there that says that it's in the early stages.

06:40 But that might be a great way for other people to get involved if they want to help out and push this further.

06:45 Absolutely.

06:46 So that's definitely a warning you want to be careful about.

06:48 They say it's not quite production ready, mostly not because it doesn't work, but because they think the API may change.

06:54 They don't want to break your code.

06:55 So I think that there's an opportunity here.

06:58 There are so many people that say, I really want to get started open source.

07:01 And they look at Django or CPython.

07:03 I'm like, whoa, this is complicated.

07:05 And changing this is really hard.

07:07 Something like this, you could totally contribute to a project like this without getting overwhelmed in the early stages.

07:13 So check it out.

07:13 Yeah, definitely.

07:14 Cool.

07:14 All right.

07:14 So let's switch to a totally different topic and talk about REST and APIs.

07:18 Yeah.

07:20 Yeah.

07:21 So I wanted actually to combine these two things because I ran across them in the same week for one.

07:26 And this was shared by a listener.

07:29 And I'm sorry that I didn't write down the name.

07:31 But yeah, thank you for submitting that.

07:33 That's awesome.

07:33 I saw that coming as well over email.

07:34 There's an article called, I've got it turned around, using JSON schema, which I hadn't heard of before, using JSON schema for REST API endpoint testing.

07:44 And the idea, had you heard of JSON schema before?

07:48 I have heard of JSON schema.

07:50 It's basically a way to say my, it's kind of like what your test does, but at a different level.

07:54 You say, this is what the JSON is supposed to look like.

07:56 This is supposed to be an integer.

07:57 This is supposed to be a string and so on.

07:59 But I haven't ever used it.

08:00 So like, I pretty much have exhausted my knowledge of it now.

08:02 The example of it, they do a Django, which I don't really know Django.

08:07 So I kind of read that anyway, but I don't think that's necessary.

08:10 I think you could use this for anything.

08:12 But the idea is you can implement a schema to describe what your data should look like and then actually serve that within.

08:21 So within your, on your server code, serve that as well.

08:25 And then for your tests, you can grab the schema and then grab, grab whatever data you wanted and validate, use a test to validate that the data you're getting it adheres to the schema.

08:37 And then you can also go out and make sure the values are correct and things like that.

08:41 But actually, I'm just curious what you think of this.

08:44 I think it's pretty cool, actually, especially if the API already has a JSON schema associated with it.

08:50 Right.

08:51 Like if they're like, here's the schema, here's the API, then you could just, okay, and here's how I test.

08:56 You know, one thing that might be interesting is like, it's interesting if you are the maintainer of that thing.

09:01 So that, you know, if the test break that you're verifying, you have to go and update the documentation or something like this.

09:09 But it's also interesting, I think, to point it at APIs you depend upon and say, I'm going to call this and I want to know if the schema changes.

09:18 Because it's totally common that people will document one API, the API will change, your stuff will stop working.

09:25 You're like, but I'm doing what they say.

09:27 It's like, you know, what has happened, right?

09:29 So if you knew the schema of APIs you depended upon changed, this is a good way to do that.

09:34 I think that'd be great.

09:35 Yeah.

09:35 Or you could even, I mean, even if you didn't have a schema provided to you, you could define one for.

09:39 Yeah.

09:40 It's usually not too hard, right?

09:41 Actually, that's a great idea.

09:42 And another thought with that is that it's not just RESTful APIs.

09:46 You can, anything that's using JSON, you can use that to test any API.

09:51 So.

09:51 Yeah, definitely.

09:52 It's very neat.

09:52 So check that out as well.

09:54 All right.

09:55 Before we get on to the next thing, I want to tell you where your audio came from.

09:59 It came from DigitalOcean Spaces.

10:01 So that's right.

10:03 Those guys are sponsoring this episode.

10:04 As I said at the top of the show, check them out at do.co slash Python.

10:08 Get a free two month trial of Spaces.

10:10 And Spaces is object storage and delivery in the cloud.

10:14 You know, things like AWS or Azure Blob Storage.

10:17 Sorry, AWS S3, Azure Blob Storage.

10:20 Things like that.

10:21 But way, way better.

10:22 Better pricing.

10:23 Simpler.

10:24 Things like this.

10:25 So I've been using it for this podcast.

10:28 I just recently, big announcement, switched to using it as the video delivery network for my courses.

10:36 So I'm trying that out on a few courses.

10:37 And that's been super, super smooth as well.

10:40 And what's really interesting, the way that I wrote the API for accessing the video files and stuff was I imported Voto 3.

10:49 That's the S3 AWS API.

10:51 So the API is compatible with S3, like quite literally.

10:54 It's the same API even.

10:56 Just pointed at some different base URL and you're good to go.

11:00 So if you've been using something like S3, you really owe it to yourself to check out DigitalOcean Spaces.

11:04 And do.co slash Python.

11:06 Very cool stuff.

11:07 Yeah, very neat.

11:08 And cool that you tried that out, that the API is compatible.

11:11 So far it's working really well.

11:12 I was thinking that some music could be nice.

11:15 I love to listen to music when I code.

11:16 Do you?

11:17 Yeah, all the time.

11:18 It's funny.

11:18 I find like a little bit of distraction kind of helps keep the mind focused.

11:22 I don't know.

11:22 People are weird that way.

11:23 I work in coffee shops as well.

11:25 And I like that as well.

11:26 But this is a different kind of music to coding.

11:30 So this is almost like music as performance art.

11:34 So there's this presentation called Programming Music for Performance, Live Coding with Fox Dot.

11:40 This is by Ryan Kirkbride at PyCon UK.

11:44 So this is a really short video, but maybe it will inspire some people to do.

11:48 Some similar performances.

11:49 And he's doing some similar performances.

11:49 Basically, he's up there writing code to dramatic, electric, classical type music.

11:57 And it's just, it's really interesting to see it go.

12:00 What did you think of it, Brian?

12:01 I thought it was really interesting, but I'm a little lost.

12:04 So I was hoping you could explain to me what's going on.

12:06 I wasn't at the talk.

12:07 So the video is not that long.

12:09 So I didn't see the introduction.

12:11 But what I think it is, is it's like, I'm going to show you some cool thing by writing a demo live and do it.

12:17 But instead of explaining it to you, I'm going to do it to a dramatic music and make it like a performance art.

12:24 Remember how we talked about code is like poetry a while back?

12:29 This is like code as performance art, I think.

12:31 Yeah, I guess I'll have to check out what all FoxDot is and how that works with that.

12:36 Yeah.

12:37 Yeah.

12:37 There's not, sadly, there's not that much information in this video because it's like partial and it's short.

12:42 But this is from Ian Watt, another listener suggestion.

12:44 And I thought it might inspire some of you guys out there.

12:47 So just, you know, short, have a look at this little video.

12:49 It's cool.

12:49 But be sure to turn on the audio.

12:51 Plus he did a talk without speaking, which is good.

12:53 Exactly.

12:54 We've talked about should you do live coding during your new demos.

12:57 This is like the opposite of a should I do live coding.

12:59 It's like only live coding and there's nothing else.

13:02 There's not even words.

13:02 Yeah.

13:03 Yeah.

13:03 That's good.

13:04 That's awesome.

13:05 But if we had like a weekly Python chat, there'd be words, right?

13:08 There would be words and video and audio.

13:11 All right.

13:11 So tell us about what you got going on this weekly Python chat.

13:14 I saw you were just on it, right?

13:15 Yeah.

13:15 Yeah.

13:16 So it was super fun.

13:18 So weekly Python chat is at, how did you get that?

13:22 It's at weeklypython.chat.

13:25 Nice.

13:25 But it's Trey Hunter.

13:26 And he's a, he's can't remember exactly what he does, but he's part of the Python software

13:32 foundation, but he's also Python instructor.

13:35 And he does quite a bit.

13:36 He's a super nice guy.

13:37 He has these weekly chats where he just picks somebody in the Python community and often requested

13:44 by other people that listen and does like a little, like a under an hour or approximately

13:50 an hour video chat with somebody else.

13:52 But they're also, you can do live coding and then there's people in the chat room asking

13:57 questions while it's going on.

13:59 So it's a live thing, but then it's also recorded so you can watch old ones.

14:03 So yeah, the last one last week on November 9th was a testing Python with pytest.

14:08 So those with me.

14:10 That's awesome.

14:10 And I'm highlighting it because I want more.

14:12 It's really cool.

14:13 It's fun.

14:14 It allows to ask questions of people that they wouldn't, maybe you don't go to conferences

14:19 that much, but you could stay up for a weird hour or what, depending on your, where you

14:24 live in the world, but you can ask questions of people you wouldn't get a chance to otherwise.

14:29 So that's good.

14:30 Yeah.

14:30 Very cool.

14:30 Nice.

14:31 So yeah, check that out.

14:32 We got the link in the show notes.

14:33 So let's run this out with a bunch of mistakes.

14:36 I think that's a good one.

14:37 So our last topic is sort of, I think actually has a mistake in it.

14:41 It's 10 common beginner mistakes in Python.

14:44 So this comes to us from a blog post at a checkio.org or maybe better pull it up pi.checkio.org.

14:50 Have you played with pi.checkio.org?

14:53 It's like a video game for programming.

14:55 I think I have.

14:56 Yeah, I have.

14:56 Yeah.

14:57 Yeah.

14:57 It's funky.

14:57 So you basically, you have these little islands, you got to conquer the islands and you go,

15:01 the way you conquer them is by solving all the puzzles.

15:03 It's a little bit like Myst, but programming.

15:05 One of the things I think is really cool about playing the game actually is you solve some

15:10 little puzzle and then you see how everybody else solved it.

15:13 And then you get to see your style of programming relative to other solutions.

15:17 And it's kind of like code reviews because you can, you can comment on other people's solutions

15:21 and stuff.

15:22 Yeah.

15:22 So it's pretty cool.

15:23 Yeah.

15:23 So these guys wrote a blog post based on the mistakes they see people making from that

15:28 area.

15:28 And they said 10 common beginner mistakes.

15:30 So let's go through real quick.

15:31 Incorrect indentation tabs versus spaces.

15:34 Obvious.

15:35 But you can imagine if you come from Java that you don't know that, right?

15:38 This one's more subtle using a mutable value as a default value.

15:42 So like, you know, append to list and then you give it like a source list equals bracket bracket

15:50 as a default value.

15:51 That is a super bad idea, but not at all obvious why it's bad.

15:55 Right?

15:56 Because every time you call it with, without specifying that argument explicitly, it's going

16:02 to use the same list because that is initialized at like not quite compilation time.

16:08 But as Python sees and determines that method, it finds that default value and sets it.

16:12 It doesn't actually recompute it every call.

16:15 Yeah.

16:15 That's a, that's a fun one.

16:16 Yeah.

16:16 It's definitely fun and tricky.

16:18 Write a lot of comments and doc strings.

16:20 You know, my theory is comments, not so much doc strings, but comments are deodorant for

16:26 code smells and problems.

16:27 So I'm not so sure I'm going to recommend that as much, but documentation, good stuff

16:31 for sure.

16:31 Scoping, you know, if you come from a C based language with curly brace scoping, block scoping,

16:37 Python is different with its functions, scoping and closures and whatnot.

16:41 So that's definitely a mistake to be made.

16:43 One that I really love they covered is called edge cases first.

16:47 And you could have like a loop with a test that does another loop with another test.

16:52 And it could be some super indented thing, or you could do the negative test, the edge

16:58 case that you're going to break out of and then the loop.

17:00 And then you're going to do the edge case you're going to break out of and then the inner

17:03 loop.

17:03 And it's way less indented.

17:04 And, you know, that's one of the Xenopython things, but also just a great design pattern.

17:09 I mean, if you utilize, I see a lot of that when people are used to old style C code or

17:15 something that they don't trust the exception handling.

17:18 Oftentimes you don't have to check for, you don't have to make things bulletproof if the

17:24 function you're calling is going to check it for you anyway.

17:27 Exactly.

17:27 The easier to ask for forgiveness than for permission style is better than the look before

17:31 you leap.

17:32 We got copying.

17:33 Everything is a pointer in Python.

17:35 So the pointers means you may be sharing the same object, not a new one.

17:39 So it talks about that, especially around the lists and data structures.

17:42 Range is half closed.

17:44 Range one to 10 actually is one to nine.

17:46 Wrong capitalization.

17:48 So you're just writing like camel case, Java, C# style, or some, you know, JavaScript

17:54 style of naming for variables, classes, functions, whatever.

17:59 And then finally, using class variables incorrectly.

18:02 This one's a little bit interesting about class level variables and inheritance.

18:07 And you can check that out.

18:08 But they have nice little examples for all of them.

18:09 And as far as I could tell, there's only nine mistakes.

18:12 So I'm not sure what the 10th mistake is.

18:14 But maybe I read it wrong.

18:16 I read it twice.

18:16 I didn't see it.

18:17 So could be tired.

18:18 Well, I mean, if the range is one to nine, if it's...

18:22 Yeah, that's true.

18:22 It could be range, range one to 10 common beginner mistakes in Python.

18:26 Yeah.

18:26 Yeah.

18:26 Perfect.

18:28 All right.

18:28 So anyway, if you're getting started in Python, and you want to kind of level it up a little

18:32 bit, you know, check that out.

18:33 Or if you're working with new developers or mentoring new people, this is all good information.

18:39 Yeah.

18:39 And also, if you got somebody that works for you that's on Check.io at their lunch break,

18:43 they're not just goofing off.

18:44 They're upskilling.

18:45 So...

18:46 That's right.

18:46 Let them goof off on Check.io.

18:48 That's one of the best possible options.

18:50 Beats Facebook every day.

18:51 That's our six.

18:52 Do you have any news for us?

18:54 I do.

18:54 I have two pieces of news or ideas I want to run by you.

18:57 First, have you tried Firefox Quantum, the brand new Firefox that came out yesterday?

19:01 No.

19:02 It's supposed to be twice as fast.

19:03 A lot of it's rewritten in Rust.

19:05 Use a way, way less memory than Chrome.

19:08 So these are all pretty exciting.

19:10 So I'm actually checking out Firefox Quantum.

19:12 I'm doing even the show from it this week.

19:14 Pretty cool.

19:15 So yeah, if that sounds interesting to you, check it out.

19:18 It sounds like Firefox might make a good comeback.

19:20 And they're definitely the most open source friendly of all the browsers.

19:24 So I love to see them actually alive.

19:26 Rust is that language that I'm always meaning to try to look at, but I haven't yet.

19:33 Yeah.

19:33 Well, it's getting dark and cold and rainy here in Portland.

19:36 Maybe you have a Sunday afternoon and you're like, you know, I just need to get a book and

19:38 just sit by the fire.

19:39 Yeah.

19:40 And rain and rust go together really well.

19:42 They do.

19:42 You can start with some regular metal.

19:44 Put it outside.

19:45 By the time you know rust, it'll be rust.

19:47 It's all going to go together great.

19:48 It's good.

19:48 So the other thing I wanted to run by you is by everybody is how interested would people

19:54 out there be in having an Amazon flash briefing that is this show, right?

20:00 So what I'm talking about, if you don't have an Amazon Echo, there's a way to ask it in

20:05 the morning.

20:05 You could ask it whenever, but I think the idea is in the morning, like, hey, what's my news

20:10 today while I'm brushing my teeth, you know, getting ready for work, whatever, right?

20:13 Or just sit down at my desk and I'm not really ready to work yet.

20:15 You could ask for your flash briefing and you can configure different sources like Reuters or

20:21 NPR or whatever.

20:22 And I was thinking it might be really fun if we took our little items and shipped one

20:26 of them per day as a flash briefing.

20:28 I think then every day somebody would have, people would have a thing that we talk about

20:32 for a couple of minutes for Python.

20:33 Yeah, we should do that.

20:34 Sound fun.

20:35 So if people are super into this, send us an email or something on Twitter and let us know.

20:38 Yeah, let us know.

20:39 Yeah.

20:39 If not, then I won't write it.

20:41 If we do it, then I can get a, like an Amazon device as a business expense.

20:46 Absolutely.

20:46 I think that's totally great.

20:48 Yeah.

20:48 So the Echo Dot, it just as functional as the full expensive one.

20:53 It's just the speakers aren't as good, but it's like 45, 50 bucks for one of those things.

20:57 It's not outrageous.

20:58 Yeah.

20:58 And everybody's got them on sale for the after Thanksgiving thing.

21:01 Yeah, that's right.

21:02 It's coming up.

21:03 All right, cool.

21:03 Well, that's all I have for us.

21:05 Yeah, me too.

21:05 Yeah, so just once again, thank you everybody for helping the show be one year old.

21:10 It's really awesome.

21:10 Yeah, thanks.

21:11 Yep.

21:11 And thanks, Brian.

21:12 Catch you next time.

21:13 Thank you for listening to Python Bytes.

21:16 Follow the show on Twitter via at Python Bytes.

21:19 That's Python Bytes as in B-Y-T-E-S.

21:21 And get the full show notes at Pythonbytes.fm.

21:25 If you have a news item you want featured, just visit Pythonbytes.fm and send it our way.

21:29 We're always on the lookout for sharing something cool.

21:32 On behalf of myself and Brian Okken, this is Michael Kennedy.

21:35 Thank you for listening and sharing this podcast with your friends and colleagues.

Back to show page