Comments (9)
@delijati Ok, I didn't get the point.
Now I'm writing from my smartphone.
I take in charge this issue and analyze it tomorrow.
I'll let you know.
from fastapi-router-controller.
@KiraPC thanks for taking a look into it :)
from fastapi-router-controller.
Nice Thanks. What i see looks like what i was expecting :) I will give it a try after eastern :)
from fastapi-router-controller.
I'll merge the PR tomorrow. So publish on pypi.
from fastapi-router-controller.
Hi @delijati, thank you for using this library.
To reply to your issue, you are defining "x" as Class Attribute, so that's correct that you can use that variable from the instance of that class.
If you want to use that variable you should do something like this 'SampleController.x.create()'.
Instead, if you want to define 'x' as Instance Variable, you can declare it in the class init function.
Let me share with you also an useful link where someone can explain better that me this python property.
https://www.toptal.com/python/python-class-attributes-an-overly-thorough-guide
Please, just let me know if I can close the issue.
from fastapi-router-controller.
No that is not the problem. Both should work SampleController.x
and self.x
-> Even if i change self.x
to SampleController.x
i get the same error:
AttributeError: 'Depends' object has no attribute 'create'
@dmontagu does some magic to get Depends
working in https://github.com/dmontagu/fastapi-utils/blob/master/fastapi_utils/cbv.py
from fastapi-router-controller.
All good thanks for taking the time :)
from fastapi-router-controller.
Hi @delijati
From my PC the point is more clear.
I also read the fastapi-utils documentation and understood what is your expected behavior.
Since fastapi-router-controller is not a parting of the fastapi-utils CBV class, I didn't thought at this feature when I implemented it.
Anyway, If this could your work, I'll implement it as a new feature in a future version.
I'll do my best to delivery it as soon as possibile, in the meanwhile you can still use the standard fastapi way to declare a dependency.
@controller.resource()
class SampleController:
x = Depends(get_x)
@controller.route.get(
"/", tags=["sample_controller"], summary="return a sample object",
response_model=SampleObject
)
def sample_get_request(
self,
id: str = Query(...),
x = Depends(get_x)
):
print(x.create())
return SampleObject(id=id)
from fastapi-router-controller.
@delijati I've create a PR with the enhancement requested.
Are you able to check if it work before merging it?
Let me explain how I thought this feature should works.
As opposed to fastapi-utils cbv, I don't like to set the dependecies as python attribute class, in order to don't create confusion when you use them.
So, you can define now the dependecies as instance properties in the init function.
from fastapi import APIRouter, Depends
from fastapi_router_controller import Controller
router = APIRouter()
controller = Controller(router)
async def amazing_fn():
return 'amazing_variable'
@controller.resource()
class ExampleController():
# you can define in the Controller init some FastApi Dependency and them are automatically loaded in controller methods
def __init__(self, x: Foo = Depends(amazing_fn)):
self.x = x
@controller.route.get(
'/some_aoi',
summary='A sample description')
def sample_api(self):
print(self.x) # -> amazing_variable
return 'A sample response'
Do you think that can solve your problem?
from fastapi-router-controller.
Related Issues (5)
- Add black and flake8 as linter check HOT 1
- Supporting Multi-Parent HOT 5
- Decorator on the endpoint HOT 5
- Setup.py has no version HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from fastapi-router-controller.