diff --git a/lark_oapi/core/http/transport.py b/lark_oapi/core/http/transport.py index e4a869bf4..e427f63fc 100644 --- a/lark_oapi/core/http/transport.py +++ b/lark_oapi/core/http/transport.py @@ -1,19 +1,31 @@ +""" +Transport module for Feishu API requests. +""" + import json +from typing import Dict, Optional import httpx import requests -from requests_toolbelt import MultipartEncoder - -from lark_oapi.core.const import * +from lark_oapi.core.const import AUTHORIZATION, PROJECT, USER_AGENT, UTF_8, VERSION from lark_oapi.core.json import JSON from lark_oapi.core.log import logger -from lark_oapi.core.model import * +from lark_oapi.core.model import ( + AccessTokenType, + BaseRequest, + Config, + RawResponse, + RequestOption, +) +from requests_toolbelt import MultipartEncoder class Transport(object): @staticmethod - def execute(conf: Config, req: BaseRequest, option: Optional[RequestOption] = None) -> RawResponse: + def execute( + conf: Config, req: BaseRequest, option: Optional[RequestOption] = None + ) -> RawResponse: if option is None: option = RequestOption() @@ -34,12 +46,15 @@ def execute(conf: Config, req: BaseRequest, option: Optional[RequestOption] = No params=req.queries, data=data, timeout=conf.timeout, + proxies=conf.proxies, ) - logger.debug(f"{str(req.http_method.name)} {url} {response.status_code}, " - f"headers: {JSON.marshal(headers)}, " - f"params: {JSON.marshal(req.queries)}, " - f"body: {str(data, UTF_8) if isinstance(data, bytes) else data}") + logger.debug( + f"{str(req.http_method.name)} {url} {response.status_code}, " + f"headers: {JSON.marshal(headers)}, " + f"params: {JSON.marshal(req.queries)}, " + f"body: {str(data, UTF_8) if isinstance(data, bytes) else data}" + ) resp = RawResponse() resp.status_code = response.status_code @@ -49,7 +64,9 @@ def execute(conf: Config, req: BaseRequest, option: Optional[RequestOption] = No return resp @staticmethod - async def aexecute(conf: Config, req: BaseRequest, option: Optional[RequestOption] = None) -> RawResponse: + async def aexecute( + conf: Config, req: BaseRequest, option: Optional[RequestOption] = None + ) -> RawResponse: if option is None: option = RequestOption() @@ -79,6 +96,7 @@ async def aexecute(conf: Config, req: BaseRequest, option: Optional[RequestOptio data=data, files=files, timeout=conf.timeout, + # TODO: Add proxy support if needed ) logger.debug( diff --git a/lark_oapi/core/model/config.py b/lark_oapi/core/model/config.py index c98ceaca2..0673a2993 100644 --- a/lark_oapi/core/model/config.py +++ b/lark_oapi/core/model/config.py @@ -1,4 +1,4 @@ -from typing import Optional +from typing import Dict, Optional from lark_oapi.core import AppType, LogLevel from lark_oapi.core.cache import ICache @@ -11,7 +11,12 @@ def __init__(self) -> None: self.app_secret: Optional[str] = None self.domain: str = FEISHU_DOMAIN # 域名, 默认为 https://open.feishu.cn self.timeout: Optional[float] = None # 客户端超时时间, 单位秒, 默认永不超时 - self.app_type: AppType = AppType.SELF # 应用类型, 默认为自建应用; 若设为 ISV 需在 request_option 中配置 tenant_key - self.enable_set_token: bool = False # 是否允许手动设置 token, 默认不开启; 开启后需在 request_option 中配置 token + self.app_type: AppType = ( + AppType.SELF + ) # 应用类型, 默认为自建应用; 若设为 ISV 需在 request_option 中配置 tenant_key + self.enable_set_token: bool = ( + False # 是否允许手动设置 token, 默认不开启; 开启后需在 request_option 中配置 token + ) self.cache: Optional[ICache] = None # 自定义缓存, 默认使用预置的本地缓存 self.log_level: LogLevel = LogLevel.WARNING # 日志级别, 默认为 WARNING + self.proxies: Optional[Dict[str, str]] = None # 代理设置, 默认为 None