Skip to content

Commit

Permalink
Merge pull request #48 from scrapinghub/url-page-inputs-docs
Browse files Browse the repository at this point in the history
update doc examples for RequestUrl/ResponseUrl
  • Loading branch information
kmike authored Jun 8, 2022
2 parents ba4e615 + 9287744 commit 237fab6
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
20 changes: 12 additions & 8 deletions docs/advanced/additional-requests.rst
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,9 @@ 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.method) # POST
print(request.url) # https://www.api.example.com/product-pagination/
print(type(request.url)) # <class 'web_poet.page_inputs.http.RequestUrl'>
print(request.method) # POST
print(type(request.headers) # <class 'web_poet.page_inputs.HttpRequestHeaders'>
print(request.headers) # <HttpRequestHeaders('Content-Type': 'application/json;charset=UTF-8')>
Expand All @@ -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.
Expand All @@ -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) # 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)) # <class 'web_poet.page_inputs.http.RequestUrl'>
print(request.method) # GET
print(type(request.headers) # <class 'web_poet.page_inputs.HttpRequestHeaders'>
print(request.headers) # <HttpRequestHeaders()>
Expand Down Expand Up @@ -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) # https://www.api.example.com/product-pagination/
print(type(response.url)) # <class 'str'>
print(response.url) # https://www.api.example.com/product-pagination/
print(type(response.url)) # <class 'web_poet.page_inputs.http.ResponseUrl'>
print(response.body) # b'{"data": "value \xf0\x9f\x91\x8d"}'
print(type(response.body)) # <class 'web_poet.page_inputs.HttpResponseBody'>
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion docs/intro/from-ground-up.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down

0 comments on commit 237fab6

Please sign in to comment.