Commit fc680e70 authored by Nicolas Joyard's avatar Nicolas Joyard

Sectionnement API

parent 84b64083
......@@ -17,7 +17,8 @@ class API(object):
def summary(self):
descs = {k: {'desc': v.desc, 'count': v.model.query.count()}
descs = {k: {'section': v.section, 'stable': v.stable, 'desc': v.desc,
'count': v.model.query.count()}
for k, v in self.registry.items()
if not v.hidden}
return collections.OrderedDict(sorted(descs.items()))
......@@ -26,7 +27,8 @@ class API(object):
ep = Endpoint(self,, model, **kwargs)
self.registry[ep.table] = ep
def get_endpoint_or_404(self, table):
def get_endpoint_or_404(self, section, stable):
table = '{}_{}'.format(section, stable)
if table not in self.registry:
return self.registry[table]
......@@ -34,44 +36,57 @@ class API(object):
def setup_routes(self, app, prefix='/rest/'):
prefix = '/%s/' % prefix.strip('/')
@app.route('%s<table>/' % prefix)
def api_list(table):
endpoint = self.get_endpoint_or_404(table)
@app.route('%s<section>/<stable>/' % prefix)
def api_list(section, stable):
endpoint = self.get_endpoint_or_404(section, stable)
return endpoint.list()
@app.route('%s<table>/meta' % prefix)
def api_meta(table):
endpoint = self.get_endpoint_or_404(table)
@app.route('%s<section>/<stable>/meta' % prefix)
def api_meta(section, stable):
endpoint = self.get_endpoint_or_404(section, stable)
return endpoint.describe()
@app.route('%s<table>/<id>' % prefix)
def api_detail(table, id):
endpoint = self.get_endpoint_or_404(table)
@app.route('%s<section>/<stable>/<id>' % prefix)
def api_detail(section, stable, id):
endpoint = self.get_endpoint_or_404(section, stable)
@app.route('%s<table>/<id>/<custom>' % prefix)
def api_custom(table, id, custom):
endpoint = self.get_endpoint_or_404(table)
@app.route('%s<section>/<stable>/<id>/<custom>' % prefix)
def api_custom(section, stable, id, custom):
endpoint = self.get_endpoint_or_404(section, stable)
return, custom)
def split_table(self, table):
return table.split('_')[0], table.split('_', 1)[1]
def list_url(self, table):
return url_for('api_list', table=table, _external=True)
section, stable = self.split_table(table)
return url_for('api_list', section=section, stable=stable,
def list_url_field(self, table):
return'api_list', table=table)
section, stable = self.split_table(table)
return'api_list', section=section,
def item_url(self, table, id):
return url_for('api_detail', table, id)
section, stable = self.split_table(table)
return url_for('api_detail', section=section, stable=stable, id=id)
def item_url_field(self, table, attr='id'):
return'api_detail', table=table, id='<%s>' % attr)
section, stable = self.split_table(table)
return'api_detail', section=section,
stable=stable, id='<%s>' % attr)
def relation_url(self, table, id, rel):
return url_for('api_custom', table, id, rel)
section, stable = self.split_table(table)
return url_for('api_custom', section=section, stable=stable, id=id,
def relation_url_field(self, table, rel):
return'api_custom', table=table, id='<id>',
section, stable = self.split_table(table)
return'api_custom', section=section,
stable=stable, id='<id>', custom=rel)
def page_url(self, table, page, size, **kwargs):
qs = copy(kwargs)
......@@ -48,6 +48,10 @@ class Endpoint(object):
self.model = model
self.table = model.__tablename__
self.section = self.table.split('_')[0]
self.stable = self.table.split('_', 1)[1]
self.desc = description or self.table
self.hidden = hidden
......@@ -29,7 +29,7 @@ from import (
from ..models.parlapi import Job
def setup_an_api(app):
def setup_api(app):
api = API(app)
......@@ -9,9 +9,8 @@ import humanize
from .models.parlapi import Job
def setup_routes(app, rest_apis, graphql_api):
for chamber, rest_api in rest_apis.items():
rest_api.setup_routes(app, '/rest/{}/'.format(chamber))
def setup_routes(app, rest_api, graphql_api):
rest_api.setup_routes(app, '/rest/')
graphql_prefix = '/graphql/'
graphql_api.setup_routes(app, graphql_prefix)
......@@ -51,7 +50,11 @@ def setup_routes(app, rest_apis, graphql_api):
return render_template(
rest_apis={ k: v.summary for k, v in rest_apis.items() },
'an': u'Assemblée nationale',
'parlapi': 'ParlAPI'
......@@ -23,8 +23,8 @@ def setup_app(name):
migrate = Migrate(app, db)
# Setup REST API
from .rest.setup import setup_an_api as setup_an_rest_api
an_rest_api = setup_an_rest_api(app)
from .rest.setup import setup_api as setup_rest_api
rest_api = setup_rest_api(app)
# Setup GraphQL API
from .graphql.setup import setup_api as setup_graphql_api
......@@ -32,7 +32,7 @@ def setup_app(name):
# Setup routes
from .routes import setup_routes
setup_routes(app, { 'an': an_rest_api }, graphql_api)
setup_routes(app, rest_api, graphql_api)
# Enable Markdown
......@@ -63,17 +63,15 @@
<th>Point d'entrée API</th>
{% for chamber, rest_api in rest_apis.items() %}
{% for table, item in rest_api.items() %}
<td><img class="chamber-icon" src="{{ url_for('static', filename='chamber-' + chamber + '.png') }}"</td>
<td>{{ item.desc }}</td>
<td>{{ item.count }}</td>
<a href="/rest/an/{{ table }}"><code>/rest/an/{{ table }}</code></a>
{% endfor %}
{% for table, item in rest_api.items() %}
<td><img class="chamber-icon" src="{{ url_for('static', filename='' + item.section + '.png') }}" alt="{{ sections[item.section] }}" title="{{ sections[item.section] }}"></td>
<td>{{ item.desc }}</td>
<td>{{ item.count }}</td>
<a href="/rest/{{ item.section }}/{{ item.stable }}"><code>/rest/{{ item.section }}/{{ item.stable }}</code></a>
{% endfor %}
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment