python-httpfile 0.0.4.4__py3-none-any.whl → 0.0.5__py3-none-any.whl

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.
httpfile/__init__.py CHANGED
@@ -4,7 +4,7 @@
4
4
  from __future__ import annotations
5
5
 
6
6
  __author__ = "ChenyangGao <https://chenyanggao.github.io>"
7
- __version__ = (0, 0, 4)
7
+ __version__ = (0, 0, 5)
8
8
  __all__ = ["HTTPFileReader", "AsyncHTTPFileReader"]
9
9
 
10
10
  import errno
@@ -1119,8 +1119,55 @@ if find_spec("aiohttp"):
1119
1119
  if find_spec("httpx"):
1120
1120
  from filewrap import bytes_iter_to_reader, bytes_iter_to_async_reader
1121
1121
 
1122
- _httpx_urlopen = None
1123
- _httpx_urlopen_async = None
1122
+ def httpx_request(
1123
+ url,
1124
+ method: str = "GET",
1125
+ auth = None,
1126
+ follow_redirects: bool = True,
1127
+ stream: bool = True,
1128
+ session = None,
1129
+ **request_kwargs,
1130
+ ):
1131
+ if session is None:
1132
+ from httpx import Client
1133
+ if "__del__" not in Client.__dict__:
1134
+ setattr(Client, "__del__", lambda self: self.close())
1135
+ session = Client()
1136
+ return session.send(
1137
+ request=session.build_request(
1138
+ method=method,
1139
+ url=url,
1140
+ **request_kwargs,
1141
+ ),
1142
+ auth=auth,
1143
+ follow_redirects=follow_redirects,
1144
+ stream=stream,
1145
+ )
1146
+
1147
+ async def httpx_request_async(
1148
+ url,
1149
+ method: str = "GET",
1150
+ auth = None,
1151
+ follow_redirects: bool = True,
1152
+ stream: bool = True,
1153
+ session = None,
1154
+ **request_kwargs,
1155
+ ):
1156
+ if session is None:
1157
+ from httpx import AsyncClient
1158
+ if "__del__" not in AsyncClient.__dict__:
1159
+ setattr(AsyncClient, "__del__", lambda self: run_async(self.aclose()))
1160
+ session = AsyncClient()
1161
+ return await session.send(
1162
+ request=session.build_request(
1163
+ method=method,
1164
+ url=url,
1165
+ **request_kwargs,
1166
+ ),
1167
+ auth=auth,
1168
+ follow_redirects=follow_redirects,
1169
+ stream=stream,
1170
+ )
1124
1171
 
1125
1172
  class HttpxFileReader(HTTPFileReader):
1126
1173
 
@@ -1133,24 +1180,14 @@ if find_spec("httpx"):
1133
1180
  seek_threshold: int = 1 << 20,
1134
1181
  urlopen = None,
1135
1182
  ):
1136
- global _httpx_urlopen
1137
- from httpx import stream, Client
1183
+ from httpx import Client
1138
1184
 
1139
- if isinstance(urlopen, Client):
1140
- urlopen = partial(urlopen.stream, "GET")
1141
- elif urlopen is None:
1142
- if _httpx_urlopen is None:
1143
- if "__del__" not in Client.__dict__:
1144
- setattr(Client, "__del__", lambda self: self.close())
1145
- urlopen = _httpx_urlopen = partial(stream, "GET")
1146
- else:
1147
- urlopen = _httpx_urlopen
1185
+ if urlopen is None or isinstance(urlopen, Client):
1186
+ urlopen = partial(httpx_request, session=urlopen)
1148
1187
 
1149
1188
  def urlopen_wrapper(url: str, headers: None | Mapping = headers):
1150
- context = urlopen(url, headers=headers)
1151
- resp = context.__enter__()
1189
+ resp = urlopen(url, headers=headers)
1152
1190
  resp.raise_for_status()
1153
- resp.context = context
1154
1191
  file = bytes_iter_to_reader(resp.iter_raw())
1155
1192
  self.__dict__["file"] = file
1156
1193
  return resp
@@ -1178,64 +1215,14 @@ if find_spec("httpx"):
1178
1215
  seek_threshold: int = 1 << 20,
1179
1216
  urlopen = None,
1180
1217
  ):
1181
- global _httpx_urlopen_async
1182
1218
  from httpx import AsyncClient
1183
1219
 
