Skip to content

Instantly share code, notes, and snippets.

@iolloyd
Last active August 27, 2019 19:19
Show Gist options
  • Save iolloyd/4531281 to your computer and use it in GitHub Desktop.
Save iolloyd/4531281 to your computer and use it in GitHub Desktop.
Dynamically generating endpoints based on model in flask
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