diff --git a/requirements.txt b/requirements.txt index 30954f6..f2e85cc 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,6 +2,7 @@ alabaster==0.7.16 astroid==2.15.8 babel==2.18.0 balderhub-unit==0.0.1b0 +balderhub-url==0.0.1b0 balderplugin-junit==0.0.3 baldertest==0.2.0 certifi==2026.2.25 diff --git a/setup.cfg b/setup.cfg index b0ad0d4..dab9718 100644 --- a/setup.cfg +++ b/setup.cfg @@ -33,13 +33,17 @@ packages = balderhub.http balderhub.http.lib balderhub.http.lib.scenario_features + balderhub.http.lib.scenario_features.client + balderhub.http.lib.scenario_features.server balderhub.http.lib.setup_features + balderhub.http.lib.setup_features.client + balderhub.http.lib.setup_features.server balderhub.http.lib.utils - balderhub.http.scenarios - balderhub.http.setups install_requires = baldertest + balderhub-url + balderhub-auth python_requires = >=3.10 package_dir = =src diff --git a/src/balderhub/http/lib/__init__.py b/src/balderhub/http/lib/__init__.py index e1b3118..e69de29 100644 --- a/src/balderhub/http/lib/__init__.py +++ b/src/balderhub/http/lib/__init__.py @@ -1,6 +0,0 @@ -# TODO add classes / functions from module (if any) -# TODO delete module if it has no sub elements - -__all__ = [ - -] diff --git a/src/balderhub/http/lib/scenario_features/__init__.py b/src/balderhub/http/lib/scenario_features/__init__.py index e1b3118..94ce4a4 100644 --- a/src/balderhub/http/lib/scenario_features/__init__.py +++ b/src/balderhub/http/lib/scenario_features/__init__.py @@ -1,6 +1,7 @@ -# TODO add classes / functions from module (if any) -# TODO delete module if it has no sub elements +from . import client +from . import server __all__ = [ - + "client", + "server" ] diff --git a/src/balderhub/http/lib/scenario_features/client/__init__.py b/src/balderhub/http/lib/scenario_features/client/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/balderhub/http/lib/scenario_features/server/__init__.py b/src/balderhub/http/lib/scenario_features/server/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/balderhub/http/lib/setup_features/__init__.py b/src/balderhub/http/lib/setup_features/__init__.py index e1b3118..94ce4a4 100644 --- a/src/balderhub/http/lib/setup_features/__init__.py +++ b/src/balderhub/http/lib/setup_features/__init__.py @@ -1,6 +1,7 @@ -# TODO add classes / functions from module (if any) -# TODO delete module if it has no sub elements +from . import client +from . import server __all__ = [ - + "client", + "server" ] diff --git a/src/balderhub/http/lib/setup_features/client/__init__.py b/src/balderhub/http/lib/setup_features/client/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/balderhub/http/lib/setup_features/server/__init__.py b/src/balderhub/http/lib/setup_features/server/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/balderhub/http/lib/utils/__init__.py b/src/balderhub/http/lib/utils/__init__.py index e1b3118..0c69c7e 100644 --- a/src/balderhub/http/lib/utils/__init__.py +++ b/src/balderhub/http/lib/utils/__init__.py @@ -1,6 +1,9 @@ -# TODO add classes / functions from module (if any) -# TODO delete module if it has no sub elements +from .http_method import HttpMethod +from .http_request_message import HttpRequestMessage +from .response import Response __all__ = [ - + "HttpMethod", + "HttpRequestMessage", + "Response", ] diff --git a/src/balderhub/http/lib/utils/functions.py b/src/balderhub/http/lib/utils/functions.py new file mode 100644 index 0000000..30b6dc8 --- /dev/null +++ b/src/balderhub/http/lib/utils/functions.py @@ -0,0 +1,30 @@ +import requests + +from balderhub.url.lib.utils import Url + + +from .response import Response + +def convert_requests_response(response: requests.Response) -> Response: + """ + Converts a `requests.Response` object into the BalderHub `Response` object. + + :param response: A `requests.Response` object that contains metadata, content, + and history of the HTTP request/response cycle. + :return: A transformed `Response` object containing the status code, URL, + content, and processed history of the `requests.Response`. + """ + history = [] + for cur_history in response.history: + history.append( + Response( + status_code=cur_history.status_code, + url=Url(cur_history.url), + content=cur_history.content) + ) + return Response( + status_code=response.status_code, + url=Url(response.url), + history=history, + content=response.content + ) diff --git a/src/balderhub/http/lib/utils/http_method.py b/src/balderhub/http/lib/utils/http_method.py new file mode 100644 index 0000000..156b0a9 --- /dev/null +++ b/src/balderhub/http/lib/utils/http_method.py @@ -0,0 +1,14 @@ +from enum import Enum + +class HttpMethod(Enum): + """ + This enum describes all common HTTP methods supported by BalderHub. + """ + GET = "GET" + POST = "POST" + PUT = "PUT" + PATCH = "PATCH" + DELETE = "DELETE" + HEAD = "HEAD" + OPTIONS = "OPTIONS" + TRACE = "TRACE" diff --git a/src/balderhub/http/lib/utils/http_request_message.py b/src/balderhub/http/lib/utils/http_request_message.py new file mode 100644 index 0000000..3aaf3c9 --- /dev/null +++ b/src/balderhub/http/lib/utils/http_request_message.py @@ -0,0 +1,44 @@ +from balderhub.url.lib.utils import Url + +from balderhub.http.lib.utils import HttpMethod + + +class HttpRequestMessage: + """Represents an HTTP request message consisting of a URL and an HTTP method. + + This class encapsulates the essential parts of an HTTP request. + + :param url: The target URL for the HTTP request. + :param method: The HTTP method to be used for the request. + """ + + def __init__(self, url: Url, method: HttpMethod): + """Initialize the HTTP request message. + + :param url: The target URL for the HTTP request. + :param method: The HTTP method to be used for the request. + """ + self._url = url + self._method = method + + def __eq__(self, other): + """Check equality with another object. + + Two :class:`HttpRequestMessage` instances are considered equal if they have the same URL and HTTP method. + + :param other: The object to compare with. + :return: ``True`` if both instances have the same URL and method, ``False`` otherwise. + """ + if not isinstance(other, HttpRequestMessage): + return False + return self._url == other._url and self._method == other._method + + @property + def url(self) -> Url: + """The target URL for the HTTP request.""" + return self._url + + @property + def method(self) -> HttpMethod: + """The HTTP method to be used for the request.""" + return self._method diff --git a/src/balderhub/http/lib/utils/response.py b/src/balderhub/http/lib/utils/response.py new file mode 100644 index 0000000..03c8bd9 --- /dev/null +++ b/src/balderhub/http/lib/utils/response.py @@ -0,0 +1,30 @@ +from __future__ import annotations +import dataclasses + +from balderhub.url.lib.utils import Url + + + +@dataclasses.dataclass +class Response: + """ + Represents an HTTP response received from a request made to a specific URL. + + This class encapsulates the details of an HTTP response, including the status code, + the URL from which the response was received, the content of the response, and the + history of any preceding responses (e.g., in cases of redirects or retries). + + :ivar status_code: Status code returned by the server. + :type status_code: int + :ivar url: The URL from which the response was received. + :type url: Url + :ivar content: The raw binary content of the response. + :type content: bytes + :ivar history: List of preceding Response objects that represent the redirection + or retry history leading to this response. Defaults to an empty list. + :type history: list[Response] + """ + status_code: int + url: Url + content: bytes + history: list[Response] = dataclasses.field(default_factory=list) diff --git a/src/balderhub/http/scenarios/__init__.py b/src/balderhub/http/scenarios/__init__.py deleted file mode 100644 index e1b3118..0000000 --- a/src/balderhub/http/scenarios/__init__.py +++ /dev/null @@ -1,6 +0,0 @@ -# TODO add classes / functions from module (if any) -# TODO delete module if it has no sub elements - -__all__ = [ - -] diff --git a/src/balderhub/http/setups/__init__.py b/src/balderhub/http/setups/__init__.py deleted file mode 100644 index e1b3118..0000000 --- a/src/balderhub/http/setups/__init__.py +++ /dev/null @@ -1,6 +0,0 @@ -# TODO add classes / functions from module (if any) -# TODO delete module if it has no sub elements - -__all__ = [ - -]