mirror of
https://github.com/EDCD/EDMarketConnector.git
synced 2025-04-12 15:27:14 +03:00
[2051] Don't Overwrite Every Session
Whoops. Only fetch a new session if it's None. Co-Authored-By: Phoebe <40956085+C1701D@users.noreply.github.com>
This commit is contained in:
parent
ebd1813612
commit
b41dfaa594
@ -7,8 +7,7 @@ See LICENSE file.
|
|||||||
"""
|
"""
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import requests
|
from requests import Session, Response
|
||||||
from requests import Session
|
|
||||||
from requests.adapters import HTTPAdapter
|
from requests.adapters import HTTPAdapter
|
||||||
from config import user_agent
|
from config import user_agent
|
||||||
|
|
||||||
@ -25,7 +24,7 @@ class TimeoutAdapter(HTTPAdapter):
|
|||||||
|
|
||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
|
|
||||||
def send(self, *args, **kwargs) -> requests.Response:
|
def send(self, *args, **kwargs) -> Response:
|
||||||
"""Send, but with a timeout always set."""
|
"""Send, but with a timeout always set."""
|
||||||
if kwargs["timeout"] is None:
|
if kwargs["timeout"] is None:
|
||||||
kwargs["timeout"] = self.default_timeout
|
kwargs["timeout"] = self.default_timeout
|
||||||
@ -33,9 +32,7 @@ class TimeoutAdapter(HTTPAdapter):
|
|||||||
return super().send(*args, **kwargs)
|
return super().send(*args, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
def new_session(
|
def new_session(timeout: int = REQUEST_TIMEOUT, session: Session | None = None) -> Session:
|
||||||
timeout: int = REQUEST_TIMEOUT, session: requests.Session | None = None
|
|
||||||
) -> requests.Session:
|
|
||||||
"""
|
"""
|
||||||
Create a new requests.Session and override the default HTTPAdapter with a TimeoutAdapter.
|
Create a new requests.Session and override the default HTTPAdapter with a TimeoutAdapter.
|
||||||
|
|
||||||
@ -43,9 +40,11 @@ def new_session(
|
|||||||
:param session: the Session object to attach the Adapter to, defaults to a new session
|
:param session: the Session object to attach the Adapter to, defaults to a new session
|
||||||
:return: The created Session
|
:return: The created Session
|
||||||
"""
|
"""
|
||||||
with Session() as session:
|
session = session or Session()
|
||||||
session.headers['User-Agent'] = user_agent
|
session.headers.setdefault("User-Agent", user_agent)
|
||||||
adapter = TimeoutAdapter(timeout)
|
|
||||||
session.mount("http://", adapter)
|
adapter = TimeoutAdapter(timeout)
|
||||||
session.mount("https://", adapter)
|
for prefix in ("http://", "https://"):
|
||||||
return session
|
session.mount(prefix, adapter)
|
||||||
|
|
||||||
|
return session
|
||||||
|
Loading…
x
Reference in New Issue
Block a user