Code Monkey home page Code Monkey logo

Comments (6)

nicolaiarocci avatar nicolaiarocci commented on May 10, 2024

I'm on it.

Current draft offers two events on_hitting_endpoint(url, method) and on_endpoint_hit(url, method) to which it is possible to subscribe with multiple listeners (if you're confident with C# EventHandler, that's the model). Wondering if events should be more granular/specialized, like in on_getting(url), on_patching() etc.

Also, would the url be enough, or should stuff like request.headers and request.data be returned to the callback functions?

I'm leaning toward more specialized events, also providing more detailed data. Thoughts?

from eve.

sgardini avatar sgardini commented on May 10, 2024

About the returned data I think the url are enough, the developer will pick them only if needed.
Instead I think some specialized events can be useful.

from eve.

inactivist avatar inactivist commented on May 10, 2024

I prefer maximum flexibility/granularity here if it can be done without excessive complication/complexity.

Also, would the url be enough, or should stuff like request.headers and request.data be returned to the callback functions?

Yes, please provide request data and other contextual data to the callbacks -- why not just forward request as one of the callback parameters? Why filter out anything?

Will an event callback be able to terminate endpoint processing by raising an exception or via a return value to the caller?

from eve.

nicolaiarocci avatar nicolaiarocci commented on May 10, 2024

Hmm on second thought I'm wondering if we really need to reinvent the wheel.

Flask already supports callback functions via decorators such as before_request and after_request. Being a subclass of Flask, Eve supports this mechanism too, and it's pretty darn powerful. The catch is that you need to be quite familiar with Flask internals, so for example if you want to inspect the request object you have to explicitly import it from flask:

from flask import request
from eve import Eve

app = Eve()

@app.before_request
def before():
    print 'the whole request object, ready to be processed:', request
    ...

@app.after_request
def after(response):
    print 'and here we have the response:', response
    ...
    return response        

if __name__ == '__main__':
    app.run(debug=True)

Don't see the point in re-engineering this... what do you guys think? We could add this snippet (or a more complete one) to the documentation, or maybe to the examples folder.

from eve.

inactivist avatar inactivist commented on May 10, 2024

Flask already supports callback functions via decorators such as before_request and after_request. Being a subclass of Flask, Eve supports this mechanism too, and it's pretty darn powerful. The catch is that you need to be quite familiar with Flask internals, so for example if you want to inspect the request object you have to explicitly import it from flask[...]

I'd prefer to re-use Flask native features if possible! I agree that re-inventing the wheel is not desirable.

I think anyone using Eve is going to have to know about how Flask works at some level (and features like @before_request and @after_request) -- I don't consider myself a Flask guru and I've used those features.

Documentation guidance will help, of course.

But: does the proposed solution address the specific use case described in the original issue request? I think so... though it may require more 'plumbing' than the OP wanted.

from eve.

nicolaiarocci avatar nicolaiarocci commented on May 10, 2024

I made a repo out of the work done while working at this issue: Events, C#-Style Events in Python.

from eve.

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.