Skip to content

Commit

Permalink
use yarl underneath ResponseURL and RequestURL
Browse files Browse the repository at this point in the history
  • Loading branch information
BurnzZ committed Jun 1, 2022
1 parent b2c665e commit e9a6ecf
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 3 deletions.
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
'url-matcher',
'multidict',
'w3lib >= 1.22.0',
'yarl',
],
classifiers=[
'Development Status :: 2 - Pre-Alpha',
Expand Down
18 changes: 18 additions & 0 deletions tests/test_page_inputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import requests

from web_poet.page_inputs import (
ResponseURL,
RequestURL,
HttpRequest,
HttpResponse,
HttpRequestBody,
Expand All @@ -14,6 +16,22 @@
)


@pytest.mark.parametrize("cls", [ResponseURL, RequestURL])
def test_url(cls):
url_value = "https://example.com/category/product?query=123&id=xyz#frag1"

url = cls(url_value)

assert str(url) == url_value
assert url.scheme == "https"
assert url.host == "example.com"
assert url.path == "/category/product"
assert url.query_string == "query=123&id=xyz"
assert url.fragment == "frag1"

new_url = cls(url)


@pytest.mark.parametrize("body_cls", [HttpRequestBody, HttpResponseBody])
def test_http_body_hashable(body_cls):
http_body = body_cls(b"content")
Expand Down
Binary file added web_poet/.overrides.py.swp
Binary file not shown.
2 changes: 1 addition & 1 deletion web_poet/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def base_url(self) -> str:
# FIXME: move it to HttpResponse
if self._cached_base_url is None:
text = self.html[:4096]
self._cached_base_url = get_base_url(text, self.url)
self._cached_base_url = get_base_url(text, str(self.url))
return self._cached_base_url

def urljoin(self, url: str) -> str:
Expand Down
2 changes: 2 additions & 0 deletions web_poet/page_inputs/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from .meta import Meta
from .client import HttpClient
from .http import (
ResponseURL,
RequestURL,
HttpRequest,
HttpResponse,
HttpRequestHeaders,
Expand Down
39 changes: 37 additions & 2 deletions web_poet/page_inputs/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
http_content_type_encoding
)

import yarl
from web_poet._base import _HttpHeaders
from web_poet.utils import memoizemethod_noargs

Expand All @@ -18,12 +19,46 @@
_AnyStrDict = Dict[AnyStr, Union[AnyStr, List[AnyStr], Tuple[AnyStr, ...]]]


class ResponseURL(str):
class _URL:
def __init__(self, url: Union[str, yarl.URL]):
self._url = yarl.URL(str(url))

def __str__(self) -> str:
return str(self._url)

def __repr__(self) -> str:
return str(self._url)

def __eq__(self, other) -> bool:
return str(self._url) == str(other)

@property
def scheme(self) -> str:
return self._url.scheme

@property
def host(self) -> str:
return self._url.host

@property
def path(self) -> str:
return self._url.path

@property
def query_string(self) -> str:
return self._url.query_string

@property
def fragment(self) -> str:
return self._url.fragment


class ResponseURL(_URL):
""" URL of the response """
pass


class RequestURL(str):
class RequestURL(_URL):
""" URL of the request """
pass

Expand Down

0 comments on commit e9a6ecf

Please sign in to comment.