Skip to content

Commit 5a13a79

Browse files
committed
Handle custom attributes received in the API response.
1 parent 106b099 commit 5a13a79

File tree

4 files changed

+38
-1
lines changed

4 files changed

+38
-1
lines changed

scrapy_zyte_api/providers.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,9 @@ async def __call__( # noqa: C901
327327
continue
328328
assert issubclass(cls_stripped, Item)
329329
result = cls_stripped.from_dict(api_response.raw_api_response[kw]) # type: ignore[attr-defined]
330+
custom_attrs = api_response.raw_api_response.get("customAttributes")
331+
if custom_attrs:
332+
result.customAttributes = custom_attrs
330333
if is_typing_annotated(cls):
331334
result = AnnotatedInstance(result, cls.__metadata__) # type: ignore[attr-defined]
332335
results.append(result)

setup.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ def get_version():
3333
"andi>=0.6.0",
3434
"scrapy-poet>=0.22.3",
3535
"web-poet>=0.17.0",
36-
"zyte-common-items>=0.20.0",
36+
# https://github.com/zytedata/zyte-common-items/pull/100
37+
"zyte-common-items @ git+https://github.com/scrapinghub/zyte-common-items.git@custom-attrs",
3738
]
3839
},
3940
classifiers=[

tests/mockserver.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,12 @@ def render_POST(self, request):
230230
"name"
231231
] += f" (country {request_data['geolocation']})"
232232

233+
if "customAttributes" in request_data:
234+
response_data["customAttributes"] = {
235+
"attr1": "foo",
236+
"attr2": 42,
237+
}
238+
233239
return json.dumps(response_data).encode()
234240

235241

tests/test_providers.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,33 @@ def parse_(self, response: DummyResponse, page: GeoProductPage): # type: ignore
394394
assert "Geolocation dependencies must be annotated" in caplog.text
395395

396396

397+
@ensureDeferred
398+
async def test_provider_custom_attrs(mockserver):
399+
settings = create_scrapy_settings()
400+
settings["ZYTE_API_URL"] = mockserver.urljoin("/")
401+
settings["SCRAPY_POET_PROVIDERS"] = {ZyteApiProvider: 0}
402+
settings["ZYTE_API_PROVIDER_PARAMS"] = {
403+
"customAttributes": {
404+
"attr1": {"type": "string", "description": "descr1"},
405+
"attr2": {"type": "number", "description": "descr2"},
406+
}
407+
}
408+
409+
item, url, _ = await crawl_single_item(ZyteAPISpider, HtmlResource, settings)
410+
assert item["product"] == Product.from_dict(
411+
dict(
412+
url=url,
413+
name="Product name",
414+
price="10",
415+
currency="USD",
416+
customAttributes={
417+
"attr1": "foo",
418+
"attr2": 42,
419+
},
420+
)
421+
)
422+
423+
397424
class RecordingHandler(ScrapyZyteAPIDownloadHandler):
398425
"""Subclasses the original handler in order to record the Zyte API parameters
399426
used for each downloading request.

0 commit comments

Comments
 (0)