8
8
9
9
# Using a placeholder refresh token for this example
10
10
REFRESH_TOKEN = 'PLACEHOLDER'
11
- UNAUTHORIZED_STATUS_CODE = 401
11
+ UNAUTHORIZED_CODE = 401
12
12
13
13
14
14
async def main () -> None :
@@ -17,14 +17,14 @@ async def main() -> None:
17
17
# Only treat 403 as a blocking status code, not 401
18
18
session_pool = SessionPool (create_session_settings = {'blocked_status_codes' : [403 ]}),
19
19
# Don't treat 401 responses as errors
20
- ignore_http_error_status_codes = [401 ],
20
+ ignore_http_error_status_codes = [UNAUTHORIZED_CODE ],
21
21
)
22
22
23
23
@crawler .router .default_handler
24
24
async def default_handler (context : HttpCrawlingContext ) -> None :
25
25
context .log .info (f'Processing { context .request .url } ...' )
26
26
# Now we can handle 401 responses ourselves
27
- if context .http_response .status_code == UNAUTHORIZED_STATUS_CODE :
27
+ if context .http_response .status_code == UNAUTHORIZED_CODE :
28
28
# Get a fresh access token
29
29
headers = {'authorization' : f'Bearer { REFRESH_TOKEN } ' }
30
30
response = await context .send_request (
@@ -38,7 +38,7 @@ async def default_handler(context: HttpCrawlingContext) -> None:
38
38
}
39
39
context .request .headers = HttpHeaders (new_headers )
40
40
# Trigger a retry with our updated headers
41
- raise HttpStatusCodeError ('Unauthorized' , status_code = 401 )
41
+ raise HttpStatusCodeError ('Unauthorized' , status_code = UNAUTHORIZED_CODE )
42
42
43
43
await crawler .run (['http://httpbingo.org/status/401' ])
44
44
0 commit comments