Latest / Talk Python To Me / #450: Versioning Web APIs in Python
Transcript
- 0:00You've built an awesome set of APIs, and now you have a wide array of devices and clients
- 0:04using them. Congratulations. Then you need to upgrade an endpoint or make a change that is
- 0:10meaningful in some way. Now what? That's the conversation I dive into over the next hour
- 0:15with Stanislav Zmiev. We're talking about versioning APIs on this, the 450th episode
- 0:21of Talk Python To Me, recorded January 8th, 2024.
- 0:25Welcome to Talk Python To Me, a weekly podcast on Python. This is your host, Michael Kennedy.
- 0:44Follow me on Mastodon, where I'm @mkennedy, and follow the podcast using @talkpython,
- 0:49both on fosstodon.org. Keep up with the show and listen to over seven years of past
- 0:55episodes at talkpython.fm. We've started streaming most of our episodes live on YouTube.
- 1:01Subscribe to our YouTube channel over at talkpython.fm/youtube to get notified
- 1:05about upcoming shows and be part of that episode.
- 1:08This episode is sponsored by Neo4j. It's time to stop asking relational databases to do more than
- 1:16they were made for and simplify complex data models with graphs. Check out the sample FastAPI project
- 1:23and see what Neo4j, a native graph database, can do for you. Find out more at talkpython.fm slash
- 1:30Neo4j. And it's brought to you by Sentry. Don't let those errors go unnoticed. Use Sentry like we do
- 1:38here at Talk Python. Sign up at talkpython.fm/sentry.
- 1:42Stanislav, welcome to Talk Python To Me. It's great to have you here.
- 1:47It's good to be here. Yeah, it's good to have you here. I wonder what version that we're going to end up shipping of this episode.
- 1:53We're going to talk about API versions.
- 1:56API versions, API versions.
- 1:59It's good to have many of them, you know?
- 2:00Yeah, it's one of those problems that, you know, as soon as you create an API and people start to use it,
- 2:06they don't want to keep rewriting their code.
- 2:08So, but you need to evolve your API over time as it gets new features, as maybe more importantly,
- 2:15you realize like, oh, that was a bad choice.
- 2:18How do you fix this, right?
- 2:19So that's kind of the theme of this episode.
- 2:22We're going to talk about concrete tools that you can use to add versioning to various API frameworks in Python
- 2:30and some of the ideas behind it.
- 2:32So it's going to be a ton of fun.
- 2:34Let's start, though, with your story.
- 2:36Tell us a bit about yourself.
- 2:37Previously, I have been a back-end engineer for six or even seven years now.
- 2:43But recently, I have shifted into infrastructure.
- 2:48Right now, I'm a platform engineering tech lead at Monite.
- 2:52What I do is I make architectural decisions.
- 2:55I mentor the teams.
- 2:57I help the teams with Python-specific questions.
- 3:00But mostly, I build Python-specific infrastructure.
- 3:04And my main job is making our business teams happy.
- 3:07I try to do that in my open source work as well.
- 3:10I like to make my own open source projects.
- 3:13I like to contribute to big projects, small projects.
- 3:16It's all over the place.
- 3:17And we're going to be mentioning one of them today.
- 3:19Yeah.
- 3:19You build a really cool library that we're going to talk about.
- 3:22And that'll be a lot of fun.
- 3:23It looks really useful.
- 3:24And some other third-party ones as well.
- 3:26So I wanted to ask you a little bit about what is the difference between being a back-end software developer,
- 3:33which I think a lot of people get the sense of, like building APIs, building the back-end, at least, aspect of websites and so on,
- 3:41versus, say, building infrastructure in Python.
- 3:43Like, what are these two worlds?
- 3:45How are they the same?
- 3:45How are they different?
- 3:46So when you're building them, especially when you're building products, it's very similar.
- 3:50However, in back-end, there is already a lot of infrastructure around you.
- 3:56There is already a lot of people who do portions of your job.
- 4:01So, like, there are people who can sell your product.
- 4:03There are people who are thinking about how to, you know, how to move it forward, how to evolve it.
- 4:08There are people who are, you know, designing it, thinking about it.
- 4:12But when you're a platform engineer, you've got to do all of that yourself.
- 4:15And also, one big portion of any library framework or anything else, any developer tools development,
- 4:23is the fact that unlike an API, well, a web API, a REST API, which has a very strict sort of interface
- 4:32where the developer wouldn't be able to use it in a million ways.
- 4:36A library, especially in a language as introspective as Python, the developers can do crazy things with your stuff.
- 4:43So you've got to think forward for all of these use cases.
- 4:47And you've got to be sure that you don't expose extra private interfaces.
- 4:51Because once you do expose them, it won't be nice to break them in the future.
- 4:56I guess it's a little more restricted with APIs than it is with handing out packages, right?
- 5:01Because people, they can just dive into the packages and do whatever you want.
- 5:05And the API is more or less limited by what the website's willing to process, right?
- 5:10Yep.
- 5:10On the flip side, you have code on the internet.
- 5:13And anytime you have code on the internet, that's always a little stressful, right?
- 5:17Just like whatever is out there, it's incredible.
- 5:20If you have a website, you pull up the logs, how much abuse your website is taking constantly.
- 5:26True.
- 5:26The great thing about building packages is that you're not worried about what happens with them after.
- 5:31You know, you just skim them.
- 5:32And then if it breaks, it's not you.
- 5:35It's not on your servers, hopefully.
- 5:37But also, as I mentioned, you've got to both be its designer.
- 5:41You've got to sell it.
- 5:42You've got to people through that.
- 5:44You've got to document it.
- 5:45You've got to, as a platform engineer, you do all of the parts that backend engineers are
- 5:50abstracted away from, or at least in most bigger companies, abstracted away from.
- 5:55Yeah, super interesting.
- 5:56All right.
- 5:56Let's talk about, I guess, one more thing for your introduction before we move on.
- 6:01You've worked on quite a few interesting projects here.
- 6:05So maybe give us just a little bit of background on some of your open source work.
- 6:09I'll just talk about, I guess, the most popular ones.
- 6:11The coolest one was PathWalk.
- 6:14If you remember, in Python, there is OSWalk.
- 6:16But then in Python 312, I added PathWalk.
- 6:19I have also added the type hints for that into TypeShed.
- 6:23I think some other things.
- 6:24And I am, I think, top 10 contributor of Tortoise ORM, the asynchronous ORM that claims to be the
- 6:33fastest, or at least one of the fastest in Python.
- 6:36And a few smaller projects, which I don't think there is anything particularly fun to talk about there.
- 6:43It's just mostly bug fixes and small features.
- 6:45But then my other projects, the ones that I maintain, are way cooler.
- 6:50For example, Pydantic Duality enables you to have multiple versions of your Pydantic models for
- 6:57almost free in terms of performance and without any code generation.
- 7:02And Cadwin, the project we're going to be discussing today, implements, I guess, the fullest and most
- 7:08sophisticated API versioning paradigm there is at this point.
- 7:12Excellent.
- 7:12Yeah.
- 7:13A lot of cool stuff.
- 7:14Pydantic Duality looks, yeah, Pydantic Duality looks really, really cool.
- 7:17So a lot of neat stuff here.
- 7:19A lot of different ways people might know about you.
- 7:21So yeah, Cadwin is the one we're going to be focusing on for here.
- 7:24So let's start with talking about APIs.
- 7:28You talked about API-first companies.
- 7:32What's that?
- 7:32API-first company, just like any other term that gets popular, is a buzzword, obviously.
- 7:39But the truth behind it is an API-first company is any company whose main product is an API
- 7:47and the SDKs associated with it.
- 7:49And as a result, API-first company has to document their API really thoroughly, think about their
- 7:56API, both the internal one and the external one, and take great care of that because it's
- 8:02their main product.
- 8:03We have a lot of API-first companies right now.
- 8:07But with every year, new industries are joining.
- 8:09And if you've ever heard of open banking, things like that, banks are going to join us soon.
- 8:16In some countries, they already have.
- 8:17And Monite is doing similar things and tries to bring that innovation, that open banking,
- 8:24that API-first approach into banking.
- 8:27Yeah.
- 8:27So the company you work for Monite, they build, you all build APIs to basically facilitate
- 8:34other people doing invoicing and bill pay in their apps.
- 8:37Is that correct?
- 8:38Yeah.
- 8:38You could call us a B2B2B company.
- 8:41Because essentially, many banks, many marketplaces are very outdated in terms of how they handle
- 8:48invoicing and payments.
- 8:49And we try to take all of that, combine that, and offer that as a white label product to them.
- 8:56So we do this for them.
- 8:58They integrate with us once, and then they forget it was ever a problem.
- 9:03They just give our product as theirs straight to their clients.
- 9:06Yeah.
- 9:06That's excellent.
- 9:07So this seems like kind of in that category of API-first.
- 9:12Oh, yes.
- 9:12Yeah.
- 9:12Okay.
- 9:13It's cool that you have concrete experience with it, right?
- 9:17Of course.
- 9:17Give us some of the laws.
- 9:20We coordinated a little bit beforehand.
- 9:22And there's some interesting laws for designing good APIs.
- 9:26Like one of them is the reverse Conway maneuver, which to me sounds like a jujitsu sort of
- 9:32move that you might try to pull.
- 9:34What is this?
- 9:35Oh, yes.
- 9:35So when you are designing any system, you got to think about the way it's going to end up
- 9:42and the laws that are going to govern them.
- 9:44And Conway's law is one of the main laws that do that.
- 9:48To explain that to people in simple terms, if you have four teams designing a compiler,
- 9:53you're going to have a four-pass compiler.
- 9:55Essentially, the way you set up communications within your company, the way you set up your teams,
- 10:01your structure is going to be reflected in your product.
- 10:04And you can't do anything about it.
- 10:06You can spend money.
- 10:07You can coordinate people.
- 10:08But at some point, Conway's law is going to affect you.
- 10:12And it's going to be harsh.
- 10:13So one of the ways to go around it is to first think of an interface that you want to provide.
- 10:20And it applies to anything, not just APIs.
- 10:22APIs is just a really good example of that.
- 10:24And then design your company and the communications within your company, the teams within your company,
- 10:31to reflect the interfaces that you want to get in the end.
- 10:34And that's reverse Conway maneuver.
- 10:36Okay.
- 10:37Because your code and your APIs and all those things are going to reflect your company structure
- 10:41and culture.
- 10:42So instead of having that being implicit, make it more explicit by saying, well, this is the
- 10:47endpoint that we want, the end result that we want.
- 10:50How do we work that backwards to end up structuring teams or whatever to get this right?
- 10:55Precisely.
- 10:56Yes.
- 10:56Yeah.
- 10:56It is a little bit like jujitsu.
- 10:58Anyway.
- 10:58But there are more laws that do that.
- 11:02Of course, there is REST, which I guess not really laws, but principles.
- 11:06Yeah.
- 11:06Which most people, or at least most companies I saw, don't really like to follow it to the
- 11:13letter.
- 11:13But if you do, if you do follow REST, if you try to make your API as available as you can
- 11:22and as standardized as you can, then it's much easier for new people, especially junior
- 11:27developers to learn.
- 11:28And then that makes it much easier for your clients to integrate with you, which is great.
- 11:32There is also, there are also a few, I guess, more advanced things.
- 11:37For example, when you are designing an API, when you're doing microservices and when you
- 11:41have many services talking to each other, which in the end provide your final product to your
- 11:47customers, a good idea is to omit any internal APIs.
- 11:51And make all of your APIs, or at least the majority of your APIs external.
- 11:56That is also one of the things about API first.
- 11:59Once all of your APIs are external, it means that your clients will be able to build a product
- 12:06that is of similar quality to yours.
- 12:08We're using your APIs, using your data, which provides them with infinite capabilities of using
- 12:14you, which is great.
- 12:15Yeah, that is great.
- 12:16It's certainly frustrating when you see, you know, it's not on the API level, but you
- 12:21see things in like the iOS platform or in Windows or wherever you're like, why can't I do this?
- 12:27Like, these are private things that either Apple or Microsoft or whoever are keeping to themselves
- 12:31and clearly are important for building apps, but you can't use them, right?
- 12:36So you're suggesting that by having basically whatever APIs you need, I guess there's probably
- 12:41limits, right?
- 12:42Like you don't want to expose, say you're like HR API if you're not an HR company, right?
- 12:46But in general, having your APIs just be all public, right?
- 12:51It probably makes them better as well because they just get so many more use cases.
- 12:54They get treated with things like versioning and stuff more seriously.
- 12:58Exactly.
- 12:58Excellent.
- 12:59It's a complex topic that could span, you know, we could discuss it for hours, you know,
- 13:03which parts of your API do you make public?
- 13:06Which parts of your API do you start versioning?
- 13:09And if you're combining your APIs using a gateway or a federation, you know, it's a long story.
- 13:15But I guess with a federation, you wouldn't really make it public.
- 13:18Sure.
- 13:19Let's dive into that a little bit.
- 13:20Let's talk about API versioning.
- 13:22We know what versioning is in Python.
- 13:24We have lock files.
- 13:27We pin the version.
- 13:29We might say we're using Pydantic 2.0.5 or whatever it is.
- 13:33And that leaves the Pydantic team free to do whatever they want.
- 13:38And we can trust that our code is going to stay the way it stayed.
- 13:41I guess API versioning is that, but for web endpoints rather than software links to packages,
- 13:47right?
- 13:47It's a little different though, in some regards.
- 13:50For example, when you're packaging your, I don't know, software and you just send it
- 13:55to clients, thing is you need to have versions for every single thing.
- 13:59For example, if we are doing a bug fix and we need to ship that to our clients, that is
- 14:04going to be a separate version.
- 14:05But in web, when you're hosting it on a server and your clients just make requests to your
- 14:11servers, you don't need that.
- 14:12You don't need to ship bug fixes as separate versions.
- 14:15You can just ship them because they're not going to break anything for your clients and
- 14:19your clients will be able to automatically get them.
- 14:20Same goes for minor versions.
- 14:22Minor versions that just add features don't really need to be for a version on APIs.
- 14:28That leaves us with just major versions, which is why many companies like Stripe at Monite
- 14:33use dates for their versions?
- 14:37Because we don't really need all of the power of semantic versioning.
- 14:41Yeah.
- 14:41Yeah.
- 14:41Sure.
- 14:41More calendar versioning because all you really need to say is this one is different than before,
- 14:47right?
- 14:47It doesn't.
- 14:48Exactly.
- 14:48Stripe does a lot of interesting things.
- 14:50We're going to talk a little bit about what they're up to.
- 14:54But you're right.
- 14:55When I think about APIs that I've created, sometimes I've had other versions, right?
- 15:00You know, one of the main areas is in the apps that we have over URL work.
- 15:07It does.
- 15:07In the apps for consuming the courses, right?
- 15:10We had two completely different versions of the app.
- 15:14One got completely, completely rewrote it.
- 15:16And many of the API endpoints were just fine.
- 15:19We could just use them over and didn't have to change anything.
- 15:22But three or four were like, you know, this really would be better if it was different.
- 15:27And so added like a slash v2 or something in there, right?
- 15:31But really, really limited.
- 15:33So the thing that's interesting, though, that you point out here is like until that happened,
- 15:37I'd shipped many, many versions of that API.
- 15:41And long as the schema didn't change, I didn't bother even specifying that it had a version,
- 15:46right?
- 15:47It was just like slash course, my courses or whatever it is, right?
- 15:51Like just give those to me.
- 15:52As long as it kept giving the courses back, it was good.
- 15:54That's the thing.
- 15:55As long as you extend stuff, it's going to be fine.
- 15:58But as you mentioned in the beginning, you can't really do that forever.
- 16:02At some point, you're going to make wrong design decisions.
- 16:06At some point, even if you did everything perfectly, you know, your product is just going to evolve.
- 16:11It's just going to evolve.
- 16:12And then you'll need to use old endpoints.
- 16:16You know, you can't do that.
- 16:20In some breaking ways.
- 16:21Of course, there are ways around it.
- 16:22You can use API evolution.
- 16:24You can just, you know, add fields constantly and duplicate the old functionality.
- 16:29But after a few of such versions, your API will look terrible, which is why we use versioning.
- 16:34And which is why when your company's main product is an API, when you are an API first company, you can't do anything without versioning.
- 16:44You can live for a short period of time.
- 16:46For example, a pre-Series A company, I don't think should deal with versioning.
- 16:51But then after a series A, after you gather, I don't know, 15, 20 clients, it will be really hard to survive without versioning.
- 16:59Yeah.
- 16:59And it's a really quick and easy way to make loyal customers angry by making changes to your API that just start breaking code.
- 17:08Even if you wait for your clients, even if you, you know, warn them, it's still not nice when you're trying to use some API.
- 17:14And then every three months or every month, it breaks everything.
- 17:18You know, you wouldn't like that.
- 17:19Even with a warning.
- 17:21You put in all the work to do the integration.
- 17:23You got it working.
- 17:24You tested it.
- 17:24Your job is, your product is not the integration.
- 17:27The integration facilitates what you're trying to build.
- 17:30And every time you got to go back and rewrite that stuff, it's not great.
- 17:34I was working, I have MailChimp for our newsletter marketing side of stuff, the courses and use them for a long time.
- 17:42And I got a message like, hey, we're retiring this API you're using.
- 17:46And their API is dreadful.
- 17:49Like their Python library API is dreadful.
- 17:52It's like star args, star star kwo args.
- 17:55You better pass the right things.
- 17:56But there's no enforcement or instruction about what goes in there, right?
- 18:00And so some random dictionary that was going in there was no longer right and had to be like recreated.
- 18:05So they just decided, you know, we're going to just take different stuff and we're just going to shut down that endpoint.
- 18:10And when they did, all of the aspects of my website just started breaking.
- 18:16Like it wasn't crashing, but just anything around, hey, put me on your newsletter.
- 18:19Like, nope, you couldn't be on the newsletter because it, you know, people started sending me a message.
- 18:23Michael, I'm trying to sign up to be on the newsletter.
- 18:25But it just keeps saying that something went wrong with our site.
- 18:29We're sorry.
- 18:29I'm like, oh, boy.
- 18:30Go look.
- 18:31And they just turned off that endpoint.
- 18:33And it was just like, nope, not doing that anymore.
- 18:36And it was super frustrating.
- 18:37It took me eight hours to rewrite it because, like I said, there was no real guidance on like, oh, you just passed the dictionary.
- 18:44Like, well, what goes in the dictionary?
- 18:46You know, it was like some word of one of the keys changed.
- 18:49I was so, it just took forever.
- 18:51It was frustrating.
- 18:51So what we're going to talk about here is a lot about how, how do you not do, how do you not pull a MailChimp?
- 18:58How do you set it up so that your code keeps running?
- 19:01You know, the reason why they did that is they're like, well, we feel like we wrote it wrong before.
- 19:06We want to fix those for, to make it better, but we don't want to maintain two versions.
- 19:10That's probably the heart of it.
- 19:12And that's a sane idea.
- 19:13It is.
- 19:14Until you have half a million users of the old API.
- 19:17And how many of those, like, so for me, yeah, I was frustrated.
- 19:20Yeah, I'm like, all right, well, I had plans today for constructive work, but I'm going to go rewrite what I did four years ago.
- 19:25And then I'll do something again tomorrow.
- 19:27But, okay, that's like a pretty good outcome.
- 19:29A more common outcome is probably I hired a consultant to build me an app and integrate MailChimp, and they're gone.
- 19:37And now my thing that I built a year ago doesn't even work.
- 19:41And I don't have the skills.
- 19:43I got to go hire somebody again to dig back in there and fix it, right?
- 19:46Like, that would be really frustrating.
- 19:48Yeah, that's crazy.
- 19:49And it could be devastating to some clients, especially if the changes are huge, especially if it's not just a single endpoint.
- 19:56Or if there is only one endpoint, and it does a lot of things, then, oh my God, it's scary.
- 20:02It is.
- 20:02This portion of Talk Python To Me is brought to you by Neo4j.
- 20:08Do you know Neo4j?
- 20:10Neo4j is a native graph database.
- 20:13And if the slowest part of your data access patterns involves computing relationships,
- 20:17why not use a database that stores those relationships directly in the database, unlike your typical relational one?
- 20:25A graph database lets you model the data the way it looks in the real world, instead of forcing it into rows and columns.
- 20:31It's time to stop asking a relational database to do more than they were made for and simplify complex data models with graphs.
- 20:40If you haven't used a graph database before, you might be wondering about common use cases.
- 20:44You know, what's it for?
- 20:45Here are just a few.
- 20:46Detecting fraud.
- 20:47Enhancing AI.
- 20:49Managing supply chains.
- 20:51Gaining a 360-degree view of your data.
- 20:54And anywhere else you have highly connected data.
- 20:57To use Neo4j from Python, it's a simple pip install Neo4j.
- 21:03And to help you get started, their docs include a sample web app demonstrating how to use it, both from Flask and FastAPI.
- 21:10Find it in their docs or search GitHub for Neo4j Movies Application Quick Start.
- 21:15Developers are solving some of the world's biggest problems with graphs.
- 21:19Now it's your turn.
- 21:20Visit talkpython.fm/neo4j to get started.
- 21:24That's talkpython.fm/neo4j.
- 21:28Thank you to Neo4j for supporting Talk Python To Me.
- 21:32Before we move on to maybe some of the approaches, Alex, Alexey out in the audience says,
- 21:40One more law.
- 21:41Hiram's law is also great.
- 21:43With sufficient number of users of an API, it does not matter what you promise in the contract.
- 21:48All observable behaviors of your system will be depended on by somebody.
- 21:52It is one of my favorite laws about any system.
- 21:56That's true.
- 21:57That is very true.
- 21:58And it's a scary law, you know, because at some point you'll have to version everything, which is why it's a good idea to make things, you know, I guess to describe all the behaviors and to try to handle all of the possible behaviors early.
- 22:14So that you know what is going to become a part of it.
- 22:17Because at some point you'll just not be able to stop it.
- 22:21And then you break something that was never intended to work.
- 22:24And then a hundred of your users are like, oh, we were using that.
- 22:27We were, it wasn't a bug.
- 22:29It was a feature for us.
- 22:30Yeah.
- 22:32We needed that.
- 22:33We found that's the only way we could use it.
- 22:34Yeah, for sure.
- 22:36All right.
- 22:36Let's talk about some of the techniques you put together.
- 22:39A nice thing here, like how to maintain hundreds of API versions and survive.
- 22:42So what are some of the ways in which people version APIs?
- 22:46First of all, it's really hard to really figure out how people do this and how to do it.
- 22:53Because 90% of the time when you're going to be researching API versioning, what you're going to see is people having tough decisions between putting it into a header or putting it into a path.
- 23:03But then when you ask, well, how do you version?
- 23:06Nobody really has an answer.
- 23:07Well, there are a few ways.
- 23:09First of all, you can go from the lowest level and version everything, including your database.
- 23:15You just make a separate copy of your app.
- 23:18You can host it on a separate server, even a separate Kubernetes namespace and forget about it.
- 23:23And, you know, you have two completely separate apps.
- 23:26And now you need to have double the people for the maintenance or at least a smaller separate team to maintain the second product.
- 23:33This approach is really nice if you have a product that cannot break.
- 23:37Like there needs to be zero possibility of that breaking.
- 23:41You know, if you are on a nuclear power plant, you'll probably do that.
- 23:45You know, you can't have the old one broken and, you know, the new one working.
- 23:50But it's the most expensive approach, obviously.
- 23:52You can, however, limit that.
- 23:55You can start versioning just the application, but not the database.
- 23:59Okay, now we have the same thing, you know, a separate branch in our code, a separate, not in our code, in our repository, a separate Kubernetes namespace, whatever.
- 24:08But now it's still expensive.
- 24:11It's still a whole separate copy of everything you have built.
- 24:14And now you have to make sure that the new one or the old one doesn't break the database because the database might have changed.
- 24:21But some of the one of the versions of code might not know about that.
- 24:24And then you realize that you go.
- 24:27Yeah, the database.
- 24:29This aspect of it sounds especially tricky, especially a relational database with super strong schemas.
- 24:35Like, you know what?
- 24:36That used to be a varchar and now it's an int.
- 24:40And so, nope, it's not like it's just not going to work, right?
- 24:43There's really little flexibility.
- 24:45So even if you run the two things in parallel, then you almost have to have two databases.
- 24:50And then like Emil points out and then you have to sync the databases.
- 24:54And like, it's non-trivial.
- 24:56Oh, yes.
- 24:57And then you start realizing, okay, why do I need a separate branch and a separate namespace?
- 25:01Let's just keep them within the same app.
- 25:04Because as long as, you know, you have separate paths for this, even you can build separate swaggers from them.
- 25:10You can have them within the same branch, within the same repository, just in different directories.
- 25:16And you can just copy all of your code.
- 25:17And now I call that versioning by suffering.
- 25:20Because you think, oh, I'm going to version less this time.
- 25:24You know, now I'm not versioning the database.
- 25:26Now it's not a separate branch.
- 25:27It's going to be much easier to make fixes in both places.
- 25:31But now you have to support both of the code bases.
- 25:35And once you update anything, any of the related resources, for example, the database models, you have to update them twice.
- 25:41If you update any of the dependencies, you have to update them twice.
- 25:45And that can get painful.
- 25:47And after a while, if you have more than two of these versions, you're going to be asking yourself, okay, what is the difference between these versions?
- 25:55And if you weren't careful with documentation and code review, oh, you're in for a treat.
- 26:00Yeah.
- 26:01But that's just the beginning.
- 26:02You can version much less.
- 26:04Now, instead of the whole directories, you can version just separate endpoints.
- 26:08And I think that's the way that I recommend for everyone.
- 26:12Everyone who is doing API versioning who wants to, you know, to be with the cool kids, I recommend this approach.
- 26:18As long as you are a small company or an individual and you want something really simple, that's a good way.
- 26:24You just copy a single endpoint that you have changed, create another schema for that, and then create another branch in your business logic.
- 26:31Or maybe even no branch.
- 26:33That's even better, you know, using the same business logic.
- 26:36But then at some point, once you want to support many versions or your versions become too different, that's where you get a problem.
- 26:44Because now all of a sudden, you have all of these, oh, I have this endpoint in this version, that endpoint in that version.
- 26:50Oh, it's going to be bad.
- 26:51Really bad.
- 26:52Again.
- 26:53We've been talking like there's the old version and the new version.
- 26:55But in practice, somebody starts to depend on the new version, and then you evolve the new version, and it just, it combinatorially explodes and it's out of control.
- 27:04Yeah.
- 27:05Precisely.
- 27:05It's going to be really bad for your developers.
- 27:07Your clients are going to be fine with it.
- 27:09Your developers are going to hate you.
- 27:11Although not as bad as with the previous approach.
- 27:14Now, the next one you get to is you start thinking, okay, what if instead of duplicating all of that stuff, I am just going to change the schemas.
- 27:25So, okay, if I do need to rename my endpoint, I am going to duplicate it.
- 27:28But most of the time, you're not.
- 27:29Most of the time, you're just changing some field in some schema.
- 27:32You're renaming it.
- 27:33You're extending it, changing its type, you know, stuff like that.
- 27:36That's like 90% of your use cases.
- 27:38Okay, what if we just change the schemas and then have some serializers?
- 27:42For example, you know, Django DRF loves that.
- 27:47They love, you know, the serializers and re-serializers.
- 27:50And you can just have each, we have five versions, and version one serializes into version five, version two serializes into version five, yada, yada, yada.
- 27:58And so our client sends us a request, we re-serialize it into the correct version, then we return a response, we use the same business logic,
- 28:06and then we serialize it back into each version.
- 28:08Great.
- 28:09Now, we have solved all the problems of the previous approaches.
- 28:13Now, you need to realize that when you're doing something like this, you are in a little bit of danger of creating bugs in the new versions,
- 28:22and these bugs cascading to the old versions, which hopefully you have tests to handle that.
- 28:27But at least your clients are happy because, you know, you can support versions for a long time, and your developers are happy because the burden of support is now on the serializers.
- 28:37But you still have to support these serializers.
- 28:40It's still a lot of stuff.
- 28:41You still have to, you know, put every field in there, you know, and take care of any problems that happen.
- 28:48And each new version will change basically all of your serializers.
- 28:52A little bit, yes.
- 28:53But you won't be able to support 20 versions like that.
- 28:56It's going to be crazy.
- 28:56And now that's where we get to the final approaches, the approaches that I guess are the most sophisticated in the industry.
- 29:05If you are making a backend for frontend, for your own frontend, then you can just use GraphQL.
- 29:10GraphQL basically requires no versioning, and that's one of the benefits of it.
- 29:16I would recommend that if you have developers that are, you know, good with GraphQL, that's great.
- 29:22You can omit versioning altogether.
- 29:23But if you're not, if it's a public API, then that's where Stripe's approach comes.
- 29:29I think they were the first ones to publish an article about that.
- 29:33It was Brander Leach, the author of this article.
- 29:35He has supported API versioning at Stripe for a while, and he has helped us with building our API versioning approach.
- 29:43Essentially, what Stripe did is said, okay, let's imagine that our developers are only supporting one version, the latest one, just like with serializers.
- 29:52But instead of serializing every version to the latest one, let's say that each version knows how to serialize into the next version.
- 30:00Kind of like database migrations.
- 30:02Let's say that I return response from the latest version.
- 30:05This response, or at least they call them, I think, version gates.
- 30:10These version gates, version gate for fifth version, knows how to convert back my response back into the fourth version.
- 30:16The version gate for the fourth version knows how to convert it back into the third version.
- 30:21And all of a sudden, you get simple version gates.
- 30:25Because, you know, if it's just field renaming in a single version, then it's just a single line.
- 30:29It's, they're really simple.
- 30:30But at the same time, they're immutable.
- 30:32Because these version gates, they're not describing the whole schema.
- 30:35They're describing the difference between two versions.
- 30:38And because we know that we don't want to, you know, change the old version.
- 30:42We want the word, the old version to work.
- 30:44Which is why the difference between version two and version three is going to stay.
- 30:48It's not going to change.
- 30:50Which is why these converters are also immutable.
- 30:52Which means that you don't need to spend much time supporting them.
- 30:56Right.
- 30:57Once you get, if you're on version three and you get the transition from one to two set, like that's not going to change ever, right?
- 31:03Precisely.
- 31:04Well, at some point, you need it for optimizations or for returning extra data.
- 31:09But it's such a rare use case that I haven't seen it so far.
- 31:13And I hope that I won't.
- 31:14So this is Stripe's approach.
- 31:16And one of the things that's really interesting that Stripe does and is laid out in this article that you're bringing to the table here is,
- 31:22they also implicitly assign you a version when you first make a call, which was really interesting.
- 31:29I use Stripe and I didn't realize that when I've made calls into Stripe that the first time you make a call,
- 31:36it pins you somewhere like on your user account on their side, pins you to that version of the API.
- 31:42And you never know you're on a different, you're on a particular version, but they keep you on that version.
- 31:47That's wild.
- 31:47That's great.
- 31:48Imagine you log into Stripe, you make a call,
- 31:52and then every time you open a documentation in your account, you're going to see your version.
- 31:57Every time you make a call, you're going to get response from your version.
- 32:00And then because they keep versions for years, for example, we are using, I think, a three or four year old version of Stripe's API and they still keep it.
- 32:10According to this article, they keep all of their versions, all since the company's beginning.
- 32:15I'm not sure how true that is.
- 32:18They keep years worth of versions.
- 32:20That is very true.
- 32:21Now you might see that as a negative.
- 32:22Like you might think, well, I'm pinned to this version.
- 32:25Like how do I escape the old version if I want the new version?
- 32:28But you can pass it in a header or there's other ways to say I explicitly choose something else.
- 32:34But by default, you're just, you're living in the past.
- 32:36That's really cool.
- 32:37You integrate and then you live in the past for as long as you want.
- 32:39The thing is, yes, you can change it using a header.
- 32:43Monite has a similar structure.
- 32:45You can change it in your partner's portal.
- 32:50And you can also, or I guess developer's portal.
- 32:52And you can also specify, for example, if you receive webhooks from Stripe, you can specify,
- 32:57okay, I want this webhook of this version and that webhook of that version.
- 33:00That's cool.
- 33:01Yeah.
- 33:01It's a really neat interface that if you don't want to think about versioning, you don't.
- 33:06You forget about it.
- 33:06But the second you want to, the second you want to have the latest API, they give you all the tools to work with it nicely.
- 33:14And that's the thing.
- 33:15This approach allows you to keep years worth of versions or maybe even decades worth of versions, actually, without your developers getting overburdened.
- 33:25Because there is still a single version of the business logic.
- 33:27They work on a single version of schemas and they just need to make sure that when they release a new version, they write a converter for it.
- 33:34And your clients are also happy because they never need to switch.
- 33:37It sounds really nice.
- 33:38I suspect there's not complete ability to just ignore the old versions because, you know, maybe you want in your new version, you're going to imagine this piece of data doesn't even exist.
- 33:49And we're going to remove it from the database.
- 33:51But if it needs to be there for the old ones, you know, there's probably some level of you're constrained a little bit, but it sounds a lot better still.
- 33:58You're still constrained.
- 33:59But remember, API versioning, if we translate it, you know, application programming interface versioning.
- 34:06We're not versioning the app.
- 34:07We're versioning the interface to the app.
- 34:09Yeah.
- 34:10So a lot of times it's like, what fields are we passing or something, right?
- 34:13If you are trying to change the data between versions, if the data is different, entirely different, I mean, then you're probably not doing API versioning.
- 34:21Then you probably should be calling it application versioning.
- 34:25And actually, Stripe has, I guess, a hack for that too.
- 34:29If you take a look at their API, they still have a V1.
- 34:32But that's the thing.
- 34:33They don't use this V1.
- 34:35They use date-based versioning that is in the headers and their settings.
- 34:38But the V1 is precisely for this situation.
- 34:42If someday they will need to have something else, if someday they will need to break everything and, you know, change the data required or any other crazy changes, they will be able to do it using that V1.
- 34:55Yeah.
- 34:55It's just there in case.
- 34:56Exactly.
- 34:57Yeah.
- 34:57Cool.
- 34:57All right.
- 34:58Well, let's talk about your project.
- 34:59Let's talk about your project that makes this possible in Python because the Stripe examples, I believe this is Ruby that they all have here, right?
- 35:06It is really nice to read until you try to understand it.
- 35:10Until you try to understand what it actually does.
- 35:12And it's crazy.
- 35:13I know.
- 35:13When I was reading this today, I was like, you know, I'm pretty glad that I'm not writing Ruby right now.
- 35:18Okay.
- 35:18All right.
- 35:19Let's talk about Python, though.
- 35:21You've got a project called Cadwin that takes the Stripe philosophy and applies it to versioning for the most popular API framework for Python, FastAPI.
- 35:33Tell us about it.
- 35:34Here's the thing.
- 35:35When I started developing it, we were using versioning by suffering.
- 35:39So we were using directory copies and none of us really enjoyed it.
- 35:43So there needed to be a solution.
- 35:45And I went to Stripe.
- 35:46I went to other companies.
- 35:47I spoke with them.
- 35:49I tried to understand the nitty gritty details.
- 35:52As I mentioned, Brander Leach extended us huge help when he, I guess, described how he thinks about API versioning right now.
- 36:01And I built it.
- 36:03The first version was just like Stripe.
- 36:05It was exactly what Stripe did to the letter.
- 36:08But then at some point, we started applying it at Monite.
- 36:11And I realized, wow, Stripe had a lot of legacy.
- 36:15So they didn't implement some things.
- 36:17For example, in Stripe's article, you can see that they're not using any schema validation or at least, how do I describe it?
- 36:25Their business logic just receives a hash map, a dictionary.
- 36:29It's not statically typed.
- 36:31But FastAPI uses Pydantic.
- 36:33So it's a bit more complex than that.
- 36:35Okay, we added support for that.
- 36:38It's a bit more complex.
- 36:39It's a bit more, I guess, sophisticated now.
- 36:41But then I also realized that Stripe, they don't migrate requests.
- 36:46They only migrate responses.
- 36:47Because a migration of a request is usually not just a breaking change.
- 36:53It's a change in a business logic.
- 36:54Because when you return a response, you can just return the whole thing and then, you know, just play around with it.
- 36:59But when you are changing the request, your business logic also needs to change.
- 37:04And that's what they did.
- 37:06You know, they have extra ifs and extra levels of business logic to handle that.
- 37:10Well, apparently, it's not necessary.
- 37:11Apparently, requests are also migratable.
- 37:14And that's the second feature that greatly distinguishes us from Stripe.
- 37:20This thing can statically, well, by statically, I mean, it co-generates your schema so that you can see everything before you run your app.
- 37:31This thing can migrate both requests and responses.
- 37:34And it supports everything that Stripe does.
- 37:36And we have pretty big plans for extending it.
- 37:40And as an added bonus, we also have pretty big documentation.
- 37:45Well, not compared to Django or FastAPI.
- 37:48Django is, you know, older than me and FastAPI.
- 37:52It's pretty amazing in terms of its documentation.
- 37:55Sebastian Ramirez is an expert at making tutorials.
- 37:58And if you take a look at our reference documentation to the left and our, how do I describe it, recipes documentation,
- 38:04they basically describe to you every single use case that you could have with API versioning.
- 38:13Every single problem that you could solve with it.
- 38:15And then we try to apply our approach to it.
- 38:19So it's not just the framework.
- 38:21It's all the ways you could use the framework.
- 38:24Someday, hopefully, we will also extend it beyond FastAPI and Pydantic.
- 38:29And we also have sections for theory for people who are not Pythonistas, who just want to learn how to do API versioning, but fail to find any resources.
- 38:39So Cadwin also becomes this, I guess, hub for API versioning resources and anything related to them.
- 38:46It seems like it would probably be pretty easy to adapt to anything that uses Pydantic for its data exchange.
- 38:52Oh, yes.
- 38:52Yeah.
- 38:53So like, for example, in Flask, Flask doesn't do the model binding type stuff on the endpoint, but you can just take the input data and just star start into a Pydantic model.
- 39:03And then, you know, now you're in Pydantic world, right?
- 39:06So it sounds like maybe something along those lines might be a good bridge.
- 39:10There's a lot of magic involved in trying to interface with FastAPI simply due to its internal complexity.
- 39:17Right.
- 39:17This portion of Talk Python To Me is brought to you by Sentry.
- 39:22You know Sentry for their error tracking service.
- 39:24But did you know you can take that all the way through your multi-tiered and distributed app with their distributed tracing feature?
- 39:31Distributed tracing is a debugging technique that involves tracking requests of your system, starting from the very beginning like a user action, all the way to the backend, database, and third-party services.
- 39:42This can help you identify if the cause of an error in one project is due to the error in another.
- 39:47Every system can benefit from distributed tracing, but they're especially useful for microservices.
- 39:52In this architecture, logs won't give you the full picture, so you can't debug every request in full just by reading the logs.
- 40:00Distributed tracing with a platform like Sentry gives you a visual overview about which services were called during the execution of certain requests.
- 40:07Aside from debugging and visualizing your architecture, distributed tracing also helps you identify performance bottlenecks.
- 40:14Through a visual like a Gantt chart, you can see if a particular span in your stack took longer than expected and how it could be causing slowdowns in other parts of your app.
- 40:23Learn more and see some examples in the tracing section at docs.sentry.io.
- 40:29To take advantage of all the features of the Sentry platform, just create your free account.
- 40:33And for all of you Talk Python listeners, use the code TALKPYTHON, all one word, and you'll activate a free month of their premium paid features.
- 40:41Get started today at talkpython.fm/sentry-trace.
- 40:46That link is in your podcast player show notes and the episode page.
- 40:49Thank you to Sentry for supporting Talk Python To Me.
- 40:54So some good questions, but I think they'll be somewhat answered if we just maybe talk us through your tutorial here.
- 41:02Like how do we use Cadwyn and how does it manifest?
- 41:05How does it work at runtime?
- 41:07Stuff like that.
- 41:07First, before using Cadwyn, you know, you write your regular app, but then you just structure it a little bit differently.
- 41:13When you are versioning, you need a directory that you will consider versioned.
- 41:19And this directory will be duplicated for every version.
- 41:23No worries, you're not going to support the version code.
- 41:26It's going to be code generation all the way through.
- 41:29And you can use it to generate connectors for your application, or you could use it to generate schemas or enums, you know, the different version of them and anything else related to them.
- 41:39So you pick such a directory, you call it latest as for your latest version.
- 41:44Then you define a single version bundle.
- 41:48If you go a little bit down, you will see somewhere in here after defining versions.
- 41:53Yes, we define our version bundle with a single version, whichever version it is for you, whichever date it is for you.
- 42:00And then you run code generation.
- 42:03It generates a directory that simply imports from your latest.
- 42:07And that's where you can start doing the magic.
- 42:10Now you just use our versioned app.
- 42:13If you take a look, we have a versioned API router.
- 42:17Then you use the Cadwyn version app.
- 42:19You generate version routers.
- 42:23And then it's going to generate a really nice Swagger for them.
- 42:27If you open it somewhere, I think, yep, this one.
- 42:29So now your dashboard, the one on docs, is not the Swagger page, but the dashboard of all your versions.
- 42:36And that's it.
- 42:37After that, you can simply repeat the steps above.
- 42:41So you write a migration, write that converter that describes the difference between the versions.
- 42:47We have it somewhere there.
- 42:49If we had an address as a string and we want to change it to a list, we say, okay, schema, base user, field addresses didn't exist.
- 42:57You have a user object, which is a Pydantic model.
- 42:59The user object in the initial version just had a field called address, which was a string.
- 43:06But you said, you know what?
- 43:07People might have multiple addresses that they live at.
- 43:10So let's make it a list of strings and make it addresses, right?
- 43:15Which seems like a minor change, but clearly that's going to be a major problem for the things that assume there's an address and it's no longer there.
- 43:22Right.
- 43:22So you create this version change derived class that has a upgrade downgrade basically feature, right?
- 43:29You simply describe how do you migrate your schemas?
- 43:32How do you change your schema classes, your Pydantic models?
- 43:36And then you describe how you convert your requests and response.
- 43:40And that's it.
- 43:40This class is the only thing you write for the new versions.
- 43:44Then you run code generation, run your app.
- 43:46That's it.
- 43:47Everything works.
- 43:48Yeah, that's super cool.
- 43:49So a couple of questions that were comments that are interesting here is Emile asks, is it like database migrations?
- 43:56And Mike Fila points out, it's really, really similar to like Alembic, right?
- 44:00You say, generate me the changes.
- 44:02Alembic is a little different in that it looks at the two different database schemas and tries to actually write the migration code to do the DDL changes.
- 44:11But in principle, it's like every time there's a version change, there's some bit of code that is generated that will run.
- 44:18And then the other one is about, you know, how does this affect performance?
- 44:21And I guess the only real performance change is like there'll be a couple of steps to the serialization, but they're often small.
- 44:28Like in this case, the new version takes the old data and says, well, we're just going to have a list with one address.
- 44:33So we're going to stick the one address into the list of addresses.
- 44:37And vice versa, you just set the address field to be the first thing in the address list.
- 44:41And like, that's a really low amount of code overhead to go back.
- 44:45And hopefully people are moving towards the new versions eventually, you know, that kind of thing, right?
- 44:50But if they don't, then this code just runs to transform the dictionaries that goes back to it, right?
- 44:55Imagine you have like Stripe, I don't know, 13 years worth of versions.
- 45:00Now, if you are a successful company, and if you are alive, you are probably a successful company.
- 45:05After 13 years, you know, if you are a successful company, that most of your clients will probably be newer ones.
- 45:11The longer you exist, the more clients you have, and the more clients you have, the more, you know, newer clients you have, the newer API versions they will use.
- 45:19Even if they're old clients, they might decide to, they might rewrite their own app and get a new version, right?
- 45:25Like, they could upgrade their code, because their code is going to be 13 years old or 12 years old at this point.
- 45:30They're like, they need to change too.
- 45:32At some point, yes, your old client might, you know, from the very first version might request something, and then we'll go through all converters.
- 45:40Well, not all, all converters that are related to the specific request and response, and that is probably going to be in the tens, not even in the hundreds, even after 13 years.
- 45:50But your clients from the latest versions, or even from the middle versions, will not suffer that.
- 45:55So the majority of your clients will actually see almost no performance difference.
- 46:01And even your old clients, even your clients for 13 years old will probably, even if they do see a performance difference, it's not going to be multiple orders.
- 46:10It's not going to be 2x, 3x.
- 46:12It's probably going to be maybe 20%, maybe even 10%, depending on how slow your app is.
- 46:17And that's considering how many years have passed is crazy fast.
- 46:22Yeah, absolutely.
- 46:23That's very cool.
- 46:24So what about the URL?
- 46:26What about the endpoint?
- 46:28How much do consumers of your API have to actually think about, I'm using this version or that version?
- 46:36Is it passed as a, is the version passed as a header?
- 46:38Is the query string part of the URL?
- 46:41The version is passed as a header.
- 46:43And Cadwin, by default, requires it to be.
- 46:47But if you have some developer portal, if you want to, you know, store your version in some database,
- 46:52then even though it's a little more advanced, you can write yourself your own middleware that will, you know, check if the client already has a version and then pick that version and put it into the context bar.
- 47:04And it's possible.
- 47:06So by default, you're going to have, no, no, no, middlewares are the more advanced sections, not in the tutorial.
- 47:14There's this other one we're going to talk about in a section.
- 47:16It has just an example of all, you basically just, in FastAPI, when you create the app, you know, like app equals FastAPI, you just say middleware equals a list of classes that implement these behaviors.
- 47:28So what you're suggesting is, like, you just create a class and you pass it in at FastAPI startup, and it just goes, is there a version being passed to me?
- 47:36If not, let's see who the user is based on their API key, pull that from a database, and maybe just set that header right there before the request and then let it fly, right?
- 47:45Something like that?
- 47:45Yep.
- 47:46And then your API will essentially be Stripe-like, where your client won't need to think about versioning at all.
- 47:51But by default, of course, the clients need to always pass it with a header, which might sound like something really unnecessary and hard for the clients.
- 48:01But if you imagine that you're writing code, it's exceptionally easy to just put it into your, I don't know, requests or HTTPX client and forget about it, you know?
- 48:11It is pretty easy.
- 48:12If you're consuming APIs in the fashion that we're talking about, you're probably already putting an API key or some kind of thing into the header as well, right?
- 48:23Like, so you're already messing with the header anyway.
- 48:25Just put another value in there.
- 48:27Precisely.
- 48:27All right.
- 48:27So right now, this is a FastAPI thing, but you're considering opening up to other areas, other platforms, other web frameworks?
- 48:36It has a, what is it called, a milestone for that, which I am planning to extend with tasks, with plans.
- 48:44First, you know, it's like with Pydantic, you know, at some point it separated the core when it was moving to Pydantic 2.
- 48:51And that's one of the plans for Cadwin, you know, separate the core without breaking the public interfaces.
- 48:56And then at some point use this core to build more Cadwins, you know, Cadwin Django, Cadwin Flask, whatever.
- 49:02Or it doesn't even have to be a part of Cadwin.
- 49:05The approach is there so anyone can use it.
- 49:08Because 99% of complexity in implementing Cadwin was actually figuring out these patterns and understanding how to do it.
- 49:18Because unlike a web app, unlike, I don't know, many apps that we develop daily, it doesn't, these things haven't been done.
- 49:27People have migrated stuff, but it was mostly, you know, oh, change this dictionary without a proper DSL or without code generation, you know, without the niceties that would make your IDs happy.
- 49:41And that would make, you know, auto-completion happy.
- 49:44Yeah, you are a contributor to TypeShed, which is awesome.
- 49:48So you must care about, you know, conveying the type information of the different versions and stuff like that.
- 49:56That's tricky, right?
- 49:57And of course, working on FastAPI and Pydantic, those are very type.
- 50:00Oh, and the shirt says TypeHints for Life.
- 50:03I love it.
- 50:03I don't have any cool slogan in my eyes to have gray stripes, but I should.
- 50:07Very cool.
- 50:08So yeah, want to speak to the TypeHint type information aspect of this whole thing?
- 50:13Essentially, as I mentioned, Stripe did it using, you know, simply dictionaries.
- 50:19And it was mostly untyped.
- 50:22As probably many people know, Stripe, after having so many problems with scalability in terms of like the team scalability and problems with typing,
- 50:32they have decided to adopt Sorbet.
- 50:35Well, not adopt, create Sorbet, which is essentially a type checker.
- 50:41It's like a type checker and DSL for Ruby for TypeKins, which is really nice.
- 50:46It has both the runtime component and a static component, as far as I remember.
- 50:51But they still, most of the time, work with untyped code, which essentially can lead to many errors.
- 50:57Cadwin tries to make sure that you still work with your schemas.
- 51:02You still know which schema you work with.
- 51:03It's always the latest.
- 51:04And Cadwin tries to make sure that you're not going to have any type errors if you don't want to.
- 51:12And you're going to have all of the niceties of having this dynamic request response conversion thing.
- 51:19So we're trying to take the best of both worlds, have you support very little code, but like make sure that you can type in everything without any issue and you're not going to have problems and your type checker is going to check everything for you.
- 51:31And your VS Code is going to see which field something has, which type something has.
- 51:37Excellent.
- 51:37One of the big problems with like all the runtime modifications, like one way you could have done this is that app startup, it could look at all of those things and it could just dynamically generate all the different versions and all the things, you know, using meta programming.
- 51:54But then the editors hate that.
- 51:55Yeah.
- 51:56Yeah.
- 51:56It does that with the endpoints because endpoints are not your editors.
- 52:01They don't care about endpoints.
- 52:02You never call your endpoint from your code.
- 52:06No, you usually call the business logic from it.
- 52:08Yeah.
- 52:08Which is why I decided to do it in runtime.
- 52:10You don't really need it.
- 52:11But for schemas, yes.
- 52:12For schemas, having them generated and then having people guessing and then having their IDs just put everything in red.
- 52:20That is horrible.
- 52:21I wouldn't want anyone to have that.
- 52:23Yeah.
- 52:23Yeah.
- 52:23I don't like it either.
- 52:24Not at all.
- 52:25It's right up there with the star, star, star, star, KW orgs.
- 52:28Have a good time guessing in my world.
- 52:31I don't like either of them.
- 52:32Awesome.
- 52:33All right.
- 52:33Well, let's just maybe touch real quickly on a couple of other choices that are out here.
- 52:39You know, Cadwin looks awesome.
- 52:40But we also have, for example, we have from Dean Way, we have FastAPI versioning, which has a very different philosophy.
- 52:47It has kind of the V1, the V2 style that you might maintain multiple versions of.
- 52:54You want to just maybe speak to your thoughts on this one real quick and that people can take that if they like this style.
- 52:59I think it's either the first or the most popular.
- 53:01It's definitely the most popular, but I think it might even be the first library in FastAPI to implement versioning, implement proper versioning.
- 53:08It has a few issues.
- 53:10First of all, it only as long, as far as I remember, it is mostly duplication based.
- 53:15So it doesn't automate anything for you, which is a good thing if you know what you're doing and you're not going to have a lot of versions.
- 53:22If you have 10 or 20 versions, if you want to keep them for a long time, this is the, you know, endpoint point copying approach that I mentioned that with time is going to kill you, which is a problem here.
- 53:35The second problem is that it, as far as I understand, kind of recommends semantic versioning, though it doesn't bind you to it.
- 53:43You can use dates, though you have to make sure that they're dates yourself.
- 53:47And you can use just simple integers.
- 53:50So you're not bound by semantic versioning, but it has a few examples with that.
- 53:55Aside from these two, it's an amazing library.
- 53:58And for anyone who just wants to version a small app, you know, just add a single version that is going to be, you know, the old version is going to be deleted in like two months.
- 54:06Use that.
- 54:06Use FastAPI versioning.
- 54:08It's an amazing library and you don't need anything else.
- 54:11Catwin is for use cases where you need to keep a larger number of versions or if the changes between your versions are much trickier.
- 54:21Yeah, like this might be pretty good for my example I talked about with the courses app that we built, right?
- 54:27We literally have two versions.
- 54:28It's been around since 2018 and there's two versions.
- 54:31You know, like that would be a decorator and I'm probably good to go.
- 54:35I don't need to think too much about it.
- 54:36Precisely.
- 54:37No code generation, no nothing.
- 54:38You just, you know, you just put it in and you go.
- 54:41Another one.
- 54:42If you're a Flask person, you could have Flask rebar, which seems to have a real similar philosophy to what you just described, but, you know, works for Flask.
- 54:52I feel like it's a little more complex than that.
- 54:56It is also the parent point.
- 54:58So it also allows you to duplicate single endpoints.
- 55:02It also allows you to do arbitrary prefixes.
- 55:05So you can have integers, you can have dates, you can have, you know, whatever.
- 55:09Old, new, whatever.
- 55:10Yeah.
- 55:11Yeah.
- 55:11Old, new.
- 55:12I guess some people do that.
- 55:13If you want.
- 55:15Yeah.
- 55:16Latest, latest.
- 55:17Yeah.
- 55:17Yeah.
- 55:18Latest, really latest, final.
- 55:19Like a new folder, new folder.
- 55:21Yes, exactly.
- 55:22But yeah, it's really similar.
- 55:25And I guess.
- 55:26They probably use zipping up folders for version control as well.
- 55:29I'm just naming them by the date.
- 55:30Final, final, final.
- 55:32Let's not use, let's not use Git.
- 55:34Let's, let's just copy them.
- 55:36As for versioning, that's like a really good metaphor.
- 55:38If you are a person that just needs another version of their Google Doc, you wouldn't use
- 55:43Git.
- 55:44Well, unless you use Git for everything else.
- 55:46And same here.
- 55:47You don't need a huge combine for using Flask.
- 55:51And Flask usually, at least in my experience, tries to build simpler APIs.
- 55:56And you wouldn't use, well, most companies that do API first don't do, don't use Flask.
- 56:02They try to do, you know, Django DRF or FastAPI, which is why this library seems like a perfect
- 56:08choice for that.
- 56:09Yeah, indeed.
- 56:09It seems really nice.
- 56:11One of the things that we were speaking of duplication, one of the things that they just
- 56:16jump right into to demonstrate here.
- 56:18And I imagine the FastAPI version one as well could do it.
- 56:22But there's one endpoint that doesn't change.
- 56:25And there's one that is different.
- 56:26So they copy the one that's different.
- 56:28But for the one that doesn't change, they just put a V1 and a V2 decorator on that one.
- 56:33And it, it remains unchanged, basically, right?
- 56:35Cadwyn tries to escape this kind of verbosity.
- 56:38And essentially, you have all endpoints everywhere.
- 56:41And if you want to, you know, omit some endpoint or change some endpoint or add some endpoint,
- 56:45into the new version, you just add a version change and say, oh, this endpoint didn't exist
- 56:51in the old version.
- 56:52That's it.
- 56:53Very cool.
- 56:53Yeah, because if you had 20 of these, you'd have 20 decorators in the little function.
- 56:57Right?
- 56:58That's where it starts to get.
- 56:59Not a huge issue for Flask.
- 57:01I haven't seen anyone building, I don't know, 50 or 200 endpoint apps with Flask.
- 57:07I'm sure people do that.
- 57:08But I think it's the minority.
- 57:10Other side of the fence, Django REST framework, DRF, you already mentioned them a little bit.
- 57:14And so Django, they already have like versioning built into the framework itself, right?
- 57:21They have versioning interfaces built into.
- 57:23So like, it's not, they're not implementing versioning for you, but they give you all the
- 57:29tools to implement your own versioning.
- 57:30Yeah.
- 57:31Okay.
- 57:31So they kind of have the building blocks for it.
- 57:33Excellent.
- 57:33Exactly.
- 57:34All right.
- 57:34Well, I think we're pretty much out of time, but thank you for being here.
- 57:38It's been really interesting.
- 57:39Oh, it was for me as well.
- 57:41Thank you for having me here.
- 57:42Yeah.
- 57:43You've given everyone a lot to think about, you know, before we wrap it up, two quick
- 57:46things, you know, notable PyPI package.
- 57:49I mean, we've already given Cadwin a shout out.
- 57:51Anything else that like, oh, I came across this and it was super cool.
- 57:55Maybe it deserves a little bit of a shout out.
- 57:57Anything that like you use a lot that you would recommend?
- 57:59For us in our company, we started using one more package of mine.
- 58:04It's called pytest Fixture Classes.
- 58:06Okay.
- 58:06Essentially, when you are building groups of fixtures that create some objects for you,
- 58:12for you to use in your tests, and then all of a sudden, one of your tests needs an object
- 58:16that's a little different, you know, just a little bit.
- 58:19And then everything just falls apart.
- 58:21Yeah.
- 58:21A factory fixture pattern that you can see in pytest allows you to, you know, make a function
- 58:28within a function and one of them is a fixture and that's all cool and nice, but then it's
- 58:34not typed.
- 58:35You can use, you know, the callable syntax and then you're not going to have the keyword arguments
- 58:40there.
- 58:41And otherwise you can write a protocol and it's a lot of code.
- 58:45What pytest Fixture Classes does is it allows you to type everything there.
- 58:50So you write the same amount of code, but it's all typed.
- 58:54And now you have, you can have groups of fixture classes building on top of each other, which
- 58:59is a nice pattern to have, but a really bad pattern to abuse.
- 59:03Very cool.
- 59:04All right.
- 59:04Well, nice, nice recommendation.
- 59:06People can check that out.
- 59:07Emil, an audience already says, amazing thing.
- 59:10I'm already using it.
- 59:11So final call to action.
- 59:12People have APIs.
- 59:14They've listened to us talk for an hour and they're like, oh, we should be versioning our
- 59:19APIs.
- 59:19We probably should be doing something here.
- 59:22What do you recommend people do?
- 59:24How do they get started?
- 59:25How do they go further?
- 59:26So first you need to decide whether your use case is simple.
- 59:29How many versions are you going to support?
- 59:31If you're not planning to support more than two versions, or if you are supporting versions
- 59:36only for your internal microservices or only for your front end, then consider using the
- 59:43simple tools.
- 59:43Consider using the FastAPI versioning, you know, fast rebar, things like that.
- 59:49But then once you realize that you need to support versions for months and you need three
- 59:55or more versions and you're providing your versions as a public, you know, to the public,
- 1:00:00then I advise moving straight into Cadwin or Cadwin-like frameworks.
- 1:00:05If you use FastAPI, you can just use Cadwin.
- 1:00:08It has a bit of a learning curve, but within making one version, you will realize just how
- 1:00:13simple it is.
- 1:00:13And if you're not using Cadwin, that's fine as well.
- 1:00:17I have all of the resources that I use to build it and all of the resources, you know,
- 1:00:23describing different ways of API versioning there in the repository in the theory section.
- 1:00:28So you can just read that.
- 1:00:30And even if you don't like that approach, we describe everything else there.
- 1:00:34So you'll be able to pick the one you like.
- 1:00:36But my personal recommendation, don't pick the directories.
- 1:00:39They're painful.
- 1:00:41Awesome.
- 1:00:41Really good advice.
- 1:00:43Super cool project.
- 1:00:44So thank you for being here, Stanislav.
- 1:00:46And talk to you later.
- 1:00:48Talk to you later.
- 1:00:48This has been another episode of Talk Python To Me.
- 1:00:52Thank you to our sponsors.
- 1:00:53Be sure to check out what they're offering.
- 1:00:55It really helps support the show.
- 1:00:58It's time to stop asking relational databases to do more than they were made for and simplify
- 1:01:03complex data models with graphs.
- 1:01:06Check out the sample FastAPI project and see what Neo4j, a native graph database, can do
- 1:01:12for you.
- 1:01:12Find out more at talkpython.fm/Neo4j.
- 1:01:18Take some stress out of your life.
- 1:01:20Get notified immediately about errors and performance issues in your web or mobile applications with
- 1:01:26Sentry.
- 1:01:26Just visit talkpython.fm/sentry and get started for free.
- 1:01:31And be sure to use the promo code talkpython, all one word.
- 1:01:34Want to level up your Python?
- 1:01:37We have one of the largest catalogs of Python video courses over at Talk Python.
- 1:01:41Our content ranges from true beginners to deeply advanced topics like memory and async.
- 1:01:46And best of all, there's not a subscription in sight.
- 1:01:48Check it out for yourself at training.talkpython.fm.
- 1:01:51Be sure to subscribe to the show.
- 1:01:53Open your favorite podcast app and search for Python.
- 1:01:56We should be right at the top.
- 1:01:58You can also find the iTunes feed at /itunes, the Google Play feed at /play,
- 1:02:03and the direct RSS feed at /rss on talkpython.fm.
- 1:02:07We're live streaming most of our recordings these days.
- 1:02:10If you want to be part of the show and have your comments featured on the air,
- 1:02:13be sure to subscribe to our YouTube channel at talkpython.fm/youtube.
- 1:02:18This is your host, Michael Kennedy.
- 1:02:20Thanks so much for listening.
- 1:02:21I really appreciate it.
- 1:02:22Now get out there and write some Python code.
- 1:02:24I'll see you next time.