question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Question regarding protected routes, Depends() and decorators.

See original GitHub issue

Hi.

I am in the process of moving away from using Flask and Sanic for Web APIs for my Python projects, and would like to start using FastAPI instead as it seems more professional and more suitable for larger projects (in my opinion).

So far I like this library better than the two I have used before, except for one thing.

I have been investigating the documentation of FastAPI regarding protected routes, and was wondering if it is possible to protect a route without passing Depends() as a parameter to the protected function? No offense, but in my personal opinion this makes the syntax look dirty compared to using decorators, or protecting routes by context group such as in Laravel (PHP).

My question is therefore, are there any other possible ways to protect routes somewhat like the example below:


USER = Depends(get_user(fake_users_db, username=username))

@app.get("/profile")
async def get_profile(current_user=USER):
    return current_user.username

Or is it possible to create a decorator for Depends() that can be used instead (similar to what Flask has)?

The main thing is that I do not want to pass Depends() as a parameter like this:

@app.get("/login_basic")
async def login_basic(auth: BasicAuth = Depends(basic_auth)):
    ...

I know this is not an issue and is intended to be like this in the project, but this question is asked out of personal preference of code styling/formatting.

Edit: Additional question since I mentioned Laravels way of handling routes. Would it be possible to create something similar to this?

Route::middleware('auth:api', 'throttle:20,1')->group(function() {
    Route::get('/usage',   'AbcController@usage')->name('usage-route');
    Route::get('/search/{searchQuery}', 'SearchController@search_method')->name('search-route');
});
from AbcController import Usage
from SearchController import Search

app.add_route_group('authRoutes', [
	app.add_route("/usage", Usage())
	app.add_route("/search/{searchQuery}", Search(...))
])

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

7reactions
mRokitacommented, Sep 11, 2020

I would recommend using the dependencies argument.

@app.get(
    '/restricted',
    dependencies=[Depends(basic_auth)],
)
def restricted():
   ...
2reactions
Kludexcommented, Sep 11, 2020

You can create the decorator, just use functools.wraps to not lose the function signature.

And you can use the first approach as well, but not in the way you have written, unless get_user(...) returns a callable. If does, then you can do the way you want.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Router Guards • Angular - codecraft.tv
Guards return either true if the user can access a route or false if they can't. They can also return an Observable or...
Read more >
ASP.NET MVC Interview Questions - C# Corner
Most Asked ASP.NET MVC Interview Questions and Answers. Here is a detailed article on Creating a Simple Application Using MVC 4.0.
Read more >
How to add a custom decorator to a FastAPI route?
So my question is - how can I add any decorators to FastAPI endpoints? Should I get into router.routes and modify the existing...
Read more >
Testing Dependencies with Overrides - FastAPI
You can set a dependency override for a dependency used anywhere in your FastAPI application. The original dependency could be used in a...
Read more >
Angular Interview Questions — You Must Know (2022) - Medium
We have collected over 200+ Angular Interview Questions from ... So you can have your services depend on other services throughout your ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found