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.

00:10 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 wanna 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.

00:26 - Yeah, we shipped the entire first year, 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:38 So pretty exciting.

00:38 You know, and I just wanna say thank you to everybody out there who listens to the show on a weekly basis, 'cause that's why we do it.

00:44 We do it for you guys, and we wouldn't do it if you weren't interested and excited.

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

00:50 - Yeah, definitely, thank you.

00:51 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.

00:57 So you guys deposit now, get a coffee, settle in or whiskey if it's late.

01:02 Now, actually we're going to keep to the same format.

01:04 All right, cool.

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

01:10 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 the show.

01:18 Thank you, thank you.

01:19 They just launched Spaces.

01:20 check it out at do.co/python.

01:23 Tell you more about it later.

01:24 Right now, I want to spend a while on APIs, Brian.

01:27 - Yeah, I've got APIs on the brain right now.

01:30 So we'll start with a new pro, I don't know if it's new actually, a project called Tavern.

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

01:37 (Brian laughs)

01:38 - No, it's a testing, it's testing RESTful APIs.

01:42 So I don't know why it's called Tavern.

01:44 I'll have to ask him about that.

01:45 - It's a really cool project though.

01:46 I checked it out.

01:47 I like it a lot actually.

01:48 - Yeah, so it's at taverntesting.github.io.

01:51 Like I said, it's a RESTful API testing.

01:54 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.

02:19 And one of my favorite things about this is it comes with a pytest plugin, and they say it works best integrated with pytest.

02:27 - That's really awesome.

02:28 So you basically describe, I want you to call this URL, it's gonna be a get, and you expect to get kind of this thing back, and then you can just assert against it.

02:37 Is that how it works?

02:38 - With the YAML syntax, you don't even have to specify asserts.

02:41 You just specify what you expect to get back, and it just automatically tests for all that For things like this, 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 an editor that colorizes your YAML files.

03:01 And I think it's good for, especially, you can discuss it with non-programmer people.

03:07 So that's one of the benefits of that.

03:09 - Okay, yeah, that's really cool.

03:10 Yeah, that's true.

03:11 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, they can say, yeah, okay, these are the things that let's slot in here, right?

03:22 Or to the person who built the API.

03:24 - Like for instance, the sequences, you can say, okay, here's the kind of the test is called this, and this is sort of the sequence we're gonna go through.

03:31 First you log in, and then you have to do this.

03:33 And there is also with a lot of sequences, you have to collect, get information like tokens or something 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.

03:51 - That's Tavern.

03:52 - Tavern sounds really cool.

03:53 Definitely worth checking out.

03:54 The thing I wanna talk about is not for testing APIs, but consuming APIs, calling APIs.

03:59 What if you weren't looking at this document we're sharing, just like thinking of like, "Hey, I'm gonna call an API from Python." What library do you think you would use?

04:07 - Oh, requests.

04:08 - Obviously, everyone uses requests.

04:09 And request is one of the absolute most popular libraries since downloaded an insane number of times.

04:15 And what I find myself doing a lot when I, I know this is like a proper API I'm going to consume.

04:21 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, right?

04:31 Like log in or, you know, get courses or whatever your API is about, right?

04:36 And you sort of put use request to implement it, but deep down, you kind of bury requests and hopefully you've got some 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.

04:58 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?

05:03 So let me just describe real quick how you use this.

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

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

05:14 I'm gonna 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:30 And that just applies to all the functions you call on this class.

05:33 If I wanna get the users, I'd say create a function called get user.

05:36 and I'd say @get /users/curly username, and that curly username there maps to the argument.

05:44 So when I call it, I say, github.getuser, yeah, Mike C. Kennedy is my username there, 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:56 And they have another example for updating a user, that's a patch call, so you say @json @patch, and then the arguments to the method, You can pass in just like a body of basically KW args 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 three to decorate as a query.

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

06:24 This is so smooth.

06:26 I really like it.

06:27 So if I'm building like a super structured API that's got really strict restful requirements like this, I'm definitely gonna check out uplink.

06:33 I'm gonna definitely watch this, this is neat.

06:35 They have a little warning in there that says that it's in the early stages, but that might be a great way for other people to get involved if they wanna help out and push this further.

06:46 - Absolutely, so that's definitely a warning you wanna 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 wanna break your code.

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

