cubicweb/pyramid/rest_api.py
author Nicolas Chauvat <nicolas.chauvat@logilab.fr>
Tue, 10 Mar 2020 23:47:50 +0100
changeset 12911 a17cbf539a69
parent 11967 83739be20fab
permissions -rw-r--r--
[pyramid] add routes /{eid} and /{etype}/{eid} to return RDF when rdf mimetype in Accept HTTP headers * simplify pyramid/resources.py by making the classmethod that returns a closure a simple function and removing the EntityResource and ETYpeResource classes that are barely used * replace predicate MatchIsETypePredicate with MatchIsETypeAndEIDPredicate Team: famarger, schabot, nchauvat, fferry, ethieblin
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
11967
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11959
diff changeset
     1
# copyright 2017 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11959
diff changeset
     2
# copyright 2014-2016 UNLISH S.A.S. (Montpellier, FRANCE), all rights reserved.
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11959
diff changeset
     3
#
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11959
diff changeset
     4
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11959
diff changeset
     5
#
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11959
diff changeset
     6
# This file is part of CubicWeb.
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11959
diff changeset
     7
#
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11959
diff changeset
     8
# CubicWeb is free software: you can redistribute it and/or modify it under the
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11959
diff changeset
     9
# terms of the GNU Lesser General Public License as published by the Free
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11959
diff changeset
    10
# Software Foundation, either version 2.1 of the License, or (at your option)
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11959
diff changeset
    11
# any later version.
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11959
diff changeset
    12
#
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11959
diff changeset
    13
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11959
diff changeset
    14
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11959
diff changeset
    15
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11959
diff changeset
    16
# details.
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11959
diff changeset
    17
#
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11959
diff changeset
    18
# You should have received a copy of the GNU Lesser General Public License along
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11959
diff changeset
    19
# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11959
diff changeset
    20
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11959
diff changeset
    21
"""Experimental REST API for CubicWeb using Pyramid."""
83739be20fab [pyramid] Add a copyright and docstring to all modules
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11959
diff changeset
    22
12911
a17cbf539a69 [pyramid] add routes /{eid} and /{etype}/{eid} to return RDF when rdf mimetype in Accept HTTP headers
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 11967
diff changeset
    23
import rdflib
11596
1f2570f1d72a [routes] Add a 'cwentities' route with traversal
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    24
11599
f8ba6ea94af9 [refactoring] Move EntityResource and ETypeResource to a separate module
Rabah Meradi <rabah.meradi@logilab.fr>
parents: 11598
diff changeset
    25
from pyramid.view import view_config
12911
a17cbf539a69 [pyramid] add routes /{eid} and /{etype}/{eid} to return RDF when rdf mimetype in Accept HTTP headers
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 11967
diff changeset
    26
from pyramid.response import Response
a17cbf539a69 [pyramid] add routes /{eid} and /{etype}/{eid} to return RDF when rdf mimetype in Accept HTTP headers
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 11967
diff changeset
    27
a17cbf539a69 [pyramid] add routes /{eid} and /{etype}/{eid} to return RDF when rdf mimetype in Accept HTTP headers
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 11967
diff changeset
    28
from cubicweb import rdf
a17cbf539a69 [pyramid] add routes /{eid} and /{etype}/{eid} to return RDF when rdf mimetype in Accept HTTP headers
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 11967
diff changeset
    29
from cubicweb.pyramid.resources import rdf_context_from_eid, RDFResource
11596
1f2570f1d72a [routes] Add a 'cwentities' route with traversal
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    30
1f2570f1d72a [routes] Add a 'cwentities' route with traversal
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    31
1f2570f1d72a [routes] Add a 'cwentities' route with traversal
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    32
@view_config(
12911
a17cbf539a69 [pyramid] add routes /{eid} and /{etype}/{eid} to return RDF when rdf mimetype in Accept HTTP headers
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 11967
diff changeset
    33
    route_name='one_entity',
a17cbf539a69 [pyramid] add routes /{eid} and /{etype}/{eid} to return RDF when rdf mimetype in Accept HTTP headers
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 11967
diff changeset
    34
    context=RDFResource,
a17cbf539a69 [pyramid] add routes /{eid} and /{etype}/{eid} to return RDF when rdf mimetype in Accept HTTP headers
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 11967
diff changeset
    35
)
a17cbf539a69 [pyramid] add routes /{eid} and /{etype}/{eid} to return RDF when rdf mimetype in Accept HTTP headers
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 11967
diff changeset
    36
@view_config(
a17cbf539a69 [pyramid] add routes /{eid} and /{etype}/{eid} to return RDF when rdf mimetype in Accept HTTP headers
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 11967
diff changeset
    37
    route_name='one_entity_eid',
a17cbf539a69 [pyramid] add routes /{eid} and /{etype}/{eid} to return RDF when rdf mimetype in Accept HTTP headers
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 11967
diff changeset
    38
    context=RDFResource,
a17cbf539a69 [pyramid] add routes /{eid} and /{etype}/{eid} to return RDF when rdf mimetype in Accept HTTP headers
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 11967
diff changeset
    39
)
a17cbf539a69 [pyramid] add routes /{eid} and /{etype}/{eid} to return RDF when rdf mimetype in Accept HTTP headers
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 11967
diff changeset
    40
