10BC0 Need a "clean" method for parameter validation · Issue #56 · python-amazon-mws/python-amazon-mws · GitHub
[go: up one dir, main page]

Skip to content
This repository was archived by the owner on Jan 4, 2025. It is now read-only.
This repository was archived by the owner on Jan 4, 2025. It is now read-only.

Need a "clean" method for parameter validation #56

@GriceTurrble

Description

@GriceTurrble

While writing some request method tests recently, I came across some mild confusion, conceptually speaking.

We perform a parameter validation for datetimes within the make_request method, here:

for key, value in extra_data.items():
if isinstance(value, (datetime 7923 .datetime, datetime.date)):
extra_data[key] = value.isoformat()

Aside from this, params are not validated much further. The next time they are touched in a similar fashion is inside calc_request_description:

for key, val in params.items():
encoded_val = quote(str(val), safe='-_.~')
description_items.append('{}={}'.format(key, encoded_val))
return '&'.join(sorted(description_items))

Here, the parameter value passes through str and urllib.parse.quote (on Python 2, urllib.quote). That str conversion is necessary to prevent errors when passing the value through quote, which is fine.


Problem: These two spots represent two different points of input validation that only A) convert datetime objects to strings in ISO-8601 format, and B) convert any other value to str (while not catching any exceptions that could be raised by str). While it isn't breaking much, it limits options for further validation, as well as adding some confusion as to whether and/or when any validation is being performed.


Solution: Would like to write a "clean" method that all parameters pass through, taking the "raw" parameters as input and returning a "cleaned" version of the params dict. Downstream code should be able to rely on every value of the parameter dict being a string or a useful exception being raised if a parameter value cannot be converted.

The "clean" method should be called inside the make_request method, in about the same spot as the current datetime validation code (the call to the "clean" method should replace that validation, in fact).


At time of writing, I'm busy with test methods, day job, and home stuff. If anyone else wants to pick this off, feel free to do so.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0