Exemplo n.º 1
0
    def get(self, collection):
        """Search documents from collection."""

        args = coll_parsers.search_parser.parse_args(request)

        try:
            try:
                page = args.get('page')
                query = args.get('query') or '[]'
                per_page = args.get('per_page')
                data = json.loads(query)
            except JSONDecodeError:
                raise gmap_exc.SearchException('Parameter query is invalid')
            else:
                res = facade.search(collection, data, page, per_page)
                return res, 200

        except gmap_exc.CollectionNotExist as err:
            app.logger.error(err.message)
            api.abort(404, errors=err.message)

        except ValidationError as error:
            res = validate(error)
            app.logger.error(res)
            api.abort(400, errors=res)
Exemplo n.º 2
0
    def get(self, graph):
        """Search traversal."""

        args = graphs_parsers.traversal_parser.parse_args(request)

        try:
            search_dict = {
                'graph_name': graph,
                'start_vertex': args.get('start_vertex'),
                'direction': args.get('direction'),
                'item_order': args.get('item_order'),
                'strategy': args.get('strategy'),
                'order': args.get('order'),
                'edge_uniqueness': args.get('edge_uniqueness'),
                'vertex_uniqueness': args.get('vertex_uniqueness'),
                'max_iter': args.get('max_iter'),
                'min_depth': args.get('min_depth'),
                'max_depth': args.get('max_depth'),
                'init_func': args.get('init_func'),
                'sort_func': args.get('sort_func'),
                'filter_func': args.get('filter_func'),
                'visitor_func': args.get('visitor_func'),
                'expander_func': args.get('expander_func')
            }

            res = facade.search_traversal(**search_dict)
            return res, 200

        except gmap_exc.GraphTraverseException as err:
            app.logger.error(err.message)
            api.abort(400, errors=err.message)

        except gmap_exc.GraphNotExist as err:
            app.logger.error(err.message)
            api.abort(404, errors=err.message)
Exemplo n.º 3
0
    def get(self):
        """Search document in collections of kind document from DB."""

        args = coll_parsers.search_all_parser.parse_args(request)

        try:
            try:
                page = args.get('page')
                query = args.get('query') or '[]'
                per_page = args.get('per_page')
                collections = args.get('collections').split(',')
                data = json.loads(query)
                app.logger.debug('Receive Data: %s', data)
            except JSONDecodeError:
                raise gmap_exc.SearchException('Parameter query is invalid')
            else:
                res = facade.search_collections(
                    collections, data, page, per_page)
                return res, 200

        except gmap_exc.CollectionNotExist as err:
            app.logger.error(err.message)
            api.abort(404, errors=err.message)

        except ValidationError as error:
            res = validate(error)
            app.logger.error(res)
            api.abort(400, errors=res)
Exemplo n.º 4
0
    def delete(self, key):
        """Delete query by key."""

        try:
            res = facade.delete_query(key)
            return res, 200

        except gmap_exc.DocumentNotExist as error:
            app.logger.warning(error.message)
            api.abort(404, errors=error.message)
Exemplo n.º 5
0
    def post(self):
        """Create graph in DB."""

        try:
            data = request.get_json()
            app.logger.debug('Receive Data: %s', data)
            facade.create_graph(data)
            return {}, 200

        except ValidationError as error:
            res = validate(error)
            app.logger.error(res)
            api.abort(400, errors=res)
Exemplo n.º 6
0
    def get(self, key):
        """Get query by key."""

        args = query_parsers.execute_query_parser.parse_args(request)
        variable = args.get('variable')

        try:
            res = facade.execute_query(key, variable)
            return res, 200

        except gmap_exc.DocumentNotExist as error:
            app.logger.warning(error.message)
            api.abort(404, errors=error.message)
Exemplo n.º 7
0
 def get(self, plugin_name):
     try:
         args = plugin_arguments.parse_args(request)
         plugin_instance = ApiPluginLoader().load_plugin(plugin_name)
         data = plugin_instance.get_data(args)
         return data
     except PluginNotFoundException as err:
         app.logger.error(str(err))
         api.abort(404, errors=str(err))
         return data
     except:
         err_msg = 'Error in plugin'
         app.logger.exception(err_msg)
         api.abort(400, errors=err_msg)
