-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #195 from scrapinghub/response
add new AnyResponse
- Loading branch information
Showing
7 changed files
with
83 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
from typing import Optional, Union | ||
|
||
import attrs | ||
|
||
from web_poet.mixins import SelectableMixin, UrlShortcutsMixin | ||
from web_poet.page_inputs.browser import BrowserResponse | ||
from web_poet.page_inputs.http import HttpResponse | ||
from web_poet.page_inputs.url import ResponseUrl | ||
|
||
|
||
@attrs.define | ||
class AnyResponse(SelectableMixin, UrlShortcutsMixin): | ||
"""A container that holds either :class:`~.BrowserResponse` or :class:`~.HttpResponse`.""" | ||
|
||
response: Union[BrowserResponse, HttpResponse] | ||
|
||
@property | ||
def url(self) -> ResponseUrl: | ||
"""URL of the response.""" | ||
return self.response.url | ||
|
||
@property | ||
def text(self) -> str: | ||
"""Text or HTML contents of the response.""" | ||
if isinstance(self.response, BrowserResponse): | ||
return self.response.html | ||
return self.response.text | ||
|
||
@property | ||
def status(self) -> Optional[int]: | ||
"""The int status code of the HTTP response, if available.""" | ||
return self.response.status | ||
|
||
def _selector_input(self) -> str: | ||
return self.text |