Comments (4)
Thank you! It now work for me. I add functools.update_wrapper for copy doc from original, example:
def gen_func(func, summary=None, description=None, operation=None):
f = partial(func)
if description is None:
update_wrapper(f, func)
description = f.__doc__
else:
f.__doc__ = description
return op.definition(summary=summary, description=description, operation=operation)(f)
.............
views.append(type(f"VIs_{name}",
(HTTPMethodView,),
{'model': model,
'_url': f'/{name}/',
'get': gen_func(viewItems_get,
summary=f'List of obj {name}',
description=f"List of objects {name}",
operation=f'get_{name}')}))
Also next I add data of body and response.
from sanic-ext.
I guess I am not sure what the problem is. What would the expected outcome be?
from sanic-ext.
I guess I am not sure what the problem is. What would the expected outcome be?
I'm trying just use template op.summary(f"Get obj {name}") with list of names.
in https://github.com/sanic-org/sanic-ext/blob/main/sanic_ext/extensions/openapi/openapi.py line 412-422
if summary:
func = glbl["summary"](summary)(func)
There 'func' is key to collect data.
But I have many dynamic view classes from same func-methods
type(f"VI_{name}",
(HTTPMethodView,),
{get': op.summary(f"Get obj {name}")(viewItem_get),....
Where viewItem_get is func.
so ...
I have one last summary for all definitions in json of openapi. With last 'name' for all.
from sanic-ext.
I think I see what you are after here. I am not sure that we are likely to change this as it would potentially have a lot of complications for a narrow use case. Especially since I think there are a few alternatives that will get you there.
Using partial
Perhaps the easiest solution is to just wrap your functions in partial
. That creates the function as a new/unique instance.
from functools import partial
from sanic import Blueprint, Sanic
from sanic.views import HTTPMethodView
from sanic_ext import openapi
app = Sanic(__name__)
bp = Blueprint("auto")
app.blueprint(bp)
def get(*_):
...
def put(*_):
...
def patch(*_):
...
def delete(*_):
...
for name in ["foo", "bar"]:
view = type(
f"VI_{name}",
(HTTPMethodView,),
{
"_url": f"/{name}",
"get": openapi.summary(f"get - {name}")(partial(get)),
"put": openapi.summary(f"put - {name}")(partial(put)),
"patch": openapi.summary(f"patch - {name}")(partial(patch)),
"delete": openapi.summary(f"delete - {name}")(partial(delete)),
},
)
bp.add_route(view.as_view(), view._url)
Factory
Personally, I would (and do) opt for this approach as it is much easier to read.
from sanic import Blueprint, Sanic
from sanic.views import HTTPMethodView
from sanic_ext import openapi
app = Sanic(__name__)
bp = Blueprint("auto")
app.blueprint(bp)
def create_view(name, bp):
class CustomView(HTTPMethodView, attach=bp, uri=f"/{name}"):
@openapi.summary(f"get - {name}")
def get(*_):
...
@openapi.summary(f"put - {name}")
def put(*_):
...
@openapi.summary(f"patch - {name}")
def patch(*_):
...
@openapi.summary(f"delete - {name}")
def delete(*_):
...
for name in ["foo", "bar"]:
create_view(name, bp)
If you are doing this, you probably also want to se operation
so that each one has a unique explicit name:
@openapi.summary(f"get - {name}")
@openapi.operation(f"get_{name}")
def get(*_):
...
@openapi.summary(f"put - {name}")
@openapi.operation(f"put_{name}")
def put(*_):
...
@openapi.summary(f"patch - {name}")
@openapi.operation(f"patch_{name}")
def patch(*_):
...
@openapi.summary(f"delete - {name}")
@openapi.operation(f"delete_{name}")
def delete(*_):
...
Or, just combine it:
@openapi.definition(
summary=f"get - {name}",
operation=f"get_{name}",
)
def get(*_):
...
@openapi.definition(
summary=f"put - {name}",
operation=f"put_{name}",
)
def put(*_):
...
@openapi.definition(
summary=f"patch - {name}",
operation=f"patch_{name}",
)
def patch(*_):
...
@openapi.definition(
summary=f"delete - {name}",
operation=f"delete_{name}",
)
def delete(*_):
...
I am closing this as I think you can achieve what you want already. LMK if this works for you or not.
from sanic-ext.
Related Issues (20)
- [Bug] Wrong default variables type of Server object.
- [Feature Request] Blueprints cannot access the 'decorator way' of managing / rendering templates
- Allow the option to define a template directory in a blueprint HOT 1
- [Bug] Dependency injection not working for requests received during server is initializing HOT 1
- [Question] How to set field description on data class?
- [Question] OpenAPI spec available on all subdomains, and route exclusion without decorators does not seem to work.
- [Bug] Dependeny Injection throw exceptions by default if @classmethod constructors don't return value/objects
- [Feature Request]
- [Bug] OpenAPI definition does not include placeholder for "headers"
- [Bug] Json validator throws 500 instead of 400
- [Feature Request] Response Compression
- [Bug] Can't use `pydantic>=2.0` model in OpenAPI definition HOT 1
- [Bug] Can't document/exclude websocket routes with OpenAPI decorator
- [Bug] Subheader "parameter" missing in sanic extensions document
- [Bug] Can't apply openapi decorators to blueprintss' routes
- [Bug]TypeError: listener() got an unexpected keyword argument 'priority' HOT 1
- [Bug] Documentation: dead link to the setting
- Query params validation is completely broken HOT 2
- Question regarding licensing info
- [Bug] Doubly defined objects in the output specification #260 HOT 2
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 sanic-ext.