Code Monkey home page Code Monkey logo

Comments (5)

hfaran avatar hfaran commented on September 27, 2024

I believe I have an idea for this.

Have a container with API much like tornado.gen.Task, as follows:

class Callback(object):
    def __init__(self, func, *args, **kwargs):
        ...

with usage as follows (assuming there exists some function, get_users, which can query the DB/cache for list of registered users and return them):

@schema.validate(
    input_schema={
        "type": "object",
        "properties": {
            "user": {
                "enum": [
                    Callback(get_users)
                ]
            },
        },
        "required": ["user"]
    }
)
def post(self):
    ...

that would be resolved whenever this post method is called and the schema would have all Callback instances replaced with the results of func(*args, **kwargs).

Now, introducing this would make schema.validate immediately slower because it would always recurse through the schema and check for Callback instances to resolve. To avoid this, I think adding a dynamic=False kwarg to schema.validate will be added, and only if dynamic is True, will schema.validate recurse through the schema to resolve it.

So in finality, your snippet would end up looking something like:

@schema.validate(
    input_schema={
        "type": "object",
        "properties": {
            "user": {
                "enum": [
                    Callback(get_users)
                ]
            },
        },
        "required": ["user"]
    },
    dynamic=True
)
def post(self):
    ...

Does this sound reasonable?

from tornado-json.

hfaran avatar hfaran commented on September 27, 2024

Implementation detail: the actual call will be done through tornado's yield gen.Task(...) pattern asynchronously.

from tornado-json.

normanjaeckel avatar normanjaeckel commented on September 27, 2024

Ok, sounds nice. Two remarks:
Please think about that I may be want to get different properties from different sources like via AsyncHttpClient oder using Motor (with MongoDB). And maybe I have to prepare the received data in Tornado after fetching them asynchroously.
What about the api documentation? Do you want to run the callback or add just a general hint?

from tornado-json.

hfaran avatar hfaran commented on September 27, 2024

Please think about that I may be want to get different properties from different sources like via AsyncHttpClient oder using Motor (with MongoDB). And maybe I have to prepare the received data in Tornado after fetching them asynchroously.

Right, so this would work if the function that did this work was called asynchronously by tornado.gen.Task, no?

class SchemaCallback(object):
    def __init__(self. func, *args, **kwargs):
        self.func = func
        self.args = args
        self.kwargs = kwargs

My idea was just to have SchemaCallback be a container class and then inside schema.validate, recurse through the schema and yield gen.Task(func, *args, **kwargs) any instances of it to resolve the full schema.

What about the api documentation? Do you want to run the callback or add just a general hint?

Maybe this could be the __str__ of the SchemaCallback?

from tornado-json.

normanjaeckel avatar normanjaeckel commented on September 27, 2024

I am new to this so to understand everything here I must see a full example how to use may be AsyncHttpClient().fetch(...) in the function. Maybe it is really easy. Let's see.

For the documentation __str__ or even better something like get_api_documentation_content() which refers to an optional init argument and only to __str__ by default would be ok.

from tornado-json.

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.