06:59 There's so many people that say, "I really wanna get started in open source," they look at Django or CPython, like, whoa, this is complicated and changing this is really hard. Something like this, you could totally contribute to a project like this without getting overwhelmed in the early stages. So check it out. Yeah, definitely. Cool. Alright, so let's switch to totally different topic and talk about rest and API.

07:18 Yeah, yeah. So I wanted actually to combine these two things because I ran across them in the same week for one. And this was shared by a listener. And I'm sorry that I didn't write down the name.

07:32 >> But yeah, thank you for submitting that.

07:33 That's awesome. I saw that coming as well over email.

07:35 >> 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:45 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, it's like what your test does, but in 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, 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 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 whatever data you wanted and validate, use a test to validate that the data you're getting adheres to the schema.

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

08:42 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, 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 It's interesting if you're the maintainer of that thing so that you know if the tests 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, and you're like, "But I'm doing what they say." It's like, what has happened, right?

09:29 So if you knew the schema of why APIs you depended upon changed, this is a good way to do that. I think that'd be great.

09:35 >> Yeah. Even if you didn't have a schema provided to you, you could define one for-

09:39 >> Yeah, it's usually not too hard, right?

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

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

09:47 Anything that's using JSON, you can use that to test any API.

09:51 >> Yeah, definitely. It's very neat. So check that out as well.

09:54 Alright, before we get on to the next thing, I want to tell you where your audio came from this week.

09:59 It came from DigitalOcean Spaces.

10:02 So that's right, those guys are sponsoring this episode, as I said at the top of the show, check them out at dio.co/python, get a free two-month trial of Spaces.

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

10:15 You know, things like AWS or Azure Blob Storage, sorry, AWS S3, Azure Blob Storage, things like that, but way, way better, better pricing, simpler, things like this.

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

10:29 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 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 Boto3.

10:49 That's the S3 AWS API.

10:51 So the API is compatible with S3, like quite literally it's the same API even.

10:57 Just point it 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 at dio.to/python.

11:07 Very cool stuff.

11:08 - Yeah, very neat.

11:09 And cool that you tried that out, that the API's compatible.

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

11:13 - I was thinking that some music would be nice.

11:15 - I love to listen to music when I code, do you?

11:17 - Yeah, all the time.

11:18 - It's funny, I find like a little bit of distraction kind of helps keep the mind focused.

11:22 I don't know, people are weird that way.

11:24 I work in coffee shops as well, and I like that as well.

11:27 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 FoxDot.

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

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

11:50 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:03 So I was hoping you could explain to me what's going on.

12:06 I wasn't at the talk, so--

12:08 and the video is not that long, 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:18 but instead of explain 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:32 - Yeah, I guess I'll have to check out what all Fox.is and how that works with that.

12:36 - Yeah, yeah, sadly, there's not that much information in this video 'cause it's like partial and it's short, but this is from Ian Watt, another listener suggestion.

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

12:47 So just, you know, chore it, 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.

12:58 Of a, should I do live?

12:59 Cause 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, 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 Right.

13:11 So tell us, 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 he get that?

13:22 It's at weekly Python dot chat, 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 foundation, but he's also Python instructor 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 by other people that listen.

13:45 and does like a little, like a under an hour, approximately an hour video chat with somebody else, but they're also, you can do live coding.

13:55 And then there's people in the chat room asking questions while it's going on.

14:00 So it's a live thing, but then it's also recorded.

14:02 So you can watch old ones.

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

14:08 So that was with me.

14:10 - That's awesome.

14:11 - And I'm highlighting it because I want more, it's really cool, it's fun.

14:14 It allows to ask questions of people that they wouldn't, maybe you don't go to conferences that much, but you could stay up for a weird hour depending on where you live in the world.

14:25 But you can ask questions of people you wouldn't get a chance to otherwise.

14:29 So that's good.

14:30 - Yeah, very cool.

14:31 Nice, so yeah, check that out.

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

14:34 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:51 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, yeah.

14:57 - Yeah, it's funky.

14:58 So you basically, you have these little islands, you gotta conquer the islands and you go, the way you conquer them is by solving all the puzzles.

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

15:06 One of the things I think is really cool about playing the game actually is you solve some 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:18 - Yeah, and it's kind of like code reviews 'cause you can comment on other people's solutions and stuff.

15:22 - Yeah, so it's pretty cool.

15:23 Yeah, so these guys wrote a blog post based on the mistakes they see people making from that area.

15:29 They said, "10 common beginner mistakes." Let's go through them real quick.

