Skip to content
Snippets Groups Projects
routes.py 1.75 KiB
Newer Older
# -*- coding: utf-8 -*-
# copyright 2022 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
# contact https://www.logilab.fr -- mailto:contact@logilab.fr
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the Free
# Software Foundation, either version 2.1 of the License, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
# details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
from pyramid.config import Configurator
from pyramid.request import Request


class Context:
    def __init__(self, request: Request):
        self.request = request
        self.cwreq = request.cw_request

    def render(self):
        raise NotImplementedError


class ApiV1Context(Context):
    def __getitem__(self, subpath: str):
        if subpath == "rql":
            return RQLContext(self.request)
        if subpath == "help":
            return HelpContext(self.request)


class RQLContext(ApiV1Context):
    def render(self):
        query = self.request.json["query"]
        params = self.request.json["params"]
        if not query:
            return {"error": "No query provided."}
        rset = self.cwreq.execute(query, params)
        return rset.rows


class HelpContext(ApiV1Context):
    def render(self):
        """TO IMPLEMENT"""
        pass


def includeme(config: Configurator):
    config.add_route("api_v1", r"/api/v1/*traverse", factory=ApiV1Context)