Exemplo n.º 8
0
    def get(self, edge, key):
        """Get edge by key."""

        try:
            res = facade.get_edge(edge, key)
            return res, 200

        except gmap_exc.EdgeNotExist as err:
            app.logger.error(err.message)
            api.abort(404, errors=err.message)

        except gmap_exc.DocumentNotExist as err:
            app.logger.warning(err.message)
            api.abort(404, errors=err.message)
Exemplo n.º 9
0
    def get(self, collection, key):
        """Get document by key."""

        try:
            res = facade.get_document(collection, key)
            return res, 200

        except gmap_exc.CollectionNotExist as err:
            app.logger.error(err.message)
            api.abort(404, errors=err.message)

        except gmap_exc.DocumentNotExist as err:
            app.logger.warning(err.message)
            api.abort(404, errors=err.message)
Exemplo n.º 10
0
    def post(self):
        """Create token"""

        try:
            data = request.get_json()
            if type(data) is dict:
                username = data.get('username')
                password = data.get('password')
            else:
                username = None
                password = None
            if not username or not password:
                app.logger.error('Username and Password is required.')
                api.abort(401, errors='Username and Password is required.')

            token = facade.create_token(username, password)
            return token, 200

        except exceptions.Unauthorized:
            app.logger.error('User %s not Unauthorized.', username)
            api.abort(401, 'User not Unauthorized.')

        except exceptions.AuthException:
            app.logger.error('Auth Unavailable.')
            api.abort(503, 'Auth Unavailable.')

        except AuthException:
            app.logger.error('Auth Unavailable.')
            api.abort(503, 'Auth Unavailable.')
Exemplo n.º 11
0
    def delete(self, collection, key):
        """Delete document by key."""

        try:
            facade.delete_document(collection, key)
            return {}, 200

        except gmap_exc.CollectionNotExist as err:
            app.logger.error(err.message)
            api.abort(404, errors=err.message)

        except gmap_exc.DocumentNotExist as err:
            app.logger.warning(err.message)
            api.abort(404, errors=err.message)
Exemplo n.º 12
0
    def post(self, collection):
        """Insert document in DB."""

        try:
            data = request.get_json()
            app.logger.debug('Receive Data: %s', data)
            res = facade.create_document(collection, data)
            return res, 200

        except ValidationError as error:
            res = validate(error)
            app.logger.error(res)
            api.abort(400, errors=res)

        except gmap_exc.CollectionNotExist as err:
            app.logger.error(err.message)
            api.abort(404, errors=err.message)

        except gmap_exc.DocumentAlreadyExist as err:
            app.logger.warning(err.message)
            api.abort(409, errors=err.message)

        except gmap_exc.DocumentException as err:
            app.logger.error(err.message)
            api.abort(400, errors=err.message)
Exemplo n.º 13
0
    def delete(self, edge, key):
        """Delete edge by key."""

        try:
            facade.delete_edge(edge, key)
            return {}, 200

        except gmap_exc.EdgeNotExist as err:
            app.logger.error(err.message)
            api.abort(404, errors=err.message)

        except gmap_exc.DocumentNotExist as err:
            app.logger.warning(err.message)
            api.abort(404, errors=err.message)
Exemplo n.º 14
0
def validate_token(token):
    auth_inst = Auth()
    try:
        auth_inst.set_token(token)
        auth_inst.validate_token()

        return auth_inst

    except exceptions.InvalidToken:
        app.logger.warning('Invalid Token')
        api.abort(401, errors='Invalid Token')

    except exceptions.AuthException:
        err_msg = 'Error to validate token'
        app.logger.exception(err_msg)
        api.abort(503)
Exemplo n.º 15
0
def get_roles(token):
    auth_inst = Auth()
    try:
        auth_inst.set_token(token)
        auth_inst.validate_token()
        token_data = auth_inst.get_token_data_details()

    except exceptions.InvalidToken:
        app.logger.warning('Invalid Token')
        api.abort(401, errors='Invalid Token')

    except exceptions.AuthException:
        err_msg = 'Error to validate token'
        app.logger.exception(err_msg)
        api.abort(503)

    return token_data
Exemplo n.º 16
0
    def post(self, collection):
        """Clear documents in collection."""

        try:
            data = request.get_json()
            app.logger.debug('Receive Data: %s', data)
            res = facade.clear_collection(collection, data)
            return res, 200

        except gmap_exc.CollectionNotExist as err:
            app.logger.error(err.message)
            api.abort(404, errors=err.message)

        except ValidationError as error:
            res = validate(error)
            app.logger.error(res)
            api.abort(400, errors=res)
