Skip to content
Snippets Groups Projects
routes.py 4.34 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 cubicweb.pyramid.core import CubicWebPyramidRequest
from cubicweb.schema_exporters import JSONSchemaExporter
from pyramid.config import Configurator
from pyramid.request import Request

Frank Bessou's avatar
Frank Bessou committed
from cubicweb_api.api_transaction import ApiTransactionsRepository
from cubicweb_api.jwt_auth import setup_jwt

class Context:
    def __init__(self, request: Request):
        self.request = request
Frank Bessou's avatar
Frank Bessou committed
        self.cwreq: CubicWebPyramidRequest = request.cw_request

    def render(self):
        raise NotImplementedError


class SchemaContext(Context):
Frank Bessou's avatar
Frank Bessou committed
    def render(self):
        schema = self.cwreq.cnx.repo.schema
        exporter = JSONSchemaExporter()
        return exporter.export_as_dict(schema)
class RQLContext(Context):
    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 JWTAuthContext(Context):
    def render(self):
        login = self.request.json_body["login"]
        pwd = self.request.json_body["password"]

        repo = self.request.registry["cubicweb.repository"]
        with repo.internal_cnx() as cnx:
            cwuser = repo.authenticate_user(cnx, login, password=pwd)

        return {
            "token": self.request.create_jwt_token(
                cwuser.eid,
                login=cwuser.login,
                firstname=cwuser.firstname,
                lastname=cwuser.surname,
            )
        }


class TransactionContext(Context):
Frank Bessou's avatar
Frank Bessou committed
    def __init__(self, request):
        super().__init__(request)
        self._transactions: ApiTransactionsRepository = (
            self.cwreq.cnx.repo.api_transactions
        )
Frank Bessou's avatar
Frank Bessou committed

    def render(self):
        action = self.request.json["action"]
        if action == "begin":
            return self._begin()
        elif action == "execute":
            return self._execute()
        elif action == "commit":
            return self._commit()
        elif action == "rollback":
            return self._rollback()

    def _begin(self):
        user = self.cwreq.user
        return self._transactions.begin_transaction(user)

    def _execute(self):
        uuid = self.request.json["uuid"]
        rql = self.request.json["query"]
        params = self.request.json["params"]
        rset = self._transactions[uuid].execute(rql, params)
        return rset.rows

    def _commit(self):
        uuid = self.request.json["uuid"]
        commit_result = self._transactions[uuid].commit()
        self._transactions[uuid].rollback()
        return commit_result

    def _rollback(self):
        uuid = self.request.json["uuid"]
        rollback_result = self._transactions[uuid].rollback()
        self._transactions.end_transaction(uuid)
        return rollback_result


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


ROUTE_NAME_TO_CONTEXT = {
    "schema": SchemaContext,
    "transaction": TransactionContext,
    "rql": RQLContext,
    "help": HelpContext,
    "login": JWTAuthContext,
Arnaud Vergnet's avatar
Arnaud Vergnet committed
def get_route_name(route_part: str):
    return f"v1_{route_part}"


def get_route_pattern(route_part: str):
    return rf"/api/v1/{route_part}"


def includeme(config: Configurator):
Frank Bessou's avatar
Frank Bessou committed
    repo = config.registry["cubicweb.repository"]
    repo.api_transactions = ApiTransactionsRepository(repo)
    for route_part, context in ROUTE_NAME_TO_CONTEXT.items():
Arnaud Vergnet's avatar
Arnaud Vergnet committed
        config.add_route(
            get_route_name(route_part), get_route_pattern(route_part), factory=context
        )