From 707ce57416ce6ae62f3fe54a82941eb647209f15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A1n=20Chaves?= Date: Mon, 15 Jul 2024 12:40:21 +0200 Subject: [PATCH] Remove HttpRequest.from_form, cover form2request in the HttpRequest reference --- setup.py | 1 - tests/test_page_inputs.py | 190 ----------------------------------- tox.ini | 1 - web_poet/page_inputs/http.py | 59 +---------- 4 files changed, 5 insertions(+), 246 deletions(-) diff --git a/setup.py b/setup.py index d63c3948..04b804fb 100644 --- a/setup.py +++ b/setup.py @@ -20,7 +20,6 @@ entry_points={"pytest11": ["web-poet = web_poet.testing.pytest"]}, install_requires=[ "attrs >= 21.3.0", - "form2request >= 0.1.1", "parsel >= 1.8.1", "url-matcher >= 0.2.0", "multidict >= 0.5.0", diff --git a/tests/test_page_inputs.py b/tests/test_page_inputs.py index b87b483e..49e81847 100644 --- a/tests/test_page_inputs.py +++ b/tests/test_page_inputs.py @@ -5,7 +5,6 @@ import parsel import pytest import requests -from parsel import Selector from web_poet import BrowserResponse, RequestUrl, ResponseUrl from web_poet.page_inputs import ( @@ -212,195 +211,6 @@ def test_http_request_init_with_response_url() -> None: assert str(req.url) == str(resp.url) -def test_http_request_from_form_get() -> None: - url = "https://example.com" - response = HttpResponse( - url, - b""" - - a -
- - - - -
- """, - ) - form_selector = response.css("#search-form")[0] - request = HttpRequest.from_form(form_selector.root, {"query": "foo"}) - assert ( - str(request.url) == "https://example.com/search?bar=tender&baz=ooka&query=foo" - ) - assert request.method == "GET" - assert request.headers == HttpRequestHeaders() - assert request.body == b"" - - -def test_http_request_from_form_post() -> None: - url = "https://example.com" - response = HttpResponse( - url, - b""" - - a -
- - - - -
- """, - ) - form_selector = response.css("#search-form")[0] - request = HttpRequest.from_form(form_selector.root, {"query": "foo"}) - assert str(request.url) == "https://example.com/search" - assert request.method == "POST" - assert request.headers == HttpRequestHeaders( - {"Content-Type": "application/x-www-form-urlencoded"} - ) - assert request.body == b"bar=tender&baz=ooka&query=foo" - - -def test_http_request_from_form_select_no_selected() -> None: - url = "https://example.com" - response = HttpResponse( - url, - b""" - - a -
- - - - -
- """, - ) - form_selector = response.css("#search-form")[0] - request = HttpRequest.from_form(form_selector.root) - assert request.body == b"query=&bar=code&baz=ooka" - - -def test_http_request_from_form_select_no_options() -> None: - url = "https://example.com" - response = HttpResponse( - url, - b""" - - a -
- - - - -
- """, - ) - form_selector = response.css("#search-form")[0] - request = HttpRequest.from_form(form_selector.root) - assert request.body == b"query=&baz=ooka" - - -def test_http_request_from_form_no_method() -> None: - url = "https://example.com" - response = HttpResponse( - url, - b""" - - a -
- - - - -
- """, - ) - form_selector = response.css("#search-form")[0] - request = HttpRequest.from_form(form_selector.root) - assert request.method == "GET" - - -def test_http_request_from_form_bad_method() -> None: - url = "https://example.com" - response = HttpResponse( - url, - b""" - - a -
- - - - -
- """, - ) - form_selector = response.css("#search-form")[0] - request = HttpRequest.from_form(form_selector.root) - assert request.method == "GET" - - -def test_http_request_from_form_no_base_url() -> None: - selector = Selector( - text=""" - - a -
- - - - -
- """, - ) - form_selector = selector.css("#search-form")[0] - with pytest.raises(ValueError): - HttpRequest.from_form(form_selector.root) - - -def test_http_request_from_form_no_action() -> None: - url = "https://example.com" - response = HttpResponse( - url, - b""" - - a -
- - - - -
- """, - ) - form_selector = response.css("#search-form")[0] - request = HttpRequest.from_form(form_selector.root) - assert str(request.url) == url - - @pytest.mark.parametrize( "cls", ( diff --git a/tox.ini b/tox.ini index fed0aa84..5a5cfcca 100644 --- a/tox.ini +++ b/tox.ini @@ -67,7 +67,6 @@ deps = zyte-common-items==0.1.0 aiohttp==3.7.0 attrs==21.3.0 - form2request==0.1.1 parsel==1.8.1 url-matcher==0.2.0 tldextract==3.0.0 diff --git a/web_poet/page_inputs/http.py b/web_poet/page_inputs/http.py index 25fa961e..c73f2831 100644 --- a/web_poet/page_inputs/http.py +++ b/web_poet/page_inputs/http.py @@ -2,21 +2,10 @@ import json from hashlib import sha1 -from typing import ( - TYPE_CHECKING, - Any, - Dict, - Iterable, - List, - Optional, - Tuple, - TypeVar, - Union, -) +from typing import Any, Optional, TypeVar, Union from urllib.parse import urljoin import attrs -from form2request import form2request from w3lib.encoding import ( html_body_declared_encoding, html_to_unicode, @@ -33,17 +22,6 @@ from .url import RequestUrl as _RequestUrl from .url import ResponseUrl as _ResponseUrl -if TYPE_CHECKING: - # typing.Self requires Python 3.11 - from lxml.html import FormElement # nosec - from lxml.html import HtmlElement # nosec - from parsel import Selector, SelectorList - from typing_extensions import Self - -FormdataVType = Union[str, Iterable[str]] -FormdataKVType = Tuple[str, FormdataVType] -FormdataType = Optional[Union[Dict[str, FormdataVType], List[FormdataKVType]]] - T_headers = TypeVar("T_headers", bound=_HttpHeaders) @@ -146,6 +124,10 @@ def declared_encoding(self) -> Optional[str]: class HttpRequest: """Represents a generic HTTP request used by other functionalities in **web-poet** like :class:`~.HttpClient`. + + .. tip:: To build a request to submit an HTML form, use the + :doc:`form2request library `, which provides + integration with web-poet. """ url: _RequestUrl = attrs.field(converter=_RequestUrl) @@ -157,37 +139,6 @@ class HttpRequest: factory=HttpRequestBody, converter=HttpRequestBody, kw_only=True ) - @classmethod - def from_form( - cls, - form: FormElement | Selector | SelectorList, - data: FormdataType = None, - *, - click: None | bool | HtmlElement = None, - method: None | str = None, - enctype: None | str = None, - **kwargs, - ) -> Self: - """Return an :class:`HttpRequest` to submit an HTML form. - - See the :doc:`form2request usage documentation ` - and :func:`~form2request.form2request` for parameter reference. - """ - request_data = form2request( - form, - data, - click=click, - method=method, - enctype=enctype, - **kwargs, - ) - return cls( - url=request_data.url, - method=request_data.method, - headers=request_data.headers, - body=request_data.body, - ) - def urljoin(self, url: Union[str, _RequestUrl, _ResponseUrl]) -> _RequestUrl: """Return *url* as an absolute URL.