def __init__(self, url, user_name=None, password=None): self.url = url self.authenticated = False # This next part pretty much just goes through the motions of what # from_url() does in the parent class. # First let's make an http client self.http_client = RequestsClient() # Now let's get the spec spec_url = urljoin(self.url, '/docs/swagger.json') loader = Loader(self.http_client) spec_dict = loader.load_spec(spec_url) # Now we can create the spec client config = {'include_missing_properties': False} # Apply bravado config defaults bravado_config = bravado_config_from_config_dict(config) # set bravado config object config['bravado'] = bravado_config_from_config_dict(config) swagger_spec = Spec.from_dict(spec_dict, origin_url=spec_url, http_client=self.http_client, config=config) # Now that we have the spec client we can init the parent class super().__init__(swagger_spec, bravado_config.also_return_response) # Go ahead and auth if we were passed creds. if user_name and password: self.authenticate(user_name, password)
def __init__(self, apikey=None, headers=None, host=HOST): # - Kong forwards consumer headers when api-key used for auth # - forward consumer headers when connecting through localhost self.__dict__ = self._shared_state self.apikey = apikey self.headers = {"x-api-key": apikey} if apikey else headers self.host = host if "swagger_spec" not in self.__dict__ or ( self.headers is not None and self.swagger_spec.http_client.headers != self.headers ): http_client = FidoClientGlobalHeaders(headers=self.headers) loader = Loader(http_client) protocol = "https" if self.apikey else "http" origin_url = f"{protocol}://{self.host}/apispec.json" spec_dict = loader.load_spec(origin_url) spec_dict["host"] = self.host spec_dict["schemes"] = [protocol] config = { "validate_responses": False, "use_models": False, "include_missing_properties": False, "formats": [email_format], } bravado_config = bravado_config_from_config_dict(config) for key in set(bravado_config._fields).intersection(set(config)): del config[key] config["bravado"] = bravado_config swagger_spec = Spec.from_dict(spec_dict, origin_url, http_client, config) super().__init__( swagger_spec, also_return_response=bravado_config.also_return_response )
def from_url(cls, spec_url, http_client=None, request_headers=None, config=None): """Build a :class:`SwaggerClient` from a url to the Swagger specification for a RESTful API. :param spec_url: url pointing at the swagger API specification :type spec_url: str :param http_client: an HTTP client used to perform requests :type http_client: :class:`bravado.http_client.HttpClient` :param request_headers: Headers to pass with http requests :type request_headers: dict :param config: Config dict for bravado and bravado_core. See CONFIG_DEFAULTS in :module:`bravado_core.spec`. See CONFIG_DEFAULTS in :module:`bravado.client`. :rtype: :class:`bravado_core.spec.Spec` """ log.debug(u"Loading from %s" % spec_url) http_client = http_client or RequestsClient() loader = Loader(http_client, request_headers=request_headers) spec_dict = loader.load_spec(spec_url) # RefResolver may have to download additional json files (remote refs) # via http. Wrap http_client's request() so that request headers are # passed along with the request transparently. Yeah, this is not ideal, # but since RefResolver has new found responsibilities, it is # functional. if request_headers is not None: http_client.request = inject_headers_for_remote_refs( http_client.request, request_headers) return cls.from_spec(spec_dict, spec_url, http_client, config)
def test_fetch_specs(self, swagger_http_server): loader = Loader( http_client=self.http_client, request_headers={'boolean-header': True}, ) spec = loader.load_spec('{server_address}/swagger.json'.format(server_address=swagger_http_server)) assert spec == SWAGGER_SPEC_DICT
def load_wes_client(service_id, http_client=None, client_library=None): """ Return an API client for the selected workflow execution service. """ if http_client is None: http_client = _init_http_client(service_id=service_id) if client_library is not None: from wes_client.util import WESClient wes_client = WESClient(service=_get_wes_opts(service_id)) return WESAdapter(wes_client) spec_path = os.path.join(os.path.dirname(os.path.dirname(__file__)), 'workflow_execution_service.swagger.yaml') spec_path = os.path.abspath(spec_path) opts = _get_wes_opts(service_id) api_url = '{}://{}'.format(opts['proto'], opts['host']) loader = Loader(http_client, request_headers=None) spec_dict = loader.load_spec('file:///{}'.format(spec_path), base_url=api_url) spec_client = SwaggerClient.from_spec(spec_dict, origin_url=api_url, http_client=http_client, config={'use_models': False}) return spec_client.WorkflowExecutionService
def test_fetch_specs(self, threaded_http_server, petstore_dict): loader = Loader( http_client=self.http_client, request_headers={'boolean-header': True}, ) spec = loader.load_spec('{server_address}/swagger.json'.format(server_address=threaded_http_server)) assert spec == petstore_dict
def from_url(cls, spec_url, http_client=None, request_headers=None, config=None): """ Build a :class:`SwaggerClient` from a url to the Swagger specification for a RESTful API. :param spec_url: url pointing at the swagger API specification :type spec_url: str :param http_client: an HTTP client used to perform requests :type http_client: :class:`bravado.http_client.HttpClient` :param request_headers: Headers to pass with http requests :type request_headers: dict :param config: bravado_core config dict. See bravado_core.spec.CONFIG_DEFAULTS """ # TODO: better way to customize the request for api calls, so we don't # have to add new kwargs for everything log.debug(u"Loading from %s" % spec_url) http_client = http_client or RequestsClient() loader = Loader(http_client, request_headers=request_headers) spec_dict = loader.load_spec(spec_url) return cls.from_spec(spec_dict, spec_url, http_client, config)
def load_client(apikey=None, headers=None, host=HOST): global client force = False if client is not None: http_client = client.swagger_spec.http_client force = bool(headers is not None and http_client.headers != headers) if force or client is None: # - Kong forwards consumer headers when api-key used for auth # - forward consumer headers when connecting through localhost headers = {"x-api-key": apikey} if apikey else headers http_client = FidoClientGlobalHeaders(headers=headers) loader = Loader(http_client) protocol = "https" if apikey else "http" spec_url = f"{protocol}://{host}/apispec.json" spec_dict = loader.load_spec(spec_url) spec_dict["host"] = host spec_dict["schemes"] = [protocol] client = SwaggerClient.from_spec( spec_dict, spec_url, http_client, { "validate_responses": False, "use_models": False, "include_missing_properties": False, "formats": [email_format], }, ) return client
def config(cls, url, http_client=None, request_headers=None): """ Accepts an optionally configured requests client with authentication details set. :param url: The URL of the service to connect to :param http_client: The http_client to use, \ defaults to :func:`RequestsClient` :param request_headers: The headers to set on each request. :return: """ swagger_path = '{}/swagger.json'.format(url.rstrip("/")) http_client = http_client or RequestsClient() loader = Loader(http_client, request_headers=request_headers) spec_dict = loader.load_spec(swagger_path) return spec_dict
def load_client(apikey=None): host = 'api.mpcontribs.org' if apikey else 'localhost:5000' protocol = 'https' if apikey else 'http' spec_url = f'{protocol}://{host}/apispec.json' http_client = RequestsClient() if apikey: http_client.set_api_key(host, apikey, param_in='header', param_name='x-api-key') loader = Loader(http_client) spec_dict = loader.load_spec(spec_url) spec_dict['host'] = host spec_dict['schemes'] = [protocol] return SwaggerClient.from_spec(spec_dict, spec_url, http_client, {'validate_responses': False})
def load_trs_client(service_id, http_client=None): """ Return an API client for the selected workflow execution service. """ if http_client is None: http_client = _init_http_client(service_id=service_id) spec_path = os.path.join(os.path.dirname(os.path.dirname(__file__)), 'ga4gh-tool-discovery.yaml') spec_path = os.path.abspath(spec_path) opts = _get_trs_opts(service_id) api_url = '{}://{}'.format(opts['proto'], opts['host']) loader = Loader(http_client, request_headers=None) spec_dict = loader.load_spec('file:///{}'.format(spec_path), base_url=api_url) spec_client = SwaggerClient.from_spec(spec_dict, origin_url=api_url, http_client=http_client, config={'use_models': False}) return spec_client.GA4GH
def test_load_yaml(yaml_spec): loader = Loader(None) result = loader.load_yaml(yaml_spec) assert result == { 'swagger': '2.0', 'info': { 'version': '1.0.0', 'title': 'Simple', }, 'paths': { '/ping': { 'get': { 'operationId': 'ping', 'responses': { '200': { 'description': 'pong', }, }, }, }, }, }
def get_swagger_json(spec_uri, exclude_formats=[]): loader = Loader(RequestsClient()) spec_dict = loader.load_spec(spec_uri) if not exclude_formats: return spec_dict # exlude formats from definitions for def_key, def_item in spec_dict['definitions'].items(): if 'properties' not in def_item: continue for prop_key, prop_item in def_item['properties'].items(): if 'format' in prop_item and prop_item['format'] in exclude_formats: prop_item.pop('format') # exlude formats from paths for path_key, path_item in spec_dict['paths'].items(): for method_key, method_item in path_item.items(): if 'parameters' not in method_item: continue for param in method_item['parameters']: if 'format' in param and param['format'] in exclude_formats: param.pop('format') return spec_dict
def load_client(apikey=None): global client if client is None: # docker containers networking within docker-compose or Fargate task host = 'api.mpcontribs.org' if apikey is None: host = 'api:5000' if DEBUG else 'localhost:5000' protocol = 'https' if apikey else 'http' spec_url = f'{protocol}://{host}/apispec.json' http_client = RequestsClient() if apikey: http_client.set_api_key(host, apikey, param_in='header', param_name='x-api-key') loader = Loader(http_client) spec_dict = loader.load_spec(spec_url) spec_dict['host'] = host spec_dict['schemes'] = [protocol] client = SwaggerClient.from_spec(spec_dict, spec_url, http_client, { 'validate_responses': False, 'use_models': False }) return client
def load(self): http_client = FidoClientGlobalHeaders(headers=self.headers) loader = Loader(http_client) origin_url = f"{self.url}/apispec.json" spec_dict = loader.load_spec(origin_url) spec_dict["host"] = self.host spec_dict["schemes"] = [self.protocol] config = { "validate_responses": False, "use_models": False, "include_missing_properties": False, "formats": [email_format, url_format], } bravado_config = bravado_config_from_config_dict(config) for key in set(bravado_config._fields).intersection(set(config)): del config[key] config["bravado"] = bravado_config swagger_spec = Spec.from_dict(spec_dict, origin_url, http_client, config) super().__init__( swagger_spec, also_return_response=bravado_config.also_return_response) # expand regex-based query parameters for `data` columns try: resp = self.projects.get_entries(_fields=["columns"]).result() except AttributeError: # skip in tests return columns = {"text": [], "number": []} for project in resp["data"]: for column in project["columns"]: if column["path"].startswith("data."): col = column["path"].replace(".", "__") if column["unit"] == "NaN": columns["text"].append(col) else: col = f"{col}__value" columns["number"].append(col) operators = {"text": ["contains"], "number": ["gte", "lte"]} for path, d in spec_dict["paths"].items(): for verb in ["get", "put", "post", "delete"]: if verb in d: old_params = deepcopy(d[verb].pop("parameters")) new_params, param_names = [], set() while old_params: param = old_params.pop() if param["name"].startswith("^data__"): op = param["name"].rsplit("__", 1)[1] for typ, ops in operators.items(): if op in ops: for column in columns[typ]: new_param = deepcopy(param) param_name = f"{column}__{op}" if param_name not in param_names: new_param["name"] = param_name desc = f"filter {column} via ${op}" new_param["description"] = desc new_params.append(new_param) param_names.add(param_name) else: new_params.append(param) d[verb]["parameters"] = new_params swagger_spec = Spec.from_dict(spec_dict, origin_url, http_client, config) super().__init__( swagger_spec, also_return_response=bravado_config.also_return_response)
def config(cls, url, http_client=None, request_headers=None): swagger_path = '{}/swagger.json'.format(url.rstrip("/")) http_client = http_client or RequestsClient() loader = Loader(http_client, request_headers=request_headers) spec_dict = loader.load_spec(swagger_path) return spec_dict
STATIC_ROOT = os.path.join(BASE_DIR, 'static') STATICFILES_DIRS = (os.path.join(BASE_DIR, 'dist'), ) WEBPACK_LOADER = { 'DEFAULT': { 'CACHE': not DEBUG, 'BUNDLE_DIR_NAME': './', 'STATS_FILE': os.path.join(BASE_DIR, 'webpack-stats.json'), } } if os.environ.get('DEPLOYMENT') == 'MATGEN': SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https') from bravado.client import SwaggerClient from bravado.requests_client import RequestsClient from bravado.swagger_model import Loader # docker containers networking within docker-compose or Fargate task apihost = 'api' if DEBUG else 'localhost' apihost = f'{apihost}:5000' spec_url = 'http://{}/apispec.json'.format(apihost) http_client = RequestsClient() loader = Loader(http_client) spec_dict = loader.load_spec(spec_url) spec_dict['host'] = apihost spec_dict['schemes'] = ['http'] swagger_client = SwaggerClient.from_spec(spec_dict, spec_url, http_client, { 'validate_responses': False, 'use_models': False })
def load_spec(): loader = Loader(http_client) return loader.load_spec(build_spec_url(name))