def test_execute_async(self, loop):
        from .py35_only import consume_aiter

        mock_client = MockAsyncClient({
            ('max: 10', 'cursor: 0'): {
                'objects': list(range(3, 13)),
                'next_cursor': 11
            },
            ('max: 10', 'cursor: 11'): {
                'objects': list(range(13, 23)),
                'next_cursor': '22'
            },
            ('max: 10', 'cursor: 22'): {
                'objects': [1, 4],
                'next_cursor': None
            },
        })
        paginated = snug.paginated(mylist(max_items=10))
        assert isinstance(paginated, snug.Query)
        paginator = snug.execute_async(paginated, client=mock_client)

        result = loop.run_until_complete(consume_aiter(paginator))
        assert result == [
            list(range(3, 13)),
            list(range(13, 23)),
            [1, 4],
        ]

        # is reusable
        assert loop.run_until_complete(
            consume_aiter(snug.execute_async(paginated, client=mock_client)))
Example #2
0
def execute_async(obj, url, **kwargs):
    """Execute a GraphQL executable asynchronously

    Parameters
    ----------
    obj: Executable
        The object to execute.
        This may be a raw string or a query
    url: str
        The URL of the target endpoint
    **kwargs
         ``auth`` and/or ``client``,
         passed to :func:`snug.query.execute_async`.

    Returns
    -------
    JSON
        The response data

    Raises
    ------
    ErrorResponse
        If errors are present in the response
    HTTPError
        If the response has a non 2xx response code
    """
    snug_query = irelay(_exec(obj), partial(middleware, url))
    return snug.execute_async(snug_query, **kwargs)
Example #3
0
def execute_async(obj, url, **kwargs):
    """Execute a GraphQL executable asynchronously

    Parameters
    ----------
    obj: Executable
        The object to execute.
        This may be a raw string or a query
    url: str
        The URL of the target endpoint
    **kwargs
         ``auth`` and/or ``client``,
         passed to :func:`snug.query.execute_async`.

    Returns
    -------
    RawResult (a dict) or the schema's return type
        In case of a raw string, a raw result.
        Otherwise, an instance of the schema's type queried for.


    Raises
    ------
    ErrorResponse
        If errors are present in the response
    HTTPError
        If the response has a non 2xx response code
    """
    snug_query = irelay(_exec(obj), partial(middleware, url))
    return snug.execute_async(snug_query, **kwargs)
Example #4
0
    def test_none_auth(self, loop):
        from .py3_only import MockAsyncClient
        client = MockAsyncClient(snug.Response(204))

        future = snug.execute_async(myquery(), auth=None, client=client)
        result = loop.run_until_complete(future)
        assert result == snug.Response(204)
        assert client.request == snug.GET('my/url')
Example #5
0
    def test_auth_callable(self, loop):
        from .py3_only import MockAsyncClient
        client = MockAsyncClient(snug.Response(204))
        auther = methodcaller('with_headers', {'X-My-Auth': 'letmein'})

        future = snug.execute_async(myquery(), auth=auther, client=client)
        result = loop.run_until_complete(future)
        assert result == snug.Response(204)
        assert client.request == snug.GET('my/url',
                                          headers={'X-My-Auth': 'letmein'})
Example #6
0
    def test_auth(self, loop):
        from .py3_only import MockAsyncClient
        client = MockAsyncClient(snug.Response(204))

        future = snug.execute_async(myquery(),
                                    auth=('user', 'pw'),
                                    client=client)
        result = loop.run_until_complete(future)
        assert result == snug.Response(204)
        assert client.request == snug.GET(
            'my/url', headers={'Authorization': 'Basic dXNlcjpwdw=='})
Example #7
0
    def test_custom_execute(self, loop):
        from .py3_only import MockAsyncClient
        client = MockAsyncClient(snug.Response(204))

        class MyQuery:
            def __execute_async__(self, client, auth):
                return client.send(snug.GET('my/url'))

        future = snug.execute_async(MyQuery(), client=client)
        result = loop.run_until_complete(future)
        assert result == snug.Response(204)
        assert client.request == snug.GET('my/url')
Example #8
0
    def test_defaults(self, loop, mocker):
        import asyncio
        from .py3_only import awaitable

        send = mocker.patch('snug._async.send_async',
                            return_value=awaitable(snug.Response(204)))

        future = snug.execute_async(myquery())
        result = loop.run_until_complete(future)
        assert result == snug.Response(204)
        client, req = send.call_args[0]
        assert isinstance(client, asyncio.AbstractEventLoop)
        assert req == snug.GET('my/url')