def get_asyncio_client(ssl_verify=None, ssl_cert=None):
    client = AsyncioClient(
        run_mode=RunMode.THREAD,
        loop=mock.Mock(spec=asyncio.AbstractEventLoop),
        ssl_verify=ssl_verify,
        ssl_cert=ssl_cert,
    )
    client.run_coroutine_func = mock.Mock("run_coroutine_func")
    return client
Beispiel #2
0
    def from_spec(cls,
                  spec_dict,
                  origin_url=None,
                  http_client=None,
                  config=None):
        """
        Build a :class:`SwaggerClient` from a Swagger spec in dict form.

        :param spec_dict: a dict with a Swagger spec in json-like form
        :param origin_url: the url used to retrieve the spec_dict
        :type  origin_url: str
        :param config: Configuration dict - see spec.CONFIG_DEFAULTS

        :rtype: :class:`bravado_core.spec.Spec`
        """
        http_client = http_client or AsyncioClient(
            run_mode=RunMode.FULL_ASYNCIO)

        # Apply aiobravado config defaults
        config = dict(CONFIG_DEFAULTS, **(config or {}))

        also_return_response = config.pop('also_return_response', False)
        swagger_spec = Spec.from_dict(
            spec_dict,
            origin_url,
            http_client,
            config,
        )
        return cls(swagger_spec, also_return_response=also_return_response)
Beispiel #3
0
async def load_url(spec_url, http_client=None, base_url=None):
    """Loads a Swagger spec.

    :param spec_url: URL for swagger.json.
    :param http_client: HTTP client interface.
    :param base_url:    Optional URL to be the base URL for finding API
                        declarations. If not specified, 'basePath' from the
                        resource listing is used.
    :return: validated spec in dict form
    :raise: IOError, URLError: On error reading api-docs.
    """
    if http_client is None:
        http_client = AsyncioClient(run_mode=RunMode.FULL_ASYNCIO)

    loader = Loader(http_client=http_client)
    return await loader.load_spec(spec_url, base_url=base_url)
Beispiel #4
0
    async 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:`aiobravado.http_client.HttpClient`
        :param request_headers: Headers to pass with http requests
        :type  request_headers: dict
        :param config: Config dict for aiobravado and bravado_core.
            See CONFIG_DEFAULTS in :module:`bravado_core.spec`.
            See CONFIG_DEFAULTS in :module:`aiobravado.client`.

        :rtype: :class:`bravado_core.spec.Spec`
        """
        log.debug(u"Loading from %s", spec_url)
        http_client = http_client or AsyncioClient(
            run_mode=RunMode.FULL_ASYNCIO)
        loader = Loader(http_client, request_headers=request_headers)
        spec_dict = await 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)
Beispiel #5
0
def test_prepare_params(params):
    client = AsyncioClient()
    prepared = client.prepare_params(params)
    assert prepared.getall("key") == params["key"]
def test_fail_on_unknown_run_mode():
    with pytest.raises(ValueError):
        AsyncioClient(run_mode="unknown/invalid")
Beispiel #7
0
def http_client(event_loop):
    http_client = AsyncioClient(loop=event_loop, run_mode=RunMode.FULL_ASYNCIO)
    yield http_client
    http_client.client_session.close()