6
6
from base64 import b64encode
7
7
from urllib .parse import unquote
8
8
9
- from scrapy .utils .python import to_bytes
10
-
11
9
try :
12
10
from scrapy import Request , Spider
11
+ from scrapy .utils .python import to_bytes
13
12
from scrapy .utils .request import request_from_dict
14
13
except ImportError as exc :
15
14
raise ImportError (
@@ -51,7 +50,8 @@ def to_apify_request(scrapy_request: Request, spider: Spider) -> dict:
51
50
Returns:
52
51
The converted Apify request.
53
52
"""
54
- assert isinstance (scrapy_request , Request ) # noqa: S101
53
+ if not isinstance (scrapy_request , Request ):
54
+ raise TypeError ('scrapy_request must be an instance of the scrapy.Request class' )
55
55
56
56
call_id = crypto_random_object_id (8 )
57
57
Actor .log .debug (f'[{ call_id } ]: to_apify_request was called (scrapy_request={ scrapy_request } )...' )
@@ -91,11 +91,14 @@ def to_scrapy_request(apify_request: dict, spider: Spider) -> Request:
91
91
Returns:
92
92
The converted Scrapy request.
93
93
"""
94
- assert isinstance (apify_request , dict ) # noqa: S101
95
- assert 'url' in apify_request # noqa: S101
96
- assert 'method' in apify_request # noqa: S101
97
- assert 'id' in apify_request # noqa: S101
98
- assert 'uniqueKey' in apify_request # noqa: S101
94
+ if not isinstance (apify_request , dict ):
95
+ raise TypeError ('apify_request must be a dictionary' )
96
+
97
+ required_keys = ['url' , 'method' , 'id' , 'uniqueKey' ]
98
+ missing_keys = [key for key in required_keys if key not in apify_request ]
99
+
100
+ if missing_keys :
101
+ raise ValueError (f"apify_request must contain { ', ' .join (map (repr , missing_keys ))} key(s)" )
99
102
100
103
call_id = crypto_random_object_id (8 )
101
104
Actor .log .debug (f'[{ call_id } ]: to_scrapy_request was called (apify_request={ apify_request } )...' )
@@ -106,14 +109,19 @@ def to_scrapy_request(apify_request: dict, spider: Spider) -> Request:
106
109
# - This process involves decoding the base64-encoded request data and reconstructing
107
110
# the Scrapy Request object from its dictionary representation.
108
111
Actor .log .debug (f'[{ call_id } ]: Restoring the Scrapy Request from the apify_request...' )
112
+
109
113
scrapy_request_dict_encoded = apify_request ['userData' ]['scrapy_request' ]
110
- assert isinstance (scrapy_request_dict_encoded , str ) # noqa: S101
114
+ if not isinstance (scrapy_request_dict_encoded , str ):
115
+ raise TypeError ('scrapy_request_dict_encoded must be a string' )
111
116
112
117
scrapy_request_dict = pickle .loads (codecs .decode (scrapy_request_dict_encoded .encode (), 'base64' ))
113
- assert isinstance (scrapy_request_dict , dict ) # noqa: S101
118
+ if not isinstance (scrapy_request_dict , dict ):
119
+ raise TypeError ('scrapy_request_dict must be a dictionary' )
114
120
115
121
scrapy_request = request_from_dict (scrapy_request_dict , spider = spider )
116
- assert isinstance (scrapy_request , Request ) # noqa: S101
122
+ if not isinstance (scrapy_request , Request ):
123
+ raise TypeError ('scrapy_request must be an instance of the Request class' )
124
+
117
125
Actor .log .debug (f'[{ call_id } ]: Scrapy Request successfully reconstructed (scrapy_request={ scrapy_request } )...' )
118
126
119
127
# Update the meta field with the meta field from the apify_request
0 commit comments