From 76bc38277716348f4ecc5d8adbc199d40d9c1064 Mon Sep 17 00:00:00 2001 From: Kyomotoi <1172294279@qq.com> Date: Tue, 5 May 2020 15:12:35 +0800 Subject: =?UTF-8?q?=E5=81=9A=E4=BA=86=E4=BA=9B=E5=B0=8F=E6=94=B9=E5=8A=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 以话慢慢补全功能 目前能用的 只有other --- AyaBot/aio/requests.py | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 AyaBot/aio/requests.py (limited to 'AyaBot/aio/requests.py') diff --git a/AyaBot/aio/requests.py b/AyaBot/aio/requests.py new file mode 100644 index 0000000..4f7b9d3 --- /dev/null +++ b/AyaBot/aio/requests.py @@ -0,0 +1,71 @@ +from typing import Optional, Any + +import requests +from requests import * + +from . import run_sync_func + + +class AsyncResponse: + def __init__(self, response: requests.Response): + self.raw_response = response + + @property + def ok(self) -> bool: + return self.raw_response.ok + + def __repr__(self): + return '' % self.raw_response.status_code + + def __bool__(self): + return self.ok + + @property + async def content(self) -> Optional[bytes]: + return await run_sync_func(lambda: self.raw_response.content) + + @property + async def text(self) -> str: + return await run_sync_func(lambda: self.raw_response.text) + + async def json(self, **kwargs) -> Any: + return await run_sync_func(self.raw_response.json, **kwargs) + + +async def request(method, url, **kwargs) -> AsyncResponse: + return AsyncResponse(await run_sync_func(requests.request, + method=method, url=url, **kwargs)) + + +async def get(url, params=None, **kwargs) -> AsyncResponse: + return AsyncResponse( + await run_sync_func(requests.get, url=url, params=params, **kwargs)) + + +async def options(url, **kwargs) -> AsyncResponse: + return AsyncResponse( + await run_sync_func(requests.options, url=url, **kwargs)) + + +async def head(url, **kwargs) -> AsyncResponse: + return AsyncResponse(await run_sync_func(requests.head, url=url, **kwargs)) + + +async def post(url, data=None, json=None, **kwargs) -> AsyncResponse: + return AsyncResponse(await run_sync_func(requests.post, url=url, + data=data, json=json, **kwargs)) + + +async def put(url, data=None, **kwargs) -> AsyncResponse: + return AsyncResponse( + await run_sync_func(requests.put, url=url, data=data, **kwargs)) + + +async def patch(url, data=None, **kwargs) -> AsyncResponse: + return AsyncResponse( + await run_sync_func(requests.patch, url=url, data=data, **kwargs)) + + +async def delete(url, **kwargs) -> AsyncResponse: + return AsyncResponse( + await run_sync_func(requests.delete, url=url, **kwargs)) -- cgit v1.2.3