Skip to content
Merged
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
2 changes: 1 addition & 1 deletion .pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions src/balderhub/http/lib/scenario_features/client/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from .web_session_feature import WebSessionFeature

__all__ = [
'WebSessionFeature'
]
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions src/balderhub/http/lib/setup_features/client/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from .web_session_with_requests_feature import WebSessionWithRequestsFeature

__all__ = [
'WebSessionWithRequestsFeature'
]
Original file line number Diff line number Diff line change
@@ -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
Loading