Skip to content

Commit 48a4766

Browse files
committed
rename zyte_api_response into zyte_api
1 parent 052d0d6 commit 48a4766

File tree

4 files changed

+21
-22
lines changed

4 files changed

+21
-22
lines changed

CHANGES.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ TBD
77
* Introduce ``ZyteAPIResponse`` and ``ZyteAPITextResponse`` which are subclasses
88
of ``scrapy.http.Response`` and ``scrapy.http.TextResponse`` respectively.
99
These new response classes hold the raw Zyte API response in its
10-
``zyte_api_response`` attribute.
10+
``zyte_api`` attribute.
1111

1212
0.1.0 (2022-02-03)
1313
------------------

README.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ key to download a request using Zyte API. Full list of parameters is provided in
9494
def parse(self, response):
9595
yield {"URL": response.url, "status": response.status, "HTML": response.body}
9696
97-
print(response.zyte_api_response)
97+
print(response.zyte_api)
9898
# {
9999
# 'url': 'https://quotes.toscrape.com/',
100100
# 'browserHtml': '<html> ... </html>',
@@ -113,7 +113,7 @@ key to download a request using Zyte API. Full list of parameters is provided in
113113
# 'download_slot': 'quotes.toscrape.com'
114114
# }
115115
116-
The raw Zyte API Response can be accessed via the ``zyte_api_response`` attribute
116+
The raw Zyte API Response can be accessed via the ``zyte_api`` attribute
117117
of the response object. Note that such responses are of ``ZyteAPIResponse`` and
118118
``ZyteAPITextResponse`` which are respectively subclasses of ``scrapy.http.Response``
119119
and ``scrapy.http.TextResponse``. Such classes are needed to hold the raw Zyte API

scrapy_zyte_api/responses.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ class ZyteAPIMixin:
1616
"content-encoding"
1717
}
1818

19-
def __init__(self, *args, zyte_api_response: Dict = None, **kwargs):
19+
def __init__(self, *args, zyte_api: Dict = None, **kwargs):
2020
super().__init__(*args, **kwargs)
21-
self._zyte_api_response = zyte_api_response
21+
self._zyte_api = zyte_api
2222

2323
def replace(self, *args, **kwargs):
2424
"""Create a new response with the same attributes except for those given
@@ -27,13 +27,13 @@ def replace(self, *args, **kwargs):
2727
return super().replace(*args, **kwargs)
2828

2929
@property
30-
def zyte_api_response(self) -> Optional[Dict]:
30+
def zyte_api(self) -> Optional[Dict]:
3131
"""Contains the raw API response from Zyte API.
3232
3333
To see the full list of parameters and their description, kindly refer to the
3434
`Zyte API Specification <https://docs.zyte.com/zyte-api/openapi.html#zyte-openapi-spec>`_.
3535
"""
36-
return self._zyte_api_response
36+
return self._zyte_api
3737

3838
@classmethod
3939
def _prepare_headers(cls, init_headers: Optional[List[Dict[str, str]]]):
@@ -60,7 +60,7 @@ def from_api_response(cls, api_response: Dict, *, request: Request = None):
6060
request=request,
6161
flags=["zyte-api"],
6262
headers=cls._prepare_headers(api_response.get("httpResponseHeaders")),
63-
zyte_api_response=api_response,
63+
zyte_api=api_response,
6464
)
6565

6666

@@ -77,5 +77,5 @@ def from_api_response(cls, api_response: Dict, *, request: Request = None):
7777
request=request,
7878
flags=["zyte-api"],
7979
headers=cls._prepare_headers(api_response.get("httpResponseHeaders")),
80-
zyte_api_response=api_response,
80+
zyte_api=api_response,
8181
)

tests/test_responses.py

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ def api_response_body():
4545
],
4646
)
4747
def test_init(api_response, cls):
48-
response = cls(URL, zyte_api_response=api_response())
49-
assert response.zyte_api_response == api_response()
48+
response = cls(URL, zyte_api=api_response())
49+
assert response.zyte_api == api_response()
5050

5151
assert response.url == URL
5252
assert response.status == 200
@@ -68,7 +68,7 @@ def test_init(api_response, cls):
6868
)
6969
def test_text_from_api_response(api_response, cls):
7070
response = cls.from_api_response(api_response())
71-
assert response.zyte_api_response == api_response()
71+
assert response.zyte_api == api_response()
7272

7373
assert response.url == URL
7474
assert response.status == 200
@@ -107,18 +107,18 @@ def test_response_replace(api_response, cls):
107107
(api_response_body, ZyteAPIResponse),
108108
],
109109
)
110-
def test_response_replace_zyte_api_response(api_response, cls):
110+
def test_response_replace_zyte_api(api_response, cls):
111111
orig_response = cls.from_api_response(api_response())
112112

113-
# The ``zyte_api_response`` should not be replaced.
114-
new_zyte_api_response = {"overridden": "value"}
115-
new_response = orig_response.replace(zyte_api_response=new_zyte_api_response)
116-
assert new_response.zyte_api_response == api_response()
113+
# The ``zyte_api`` should not be replaced.
114+
new_zyte_api = {"overridden": "value"}
115+
new_response = orig_response.replace(zyte_api=new_zyte_api)
116+
assert new_response.zyte_api == api_response()
117117

118118

119119
def test_non_utf8_response():
120120
content = "<html><body>Some non-ASCII ✨ chars</body></html>"
121-
sample_zyte_api_response = {
121+
sample_zyte_api = {
122122
"url": URL,
123123
"browserHtml": content,
124124
"httpResponseHeaders": [
@@ -132,7 +132,7 @@ def test_non_utf8_response():
132132
# for it. This is the default encoding for the "browserHtml" contents from
133133
# Zyte API. Thus, even if the Response Headers or <meta> tags indicate a
134134
# different encoding, it should still be treated as "utf-8".
135-
response = ZyteAPITextResponse.from_api_response(sample_zyte_api_response)
135+
response = ZyteAPITextResponse.from_api_response(sample_zyte_api)
136136
assert response.text == content
137137
assert response.encoding == "utf-8"
138138

@@ -148,7 +148,7 @@ def test_response_headers_removal(api_response, cls):
148148
"""Headers like 'Content-Encoding' should be removed later in the response
149149
instance returned to Scrapy.
150150
151-
However, it should still be present inside 'zyte_api_response.headers'.
151+
However, it should still be present inside 'zyte_api.headers'.
152152
"""
153153
additional_headers = [
154154
{"name": "Content-Encoding", "value": "gzip"},
@@ -161,6 +161,5 @@ def test_response_headers_removal(api_response, cls):
161161

162162
assert response.headers == {b"X-Some-Other-Value": [b"123"]}
163163
assert (
164-
response.zyte_api_response["httpResponseHeaders"]
165-
== raw_response["httpResponseHeaders"]
164+
response.zyte_api["httpResponseHeaders"] == raw_response["httpResponseHeaders"]
166165
)

0 commit comments

Comments
 (0)