Ejemplo n.º 1
0
def test_request_attribute() -> None:
    # Exception without request attribute
    exc = httpx.ReadTimeout("Read operation timed out")
    with pytest.raises(RuntimeError):
        exc.request

    # Exception with request attribute
    request = httpx.Request("GET", "https://www.example.com")
    exc = httpx.ReadTimeout("Read operation timed out", request=request)
    assert exc.request == request
Ejemplo n.º 2
0
    async def async_get(self,
                        resource: web.Request,
                        follow_redirects: bool = False,
                        headers: dict = None) -> Page:
        """Fetch the given url, returns a Page object on success, None otherwise.
        If None is returned, the error code can be obtained using the error_code property.

        @param resource: URL to get.
        @type resource: web.Request
        @param follow_redirects: If set to True, responses with a 3XX code and a Location header will be followed.
        @type follow_redirects: bool
        @param headers: Dictionary of additional headers to send with the request.
        @type headers: dict
        @rtype: Page
        """
        request = self.client.build_request("GET",
                                            resource.url,
                                            headers=headers)
        try:
            response = await self.client.send(request,
                                              stream=self.stream,
                                              allow_redirects=follow_redirects,
                                              timeout=self._timeout)
        except httpx.TransportError as exception:
            if "Read timed out" in str(exception):
                raise httpx.ReadTimeout("Request time out", request=None)

            raise exception

        return Page(response)
Ejemplo n.º 3
0
    async def async_post(self,
                         form: web.Request,
                         follow_redirects: bool = False,
                         headers: dict = None) -> Page:
        """Submit the given form, returns a Page on success, None otherwise.

        @type form: web.Request
        @type follow_redirects: bool
        @type headers: dict
        @rtype: Page
        """
        form_headers = {}
        if not form.is_multipart:
            form_headers = {"Content-Type": form.enctype}

        if isinstance(headers, dict) and headers:
            form_headers.update(headers)

        if form.referer:
            form_headers["referer"] = form.referer

        if form.is_multipart or "urlencoded" in form.enctype:
            file_params = form.file_params
            post_params = form.post_params
        else:
            file_params = None
            post_params = form.post_params

        content = None

        if post_params:
            if isinstance(post_params, str):
                content = post_params
                post_params = None
            else:
                content = None
                post_params = dict(post_params)
        else:
            post_params = None

        request = self.client.build_request(
            "POST",
            form.path,
            params=form.get_params,
            data=
            post_params,  # httpx expects a dict, hope to see more types soon
            content=content,
            files=file_params or None,
            headers=form_headers,
            timeout=self._timeout)
        try:
            response = await self.client.send(
                request, stream=self.stream, follow_redirects=follow_redirects)
        except httpx.TransportError as exception:
            if "Read timed out" in str(exception):
                raise httpx.ReadTimeout("Request time out", request=None)

            raise exception

        return Page(response)
Ejemplo n.º 4
0
async def test_timeout():
    api = MockApi(
        iter([
            httpx.ConnectTimeout(),
            [{
                "update_id": 0,
                "message": "A"
            }],
            httpx.ReadTimeout(),
            httpx.WriteTimeout(),
            [{
                "update_id": 1,
                "message": "B"
            }],
        ]))
    updater = make_updater(api)

    updates = await updater.get_updates()
    assert len(updates) == 1
    assert updates[0]["message"] == "A"
    assert updater.offset == 1

    updates = await updater.get_updates()
    assert len(updates) == 1
    assert updates[0]["message"] == "B"
    assert updater.offset == 2
Ejemplo n.º 5
0
def test_sync_send_message_exception(valid_sync_client, httpx_mock):
    httpx_mock.add_exception(
        httpx.ReadTimeout('Unable to read within timeout'))

    with pytest.raises(SigmaClientError) as exc:
        valid_sync_client.send_message('sender', '+79999999999', 'test', 'sms')

    assert exc.value.reason == 'Unable to read within timeout'
    assert exc.value.status == 'failed'
Ejemplo n.º 6
0
    async def async_request(self,
                            method: str,
                            form: web.Request,
                            follow_redirects: bool = False,
                            headers: dict = None) -> Page:
        """Submit the given form, returns a Page on success, None otherwise.

        @type method: str
        @type form: web.Request
        @type follow_redirects: bool
        @type headers: dict
        @rtype: Page
        """
        form_headers = {}
        if isinstance(headers, dict) and headers:
            form_headers.update(headers)

        if form.referer:
            form_headers["referer"] = form.referer

        post_params = form.post_params
        content = None

        if post_params:
            if isinstance(post_params, str):
                content = post_params
                post_params = None
            else:
                content = None
                post_params = dict(post_params)
        else:
            post_params = None

        request = self.client.build_request(
            method,
            form.url,
            data=post_params,
            content=content,
            files=form.file_params or None,
            headers=form_headers,
        )
        try:
            response = await self.client.send(request,
                                              stream=self.stream,
                                              allow_redirects=follow_redirects,
                                              timeout=self._timeout)
        except httpx.TransportError as exception:
            if "Read timed out" in str(exception):
                raise httpx.ReadTimeout("Request time out", request=None)

            raise exception

        return Page(response)
Ejemplo n.º 7
0
 def raise_timeout(request, ext):
     raise httpx.ReadTimeout("timeout", request=request)
Ejemplo n.º 8
0
 def raise_timeout(request, extensions: dict):
     raise httpx.ReadTimeout(
         f"Unable to read within {extensions['timeout']}", request=request)
Ejemplo n.º 9
0
 def raise_timeout(request):
     """Set the timeout for the requests."""
     raise httpx.ReadTimeout(
         f"Unable to read within {request.extensions['timeout']}",
         request=request)
Ejemplo n.º 10
0
 def raise_timeout(*args, **kwargs):
     raise httpx.ReadTimeout()
Ejemplo n.º 11
0
 def timeout_callback(http_request):
     if "sleep" in str(http_request.url):
         raise httpx.ReadTimeout("Read timed out", request=http_request)
     return httpx.Response(200, text="Hello there")
Ejemplo n.º 12
0
 def raise_timeout(request, ext):
     raise httpx.ReadTimeout(f"HTTP error occurred", request=request)
Ejemplo n.º 13
0
 def raise_timeout(request, extensions: dict):
     raise httpx.ReadTimeout('Test timeout error', request=request)
 def raise_timeout(request: httpx.Request):
     raise httpx.ReadTimeout(
         f"Unable to read within {request.extensions['timeout']} seconds",
         request=request,
     )
Ejemplo n.º 15
0
 def raise_timeout(request, ext):
     raise httpx.ReadTimeout(
         f"Unable to read within {ext['timeout']['read']}", request=request
     )