summaryrefslogtreecommitdiff
path: root/ATRI/utils
diff options
context:
space:
mode:
Diffstat (limited to 'ATRI/utils')
-rw-r--r--ATRI/utils/file.py27
-rw-r--r--ATRI/utils/request.py6
2 files changed, 31 insertions, 2 deletions
diff --git a/ATRI/utils/file.py b/ATRI/utils/file.py
new file mode 100644
index 0000000..4699f5d
--- /dev/null
+++ b/ATRI/utils/file.py
@@ -0,0 +1,27 @@
+import aiofiles
+from pathlib import Path
+
+from ATRI.exceptions import WriteError
+
+
+async def write_file(path: Path, text, encoding='utf-8') -> None:
+ try:
+ async with aiofiles.open(path, 'w', encoding=encoding) as target:
+ await target.write(text)
+ except WriteError:
+ raise WriteError("Writing file failed!")
+
+
+async def open_file(path: Path, method, encoding='utf-8'):
+ try:
+ async with aiofiles.open(path, 'r', encoding=encoding) as target:
+ if method == "read":
+ return target.read()
+ elif method == "readlines":
+ return await target.readlines()
+ elif method == "readline":
+ return await target.readline()
+ else:
+ return target.readable()
+ except EOFError:
+ raise EOFError("File not fond!")
diff --git a/ATRI/utils/request.py b/ATRI/utils/request.py
index bf05b56..31285e8 100644
--- a/ATRI/utils/request.py
+++ b/ATRI/utils/request.py
@@ -18,9 +18,11 @@ async def get_bytes(url: str, headers: Optional[dict] = None) -> bytes:
return result
-async def post_bytes(url: str, params: Optional[dict] = None) -> bytes:
+async def post_bytes(url: str,
+ params: Optional[dict] = None,
+ json: Optional[dict] = None) -> bytes:
"""异步以 Post 方式请求 url"""
async with ClientSession() as session:
- async with session.post(url, params=params) as r:
+ async with session.post(url, params=params, json=json) as r:
result = await r.read()
return result