-
-
Notifications
You must be signed in to change notification settings - Fork 103
Expand file tree
/
Copy pathblocking.py
More file actions
676 lines (531 loc) · 13.4 KB
/
blocking.py
File metadata and controls
676 lines (531 loc) · 13.4 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
import datetime
from typing import (
Any,
Sequence,
Unpack,
)
from . import (
ClientConfig,
Message,
Method,
Request,
SocketAddr,
StatusCode,
Streamer,
Version,
WebSocketRequest,
)
from .cookie import Cookie, Jar
from .header import HeaderMap
from .redirect import History
from .tls import TlsInfo
class Response:
r"""
A blocking response from a request.
"""
url: str
r"""
Get the URL of the response.
"""
status: StatusCode
r"""
Get the status code of the response.
"""
version: Version
r"""
Get the HTTP version of the response.
"""
headers: HeaderMap
r"""
Get the headers of the response.
"""
cookies: Sequence[Cookie]
r"""
Get the cookies of the response.
"""
content_length: int | None
r"""
Get the content length of the response.
"""
remote_addr: SocketAddr | None
r"""
Get the remote address of the response.
"""
local_addr: SocketAddr | None
r"""
Get the local address of the response.
"""
history: Sequence[History]
r"""
Get the redirect history of the Response.
"""
tls_info: TlsInfo | None
r"""
Get the TLS information of the response.
"""
def raise_for_status(self) -> None:
r"""
Turn a response into an error if the server returned an error.
"""
def stream(self) -> Streamer:
r"""
Get the response into a `Streamer` of `bytes` from the body.
"""
...
def text(self, encoding: str | None = None) -> str:
r"""
Get the text content with the response encoding, defaulting to utf-8 when unspecified.
"""
...
def json(self) -> Any:
r"""
Get the JSON content of the response.
"""
def bytes(self) -> bytes:
r"""
Get the bytes content of the response.
"""
...
def close(self) -> None:
r"""
Close the response.
**Current behavior:**
- When connection pooling is **disabled**: This method closes the network connection.
- When connection pooling is **enabled**: This method closes the response, prevents further body reads,
and returns the connection to the pool for reuse.
**Future changes:**
In future versions, this method will be changed to always close the network connection regardless of
whether connection pooling is enabled or not.
**Recommendation:**
It is **not recommended** to manually call this method at present. Instead, use context managers
(with statement) to properly manage response lifecycle. Wait for the improved implementation
in future versions.
"""
def __enter__(self) -> "Response": ...
def __exit__(self, _exc_type: Any, _exc_value: Any, _traceback: Any) -> None: ...
def __str__(self) -> str: ...
class WebSocket:
r"""
A blocking WebSocket response.
"""
status: StatusCode
r"""
Get the status code of the response.
"""
version: Version
r"""
Get the HTTP version of the response.
"""
headers: HeaderMap
r"""
Get the headers of the response.
"""
cookies: Sequence[Cookie]
r"""
Get the cookies of the response.
"""
remote_addr: SocketAddr | None
r"""
Get the remote address of the response.
"""
protocol: str | None
r"""
Get the WebSocket protocol.
"""
def recv(self, timeout: datetime.timedelta | None = None) -> Message | None:
r"""
Receive a message from the WebSocket.
"""
def send(self, message: Message) -> None:
r"""
Send a message to the WebSocket.
# Arguments
* `message` - The message to send.
"""
def send_all(self, messages: Sequence[Message]) -> None:
r"""
Send multiple messages to the WebSocket.
# Arguments
* `messages` - The sequence of messages to send.
"""
def close(
self,
code: int | None = None,
reason: str | None = None,
) -> None:
r"""
Close the WebSocket connection.
# Arguments
* `code` - An optional close code.
* `reason` - An optional reason for closing.
"""
def __enter__(self) -> "WebSocket": ...
def __exit__(self, _exc_type: Any, _exc_value: Any, _traceback: Any) -> None: ...
def __str__(self) -> str: ...
class Client:
r"""
A blocking client for making HTTP requests.
"""
cookie_jar: Jar | None
r"""
Get the cookie jar used by this client (if enabled/configured).
Returns:
- The provided `Jar` if the client was constructed with `cookie_provider=...`
- The auto-created `Jar` if the client was constructed with `cookie_store=True`
"""
def __init__(
self,
**kwargs: Unpack[ClientConfig],
) -> None:
r"""
Creates a new blocking Client instance.
# Examples
```python
import asyncio
import rnet
client = rnet.blocking.Client(
user_agent="Mozilla/5.0",
timeout=10,
)
response = client.get('https://httpbin.io/get')
print(response.text())
```
"""
...
def request(
self,
method: Method,
url: str,
**kwargs: Unpack[Request],
) -> "Response":
r"""
Sends a request with the given method and URL.
# Examples
```python
import rnet
import rnet.blocking
from rnet import Method
client = rnet.blocking.Client()
response = client.request(Method.GET, "https://httpbin.io/anything")
```
"""
...
def websocket(self, url: str, **kwargs: Unpack[WebSocketRequest]) -> "WebSocket":
r"""
Sends a WebSocket request.
# Examples
```python
import rnet
import rnet.blocking
client = rnet.blocking.Client()
ws = client.websocket("wss://echo.websocket.org")
ws.send(rnet.Message.from_text("Hello, WebSocket!"))
message = ws.recv()
print("Received:", message.data)
ws.close()
```
"""
...
def trace(
self,
url: str,
**kwargs: Unpack[Request],
) -> "Response":
r"""
Sends a request with the given URL.
# Examples
```python
import rnet
import rnet.blocking
from rnet import Method
client = rnet.blocking.Client()
response = client.trace("https://httpbin.io/anything")
print(response.text())
```
"""
...
def options(
self,
url: str,
**kwargs: Unpack[Request],
) -> "Response":
r"""
Sends a request with the given URL.
# Examples
```python
import rnet
import rnet.blocking
from rnet import Method
client = rnet.blocking.Client()
response = client.options("https://httpbin.io/anything")
print(response.text())
```
"""
...
def head(
self,
url: str,
**kwargs: Unpack[Request],
) -> "Response":
r"""
Sends a request with the given URL.
# Examples
```python
import rnet
import rnet.blocking
from rnet import Method
client = rnet.blocking.Client()
response = client.head("https://httpbin.io/anything")
print(response.text())
```
"""
...
def delete(
self,
url: str,
**kwargs: Unpack[Request],
) -> "Response":
r"""
Sends a request with the given URL.
# Examples
```python
import rnet
import rnet.blocking
from rnet import Method
client = rnet.blocking.Client()
response = client.delete("https://httpbin.io/anything")
print(response.text())
```
"""
...
def patch(
self,
url: str,
**kwargs: Unpack[Request],
) -> "Response":
r"""
Sends a request with the given URL.
# Examples
```python
import rnet
import rnet.blocking
from rnet import Method
client = rnet.blocking.Client()
response = client.patch("https://httpbin.io/anything", json={"key": "value"})
print(response.text())
```
"""
...
def put(
self,
url: str,
**kwargs: Unpack[Request],
) -> "Response":
r"""
Sends a request with the given URL.
# Examples
```python
import rnet
import rnet.blocking
from rnet import Method
client = rnet.blocking.Client()
response = client.put("https://httpbin.io/anything", json={"key": "value"})
print(response.text())
```
"""
...
def post(
self,
url: str,
**kwargs: Unpack[Request],
) -> "Response":
r"""
Sends a request with the given URL.
# Examples
```python
import rnet
import rnet.blocking
from rnet import Method
client = rnet.blocking.Client()
response = client.post("https://httpbin.io/anything", json={"key": "value"})
print(response.text())
```
"""
...
def get(
self,
url: str,
**kwargs: Unpack[Request],
) -> "Response":
r"""
Sends a request with the given URL.
# Examples
```python
import rnet
import rnet.blocking
from rnet import Method
client = rnet.blocking.Client()
response = client.get("https://httpbin.io/anything")
print(response.text())
```
"""
...
def delete(
url: str,
**kwargs: Unpack[Request],
) -> "Response":
r"""
Shortcut method to quickly make a request.
# Examples
```python
import rnet
import rnet.blocking
response = rnet.blocking.delete("https://httpbin.io/anything")
print(response.text())
```
"""
...
def get(
url: str,
**kwargs: Unpack[Request],
) -> "Response":
r"""
Shortcut method to quickly make a request.
# Examples
```python
import rnet
import rnet.blocking
response = rnet.blocking.get("https://httpbin.io/anything")
print(response.text())
```
"""
...
def head(
url: str,
**kwargs: Unpack[Request],
) -> "Response":
r"""
Shortcut method to quickly make a request.
# Examples
```python
import rnet
import rnet.blocking
response = rnet.blocking.head("https://httpbin.io/anything")
print(response.status)
```
"""
...
def options(
url: str,
**kwargs: Unpack[Request],
) -> "Response":
r"""
Shortcut method to quickly make a request.
# Examples
```python
import rnet
import rnet.blocking
response = rnet.blocking.options("https://httpbin.io/anything")
print(response.status)
```
"""
...
def patch(
url: str,
**kwargs: Unpack[Request],
) -> "Response":
r"""
Shortcut method to quickly make a request.
# Examples
```python
import rnet
import rnet.blocking
response = rnet.blocking.patch("https://httpbin.io/anything", json={"key": "value"})
print(response.text())
```
"""
...
def post(
url: str,
**kwargs: Unpack[Request],
) -> "Response":
r"""
Shortcut method to quickly make a request.
# Examples
```python
import rnet
import rnet.blocking
response = rnet.blocking.post("https://httpbin.io/anything", json={"key": "value"})
print(response.text())
```
"""
...
def put(
url: str,
**kwargs: Unpack[Request],
) -> "Response":
r"""
Shortcut method to quickly make a request.
# Examples
```python
import rnet
import rnet.blocking
response = rnet.blocking.put("https://httpbin.io/anything", json={"key": "value"})
print(response.text())
```
"""
...
def request(
method: Method,
url: str,
**kwargs: Unpack[Request],
) -> "Response":
r"""
Make a request with the given parameters.
# Arguments
* `method` - The method to use for the request.
* `url` - The URL to send the request to.
* `**kwargs` - Additional request parameters.
# Examples
```python
import rnet
import rnet.blocking
from rnet import Method
response = rnet.blocking.request(Method.GET, "https://www.rust-lang.org")
print(response.text())
```
"""
...
def trace(
url: str,
**kwargs: Unpack[Request],
) -> "Response":
r"""
Shortcut method to quickly make a request.
# Examples
```python
import rnet
import rnet.blocking
response = rnet.blocking.trace("https://httpbin.io/anything")
print(response.status)
```
"""
...
def websocket(
url: str,
**kwargs: Unpack[WebSocketRequest],
) -> "WebSocket":
r"""
Make a WebSocket connection with the given parameters.
# Examples
```python
import rnet
import rnet.blocking
ws = rnet.blocking.websocket("wss://echo.websocket.org")
ws.send(rnet.Message.from_text("Hello, World!"))
message = ws.recv()
print("Received:", message.data)
ws.close()
```
"""
...