Skip to content

Commit

Permalink
Improve session handling in collection endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
funilrys committed Mar 17, 2024
1 parent 2c623a6 commit 7964eca
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 10 deletions.
31 changes: 21 additions & 10 deletions PyFunceble/query/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
from PyFunceble.checker.reputation.status import ReputationCheckerStatus
from PyFunceble.checker.syntax.status import SyntaxCheckerStatus
from PyFunceble.helpers.environment_variable import EnvironmentVariableHelper
from PyFunceble.utils.system import LateImport


class CollectionQueryTool:
Expand Down Expand Up @@ -109,7 +110,7 @@ class CollectionQueryTool:
Whether we are working with the modern or legacy API.
"""

session: Optional[requests.Session] = None
_session: Optional[requests.Session] = None

def __init__(
self,
Expand All @@ -135,15 +136,6 @@ def __init__(
else:
self.guess_and_set_preferred_status_origin()

self.session = requests.Session()
self.session.headers.update(
{
"Authorization": f"Bearer {self.token}" if self.token else None,
"X-Pyfunceble-Version": PyFunceble.storage.PROJECT_VERSION,
"Content-Type": "application/json",
}
)

def __contains__(self, value: str) -> bool:
"""
Checks if the given value is in the collection.
Expand All @@ -164,6 +156,25 @@ def __getitem__(self, value: str) -> Optional[dict]:

return self.pull(value)

@property
@LateImport("from PyFunceble.factory import Requester")
def session(self) -> requests.Session:
"""
Provides the session to use.
"""

if not self._session:
self._session = Requester
self._session.headers.update(
{
"Authorization": f"Bearer {self.token}" if self.token else None,
"X-Pyfunceble-Version": PyFunceble.storage.PROJECT_VERSION,
"Content-Type": "application/json",
}
)

return self._session

@property
def token(self) -> Optional[str]:
"""
Expand Down
8 changes: 8 additions & 0 deletions PyFunceble/query/requests/requester.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,14 @@ def wrapper(self, *args, **kwargs):

return request_method

@property
def headers(self) -> dict:
"""
Provides the current state of the :code:`_headers` attribute.
"""

return self.session.headers

@property
def max_retries(self) -> int:
"""
Expand Down

0 comments on commit 7964eca

Please sign in to comment.