Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: update API docs of Router #1105

Merged
merged 2 commits into from
Mar 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 37 additions & 1 deletion src/crawlee/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,43 @@

@docs_group('Classes')
class Router(Generic[TCrawlingContext]):
"""Dispatches requests to registered handlers based on their labels."""
"""A request dispatching system that routes requests to registered handlers based on their labels.

The `Router` allows you to define and register request handlers for specific labels. When a request is received,
the router invokes the corresponding `request_handler` based on the request's `label`. If no matching handler
is found, the default handler is used.

### Usage

```python
from crawlee.crawlers import HttpCrawler, HttpCrawlingContext
from crawlee.router import Router

router = Router[HttpCrawlingContext]()


# Handler for requests without a matching label handler
@router.default_handler
async def default_handler(context: HttpCrawlingContext) -> None:
context.log.info(f'Request without label {context.request.url} ...')


# Handler for category requests
@router.handler(label='category')
async def category_handler(context: HttpCrawlingContext) -> None:
context.log.info(f'Category request {context.request.url} ...')


# Handler for product requests
@router.handler(label='product')
async def product_handler(context: HttpCrawlingContext) -> None:
context.log.info(f'Product {context.request.url} ...')


async def main() -> None:
crawler = HttpCrawler(request_handler=router)
await crawler.run()
"""

def __init__(self) -> None:
self._default_handler: RequestHandler[TCrawlingContext] | None = None
Expand Down
2 changes: 1 addition & 1 deletion uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading