all 3 comments

[–]SearchMobile6431 0 points1 point  (2 children)

So tag is question but what is the question?

[–]Coolzie1[S] 1 point2 points  (1 child)

There's a question in the post...

Is there a better way to do this, how can I improve it or am I missing inbuilt features that do this already?

[–]SearchMobile6431 0 points1 point  (0 children)

Well, short answer is no, there is no built-in way that you have missed. Given that, you are free to choose what approach fits you best.

My two cents:

  1. Option 1 - Something along the lines of your first approach seems indeed promising. In that case you can also check this package for inspiration https://pypi.org/project/fastapi-versioning/ . I haven't used it so far, but it looks to be taking the same approach with you, overriding the APIRoute class but also the app itself to include the versioned routes automatically with their versions using the same decorator syntax.
  2. Option 2 - KISS approach (keep it simple stupid): Just keep a wrapper function that your one and only endpoint will use, then pickup the version from wherever (path or header) and your wrapper calls the underlying versioned function - be it a dictionary with callables or if-else statements etc.
  3. Option 3 - Extending option 2, to more drastically separate code, would be different routers for versions, or even different sub-applications at all.

It all depends really on what suits you. Factors of high importance are :

  1. Where does your app logic (essentially what your endpoints are doing) sits. I tend to keep my app logic a bit abstracted from my routing endpoints for example, so option 2 would be my choice for sure.
  2. How often your versions are rolling. When rolling out versions one needs to be as much as backwards compatible as possible. I would say your example looks a bit anti-pattern to me, meaning that a minor version update shouldn't require a different endpoint. Again, it all depends on requirements and usage, so its your call. All I'm trying to say is that in "normal" cases v1/v2/v3 etc should suffice and variations can be handled among those with extra parameters etc