diff --git a/.pylintrc b/.pylintrc index ffbbaf6..c3bbf17 100644 --- a/.pylintrc +++ b/.pylintrc @@ -274,7 +274,7 @@ exclude-too-few-public-methods= ignored-parents= # Maximum number of arguments for function / method. -max-args=5 +max-args=7 # Maximum number of attributes for a class (see R0902). max-attributes=7 diff --git a/src/balderhub/http/lib/scenario_features/client/__init__.py b/src/balderhub/http/lib/scenario_features/client/__init__.py index e69de29..4f6feda 100644 --- a/src/balderhub/http/lib/scenario_features/client/__init__.py +++ b/src/balderhub/http/lib/scenario_features/client/__init__.py @@ -0,0 +1,5 @@ +from .web_session_feature import WebSessionFeature + +__all__ = [ + 'WebSessionFeature' +] diff --git a/src/balderhub/http/lib/scenario_features/client/web_session_feature.py b/src/balderhub/http/lib/scenario_features/client/web_session_feature.py new file mode 100644 index 0000000..fcdaf9b --- /dev/null +++ b/src/balderhub/http/lib/scenario_features/client/web_session_feature.py @@ -0,0 +1,42 @@ +from __future__ import annotations +import balder +from balderhub.url.lib.utils import Url +from balderhub.http.lib.utils import HttpMethod, Response + + +class WebSessionFeature(balder.Feature): + """ + Basic feature to work with web sessions. + """ + + def __init__(self, **kwargs: balder.Feature) -> None: + super().__init__(**kwargs) + + self._responses = [] + + def request( + self, + method: str | HttpMethod, + url: str | Url, + data: dict | bytes | None = None, + headers: dict[str, str] | None = None, + cookies: dict[str, str] | None = None, + **kwargs + ) -> Response: + """ + This method allows to execute a request. It returns a new response object. + + :param method: the http method to use + :param url: the url the request should be made to + :param data: optional the data that should be append into the body of the request + :param headers: the headers that the request should include + :param cookies: the cookies that the request should include + :return: a response object that holds status code and the answer from the resource + """ + raise NotImplementedError + + def get_last_response(self) -> Response | None: + """ + :return: returns the last response that was made over this session + """ + return self._responses[-1] if len(self._responses) > 0 else None diff --git a/src/balderhub/http/lib/setup_features/client/__init__.py b/src/balderhub/http/lib/setup_features/client/__init__.py index e69de29..febf08a 100644 --- a/src/balderhub/http/lib/setup_features/client/__init__.py +++ b/src/balderhub/http/lib/setup_features/client/__init__.py @@ -0,0 +1,5 @@ +from .web_session_with_requests_feature import WebSessionWithRequestsFeature + +__all__ = [ + 'WebSessionWithRequestsFeature' +] diff --git a/src/balderhub/http/lib/setup_features/client/web_session_with_requests_feature.py b/src/balderhub/http/lib/setup_features/client/web_session_with_requests_feature.py new file mode 100644 index 0000000..1f67276 --- /dev/null +++ b/src/balderhub/http/lib/setup_features/client/web_session_with_requests_feature.py @@ -0,0 +1,42 @@ +from __future__ import annotations + +import requests +from balderhub.url.lib.utils import Url +from ...utils.functions import convert_requests_response +from ...utils.response import Response +from ...utils.http_method import HttpMethod +from ...scenario_features.client.web_session_feature import WebSessionFeature + + +class WebSessionWithRequestsFeature(WebSessionFeature): + """ + This setup feature provides an implementation for working with web session by using the python-requests library. + """ + + def __init__(self, **kwargs): + super().__init__(**kwargs) + self.session = requests.Session() + + def request( + self, + method: str | HttpMethod, + url: str | Url, + data: dict | bytes | None = None, + headers: dict[str, str] | None = None, + cookies: dict[str, str] | None = None, + **kwargs + ) -> Response: + method_as_str = method.value if isinstance(method, HttpMethod) else method + if data is None: + data = {} + + session_response = self.session.request( + method=method_as_str, + url=str(url), + data=data, + headers=headers, + cookies=cookies + ) + response = convert_requests_response(session_response) + self._responses.append(response) + return response