commit 5df7b8ded155ec52e4322e585014214b4886d861
parent 7b0d74bf55064550dffef53bc063305635b0a8f3
Author: archiveanon <>
Date: Sat, 3 Aug 2024 16:50:43 +0000
Use httpx in place of requests
Diffstat:
1 file changed, 19 insertions(+), 23 deletions(-)
diff --git a/src/gofile/api.py b/src/gofile/api.py
@@ -9,14 +9,13 @@ import random
import textwrap
from typing import Generic, Iterator, Optional, TypeVar
-import msgspec
-
# normally I'd prefer using standard modules but streaming POST data is really important
-import requests
-from requests_toolbelt.multipart.encoder import MultipartEncoder, MultipartEncoderMonitor
+import httpx
+import msgspec
try:
import tqdm
+ import tqdm.utils
except ImportError:
tqdm = None
@@ -88,7 +87,7 @@ class GofileServerResponse(msgspec.Struct, Generic[T]):
def _gofile_api_get(*args, type: T, **kwargs) -> T:
# performs a GET request and extracts the 'data' property from the response as a given type
# if the status is not 'ok', an exception is raised
- r = requests.get(*args, **kwargs)
+ r = httpx.get(*args, **kwargs)
result = msgspec.json.decode(r.text, type=GofileServerResponse[type])
if result.status != GofileStatus.OK:
@@ -99,7 +98,7 @@ def _gofile_api_get(*args, type: T, **kwargs) -> T:
def _gofile_api_post(*args, type: T, **kwargs) -> T:
# performs a POST request and extracts the 'data' property from the response as a given type
# if the status is not 'ok', an exception is raised
- r = requests.post(*args, **kwargs)
+ r = httpx.post(*args, **kwargs)
result = msgspec.json.decode(r.text, type=GofileServerResponse[type])
if result.status != GofileStatus.OK:
@@ -129,15 +128,6 @@ def upload_single(
upload_server_result = get_upload_server()
server = upload_server_result.server
- post_data = {
- "file": (file.name, file.open("rb"), "application/octet-stream"),
- }
-
- if token:
- post_data["token"] = token
- if folder_id:
- post_data["folderId"] = folder_id
-
# automatically shorten long file names in small terminals (e.g. split panes)
termsize = os.get_terminal_size()
short_file_name = textwrap.shorten(
@@ -155,20 +145,26 @@ def upload_single(
unit_divisor=1024,
dynamic_ncols=True,
) as progress:
- multipart_data = MultipartEncoder(fields=post_data)
- monitor = MultipartEncoderMonitor(
- multipart_data,
- lambda monitor: progress.update(monitor.bytes_read - progress.n),
- )
+ post_data = {
+ "file": (
+ file.name,
+ tqdm.utils.CallbackIOWrapper(progress.update, file.open("rb"), "read"),
+ "application/octet-stream",
+ ),
+ }
+
+ if token:
+ post_data["token"] = token
+ if folder_id:
+ post_data["folderId"] = folder_id
upload_result = _gofile_api_post(
f"https://{server}.gofile.io/contents/uploadfile",
- data=monitor,
- headers={"Content-Type": multipart_data.content_type},
+ files=post_data,
type=GofileUploadResult,
)
break
- except (requests.exceptions.ConnectionError, ConnectionResetError) as e:
+ except httpx.HTTPError as e:
print(e)
pass