15:31 Incorrect indentation, tabs versus spaces, obvious, but you can imagine if you come from Java that you don't know that, right?

15:39 This one's more subtle.

15:40 Using a mutable value as a default value.

15:42 So like, append to list, and then you give it like, source list equals bracket bracket as a default value.

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

15:56 Because every time you call it without specifying that argument explicitly, it's going to use the same list.

16:04 'Cause that is initialized at like, not quite compilation time, 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, that's a fun one.

16:16 - Yeah, it's definitely fun and tricky.

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

16:20 You know, my theory is comments, not so much doc strings, but comments are deodorant for code smells and problems.

16:27 So I'm not so sure I'm gonna recommend that as much, but documentation, good stuff for sure.

16:32 Scoping, you know, if you come from a C-based language with curly brace scoping, block scoping, Python is different with its functions, scoping and closures and whatnot.

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

16:44 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 and it could be some super indented thing or you could do the negative test, the edge case that you're gonna break out of and then the loop.

17:00 And then you're gonna do the edge case you're gonna break out of and then the inner loop and it's way less indented.

17:05 And that's one of the Zen of Python 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 something, that they don't trust the exception handling.

17:19 Oftentimes you don't have to check for, you don't have to make things bulletproof if the function you're calling is gonna check it for you anyway.

17:27 - Exactly, the easier to ask for forgiveness than for permission style is better than the look before you leap.

17:33 We got copying, everything is a pointer in Python.

17:35 So the pointers means you may be sharing the same object, not a new one, so it talks about that, especially around the lists and data structures.

17:42 Range is half closed, range one to 10 actually is one to nine.

17:47 Wrong capitalization, so you're just writing like camel case, Java, C# style, or some JavaScript style of naming for variables, classes, functions, whatever.

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

18:03 This one's a little bit interesting about class level variables and inheritance, and you can check that out, They have nice little examples for all of them.

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

18:13 So I'm not sure what the 10th mistake is, but maybe I read it wrong.

18:16 I read it twice.

18:17 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:21 - Yeah, that's true.

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

18:26 Yeah. - Yeah.

18:27 (Luke laughs)

18:28 - Perfect.

18:29 All right, so anyway, if you're getting started in Python and you want to kind of level it up a little bit, you know, check that out.

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

18:39 - Yeah, and also if you got somebody that works for you that's on Check.io at their lunch break, they're not just goofing off, they're upskilling.

18:46 - That's right, let 'em goof off on Check.io.

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

18:51 Beats Facebook every day.

18:52 - That's our sixth, do you got any news for us?

18:54 - I do, I have two pieces of news or ideas I wanted 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:04 A lot of it's rewritten in Rust.

19:06 Use way less memory than Chrome.

19:09 So these are all pretty exciting.

19:11 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:21 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, well it's getting dark and cold and rainy here in Portland.

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

19:40 - Yeah, and rain and rust go together really well, so.

19:42 - They do, you can start with like some regular metal, put it outside, by the time you know rust, it'll be rust.

19:47 It's all gonna go together great.

19:48 - It's good.

19:49 - So the other thing I wanted to run by you is, by everybody is, how interested would people out there be in having an Amazon Flash Briefing that is this show?

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

20:05 You could ask it whenever, but I think the idea is in the morning.

20:08 Like, "Hey, what's my news today "while I'm brushing my teeth, "getting ready for work," whatever, right?

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

20:16 You could ask for your flash briefing and you can configure different sources like Reuters or NPR or whatever.

20:22 And I was thinking it might be really fun if we took our little items and shipped one of them per day as a flash briefing.

20:29 I think then every day somebody would have, people to have a thing that we talk about for a couple of minutes for Python.

20:34 - Yeah, we should do that.

20:35 - Sound fun.

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

20:39 - Yeah, let us know.

20:39 - Yeah, 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, I think that's totally great.

20:48 Yeah, so the Echo Dot, it just as functional as the full expensive one, 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, and everybody's got them on sale for the after Thanksgiving thing.

21:02 - Yeah, that's right.

21:02 It's coming up.

21:03 All right, cool.

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

21:05 - Yeah, me too.

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

21:10 It's really awesome.

21:11 - Yeah, thanks.

21:11 - Yep, 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 @PythonBytes.

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

21:22 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:33 On behalf of myself and Brian Okken, this is Michael Kennedy.

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

Back to show page