def test_init_config_invalid_config_url(): original_url = os.environ.get('GLOBAL_CONFIG_URL') os.environ['GLOBAL_CONFIG_URL'] = "foo://bar" with pytest.raises(RuntimeError) as rte: init_config() assert 'Invalid config url: foo://bar' in str(rte) if original_url is not None: os.environ['GLOBAL_CONFIG_URL'] = original_url else: os.environ.pop('GLOBAL_CONFIG_URL')
def ws_auth(auth_token): """ Get a list of workspace IDs that the given username is allowed to access in the workspace. """ if not auth_token: return [] # anonymous users config = init_config() ws_url = config['workspace_url'] # TODO session cache this # Make a request to the workspace using the user's auth token to find their readable workspce IDs payload = { 'method': 'Workspace.list_workspace_ids', 'version': '1.1', 'params': [{'perm': 'r'}] } headers = {'Authorization': auth_token} resp = requests.post( ws_url, data=json.dumps(payload), headers=headers ) if not resp.ok: raise RuntimeError(ws_url, resp.text) return resp.json()['result'][0]['workspaces']
async def root(request): """Handle JSON RPC methods.""" json_body = request.json method = json_body.get('method', 'show_config') params = json_body.get('params', {}) rpc_handlers = { 'show_config': _show_config, 'search_objects': search_objects, 'show_indexes': show_indexes } if method not in rpc_handlers: InvalidParameters( f'Unknown method: {method}. Available methods: {rpc_handlers.keys()}' ) config = init_config() result = rpc_handlers[method](params, request.headers, config) return sanic.response.json(result)
import time import yaml import jsonschema import jsonschema.exceptions import logging import sys import json from src.exceptions import InvalidParameters, UnknownMethod, InvalidJSON from src.utils.config import init_config from src.search_objects import search_objects from src.handle_legacy import handle_legacy from src.show_indexes import show_indexes app = sanic.Sanic() _CONFIG = init_config() _SCHEMAS_PATH = 'src/server/method_schemas.yaml' with open(_SCHEMAS_PATH) as fd: _SCHEMAS = yaml.safe_load(fd) logger = logging.getLogger('searchapi2') @app.middleware('request') async def cors_options(request): """Handle a CORS OPTIONS request.""" if request.method == 'OPTIONS': return sanic.response.raw(b'', status=204) @app.route('/')
import unittest import requests import json from src.utils.config import init_config _API_URL = 'http://web:5000' config = init_config() _TYPE_NAME = 'data' _INDEX_NAMES = [ config['index_prefix'] + '.' + 'index1', config['index_prefix'] + '.' + 'index2', ] def _init_elasticsearch(): """ Initialize the indexes and documents on elasticsearch before running tests. """ for index_name in _INDEX_NAMES: resp = requests.put(config['elasticsearch_url'] + '/' + index_name, data=json.dumps({ 'settings': { 'index': { 'number_of_shards': 3, 'number_of_replicas': 1 } } }), headers={'Content-Type': 'application/json'}) if not resp.ok and resp.json(