spiders_config_models.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. from pydantic import BaseModel, AnyUrl, validator
  2. from typing import Dict, Any, Optional, Union
  3. class BaseConfig(BaseModel):
  4. base_url: Optional[AnyUrl]
  5. request_timeout: int = 30
  6. max_retries: int = 3
  7. headers: Dict[str, Any] = {}
  8. @validator('request_timeout', 'max_retries')
  9. def validate_positive_int(cls, v, field):
  10. if v <= 0:
  11. raise ValueError(f'{field.name} must be positive')
  12. return v
  13. class PlatformConfig(BaseConfig):
  14. platform: str
  15. mode: str
  16. path: Optional[str]
  17. url: AnyUrl
  18. method: str
  19. request_body: Dict[str, Any] = {}
  20. loop_times: int = 1
  21. loop_interval: Dict[str, int] = {}
  22. response_parse: Dict[str, Any] = {}
  23. retry_times: int = 0
  24. feishu_sheetid: Optional[str] = None
  25. @validator('method')
  26. def validate_method(cls, v):
  27. allowed_methods = ['GET', 'POST', 'PUT', 'DELETE', 'PATCH']
  28. if v.upper() not in allowed_methods:
  29. raise ValueError(f'Method must be one of {", ".join(allowed_methods)}')
  30. return v.upper()
  31. @validator('loop_times')
  32. def validate_loop_times(cls, v):
  33. if v <= 0:
  34. raise ValueError('loop_times must be positive')
  35. return v
  36. @validator('loop_interval')
  37. def validate_loop_interval(cls, v):
  38. if 'min' not in v or 'max' not in v:
  39. raise ValueError('loop_interval must contain both min and max keys')
  40. if v['min'] < 0 or v['max'] < 0:
  41. raise ValueError('loop_interval values must be non-negative')
  42. if v['min'] > v['max']:
  43. raise ValueError('min value cannot be greater than max value')
  44. return v
  45. @validator('response_parse')
  46. def validate_response_parse(cls, v):
  47. if 'data_path' not in v:
  48. raise ValueError('response_parse must contain data_path')
  49. return v
  50. @validator('retry_times')
  51. def validate_retry_times(cls, v):
  52. if v < 0:
  53. raise ValueError('retry_times must be non-negative')
  54. return v
  55. @validator('request_body')
  56. def validate_request_body(cls, v):
  57. # 确保request_body中的值是基本类型或字典/列表
  58. if not isinstance(v, dict):
  59. raise ValueError('request_body must be a dictionary')
  60. def is_valid_type(value):
  61. if isinstance(value, (str, int, float, bool, type(None))):
  62. return True
  63. elif isinstance(value, (list, tuple)):
  64. return all(is_valid_type(item) for item in value)
  65. elif isinstance(value, dict):
  66. return all(isinstance(k, str) and is_valid_type(v) for k, v in value.items())
  67. return False
  68. for key, value in v.items():
  69. if not is_valid_type(value):
  70. raise ValueError(f'Invalid type for request_body["{key}"]: {type(value)}')
  71. return v