Exemplo n.º 17
0
    def post(self):
        """Create collection of kind edge in DB."""

        try:
            data = request.get_json()
            app.logger.debug('Receive Data: %s', data)
            facade.create_collection_edge(data)
            return {}, 200

        except ValidationError as error:
            res = validate(error)
            app.logger.error(res)
            api.abort(400, errors=res)

        except gmap_exc.CollectionNotExist as err:
            app.logger.error(err.message)
            api.abort(404, errors=err.message)
Exemplo n.º 18
0
    def get(self, collection):
        """Get count documents."""

        args = search_parser.parse_args(request)

        try:
            query = args.get('query') or '[]'
            data = json.loads(query)
            res = facade.count_document(collection, data)
            return res, 200

        except gmap_exc.CollectionNotExist as err:
            app.logger.error(err.message)
            api.abort(404, errors=err.message)

        except gmap_exc.DocumentNotExist as err:
            app.logger.warning(err.message)
            api.abort(404, errors=err.message)
Exemplo n.º 19
0
    def get(self):
        try:
            plugins_config = configparser.ConfigParser()
            plugins_config.read(app.config['API_PLUGINS_CONFIG_FILE'])
            keys = plugins_config.sections()
            plugins = {}

            for key in keys:
                plugins[key] = {
                    'types': ast.literal_eval(plugins_config.get(key, 'types')),
                    'parameters': ast.literal_eval(plugins_config.get(key, 'parameters')),
                    'description': plugins_config.get(key, 'description'),
                    'uri': '{}{}/{}/'.format(api.base_url, 'plugins', key)
                }

            return plugins
        except Exception:
            err_msg = 'Error in plugin'
            app.logger.exception(err_msg)
            api.abort(500, errors=err_msg)
Exemplo n.º 20
0
 def post(self, plugin_name):
     try:
         args = request.get_json()
         plugin_instance = ApiPluginLoader().load_plugin(plugin_name)
         data = plugin_instance.get_data(args)
         return data
     except PluginNotFoundException as err:
         app.logger.error(str(err))
         api.abort(404, errors=str(err))
         return data
     except PluginError as err:
         app.logger.error(str(err))
         api.abort(400, errors=str(err))
         return data
     except Exception:
         err_msg = 'Error in plugin'
         app.logger.exception(err_msg)
         api.abort(500, errors=err_msg)
Exemplo n.º 21
0
    def patch(self, collection, key):
        """Partial update document."""

        try:
            data = request.get_json()
            app.logger.debug('Receive Data: %s', data)
            res = facade.patch_document(collection, key, data)
            return res, 200

        except ValidationError as error:
            res = validate(error)
            app.logger.error(res)
            api.abort(400, errors=res)

        except gmap_exc.CollectionNotExist as err:
            app.logger.error(err.message)
            api.abort(404, errors=err.message)

        except gmap_exc.DocumentNotExist as err:
            app.logger.warning(err.message)
            api.abort(404, errors=err.message)
Exemplo n.º 22
0
    def put(self, key):
        """Update query in DB."""

        try:
            data = request.get_json()
            app.logger.debug('Receive Data: %s', data)
            res = facade.update_query(key, data)
            return res, 200

        except ValidationError as error:
            res = validate(error)
            app.logger.error(res)
            api.abort(400, errors=res)

        except gmap_exc.QueryException as error:
            app.logger.warning(error.message)
            api.abort(400, errors=error.message)

        except gmap_exc.DocumentNotExist as error:
            app.logger.warning(error.message)
            api.abort(404, errors=error.message)
Exemplo n.º 23
0
    def post(self):
        """Create queries in DB."""

        try:
            data = request.get_json()

            app.logger.debug('Receive Data: %s', data)
            res = facade.create_query(data)
            return res, 200

        except ValidationError as error:
            res = validate(error)
            app.logger.error(res)
            api.abort(400, errors=res)

        except gmap_exc.QueryException as error:
            app.logger.warning(error.message)
            api.abort(400, errors=error.message)

        except gmap_exc.DocumentAlreadyExist:
            res = 'Cannot create query, already created.'
            app.logger.warning(res)
            api.abort(409, errors=res)
Exemplo n.º 24
0
 def validate_token(self):
     token_data = self.auth.get_token_data_details()
     if not self.has_permission(token_data):
         api.abort(403, 'User have not permission to this action')
         app.logger.error('User have not permission to this action')