From aad8ae6290e3df81bd8d5324c4d28d22e4cf3031 Mon Sep 17 00:00:00 2001 From: Kevin Lloyd Bernal Date: Wed, 8 Jun 2022 12:06:09 +0800 Subject: [PATCH 1/2] update doc examples for RequestUrl/ResponseUrl --- docs/advanced/additional-requests.rst | 8 ++++---- docs/intro/from-ground-up.rst | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/docs/advanced/additional-requests.rst b/docs/advanced/additional-requests.rst index bd7b8516..931e4214 100644 --- a/docs/advanced/additional-requests.rst +++ b/docs/advanced/additional-requests.rst @@ -54,7 +54,7 @@ a generic HTTP Request: :class:`~.HttpRequest`. Here's an example: ).encode("utf-8"), ) - print(request.url) # https://www.api.example.com/product-pagination/ + print(request.url) # RequestUrl('https://www.api.example.com/product-pagination/') print(request.method) # POST print(type(request.headers) # @@ -90,7 +90,7 @@ it's perfectly fine to define them as: request = web_poet.HttpRequest("https://api.example.com/product-info?id=123") - print(request.url) # https://api.example.com/product-info?id=123 + print(request.url) # RequestUrl('https://api.example.com/product-info?id=123') print(request.method) # GET print(type(request.headers) # @@ -141,7 +141,7 @@ Let's check out an example to see its internals: headers={"Content-Type": "application/json;charset=UTF-8"} ) - print(response.url) # https://www.api.example.com/product-pagination/ + print(response.url) # ResponseUrl('https://www.api.example.com/product-pagination/') print(type(response.url)) # print(response.body) # b'{"data": "value \xf0\x9f\x91\x8d"}' @@ -604,7 +604,7 @@ from the previous subsection named: :ref:`httpclient-get-example`. except web_poet.exceptions.HttpResponseError as err: logger.warning( f"Received a {err.response.status} response status for product ID " - f"'{item['product_id']}' from this URL: {err.request.url}" + f"'{item['product_id']}' from this URL: {str(err.request.url)}" ) else: item["images"] = response.css(".product-images img::attr(src)").getall() diff --git a/docs/intro/from-ground-up.rst b/docs/intro/from-ground-up.rst index d5781650..83e8133e 100644 --- a/docs/intro/from-ground-up.rst +++ b/docs/intro/from-ground-up.rst @@ -503,7 +503,7 @@ For example, a very basic Page Object could look like this: def to_item(self) -> dict: return { - 'url': self.response.url, + 'url': str(self.response.url), 'title': self.response.css("h1::text").get() } From 9287744af2b4110a91e447d1381efca7b85a1ea2 Mon Sep 17 00:00:00 2001 From: Kevin Lloyd Bernal Date: Wed, 8 Jun 2022 15:12:37 +0800 Subject: [PATCH 2/2] fix docs on how RequestUrl/ResponseUrl is portrayed --- docs/advanced/additional-requests.rst | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/docs/advanced/additional-requests.rst b/docs/advanced/additional-requests.rst index 931e4214..54562d9e 100644 --- a/docs/advanced/additional-requests.rst +++ b/docs/advanced/additional-requests.rst @@ -54,8 +54,9 @@ a generic HTTP Request: :class:`~.HttpRequest`. Here's an example: ).encode("utf-8"), ) - print(request.url) # RequestUrl('https://www.api.example.com/product-pagination/') - print(request.method) # POST + print(request.url) # https://www.api.example.com/product-pagination/ + print(type(request.url)) # + print(request.method) # POST print(type(request.headers) # print(request.headers) # @@ -67,7 +68,8 @@ a generic HTTP Request: :class:`~.HttpRequest`. Here's an example: There are a few things to take note here: - * ``url`` and ``method`` are simply **strings**. + * ``method`` is simply a **string**. + * ``url`` is represented by the :class:`~.RequestUrl` class. * ``headers`` is represented by the :class:`~.HttpRequestHeaders` class which resembles a ``dict``-like interface. It supports case-insensitive header-key lookups as well as multi-key storage. @@ -90,8 +92,9 @@ it's perfectly fine to define them as: request = web_poet.HttpRequest("https://api.example.com/product-info?id=123") - print(request.url) # RequestUrl('https://api.example.com/product-info?id=123') - print(request.method) # GET + print(request.url) # https://api.example.com/product-info?id=123 + print(type(request.url)) # + print(request.method) # GET print(type(request.headers) # print(request.headers) # @@ -141,8 +144,8 @@ Let's check out an example to see its internals: headers={"Content-Type": "application/json;charset=UTF-8"} ) - print(response.url) # ResponseUrl('https://www.api.example.com/product-pagination/') - print(type(response.url)) # + print(response.url) # https://www.api.example.com/product-pagination/ + print(type(response.url)) # print(response.body) # b'{"data": "value \xf0\x9f\x91\x8d"}' print(type(response.body)) # @@ -174,7 +177,8 @@ methods. Here are the key take aways from the example above: - * The ``url`` and ``status`` are simply **string** and **int** respectively. + * ``status`` is simply an **int**. + * ``url`` is represented by the :class:`~.ResponseUrl` class. * ``headers`` is represented by the :class:`~.HttpResponseHeaders` class. It's similar to :class:`~.HttpRequestHeaders` where it inherits from :external:py:class:`multidict.CIMultiDict`, granting it case-insensitive @@ -604,7 +608,7 @@ from the previous subsection named: :ref:`httpclient-get-example`. except web_poet.exceptions.HttpResponseError as err: logger.warning( f"Received a {err.response.status} response status for product ID " - f"'{item['product_id']}' from this URL: {str(err.request.url)}" + f"'{item['product_id']}' from this URL: {err.request.url}" ) else: item["images"] = response.css(".product-images img::attr(src)").getall()