本文整理汇总了Python中bravado.client.SwaggerClient.from_spec方法的典型用法代码示例。如果您正苦于以下问题:Python SwaggerClient.from_spec方法的具体用法?Python SwaggerClient.from_spec怎么用?Python SwaggerClient.from_spec使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类bravado.client.SwaggerClient
的用法示例。
在下文中一共展示了SwaggerClient.from_spec方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: setup_client
# 需要导入模块: from bravado.client import SwaggerClient [as 别名]
# 或者: from bravado.client.SwaggerClient import from_spec [as 别名]
def setup_client(
url: str,
schema: Optional[Dict[str, Any]] = None,
) -> Optional[str]:
"""
:returns: error message, if appropriate.
"""
if get_abstraction().client:
return None
try:
config = {'internally_dereference_refs': True}
if not schema:
client = SwaggerClient.from_url(url, config=config)
else:
client = SwaggerClient.from_spec(schema, origin_url=url, config=config)
except requests.exceptions.ConnectionError:
return 'Unable to connect to server.'
except (
simplejson.errors.JSONDecodeError, # type: ignore
yaml.YAMLError,
HTTPError,
):
return (
'Invalid swagger file. Please check to make sure the '
'swagger file can be found at: {}.'.format(url)
)
except SwaggerValidationError:
return 'Invalid swagger format.'
get_abstraction().client = client
return None
示例2: __init__
# 需要导入模块: from bravado.client import SwaggerClient [as 别名]
# 或者: from bravado.client.SwaggerClient import from_spec [as 别名]
def __init__(self, swagger_spec: Spec):
"""
:param swagger_spec: Complete swagger specification for the API to test.
"""
self.host = "localhost"
self.swagger_spec = swagger_spec
self.base_path = swagger_spec.client_spec_dict.get("basePath", "")
self.swagger_spec.spec_dict['host'] = f'{self.host}:{TEST_PORT}'
# setting validate requests to false since we only care about
# responses
config = {
"also_return_response": True,
"use_models": False,
"validate_requests": False,
"formats": swagger_spec.config.get("formats", [])
}
self.config = config
# Bravado provides an async client, but it doesnt run on ioloop? from docs:
# Fido is a simple, asynchronous HTTP client built on top of Crochet and Twisted with an implementation
# inspired by the book "Twisted Network Programming Essentials". It is intended to be used in environments
# where there is no event loop, and where you cannot afford to spin up lots of threads (otherwise you
# could just use a ThreadPoolExecutor).
self.swagger_client = SwaggerClient.from_spec(
self.swagger_spec.spec_dict,
http_client=RequestsClient(),
config=config)
示例3: bitmex_api
# 需要导入模块: from bravado.client import SwaggerClient [as 别名]
# 或者: from bravado.client.SwaggerClient import from_spec [as 别名]
def bitmex_api(test=True, config=None, api_key=None, api_secret=None):
# config options at http://bravado.readthedocs.io/en/latest/configuration.html
if not config:
config = {
# Don't use models (Python classes) instead of dicts for #/definitions/{models}
'use_models': False,
# bravado has some issues with nullable fields
'validate_responses': False,
# Returns response in 2-tuple of (body, response); if False, will only return body
'also_return_response': True,
# 'validate_swagger_spec': True,
# 'validate_requests': True,
# 'formats': [],
}
host = 'https://www.bitmex.com'
if test:
host = 'https://testnet.bitmex.com'
spec_uri = host + '/api/explorer/swagger.json'
spec_dict = get_swagger_json(spec_uri, exclude_formats=EXCLUDE_SWG_FORMATS)
if api_key and api_secret:
request_client = RequestsClient()
request_client.authenticator = APIKeyAuthenticator(host, api_key, api_secret)
return SwaggerClient.from_spec(spec_dict, origin_url=spec_uri, http_client=request_client, config=config)
else:
return SwaggerClient.from_spec(spec_dict, origin_url=spec_uri, http_client=None, config=config)
# exclude some format from swagger json to avoid warning in API execution.
示例4: test_dereferenced_swagger_schema_bravado_client
# 需要导入模块: from bravado.client import SwaggerClient [as 别名]
# 或者: from bravado.client.SwaggerClient import from_spec [as 别名]
def test_dereferenced_swagger_schema_bravado_client(
schema_format,
test_app_deref,
):
from bravado.client import SwaggerClient
response = test_app_deref.get('/swagger.{0}'.format(schema_format))
deserializer = DESERIALIZERS[schema_format]
specs = deserializer(response)
SwaggerClient.from_spec(specs)
示例5: client
# 需要导入模块: from bravado.client import SwaggerClient [as 别名]
# 或者: from bravado.client.SwaggerClient import from_spec [as 别名]
def client():
with open("../../swagger.yml") as f:
spec = yaml.load(f)
_client = SwaggerClient.from_spec(spec)
_client.swagger_spec.api_url = HOST
return _client
示例6: bitmex
# 需要导入模块: from bravado.client import SwaggerClient [as 别名]
# 或者: from bravado.client.SwaggerClient import from_spec [as 别名]
def bitmex(test=True, config=None, api_key=None, api_secret=None):
# config options at http://bravado.readthedocs.io/en/latest/configuration.html
if not config:
config = {
# Don't use models (Python classes) instead of dicts for #/definitions/{models}
'use_models': False,
# bravado has some issues with nullable fields
'validate_responses': False,
# Returns response in 2-tuple of (body, response); if False, will only return body
'also_return_response': True,
# 'validate_swagger_spec': True,
# 'validate_requests': True,
# 'formats': [],
}
host = 'https://www.bitmex.com'
if test:
host = 'https://testnet.bitmex.com'
spec_uri = host + '/api/explorer/swagger.json'
spec_dict = get_swagger_json(spec_uri, exclude_formats=EXCLUDE_SWG_FORMATS)
if api_key and api_secret:
request_client = RequestsClient()
request_client.authenticator = APIKeyAuthenticator(host, api_key, api_secret)
return SwaggerClient.from_spec(spec_dict, origin_url=spec_uri, http_client=request_client, config=config)
else:
return SwaggerClient.from_spec(spec_dict, origin_url=spec_uri, http_client=None, config=config)
# exclude some format from swagger json to avoid warning in API execution.
示例7: __init__
# 需要导入模块: from bravado.client import SwaggerClient [as 别名]
# 或者: from bravado.client.SwaggerClient import from_spec [as 别名]
def __init__(self, url=None, config=DEFAULT_CONFIG, http_client=None, request_headers=None, local=False):
"""
Instantiates :class:`~bravado.client.SwaggerClient`.
For further documentation, refer to the documentation
for :meth:`bravado.client.SwaggerClient.from_url` and
:meth:`bravado.client.SwaggerClient.from_spec`.
:param str url: the URL of a Swagger specification. If ``local``
is True, this should be the base URL of a DOS
implementation (see ``local``).
:param dict config: see :meth:`bravado.client.SwaggerClient`
:param http_client: see :meth:`bravado.client.SwaggerClient`
:param request_headers: see :meth:`beravado.client.SwaggerClient`
:param bool local: set this to True to load the local schema.
If this is True, the ``url`` parameter should
point to the host and base path of the
implementation under test::
Client(url='https://example.com/ga4gh/drs/v1/', local=True)
If False, the ``url`` parameter should point to a
Swagger specification (``swagger.json``).
"""
self._config = config
config['formats'] = [int64_format]
if local:
# :meth:`bravado.client.SwaggerClient.from_spec` takes a schema
# as a Python dictionary, which we can conveniently expose
# via :func:`ga4gh.drs.schema.present_schema`.
schema = ga4gh.drs.schema.present_schema()
# Set schema['host'] and schema['basePath'] to the provided
# values if specified, otherwise leave them as they are
url = urlparse.urlparse(url)
schema['host'] = url.netloc or schema['host']
schema['basePath'] = url.path or schema['basePath']
self.models = SwaggerClient.from_spec(spec_dict=schema,
config=config,
http_client=http_client)
else:
# If ``local=False``, ``url`` is expected to be a ``swagger.json``
swagger_path = '{}/swagger.json'.format(url.rstrip("/"))
self.models = SwaggerClient.from_url(swagger_path,
config=config,
http_client=http_client,
request_headers=request_headers)
self.client = self.models.DataRepositoryService
示例8: get_paasta_api_client
# 需要导入模块: from bravado.client import SwaggerClient [as 别名]
# 或者: from bravado.client.SwaggerClient import from_spec [as 别名]
def get_paasta_api_client(
cluster: str = None,
system_paasta_config: SystemPaastaConfig = None,
http_res: bool = False,
) -> Any:
if not system_paasta_config:
system_paasta_config = load_system_paasta_config()
if not cluster:
cluster = system_paasta_config.get_cluster()
api_endpoints = system_paasta_config.get_api_endpoints()
if cluster not in api_endpoints:
log.error("Cluster %s not in paasta-api endpoints config", cluster)
return None
url = str(api_endpoints[cluster])
parsed = urlparse(url)
if not parsed:
log.error("Unsupported paasta-api url %s", url)
return None
api_server = parsed.netloc
# Get swagger spec from file system instead of the api server
paasta_api_path = os.path.dirname(paasta_tools.api.__file__)
swagger_file = os.path.join(paasta_api_path, "api_docs/swagger.json")
if not os.path.isfile(swagger_file):
log.error("paasta-api swagger spec %s does not exist", swagger_file)
return None
with open(swagger_file) as f:
spec_dict = json.load(f)
# replace localhost in swagger.json with actual api server
spec_dict["host"] = api_server
spec_dict["schemes"] = [parsed.scheme]
# sometimes we want the status code
requests_client = PaastaRequestsClient(
scheme=parsed.scheme, cluster=cluster, system_paasta_config=system_paasta_config
)
if http_res:
config = {"also_return_response": True}
c = SwaggerClient.from_spec(
spec_dict=spec_dict, config=config, http_client=requests_client
)
else:
c = SwaggerClient.from_spec(spec_dict=spec_dict, http_client=requests_client)
return paasta_tools.api.auth_decorator.AuthClientDecorator(c, cluster_name=cluster)