http_client_request 0.0.4__tar.gz → 0.0.5__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.1
2
2
  Name: http_client_request
3
- Version: 0.0.4
3
+ Version: 0.0.5
4
4
  Summary: http.client request extension.
5
5
  Home-page: https://github.com/ChenyangGao/python-modules/tree/main/http_client_request
6
6
  License: MIT
@@ -2,7 +2,7 @@
2
2
  # coding: utf-8
3
3
 
4
4
  __author__ = "ChenyangGao <https://chenyanggao.github.io>"
5
- __version__ = (0, 0, 4)
5
+ __version__ = (0, 0, 5)
6
6
  __all__ = ["ConnectionPool", "request"]
7
7
 
8
8
  from collections import defaultdict, deque, UserString
@@ -17,11 +17,12 @@ from types import EllipsisType
17
17
  from typing import cast, overload, Any, Final, Literal
18
18
  from urllib.error import HTTPError
19
19
  from urllib.parse import urljoin, urlsplit, urlunsplit, ParseResult, SplitResult
20
+ from warnings import warn
20
21
 
21
22
  from argtools import argcount
22
23
  from cookietools import cookies_to_str, extract_cookies
23
24
  from dicttools import get_all_items
24
- from filewrap import bio_chunk_iter, SupportsRead
25
+ from filewrap import SupportsRead
25
26
  from http_request import normalize_request_args, SupportsGeturl
26
27
  from http_response import decompress_response, parse_response
27
28
  from undefined import undefined, Undefined
@@ -259,22 +260,30 @@ def request[T](
259
260
  https_proxy = get_host_pair(proxies.get("https"))
260
261
  else:
261
262
  http_proxy = https_proxy = None
263
+ body: Any
262
264
  if isinstance(data, PathLike):
263
- data = bio_chunk_iter(open(data, "rb"))
264
- elif isinstance(data, SupportsRead):
265
- data = bio_chunk_iter(data)
266
- request_args = normalize_request_args(
267
- method=method,
268
- url=url,
269
- params=params,
270
- data=data,
271
- json=json,
272
- files=files,
273
- headers=headers,
274
- )
265
+ data = open(data, "rb")
266
+ if isinstance(data, SupportsRead):
267
+ request_args = normalize_request_args(
268
+ method=method,
269
+ url=url,
270
+ params=params,
271
+ headers=headers,
272
+ )
273
+ body = data
274
+ else:
275
+ request_args = normalize_request_args(
276
+ method=method,
277
+ url=url,
278
+ params=params,
279
+ data=data,
280
+ files=files,
281
+ json=json,
282
+ headers=headers,
283
+ )
284
+ body = request_args["data"]
275
285
  method = request_args["method"]
276
286
  url = request_args["url"]
277
- body = cast(None | Buffer | Iterable[Buffer], request_args["data"])
278
287
  headers_ = request_args["headers"]
279
288
  headers_.setdefault("connection", "keep-alive")
280
289
  need_set_cookie = "cookie" not in headers_
@@ -316,11 +325,21 @@ def request[T](
316
325
  extract_cookies(cookies, url, response) # type: ignore
317
326
  status_code = response.status
318
327
  if 300 <= status_code < 400 and follow_redirects:
319
- url = request_args["url"] = urljoin(url, response.headers["location"])
320
- if status_code == 303:
321
- method = "GET"
322
- body = None
323
- continue
328
+ if location := response.headers.get("location"):
329
+ url = request_args["url"] = urljoin(url, location)
330
+ if body and status_code in (307, 308):
331
+ if isinstance(body, SupportsRead):
332
+ try:
333
+ body.seek(0) # type: ignore
334
+ except Exception:
335
+ warn(f"unseekable-stream: {body!r}")
336
+ elif not isinstance(body, Buffer):
337
+ warn(f"failed to resend request body: {body!r}, when {status_code} redirects")
338
+ else:
339
+ if status_code == 303:
340
+ method = "GET"
341
+ body = None
342
+ continue
324
343
  elif status_code >= 400 and raise_for_status:
325
344
  raise HTTPError(
326
345
  url,
@@ -347,3 +366,4 @@ def request[T](
347
366
  return cast(Callable[[HTTPResponse, bytes], T], parse)(
348
367
  response, content)
349
368
 
369
+ # TODO: 实现异步请求,非阻塞模式(sock.setblocking(False)),对于响应体的数据加载,使用 select 模块进行通知
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "http_client_request"
3
- version = "0.0.4"
3
+ version = "0.0.5"
4
4
  description = "http.client request extension."
5
5
  authors = ["ChenyangGao <wosiwujm@gmail.com>"]
6
6
  license = "MIT"