Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 28 additions & 10 deletions lark_oapi/core/http/transport.py
Original file line number Diff line number Diff line change
@@ -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()

Expand All @@ -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
Expand All @@ -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()

Expand Down Expand Up @@ -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(
Expand Down
11 changes: 8 additions & 3 deletions lark_oapi/core/model/config.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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