Skip to content

Commit

Permalink
feat(network): 添加 timeout 为可配置参数
Browse files Browse the repository at this point in the history
  • Loading branch information
NekoAria committed Jul 11, 2022
1 parent 44477e4 commit a8a78f8
Showing 1 changed file with 24 additions and 4 deletions.
28 changes: 24 additions & 4 deletions PicImageSearch/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ def __init__(
proxies: Optional[str] = None,
headers: Optional[Dict[str, str]] = None,
cookies: Optional[str] = None,
timeout: float = 30,
bypass: bool = False,
):
self.internal: bool = internal
Expand Down Expand Up @@ -54,7 +55,7 @@ def __init__(
connector=self.conn,
headers=headers,
cookies=self.cookies,
timeout=ClientTimeout(total=20.0),
timeout=ClientTimeout(total=timeout),
)

if proxies and _flag:
Expand Down Expand Up @@ -88,13 +89,15 @@ def __init__(
proxies: Optional[str] = None,
headers: Optional[Dict[str, str]] = None,
cookies: Optional[str] = None,
timeout: float = 30,
bypass: bool = False,
):
self.client: Union[Network, ClientSession] = client or Network(
internal=True,
proxies=proxies,
headers=headers,
cookies=cookies,
timeout=timeout,
bypass=bypass,
)

Expand All @@ -118,19 +121,26 @@ def __init__(
proxies: Optional[str] = None,
headers: Optional[Dict[str, str]] = None,
cookies: Optional[str] = None,
timeout: float = 30,
bypass: bool = False,
):
self.client: Optional[ClientSession] = client
self.proxies: Optional[str] = proxies
self.headers: Optional[Dict[str, str]] = headers
self.cookies: Optional[str] = cookies
self.timeout: float = timeout
self.bypass: bool = bypass

async def get(
self, url: str, params: Optional[Dict[str, str]] = None, **kwargs: Any
) -> Tuple[str, str, int]:
async with ClientManager(
self.client, self.proxies, self.headers, self.cookies, self.bypass
self.client,
self.proxies,
self.headers,
self.cookies,
self.timeout,
self.bypass,
) as client:
async with client.get(url, params=params, **kwargs) as resp:
return await resp.text(), str(resp.url), resp.status
Expand All @@ -144,7 +154,12 @@ async def post(
**kwargs: Any
) -> Tuple[str, str, int]:
async with ClientManager(
self.client, self.proxies, self.headers, self.cookies, self.bypass
self.client,
self.proxies,
self.headers,
self.cookies,
self.timeout,
self.bypass,
) as client:
async with client.post(
url, params=params, data=data, json=json, **kwargs
Expand All @@ -153,7 +168,12 @@ async def post(

async def download(self, url: str) -> bytes:
async with ClientManager(
self.client, self.proxies, self.headers, self.cookies, self.bypass
self.client,
self.proxies,
self.headers,
self.cookies,
self.timeout,
self.bypass,
) as client:
async with client.get(url) as resp:
return await resp.read()

0 comments on commit a8a78f8

Please sign in to comment.