|
| 1 | +import asyncio |
| 2 | +from typing import Literal |
| 3 | + |
| 4 | +from pydantic import BaseModel, ConfigDict, Field, ValidationError, field_validator |
| 5 | + |
| 6 | +from apify import Actor |
| 7 | + |
| 8 | + |
| 9 | +class ActorInput(BaseModel): |
| 10 | + """Typed and validated representation of the Actor input.""" |
| 11 | + |
| 12 | + # Accept both snake_case and the input schema's camelCase; ignore extras. |
| 13 | + model_config = ConfigDict(populate_by_name=True, extra='ignore') |
| 14 | + |
| 15 | + # Required: non-empty list of search terms (normalized below). |
| 16 | + search_terms: list[str] = Field(alias='searchTerms', min_length=1) |
| 17 | + |
| 18 | + # Optional: 1-100, defaults to 10. |
| 19 | + max_results: int = Field(alias='maxResults', default=10, ge=1, le=100) |
| 20 | + |
| 21 | + # Optional: restricted to a fixed set of choices. |
| 22 | + output_format: Literal['json', 'csv'] = Field(alias='outputFormat', default='json') |
| 23 | + |
| 24 | + @field_validator('search_terms') |
| 25 | + @classmethod |
| 26 | + def _normalize_terms(cls, value: list[str]) -> list[str]: |
| 27 | + # Trim whitespace and drop empty terms. |
| 28 | + cleaned = [term.strip() for term in value if term.strip()] |
| 29 | + if not cleaned: |
| 30 | + raise ValueError('searchTerms must contain at least one non-empty term') |
| 31 | + return cleaned |
| 32 | + |
| 33 | + |
| 34 | +async def main() -> None: |
| 35 | + async with Actor: |
| 36 | + # Read the raw input (a plain dict, not yet validated). |
| 37 | + raw_input = await Actor.get_input() or {} |
| 38 | + |
| 39 | + # Validate the raw input against the model. |
| 40 | + try: |
| 41 | + actor_input = ActorInput.model_validate(raw_input) |
| 42 | + except ValidationError as exc: |
| 43 | + # Log a per-field summary, then re-raise to fail the run. |
| 44 | + Actor.log.error('The Actor input is invalid:\n%s', exc) |
| 45 | + raise |
| 46 | + |
| 47 | + # Work with typed attributes from here on. |
| 48 | + Actor.log.info('Input passed validation: %s', actor_input.model_dump()) |
| 49 | + |
| 50 | + max_results = actor_input.max_results |
| 51 | + for term in actor_input.search_terms: |
| 52 | + Actor.log.info('Processing %r (max %d results)', term, max_results) |
| 53 | + |
| 54 | + # Store the normalized input as output. |
| 55 | + await Actor.set_value('OUTPUT', actor_input.model_dump()) |
| 56 | + |
| 57 | + |
| 58 | +if __name__ == '__main__': |
| 59 | + asyncio.run(main()) |
0 commit comments