Skip to content

Commit b564f50

Browse files
Mantisusvdusek
andauthored
chore: update API docs of Router (#1105)
### Description - Update API docs of Router ### Issues - Closes: #1098 --------- Co-authored-by: Vlada Dusek <[email protected]>
1 parent 500acb6 commit b564f50

File tree

1 file changed

+37
-1
lines changed

1 file changed

+37
-1
lines changed

Diff for: src/crawlee/router.py

+37-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,43 @@
1414

1515
@docs_group('Classes')
1616
class Router(Generic[TCrawlingContext]):
17-
"""Dispatches requests to registered handlers based on their labels."""
17+
"""A request dispatching system that routes requests to registered handlers based on their labels.
18+
19+
The `Router` allows you to define and register request handlers for specific labels. When a request is received,
20+
the router invokes the corresponding `request_handler` based on the request's `label`. If no matching handler
21+
is found, the default handler is used.
22+
23+
### Usage
24+
25+
```python
26+
from crawlee.crawlers import HttpCrawler, HttpCrawlingContext
27+
from crawlee.router import Router
28+
29+
router = Router[HttpCrawlingContext]()
30+
31+
32+
# Handler for requests without a matching label handler
33+
@router.default_handler
34+
async def default_handler(context: HttpCrawlingContext) -> None:
35+
context.log.info(f'Request without label {context.request.url} ...')
36+
37+
38+
# Handler for category requests
39+
@router.handler(label='category')
40+
async def category_handler(context: HttpCrawlingContext) -> None:
41+
context.log.info(f'Category request {context.request.url} ...')
42+
43+
44+
# Handler for product requests
45+
@router.handler(label='product')
46+
async def product_handler(context: HttpCrawlingContext) -> None:
47+
context.log.info(f'Product {context.request.url} ...')
48+
49+
50+
async def main() -> None:
51+
crawler = HttpCrawler(request_handler=router)
52+
await crawler.run()
53+
"""
1854

1955
def __init__(self) -> None:
2056
self._default_handler: RequestHandler[TCrawlingContext] | None = None

0 commit comments

Comments
 (0)