1184
- if isinstance(urlopen, AsyncClient):
1185
- urlopen = partial(urlopen.stream, "GET")
1186
- elif urlopen is None:
1187
- if _httpx_urlopen_async is None:
1188
- if "__del__" not in AsyncClient.__dict__:
1189
- setattr(AsyncClient, "__del__", lambda self: run_async(self.aclose()))
1190
- def async_stream(
1191
- method,
1192
- url,
1193
- params = None,
1194
- content = None,
1195
- data = None,
1196
- files = None,
1197
- json = None,
1198
- headers = None,
1199
- cookies = None,
1200
- auth = None,
1201
- proxy = None,
1202
- proxies = None,
1203
- timeout = 5.0,
1204
- follow_redirects = False,
1205
- verify = True,
1206
- cert = None,
1207
- trust_env = True,
1208
- ):
1209
- client = AsyncClient(
1210
- cookies=cookies,
1211
- proxy=proxy,
1212
- proxies=proxies,
1213
- cert=cert,
1214
- verify=verify,
1215
- timeout=timeout,
1216
- trust_env=trust_env,
1217
- )
1218
- return client.stream(
1219
- method=method,
1220
- url=url,
1221
- content=content,
1222
- data=data,
1223
- files=files,
1224
- json=json,
1225
- params=params,
1226
- headers=headers,
1227
- auth=auth,
1228
- follow_redirects=follow_redirects,
1229
- )
1230
- urlopen = _httpx_urlopen_async = partial(async_stream, "GET")
1231
- else:
1232
- urlopen = _httpx_urlopen_async
1220
+ if urlopen is None or isinstance(urlopen, AsyncClient):
1221
+ urlopen = partial(httpx_request_async, session=urlopen)
1233
1222
 
1234
1223
  async def urlopen_wrapper(url: str, headers: None | Mapping = headers):
1235
- context = urlopen(url, headers=headers)
1236
- resp = await context.__aenter__()
1224
+ resp = await urlopen(url, headers=headers)
1237
1225
  resp.raise_for_status()
1238
- resp.context = context
1239
1226
  file = bytes_iter_to_async_reader(resp.aiter_raw())
1240
1227
  self.__dict__["file"] = file
1241
1228
  return resp
@@ -1257,3 +1244,4 @@ if find_spec("httpx"):
1257
1244
 
1258
1245
  # TODO: 增加 blacksheep 的 HTTPFileReader
1259
1246
  # TODO: 设计实现一个 HTTPFileWriter,用于实现上传,关闭后视为上传完成
1247
+ # TODO: httpx 重新实现 stream 和 async_stream 方法,确保不需要用上下文管理器,如此才能真的简化
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: python-httpfile
3
- Version: 0.0.4.4
3
+ Version: 0.0.5
4
4
  Summary: Python httpfile.
5
5
  Home-page: https://github.com/ChenyangGao/web-mount-packs/tree/main/python-module/python-httpfile
6
6
  License: MIT
@@ -0,0 +1,7 @@
1
+ LICENSE,sha256=o5242_N2TgDsWwFhPn7yr8YJNF7XsJM5NxUMtcT97bc,1100
2
+ httpfile/__init__.py,sha256=2T_eq8cMUIciKfxX2ZQZxm1Uv-XHCHfMqK970i2wjm0,40057
3
+ httpfile/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
4
+ python_httpfile-0.0.5.dist-info/LICENSE,sha256=o5242_N2TgDsWwFhPn7yr8YJNF7XsJM5NxUMtcT97bc,1100
5
+ python_httpfile-0.0.5.dist-info/METADATA,sha256=R4AglpLv8hg9G4Aa5RhNrY_NTJqS5Yk7BB2NeEeA_gk,1520
6
+ python_httpfile-0.0.5.dist-info/WHEEL,sha256=FMvqSimYX_P7y0a7UY-_Mc83r5zkBZsCYPm7Lr0Bsq4,88
7
+ python_httpfile-0.0.5.dist-info/RECORD,,
@@ -1,7 +0,0 @@
1
- LICENSE,sha256=o5242_N2TgDsWwFhPn7yr8YJNF7XsJM5NxUMtcT97bc,1100
2
- httpfile/__init__.py,sha256=_DC16mvgrbfFNy57qWMK2SpXwuhPIw4LFawsTs1mr-c,41043
3
- httpfile/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
4
- python_httpfile-0.0.4.4.dist-info/LICENSE,sha256=o5242_N2TgDsWwFhPn7yr8YJNF7XsJM5NxUMtcT97bc,1100
5
- python_httpfile-0.0.4.4.dist-info/METADATA,sha256=Jil2_JcYHXjCME0PbiXzf_8D1rSAu7IcFjayXrjW4FA,1522
6
- python_httpfile-0.0.4.4.dist-info/WHEEL,sha256=FMvqSimYX_P7y0a7UY-_Mc83r5zkBZsCYPm7Lr0Bsq4,88
7
- python_httpfile-0.0.4.4.dist-info/RECORD,,