Merge pull request #42 from khornberg/bug/params

Version: 0.9.1
This commit is contained in:
Kyle Hornberg
2018-10-05 10:10:59 -05:00
committed by GitHub
4 changed files with 42 additions and 5 deletions
+1 -1
View File
@@ -21,7 +21,7 @@ def read(*names, **kwargs):
setup(
name='octokitpy',
version='0.9.0',
version='0.9.1',
license='MIT license',
description='Python client for GitHub API',
long_description='%s\n%s' % (
+10 -4
View File
@@ -26,14 +26,20 @@ class Base(object):
def _validate(self, kwargs, params):
cached_kwargs = dict(ChainMap(kwargs, self._attribute_cache['url']))
required_params = [k for k, v in params.items() if v.get('required')]
for p in required_params:
if p not in cached_kwargs: # has all required
message = '{} is a required parameter'.format(p)
raise errors.OctokitParameterError(message)
self._validate_required_params(required_params, cached_kwargs)
for kwarg, value in kwargs.items():
param_value = params.get(kwarg)
self._validate_params(param_value, kwarg, value, required_params)
def _validate_required_params(self, required_params, cached_kwargs):
for p in required_params:
if '.' in p:
utils.walk_path(cached_kwargs, p.split('.'))
continue
if p not in cached_kwargs: # has all required
message = '{} is a required parameter'.format(p)
raise errors.OctokitParameterError(message)
def _validate_params(self, param_value, kwarg, value, required_params):
if not param_value: # is a valid param but not necessarily required
message = '{} is not a valid parameter for {}'.format(param_value, kwarg)
+7
View File
@@ -15,3 +15,10 @@ def get_json_data(filename):
def parameter_transform(params):
return {param['name']: param for param in params}
def walk_path(obj, path):
if len(path) == 1:
assert obj or obj[path[0]]
else:
walk_path(obj[path[0]], path[1:])
+24
View File
@@ -237,3 +237,27 @@ class TestClientMethods(object):
authorization_id=100, headers={'accept': 'application/vnd.github.ant-man-preview+json'}
)
requests.get.assert_called_once_with('https://api.github.com/authorizations/100', params={}, headers=headers)
def test_dictionary_keys_are_validated(self, mocker):
mocker.patch('requests.put')
headers = {'accept': 'application/vnd.github.v3+json', 'Content-Type': 'application/json'}
data = {
"required_status_checks": {
"strict": True,
"contexts": [],
},
"required_pull_request_reviews": {
"dismiss_stale_reviews": True
},
"enforce_admins": True,
"restrictions": {
"users": [],
"teams": [],
}
}
Octokit().repos.update_branch_protection(owner='user', repo='repo', branch='branch', **data)
requests.put.assert_called_with(
'https://api.github.com/repos/user/repo/branches/branch/protection',
data=json.dumps(data, sort_keys=True),
headers=headers
)