3
3
import asyncio
4
4
import sys
5
5
from functools import partial
6
- from typing import TYPE_CHECKING , Literal , TypedDict
6
+ from typing import TYPE_CHECKING , TypedDict
7
7
8
8
if sys .version_info <= (3 , 11 ):
9
9
from typing_extensions import Unpack
10
10
else :
11
11
from typing import Unpack
12
12
13
- from .primp import RClient # type: ignore
14
13
15
- if TYPE_CHECKING :
16
- HttpMethod = Literal ["GET" , "HEAD" , "OPTIONS" , "DELETE" , "POST" , "PUT" , "PATCH" ]
17
- IMPERSONATE = Literal [
18
- "chrome_100" , "chrome_101" , "chrome_104" , "chrome_105" , "chrome_106" ,
19
- "chrome_107" , "chrome_108" , "chrome_109" , "chrome_114" , "chrome_116" ,
20
- "chrome_117" , "chrome_118" , "chrome_119" , "chrome_120" , "chrome_123" ,
21
- "chrome_124" , "chrome_126" , "chrome_127" , "chrome_128" , "chrome_129" ,
22
- "chrome_130" , "chrome_131" ,
23
- "safari_15.3" , "safari_15.5" , "safari_15.6.1" , "safari_16" ,
24
- "safari_16.5" , "safari_17.0" , "safari_17.2.1" , "safari_17.4.1" ,
25
- "safari_17.5" , "safari_18" , "safari_18.2" ,
26
- "safari_ios_16.5" , "safari_ios_17.2" , "safari_ios_17.4.1" , "safari_ios_18.1.1" ,
27
- "safari_ipad_18" ,
28
- "okhttp_3.9" , "okhttp_3.11" , "okhttp_3.13" , "okhttp_3.14" , "okhttp_4.9" ,
29
- "okhttp_4.10" , "okhttp_5" ,
30
- "edge_101" , "edge_122" , "edge_127" , "edge_131" ,
31
- "firefox_109" , "firefox_117" , "firefox_128" , "firefox_133" ,
32
- ] # fmt: skip
33
- IMPERSONATE_OS = Literal ["android" , "ios" , "linux" , "macos" , "windows" ]
34
-
35
- class RequestParams (TypedDict , total = False ):
36
- auth : tuple [str , str | None ] | None
37
- auth_bearer : str | None
38
- params : dict [str , str ] | None
39
- headers : dict [str , str ] | None
40
- cookies : dict [str , str ] | None
41
- timeout : float | None
42
- content : bytes | None
43
- data : dict [str , str ] | None
44
- json : dict [str , str ] | None
45
- files : dict [str , str ] | None
46
-
47
- class ClientRequestParams (RequestParams ):
48
- impersonate : IMPERSONATE | None
49
- impersonate_os : IMPERSONATE_OS | None
50
- verify : bool | None
51
- ca_cert_file : str | None
14
+ from .primp import RClient
52
15
16
+ if TYPE_CHECKING :
17
+ from .primp import IMPERSONATE , IMPERSONATE_OS , ClientRequestParams , HttpMethod , RequestParams , Response
53
18
else :
54
19
55
20
class _Unpack :
@@ -122,42 +87,42 @@ def __init__(
122
87
"""
123
88
super ().__init__ ()
124
89
125
- def __enter__ (self ):
90
+ def __enter__ (self ) -> Client :
126
91
return self
127
92
128
93
def __exit__ (self , * args ):
129
94
del self
130
95
131
- def request (self , method : HttpMethod , url : str , ** kwargs : Unpack [RequestParams ]):
96
+ def request (self , method : HttpMethod , url : str , ** kwargs : Unpack [RequestParams ]) -> Response :
132
97
return super ().request (method = method , url = url , ** kwargs )
133
98
134
- def get (self , url : str , ** kwargs : Unpack [RequestParams ]):
99
+ def get (self , url : str , ** kwargs : Unpack [RequestParams ]) -> Response :
135
100
return self .request (method = "GET" , url = url , ** kwargs )
136
101
137
- def head (self , url : str , ** kwargs : Unpack [RequestParams ]):
102
+ def head (self , url : str , ** kwargs : Unpack [RequestParams ]) -> Response :
138
103
return self .request (method = "HEAD" , url = url , ** kwargs )
139
104
140
- def options (self , url : str , ** kwargs : Unpack [RequestParams ]):
105
+ def options (self , url : str , ** kwargs : Unpack [RequestParams ]) -> Response :
141
106
return self .request (method = "OPTIONS" , url = url , ** kwargs )
142
107
143
- def delete (self , url : str , ** kwargs : Unpack [RequestParams ]):
108
+ def delete (self , url : str , ** kwargs : Unpack [RequestParams ]) -> Response :
144
109
return self .request (method = "DELETE" , url = url , ** kwargs )
145
110
146
- def post (self , url : str , ** kwargs : Unpack [RequestParams ]):
111
+ def post (self , url : str , ** kwargs : Unpack [RequestParams ]) -> Response :
147
112
return self .request (method = "POST" , url = url , ** kwargs )
148
113
149
- def put (self , url : str , ** kwargs : Unpack [RequestParams ]):
114
+ def put (self , url : str , ** kwargs : Unpack [RequestParams ]) -> Response :
150
115
return self .request (method = "PUT" , url = url , ** kwargs )
151
116
152
- def patch (self , url : str , ** kwargs : Unpack [RequestParams ]):
117
+ def patch (self , url : str , ** kwargs : Unpack [RequestParams ]) -> Response :
153
118
return self .request (method = "PATCH" , url = url , ** kwargs )
154
119
155
120
156
121
class AsyncClient (Client ):
157
122
def __init__ (self , * args , ** kwargs ):
158
123
super ().__init__ (* args , ** kwargs )
159
124
160
- async def __aenter__ (self ):
125
+ async def __aenter__ (self ) -> AsyncClient :
161
126
return self
162
127
163
128
async def __aexit__ (self , * args ):
@@ -167,28 +132,28 @@ async def _run_sync_asyncio(self, fn, *args, **kwargs):
167
132
loop = asyncio .get_running_loop ()
168
133
return await loop .run_in_executor (None , partial (fn , * args , ** kwargs ))
169
134
170
- async def request (self , method : HttpMethod , url : str , ** kwargs : Unpack [RequestParams ]):
135
+ async def request (self , method : HttpMethod , url : str , ** kwargs : Unpack [RequestParams ]): # type: ignore
171
136
return await self ._run_sync_asyncio (super ().request , method = method , url = url , ** kwargs )
172
137
173
- async def get (self , url : str , ** kwargs : Unpack [RequestParams ]):
138
+ async def get (self , url : str , ** kwargs : Unpack [RequestParams ]): # type: ignore
174
139
return await self .request (method = "GET" , url = url , ** kwargs )
175
140
176
- async def head (self , url : str , ** kwargs : Unpack [RequestParams ]):
141
+ async def head (self , url : str , ** kwargs : Unpack [RequestParams ]): # type: ignore
177
142
return await self .request (method = "HEAD" , url = url , ** kwargs )
178
143
179
- async def options (self , url : str , ** kwargs : Unpack [RequestParams ]):
144
+ async def options (self , url : str , ** kwargs : Unpack [RequestParams ]): # type: ignore
180
145
return await self .request (method = "OPTIONS" , url = url , ** kwargs )
181
146
182
- async def delete (self , url : str , ** kwargs : Unpack [RequestParams ]):
147
+ async def delete (self , url : str , ** kwargs : Unpack [RequestParams ]): # type: ignore
183
148
return await self .request (method = "DELETE" , url = url , ** kwargs )
184
149
185
- async def post (self , url : str , ** kwargs : Unpack [RequestParams ]):
150
+ async def post (self , url : str , ** kwargs : Unpack [RequestParams ]): # type: ignore
186
151
return await self .request (method = "POST" , url = url , ** kwargs )
187
152
188
- async def put (self , url : str , ** kwargs : Unpack [RequestParams ]):
153
+ async def put (self , url : str , ** kwargs : Unpack [RequestParams ]): # type: ignore
189
154
return await self .request (method = "PUT" , url = url , ** kwargs )
190
155
191
- async def patch (self , url : str , ** kwargs : Unpack [RequestParams ]):
156
+ async def patch (self , url : str , ** kwargs : Unpack [RequestParams ]): # type: ignore
192
157
return await self .request (method = "PATCH" , url = url , ** kwargs )
193
158
194
159
@@ -230,7 +195,7 @@ def request(
230
195
headers: an optional map of HTTP headers to send with requests. If `impersonate` is set, this will be ignored.
231
196
cookies: an optional map of cookies to send with requests as the `Cookie` header.
232
197
timeout: the timeout for the request in seconds. Default is 30.
233
- content: he content to send in the request body as bytes. Default is None.
198
+ content: the content to send in the request body as bytes. Default is None.
234
199
data: the form data to send in the request body. Default is None.
235
200
json: a JSON serializable object to send in the request body. Default is None.
236
201
files: a map of file fields to file paths to be sent as multipart/form-data. Default is None.
0 commit comments