Download api/http_client.py from danishk84/NPRasa: direct link, hf CLI and curl.
- Browser
- Download file 1.33 kB
-
https://huggingface.co/spaces/danishk84/NPRasa/resolve/main/api/http_client.py
- Command line
-
hf download hf://spaces/danishk84/NPRasa/api/http_client.py
-
curl -L -o http_client.py https://huggingface.co/spaces/danishk84/NPRasa/resolve/main/api/http_client.py
1.33 kB
| from __future__ import annotations | |
| from typing import Any | |
| import httpx | |
| from tenacity import ( | |
| retry, | |
| retry_if_exception_type, | |
| stop_after_attempt, | |
| wait_exponential, | |
| ) | |
| RETRYABLE = ( | |
| httpx.ConnectError, | |
| httpx.TimeoutException, | |
| httpx.NetworkError, | |
| ) | |
| def _retry_decorator(): | |
| return retry( | |
| reraise=True, | |
| stop=stop_after_attempt(3), | |
| wait=wait_exponential(multiplier=0.5, min=0.5, max=4), | |
| retry=retry_if_exception_type(RETRYABLE), | |
| ) | |
| async def get_json( | |
| url: str, | |
| *, | |
| timeout: float = 10.0, | |
| headers: dict[str, str] | None = None, | |
| params: dict[str, Any] | None = None, | |
| follow_redirects: bool = True, | |
| ) -> httpx.Response: | |
| async with httpx.AsyncClient(timeout=timeout, follow_redirects=follow_redirects) as client: | |
| response = await client.get(url, headers=headers, params=params) | |
| return response | |
| async def post_json( | |
| url: str, | |
| *, | |
| json: dict[str, Any] | list[Any], | |
| timeout: float = 10.0, | |
| headers: dict[str, str] | None = None, | |
| ) -> httpx.Response: | |
| merged = {"Content-Type": "application/json", **(headers or {})} | |
| async with httpx.AsyncClient(timeout=timeout) as client: | |
| response = await client.post(url, json=json, headers=merged) | |
| return response | |