Created
August 9, 2016 15:04
-
-
Save mermoldy/59cb098e49ac801b9e39dacabaada005 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import yaml | |
import json | |
import click | |
import requests | |
def load_user_spec(api_version): | |
resp = requests.get('https://my.scalr.net/api/user.{}.yml' | |
.format(api_version)) | |
resp_dict = yaml.load(resp.text) | |
if 'errorMessage' in resp_dict: | |
raise Exception(resp_dict['errorMessage']) | |
else: | |
return resp_dict | |
def recursive_get(d, key): | |
if type(d) != dict: | |
return | |
head, _, tail = key.partition('.') | |
h_value = d.get(head) | |
if tail and type(h_value) == dict: | |
return recursive_get(h_value, tail) | |
if not tail: | |
return h_value | |
def item_by_ref(spec_data, ref): | |
key = '.'.join(ref.split('/')[1:]) | |
return recursive_get(spec_data, key) | |
def get_params(spec_data, schema): | |
params = {} | |
if '$ref' in schema: | |
schema = item_by_ref(spec_data, schema['$ref']) | |
if 'properties' in schema: | |
for p_key, p_value in schema['properties'].items(): | |
if '$ref' in p_value: | |
sub_item = item_by_ref(spec_data, p_value['$ref']) | |
params[p_key] = get_params(spec_data, sub_item) | |
else: | |
params[p_key] = p_value['enum'][0] if 'enum' in p_value \ | |
else p_value['type'] | |
return params | |
def user_post_data(endpoint): | |
"""Generate POST data for specified user API endpoint. | |
:param endpoint: /api/vN/user/{envId}/' | |
:return: post data | |
""" | |
api_version = endpoint.split('/')[2] | |
spec_data = load_user_spec(api_version) | |
base_path = spec_data['basePath'] | |
if endpoint.startswith(base_path): | |
endpoint = endpoint.replace(base_path, '', 1) | |
else: | |
raise Exception('API\'s basePath: {} and endpoint does not match' | |
.format(base_path)) | |
if endpoint in spec_data['paths']: | |
params_spec = spec_data['paths'].get(endpoint) | |
else: | |
raise Exception('API endpoint {} does not found'.format(endpoint)) | |
if 'post' in params_spec: | |
if 'parameters' in params_spec['post']: | |
schema = params_spec['post']['parameters'][0]['schema'] | |
post_data = get_params(spec_data, schema) | |
else: | |
post_data = {} | |
else: | |
raise Exception('POST method for endpoint {} does not exist' | |
.format(endpoint)) | |
return post_data | |
''' | |
def test_spec(api_version): | |
spec_data = load_user_spec(api_version) | |
for endpoint, params_spec in spec_data['paths'].items(): | |
if 'post' in params_spec: | |
if 'parameters' in params_spec['post']: | |
assert len(params_spec['post']['parameters']) == 1 | |
assert 'schema' in params_spec['post']['parameters'][0] | |
''' | |
if __name__ == '__main__': | |
data = user_post_data(endpoint='/api/v1beta0/user/{envId}/images/') | |
click.echo(json.dumps(data, indent=2)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment