Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_download_file_unsupported_method_fails(create_api):
DOWNLOAD_FILE = "/home/omar/Documents/captions.txt"
youtube = create_api("youtube", "v3")
with pytest.raises(ValidationError) as e:
youtube.videos.list(
part="irrelevant", download_file=DOWNLOAD_FILE, validate=True
)
assert "download_file" in str(e)
def _build_upload_media(self, upload_file):
# Check if method supports media upload
# Will check wether validate is true or false
if self._method_specs.get('supportsMediaUpload') is not True:
raise ValidationError('upload_file was provided while method doesn\'t support media upload')
# If resumable, create resumable object
resumable = self._build_resumeable_media(upload_file) if _safe_getitem(self._method_specs, 'mediaUpload', 'protocols', 'resumable') else None
# Create MediaUpload object and pass it the resumable object we just created
simple_upload_path = _safe_getitem(self._method_specs, 'mediaUpload', 'protocols', 'simple', 'path') or ''
if simple_upload_path:
media_upload_url = self._root_url[:-1] + simple_upload_path
else:
media_upload_url = None
mime_range = _safe_getitem(self._method_specs, 'mediaUpload', 'accept')
multipart = self.mediaUpload['protocols']['simple'].get('multipart', False)
# Return
return MediaUpload(upload_file, upload_path=media_upload_url, mime_range=mime_range, multipart=multipart, resumable=resumable)
def double_validator(value, schema_name=None):
if not isinstance(value, float):
raise ValidationError(
make_validation_error_msg(value, "Double type", schema_name)
)
def _validate(self, instance, schema):
ref_resolved_schema = self._resolve_ref(schema, self._schemas)
try:
return validate_(instance, ref_resolved_schema, cls=Draft3Validator)
except ValidationError_ as e:
raise ValidationError(e)
for param_name, _ in passed_query_params.items():
del uri_params[param_name]
# Warn if not all uri_params were consumed/popped
if uri_params: # should be empty by now
# If there's room for addtionalProperties, validate and add them to the URI
if validate:
if self.parameters.get("additionalProperties"):
for _, v in uri_params.items():
self._validate(
v,
self.parameters["additionalProperties"],
schema_name="Additional Url Parameters",
)
else:
raise ValidationError(
f"Invalid (extra) parameters: {uri_params} were passed"
)
else:
if not self.parameters.get("additionalProperties"):
warnings.warn(
"Parameters {} were found and they're probably of no use."
" Check if they're valid parameters".format(str(uri_params))
)
if "?" not in uri:
uri = uri + "?" + urlencode(uri_params)
else:
uri = uri + "&" + urlencode(uri_params)
# Ensure only one param for http req body.
if json and data:
raise TypeError(
def null_validator(value, schema_name=None):
if value != "null":
raise ValidationError(
make_validation_error_msg(value, "'null' NOT None", schema_name)
)
if additional_properties is False or (
isinstance(additional_properties, dict) and len(additional_properties) == 0
):
additional_properties = None
if not isinstance(additional_properties, dict):
# Typically, additionalProperties should be either False, dict(schema) or empty.
# Empty will defailt to None, and False, as shown above, also defaults to None.
# Sometimes, it's a str. Strings shouldn't be schemas. Strings will be ignored. Bad strings..
# There's just too many of them to raise an error whenever we find one (around 75). So they'l just be ignored
# raise ValidationError(f'Invalid type of addiotional properties. Shoudl be either a dict or False')
additional_properties = None
# 2. Resolve properties
if not schema.get("properties"):
if not additional_properties:
raise ValidationError(
f"""
Invalid Schema: {str(schema)}.
Neither properties nor addiotional properties found in this schema"""
)
props = {}
else:
props = schema["properties"]
# 3. Raise warnings or fail on passed dict keys that aren't mentioned in the schema
for k, _ in instance.items():
if k not in props:
# If there's a schema for additional properties validate
if additional_properties is not None:
# some additional properties are misused and
# have many properties instead of just one.
# Basically, additionalProperties is sort of like **kwargs,
def date_validator(value, schema_name=None):
msg = make_validation_error_msg(
value,
"JSON date value. Hint: use datetime.date.isoformat(), instead of datetime.date",
schema_name,
)
try:
pvalue = rfc3339.parse_date(value)
# pvalue = datetime.date.fromisoformat(value)
except Exception as e:
raise ValidationError(str(e) + msg)
if not isinstance(pvalue, datetime.date):
raise ValidationError(msg)
def minimum_validator(value, minimum, schema_name=None):
if float(value) < float(minimum):
raise ValidationError(
make_validation_error_msg(value, f"Not less than {minimum}", schema_name)
)