33import asyncio
44import sys
55from functools import partial
6- from typing import TYPE_CHECKING , Literal , TypedDict
6+ from typing import TYPE_CHECKING , TypedDict
77
88if sys .version_info <= (3 , 11 ):
99 from typing_extensions import Unpack
1010else :
1111 from typing import Unpack
1212
13- from .primp import RClient # type: ignore
1413
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
5215
16+ if TYPE_CHECKING :
17+ from .primp import IMPERSONATE , IMPERSONATE_OS , ClientRequestParams , HttpMethod , RequestParams , Response
5318else :
5419
5520 class _Unpack :
@@ -122,42 +87,42 @@ def __init__(
12287 """
12388 super ().__init__ ()
12489
125- def __enter__ (self ):
90+ def __enter__ (self ) -> Client :
12691 return self
12792
12893 def __exit__ (self , * args ):
12994 del self
13095
131- def request (self , method : HttpMethod , url : str , ** kwargs : Unpack [RequestParams ]):
96+ def request (self , method : HttpMethod , url : str , ** kwargs : Unpack [RequestParams ]) -> Response :
13297 return super ().request (method = method , url = url , ** kwargs )
13398
134- def get (self , url : str , ** kwargs : Unpack [RequestParams ]):
99+ def get (self , url : str , ** kwargs : Unpack [RequestParams ]) -> Response :
135100 return self .request (method = "GET" , url = url , ** kwargs )
136101
137- def head (self , url : str , ** kwargs : Unpack [RequestParams ]):
102+ def head (self , url : str , ** kwargs : Unpack [RequestParams ]) -> Response :
138103 return self .request (method = "HEAD" , url = url , ** kwargs )
139104
140- def options (self , url : str , ** kwargs : Unpack [RequestParams ]):
105+ def options (self , url : str , ** kwargs : Unpack [RequestParams ]) -> Response :
141106 return self .request (method = "OPTIONS" , url = url , ** kwargs )
142107
143- def delete (self , url : str , ** kwargs : Unpack [RequestParams ]):
108+ def delete (self , url : str , ** kwargs : Unpack [RequestParams ]) -> Response :
144109 return self .request (method = "DELETE" , url = url , ** kwargs )
145110
146- def post (self , url : str , ** kwargs : Unpack [RequestParams ]):
111+ def post (self , url : str , ** kwargs : Unpack [RequestParams ]) -> Response :
147112 return self .request (method = "POST" , url = url , ** kwargs )
148113
149- def put (self , url : str , ** kwargs : Unpack [RequestParams ]):
114+ def put (self , url : str , ** kwargs : Unpack [RequestParams ]) -> Response :
150115 return self .request (method = "PUT" , url = url , ** kwargs )
151116
152- def patch (self , url : str , ** kwargs : Unpack [RequestParams ]):
117+ def patch (self , url : str , ** kwargs : Unpack [RequestParams ]) -> Response :
153118 return self .request (method = "PATCH" , url = url , ** kwargs )
154119
155120
156121class AsyncClient (Client ):
157122 def __init__ (self , * args , ** kwargs ):
158123 super ().__init__ (* args , ** kwargs )
159124
160- async def __aenter__ (self ):
125+ async def __aenter__ (self ) -> AsyncClient :
161126 return self
162127
163128 async def __aexit__ (self , * args ):
@@ -167,28 +132,28 @@ async def _run_sync_asyncio(self, fn, *args, **kwargs):
167132 loop = asyncio .get_running_loop ()
168133 return await loop .run_in_executor (None , partial (fn , * args , ** kwargs ))
169134
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
171136 return await self ._run_sync_asyncio (super ().request , method = method , url = url , ** kwargs )
172137
173- async def get (self , url : str , ** kwargs : Unpack [RequestParams ]):
138+ async def get (self , url : str , ** kwargs : Unpack [RequestParams ]): # type: ignore
174139 return await self .request (method = "GET" , url = url , ** kwargs )
175140
176- async def head (self , url : str , ** kwargs : Unpack [RequestParams ]):
141+ async def head (self , url : str , ** kwargs : Unpack [RequestParams ]): # type: ignore
177142 return await self .request (method = "HEAD" , url = url , ** kwargs )
178143
179- async def options (self , url : str , ** kwargs : Unpack [RequestParams ]):
144+ async def options (self , url : str , ** kwargs : Unpack [RequestParams ]): # type: ignore
180145 return await self .request (method = "OPTIONS" , url = url , ** kwargs )
181146
182- async def delete (self , url : str , ** kwargs : Unpack [RequestParams ]):
147+ async def delete (self , url : str , ** kwargs : Unpack [RequestParams ]): # type: ignore
183148 return await self .request (method = "DELETE" , url = url , ** kwargs )
184149
185- async def post (self , url : str , ** kwargs : Unpack [RequestParams ]):
150+ async def post (self , url : str , ** kwargs : Unpack [RequestParams ]): # type: ignore
186151 return await self .request (method = "POST" , url = url , ** kwargs )
187152
188- async def put (self , url : str , ** kwargs : Unpack [RequestParams ]):
153+ async def put (self , url : str , ** kwargs : Unpack [RequestParams ]): # type: ignore
189154 return await self .request (method = "PUT" , url = url , ** kwargs )
190155
191- async def patch (self , url : str , ** kwargs : Unpack [RequestParams ]):
156+ async def patch (self , url : str , ** kwargs : Unpack [RequestParams ]): # type: ignore
192157 return await self .request (method = "PATCH" , url = url , ** kwargs )
193158
194159
@@ -230,7 +195,7 @@ def request(
230195 headers: an optional map of HTTP headers to send with requests. If `impersonate` is set, this will be ignored.
231196 cookies: an optional map of cookies to send with requests as the `Cookie` header.
232197 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.
234199 data: the form data to send in the request body. Default is None.
235200 json: a JSON serializable object to send in the request body. Default is None.
236201 files: a map of file fields to file paths to be sent as multipart/form-data. Default is None.
0 commit comments