miui-auto-tasks/utils/request.py

78 lines
2.1 KiB
Python
Raw Normal View History

"""
Date: 2023-11-11 23:34:08
LastEditors: Night-stars-1 nujj1042633805@gmail.com
LastEditTime: 2023-11-13 18:17:50
"""
import httpx
from typing import Any, Dict, Optional
from onepush import notify
from .logger import log
from .config import ConfigManager
_conf = ConfigManager.data_obj
async def get(url: str,
*,
headers: Optional[Dict[str, str]] = None,
params: Optional[Dict[str, Any]] = None,
timeout: Optional[int] = 20,
**kwargs) -> httpx.Response:
"""
说明
httpx的get请求封装
参数
:param url: url
:param headers: 请求头
:param params: params
:param data: data
:param json: json
:param timeout: 超时时间
"""
async with httpx.AsyncClient() as client:
return await client.get(url,
headers=headers,
params=params,
timeout=timeout,
**kwargs)
async def post(url: str,
*,
headers: Optional[Dict[str, str]] = None,
params: Optional[Dict[str, Any]] = None,
timeout: Optional[int] = 20,
**kwargs) -> httpx.Response:
"""
说明
httpx的post请求封装
参数
:param url: url
:param headers: 请求头
:param params: params
:param data: data
:param json: json
:param timeout: 超时时间
"""
async with httpx.AsyncClient() as client:
return await client.post(url,
headers=headers,
params=params,
timeout=timeout,
**kwargs)
def notify_me(content=""):
"""
默认推送日志
"""
notifier = _conf.ONEPUSH.notifier
params = _conf.ONEPUSH.params
if not notifier or not params:
log.error('未配置推送或未正确配置推送')
return
return notify(notifier, content=content, **params)