tmp
zhaoqingang
2025-01-14 00fe58a94292a3b9921ce134542ee38d74cd9401
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
import httpx
from pycparser.ply.yacc import token
 
 
class ChatBase:
 
    @staticmethod
    async def http_stream(url, data, headers, timeout=300):
        async with httpx.AsyncClient(timeout=timeout) as client:
 
            async with client.stream("POST", url, json=data, headers=headers) as response:
                if response.status_code == 200:
                    try:
                        async for answer in response.aiter_text():
                            yield answer
                    except GeneratorExit as e:
                        print(e)
                        return
                else:
                    yield f"Error: {response.status_code}"
 
    @staticmethod
    async def http_post(url, data, headers, timeout=300):
        async with httpx.AsyncClient(timeout=timeout) as client:
            response = await client.post(url, json=data, headers=headers)
            return response
 
    @staticmethod
    async def http_get(url, params, headers, timeout=300):
        async with httpx.AsyncClient(timeout=timeout) as client:
            response = await client.get(url, params=params, headers=headers)
            return response
 
    @staticmethod
    async def get_headers(token):
        return {
            'Content-Type': 'application/json',
            'Authorization': f'Bearer {token}'
        }