-
Notifications
You must be signed in to change notification settings - Fork 207
Need a "clean" method for parameter validation #56
Description
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:
Lines 249 to 251 in 7dd4a50
| 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:
Lines 81 to 84 in 7dd4a50
| 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.