File size: 2,811 Bytes
1f1b4db
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import httpx
from typing import Dict, Any, AsyncGenerator
from config.constants import DEFAULT_HEADERS


class HTTPClient:
    def __init__(self):
        self.client = httpx.AsyncClient(verify=False, http2=True)
    
    async def post(

        self, url: str, data: Any = None, headers: Dict = None, compress: bool = False, default_headers: bool = True, **kwargs

    ) -> httpx.Response:
        """

        Send POST request with optional compression

        """
        if default_headers:
            request_headers = {**DEFAULT_HEADERS, **(headers or {})}
        else:
            request_headers = headers or {}

        if compress:
            from .compression import compress_data

            data = compress_data(data)
            request_headers["Content-Encoding"] = "gzip"
            request_headers["Connect-Content-Encoding"] = "gzip"
            request_headers["Content-Length"] = str(len(data))

        # Remove explicitly specified params from kwargs
        for param in ['url', 'data', 'stream', 'headers', 'verify', 'timeout']:
            kwargs.pop(param, None)

        response = await self.client.post(
            url=url, 
            content=data,  # Changed from data to content
            headers=request_headers, 
            timeout=60, 
            **kwargs
        )
        return response

    async def stream_post(

        self, url: str, data: Any = None, headers: Dict = None, compress: bool = False, default_headers: bool = True, **kwargs

    ) -> AsyncGenerator[bytes, None]:
        """

        Send POST request with streaming response

        Yields chunks of response data as they arrive

        """
        if default_headers:
            request_headers = {**DEFAULT_HEADERS, **(headers or {})}
        else:
            request_headers = headers or {}

        if compress:
            from .compression import compress_data
            data = compress_data(data)
            request_headers["Content-Encoding"] = "gzip"
            request_headers["Connect-Content-Encoding"] = "gzip"
            request_headers["Content-Length"] = str(len(data))

        # Remove explicitly specified params from kwargs
        for param in ['url', 'data', 'stream', 'headers', 'verify', 'timeout']:
            kwargs.pop(param, None)

        async with self.client.stream(
            method="POST",
            url=url,
            content=data,
            headers=request_headers,
            timeout=60,
            **kwargs
        ) as response:
            async for chunk in response.aiter_bytes():
                yield chunk

    async def __aenter__(self):
        return self

    async def __aexit__(self, exc_type, exc_val, exc_tb):
        await self.client.aclose()