Last active
August 27, 2019 19:19
-
-
Save iolloyd/4531281 to your computer and use it in GitHub Desktop.
Dynamically generating endpoints based on model in flask
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def set_end_point(view_class, endpoint, url, pk='id', pk_type='int'): | |
view = view_class() | |
app.add_url_rule(url, defaults={id: None}, view_func=view.as_view, methods=['GET',]) | |
app.add_url_rule(url, view_func=view.as_view, methods=['POST',]) | |
url = '%s/<%s:%s>' % (url, pk_type, pk) | |
app.add_url_rule(url, view_func=view.as_view, methods=['GET', 'PUT', 'DELETE']) | |
def register_url(name): | |
url = '/' + name | |
endpoint = name+'_api' | |
name = name.title() + 'API' | |
class NewClass(object): | |
def as_view(self, endpoint, id=None): | |
pass | |
NewClass.__name__ = name | |
set_end_point(NewClass, endpoint, url) | |
return NewClass | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment