diff --git a/capmonstercloud_client/requests/HcaptchaComplexImageTask.py b/capmonstercloud_client/requests/HcaptchaComplexImageTask.py index 9eae3fb..06fe427 100644 --- a/capmonstercloud_client/requests/HcaptchaComplexImageTask.py +++ b/capmonstercloud_client/requests/HcaptchaComplexImageTask.py @@ -10,74 +10,55 @@ class HcaptchaComplexImageTaskRequest(ComplexImageTaskRequestBase): metadata : Dict[str, str] exampleImageUrls: Optional[List[str]] = None exampleImagesBase64: Optional[List[str]] = None - + + @staticmethod + def _validate_image_array(value, field_name, max_images): + """Helper method to validate image array""" + if value is None: + return value + if not isinstance(value, (list, tuple)): + raise TypeError(f'Expect that type {field_name} array will be or , got {type(value)}') + + if not len(value): + if 'base64' in field_name.lower(): + raise ZeroImagesErrors(f'At least one image base64 expected, got {len(value)}') + else: + raise ZeroImagesErrors(f'At least one image url expected, got {len(value)}') + + if len(value) > max_images: + raise NumbersImagesErrors(f'Maximum number of images in list {max_images}, got {len(value)}') + + contain_types = [isinstance(x, str) for x in value] + if not all(contain_types): + if 'base64' in field_name.lower(): + raise TypeError(f'Next images from imagesBase64 array are not string: {contain_types}') + else: + raise TypeError(f'Next images from imagesUrls array are not string: {contain_types}') + return value + @validator('metadata') def validate_metadata(cls, value): if value.get('Task') is None: - raise TaskNotDefinedError(f'Expect that task will be defined.') + raise TaskNotDefinedError('Expect that task will be defined.') else: return value @validator('exampleImageUrls') - def validate_urls_array(cls, value): - if value is not None: - if not isinstance(value, (list, tuple)): - raise TypeError(f'Expect that type exampleImageUrls array will be or , got {type(value)}') - elif len(value) > 1: - raise NumbersImagesErrors(f'Maximum number of images in list 1, got {len(value)}') - elif not len(value): - raise ZeroImagesErrors(f'At least one image url expected, got {len(value)}') - # Check for each element type - contain_types = [isinstance(x, str) for x in value] - if not all(contain_types): - raise TypeError(f'Next images from imagesUrls array are not string: {contain_types}') - return value + def validate_example_image_urls(cls, value): + return cls._validate_image_array(value, 'exampleImageUrls', 1) @validator('exampleImagesBase64') - def validate_urls_array(cls, value): - if value is not None: - if not isinstance(value, (list, tuple)): - raise TypeError(f'Expect that type exampleImagesBase64 array will be or , got {type(value)}') - elif len(value) > 1: - raise NumbersImagesErrors(f'Maximum number of images in list 1, got {len(value)}') - elif not len(value): - raise ZeroImagesErrors(f'At least one image base64 expected, got {len(value)}') - # Check for each element type - contain_types = [isinstance(x, str) for x in value] - if not all(contain_types): - raise TypeError(f'Next images from imagesBase64 array are not string: {contain_types}') - return value + def validate_example_images_base64(cls, value): + return cls._validate_image_array(value, 'exampleImagesBase64', 1) @validator('imagesUrls') - def validate_urls_array(cls, value): - if value is not None: - if not isinstance(value, (list, tuple)): - raise TypeError(f'Expect that type imagesUrls array will be or , got {type(value)}') - elif len(value) > 18: - raise NumbersImagesErrors(f'Maximum number of images in list 18, got {len(value)}') - elif not len(value): - raise ZeroImagesErrors(f'At least one image url expected, got {len(value)}') - # Check for each element type - contain_types = [isinstance(x, str) for x in value] - if not all(contain_types): - raise TypeError(f'Next images from imagesUrls array are not string: {contain_types}') - return value + def validate_images_urls(cls, value): + return cls._validate_image_array(value, 'imagesUrls', 18) @validator('imagesBase64') - def validate_images_array(cls, value): - if value is not None: - if not isinstance(value, (list, tuple)): - raise TypeError(f'Expect that type imagesBase64 array will be or , got {type(value)}') - elif len(value) > 18: - raise NumbersImagesErrors(f'Maximum number of images in list 18, got {len(value)}') - elif not len(value): - raise ZeroImagesErrors(f'At least one image base64 expected, got {len(value)}') - # Check for each element type - contain_types = [isinstance(x, str) for x in value] - if not all(contain_types): - raise TypeError(f'Next images from imagesBase64 array are not string: {contain_types}') - return value - + def validate_images_base64(cls, value): + return cls._validate_image_array(value, 'imagesBase64', 18) + def getTaskDict(self) -> Dict[str, Union[str, int, bool]]: task = {} @@ -86,7 +67,7 @@ def getTaskDict(self) -> Dict[str, Union[str, int, bool]]: # fill with images if self.imagesBase64 is None and self.imagesUrls is None: - raise ZeroImagesErrors(f'Expect at least one of array(imageBase64 or imageUrls) to contain images.') + raise ZeroImagesErrors('Expect at least one of array(imageBase64 or imageUrls) to contain images.') if self.imagesUrls is not None: task['imageUrls'] = self.imagesUrls @@ -113,4 +94,3 @@ def getTaskDict(self) -> Dict[str, Union[str, int, bool]]: return task - \ No newline at end of file