def view_entity_as_rdf(context, request):
a17cbf539a69 [pyramid] add routes /{eid} and /{etype}/{eid} to return RDF when rdf mimetype in Accept HTTP headers
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 11967
diff changeset
    41
    graph = rdflib.ConjunctiveGraph()
a17cbf539a69 [pyramid] add routes /{eid} and /{etype}/{eid} to return RDF when rdf mimetype in Accept HTTP headers
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 11967
diff changeset
    42
    rdf.add_entity_to_graph(graph, context.entity)
a17cbf539a69 [pyramid] add routes /{eid} and /{etype}/{eid} to return RDF when rdf mimetype in Accept HTTP headers
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 11967
diff changeset
    43
    rdf_format = rdf.RDF_MIMETYPE_TO_FORMAT[context.mime_type]
a17cbf539a69 [pyramid] add routes /{eid} and /{etype}/{eid} to return RDF when rdf mimetype in Accept HTTP headers
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 11967
diff changeset
    44
    response = Response(graph.serialize(format=rdf_format))
a17cbf539a69 [pyramid] add routes /{eid} and /{etype}/{eid} to return RDF when rdf mimetype in Accept HTTP headers
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 11967
diff changeset
    45
    response.content_type = context.mime_type
a17cbf539a69 [pyramid] add routes /{eid} and /{etype}/{eid} to return RDF when rdf mimetype in Accept HTTP headers
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 11967
diff changeset
    46
    return response
11596
1f2570f1d72a [routes] Add a 'cwentities' route with traversal
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    47
1f2570f1d72a [routes] Add a 'cwentities' route with traversal
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    48
1f2570f1d72a [routes] Add a 'cwentities' route with traversal
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    49
def includeme(config):
11959
ddc05ce75319 [pyramid] Include 'cubicweb.pyramid.predicates' where it is used
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 11811
diff changeset
    50
    config.include('.predicates')
11596
1f2570f1d72a [routes] Add a 'cwentities' route with traversal
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    51
    config.add_route(
12911
a17cbf539a69 [pyramid] add routes /{eid} and /{etype}/{eid} to return RDF when rdf mimetype in Accept HTTP headers
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 11967
diff changeset
    52
        'one_entity',
a17cbf539a69 [pyramid] add routes /{eid} and /{etype}/{eid} to return RDF when rdf mimetype in Accept HTTP headers
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 11967
diff changeset
    53
        '/{etype}/{eid}',
a17cbf539a69 [pyramid] add routes /{eid} and /{etype}/{eid} to return RDF when rdf mimetype in Accept HTTP headers
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 11967
diff changeset
    54
        factory=rdf_context_from_eid,
a17cbf539a69 [pyramid] add routes /{eid} and /{etype}/{eid} to return RDF when rdf mimetype in Accept HTTP headers
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 11967
diff changeset
    55
        match_is_etype_and_eid=('etype', 'eid'),
a17cbf539a69 [pyramid] add routes /{eid} and /{etype}/{eid} to return RDF when rdf mimetype in Accept HTTP headers
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 11967
diff changeset
    56
    )
a17cbf539a69 [pyramid] add routes /{eid} and /{etype}/{eid} to return RDF when rdf mimetype in Accept HTTP headers
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 11967
diff changeset
    57
    config.add_route(
a17cbf539a69 [pyramid] add routes /{eid} and /{etype}/{eid} to return RDF when rdf mimetype in Accept HTTP headers
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 11967
diff changeset
    58
        'one_entity_eid',
a17cbf539a69 [pyramid] add routes /{eid} and /{etype}/{eid} to return RDF when rdf mimetype in Accept HTTP headers
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 11967
diff changeset
    59
        '/{eid}',
a17cbf539a69 [pyramid] add routes /{eid} and /{etype}/{eid} to return RDF when rdf mimetype in Accept HTTP headers
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 11967
diff changeset
    60
        factory=rdf_context_from_eid,
a17cbf539a69 [pyramid] add routes /{eid} and /{etype}/{eid} to return RDF when rdf mimetype in Accept HTTP headers
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 11967
diff changeset
    61
        match_is_eid='eid'
a17cbf539a69 [pyramid] add routes /{eid} and /{etype}/{eid} to return RDF when rdf mimetype in Accept HTTP headers
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 11967
diff changeset
    62
    )
11596
1f2570f1d72a [routes] Add a 'cwentities' route with traversal
Christophe de Vienne <christophe@unlish.com>
parents:
diff changeset
    63
    config.scan(__name__)