http_client_request 0.1.1__tar.gz → 0.1.2__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: http_client_request
3
- Version: 0.1.1
3
+ Version: 0.1.2
4
4
  Summary: http.client request extension.
5
5
  License: MIT
6
6
  Keywords: http.client,request
@@ -21,12 +21,11 @@ Classifier: Topic :: Software Development
21
21
  Classifier: Topic :: Software Development :: Libraries
22
22
  Classifier: Topic :: Software Development :: Libraries :: Python Modules
23
23
  Requires-Dist: http_response (>=0.0.9)
24
- Requires-Dist: python-argtools (>=0.0.2)
25
24
  Requires-Dist: python-cookietools (>=0.1.4)
26
- Requires-Dist: python-dicttools (>=0.0.4)
25
+ Requires-Dist: python-dicttools (>=0.0.5)
27
26
  Requires-Dist: python-filewrap (>=0.2.9)
28
27
  Requires-Dist: python-http_request (>=0.1.6)
29
- Requires-Dist: python-undefined (>=0.0.3)
28
+ Requires-Dist: python-undefined (>=0.0.4)
30
29
  Requires-Dist: socket_keepalive (>=0.0.1)
31
30
  Requires-Dist: urllib3
32
31
  Requires-Dist: yarl
@@ -4,7 +4,7 @@
4
4
  from __future__ import annotations
5
5
 
6
6
  __author__ = "ChenyangGao <https://chenyanggao.github.io>"
7
- __version__ = (0, 1, 1)
7
+ __version__ = (0, 1, 2)
8
8
  __all__ = [
9
9
  "CONNECTION_POOL", "HTTPConnection", "HTTPSConnection", "HTTPResponse",
10
10
  "ConnectionPool", "request",
@@ -29,12 +29,11 @@ from urllib.error import HTTPError
29
29
  from urllib.parse import urljoin, urlsplit, urlunsplit, ParseResult, SplitResult
30
30
  from warnings import warn
31
31
 
32
- from argtools import argcount
33
32
  from cookietools import cookies_to_str, extract_cookies
34
33
  from dicttools import get_all_items
35
34
  from filewrap import SupportsRead
36
35
  from http_request import normalize_request_args, SupportsGeturl
37
- from http_response import decompress_response, parse_response
36
+ from http_response import decompress_response, parse_response, get_length
38
37
  from socket_keepalive import socket_keepalive
39
38
  from urllib3 import HTTPResponse as Urllib3HTTPResponse, HTTPHeaderDict
40
39
  from undefined import undefined, Undefined
@@ -364,7 +363,7 @@ def request[T](
364
363
  proxies: None | str | dict[str, str] = None,
365
364
  pool: None | Undefined | ConnectionPool = undefined,
366
365
  *,
367
- parse: Callable[[HTTPResponse, bytes], T] | Callable[[HTTPResponse], T],
366
+ parse: Callable[[HTTPResponse, bytes], T],
368
367
  **request_kwargs,
369
368
  ) -> T:
370
369
  ...
@@ -382,7 +381,7 @@ def request[T](
382
381
  proxies: None | str | dict[str, str] = None,
383
382
  pool: None | Undefined | ConnectionPool = undefined,
384
383
  *,
385
- parse: None | EllipsisType| bool | Callable[[HTTPResponse, bytes], T] | Callable[[HTTPResponse], T] = None,
384
+ parse: None | EllipsisType| bool | Callable[[HTTPResponse, bytes], T] = None,
386
385
  **request_kwargs,
387
386
  ) -> HTTPResponse | bytes | str | dict | list | int | float | bool | None | T:
388
387
  if pool is undefined:
@@ -485,8 +484,10 @@ def request[T](
485
484
  if status_code == 303:
486
485
  method = "GET"
487
486
  body = None
487
+ response.read()
488
488
  continue
489
489
  elif status_code >= 400 and raise_for_status:
490
+ setattr(response, "content", response.read())
490
491
  raise HTTPError(
491
492
  url,
492
493
  status_code,
@@ -495,21 +496,23 @@ def request[T](
495
496
  response,
496
497
  )
497
498
  if parse is None:
499
+ if method == "HEAD":
500
+ response.read()
498
501
  return response
499
502
  elif parse is ...:
500
- response.close()
503
+ try:
504
+ if (method == "HEAD" or
505
+ (length := get_length(response)) is not None and length <= 10485760
506
+ ):
507
+ response.read()
508
+ finally:
509
+ response.close()
501
510
  return response
511
+ content = decompress_response(response.read(), response)
502
512
  if isinstance(parse, bool):
503
- content = decompress_response(response.read(), response)
504
- if parse:
505
- return parse_response(response, content)
506
- return content
507
- ac = argcount(parse)
508
- if ac == 1:
509
- return cast(Callable[[HTTPResponse], T], parse)(response)
510
- else:
511
- content = decompress_response(response.read(), response)
512
- return cast(Callable[[HTTPResponse, bytes], T], parse)(
513
- response, content)
513
+ if not parse:
514
+ return content
515
+ parse = cast(Callable, parse_response)
516
+ return parse(response, content)
514
517
 
515
518
  # TODO: 实现异步请求,非阻塞模式(sock.setblocking(False)),对于响应体的数据加载,使用 select 模块进行通知
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "http_client_request"
3
- version = "0.1.1"
3
+ version = "0.1.2"
4
4
  description = "http.client request extension."
5
5
  authors = ["ChenyangGao <wosiwujm@gmail.com>"]
6
6
  license = "MIT"
@@ -28,12 +28,11 @@ include = [
28
28
  [tool.poetry.dependencies]
29
29
  python = "^3.12"
30
30
  http_response = ">=0.0.9"
31
- python-argtools = ">=0.0.2"
32
31
  python-cookietools = ">=0.1.4"
33
- python-dicttools = ">=0.0.4"
32
+ python-dicttools = ">=0.0.5"
34
33
  python-filewrap = ">=0.2.9"
35
34
  python-http_request = ">=0.1.6"
36
- python-undefined = ">=0.0.3"
35
+ python-undefined = ">=0.0.4"
37
36
  socket_keepalive = ">=0.0.1"
38
37
  urllib3 = "*"
39
38
  yarl = "*"