File size: 1,330 Bytes
82cffbe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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),
    )


@_retry_decorator()
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


@_retry_decorator()
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