Coder Social home page Coder Social logo

Comments (4)

charoduke avatar charoduke commented on July 2, 2024 1

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.

2022-06-01_11-11-47

from sanic-ext.

ahopkins avatar ahopkins commented on July 2, 2024

I guess I am not sure what the problem is. What would the expected outcome be?

from sanic-ext.

charoduke avatar charoduke commented on July 2, 2024

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.

ahopkins avatar ahopkins commented on July 2, 2024

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)

image

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.