def test_possible_specify_simple_api(self): api = APISpecification(version='', base_url='') api.add_resource( Resource('comments', paths=[ Path('/comments.{key}', params=[ Param('key', default_value='json', required=False, options=['json', 'js', 'xml']), ], methods=[ Method('GET', errors=[ APIError(code=301, description=''), APIError(code=404, description='') ]), Method('POST', errors=[ APIError(code=301, description=''), APIError(code=404, description='') ]), ]), ])) resource = api.resources[0] assert resource.paths[0].name == '/comments.{key}' assert resource.paths[0].params[0].name == 'key' assert resource.paths[0].params[0].default_value == 'json' assert resource.paths[0].params[0].required == False assert resource.paths[0].params[0].options == ['json', 'js', 'xml'] assert resource.paths[0].methods[0].name == 'GET' assert resource.paths[0].methods[1].name == 'POST'
def test_generate_with_params(self): api = APISpecification(version='v1', base_url='http://api.globo.com') api.add_resource( Resource('dogs', paths=[ Path('/dogs/{key}', params=[Param('key')], methods=[Method('POST'), Method('GET')]), ])) result = self.gen(api) doc = ElementTree.fromstring(result) resources = doc.getchildren()[0] resource = resources.getchildren()[0] param = resource.getchildren()[0] assert param.tag.endswith('param') assert param.get('name') == 'key' assert param.get('required') == 'true' assert param.get('type') == 'xsd:string' assert param.get('style') == 'template' method_1 = resource.getchildren()[1] assert method_1.tag.endswith('method') assert method_1.get('name') == 'POST' method_2 = resource.getchildren()[2] assert method_2.tag.endswith('method') assert method_2.get('name') == 'GET'
def test_possible_specify_simple_api(self): api = APISpecification(version='', base_url='') api.add_resource(Resource('comments', paths=[ Path('/comments.{key}', params=[ Param('key', default_value='json', required=False, options=[ 'json', 'js', 'xml' ]), ], methods=[ Method('GET', errors=[ APIError(code=301, description=''), APIError(code=404, description='') ]), Method('POST', errors=[ APIError(code=301, description=''), APIError(code=404, description='') ]), ] ), ] )) resource = api.resources[0] assert resource.paths[0].name == '/comments.{key}' assert resource.paths[0].params[0].name == 'key' assert resource.paths[0].params[0].default_value == 'json' assert resource.paths[0].params[0].required == False assert resource.paths[0].params[0].options == ['json', 'js', 'xml'] assert resource.paths[0].methods[0].name == 'GET' assert resource.paths[0].methods[1].name == 'POST'
def test_possible_add_resource(self): spec = APISpecification(version='v1', base_url='http://api.glb.com') resource = Resource('comments') resource.add_path(Path('/comments')) spec.add_resource(resource) assert len(spec.resources) == 1 assert len(resource.paths) == 1 assert resource.name == 'comments' assert resource.paths[0].name == '/comments'
def test_gen_spec_generic_with_resource(self): api = APISpecification(version='v1', base_url='http://api.globo.com') api.add_resource( Resource('comments', paths=[ Path('/comments', methods=[Method('GET')]) ])) result = self.apply_generation(api) assert len(result['apis']) == 1 assert result['apis'][0]['path'] == '/discovery/comments.swagger' assert result['apis'][0]['description'] == ''
def test_generate_with_resource(self): api = APISpecification(version="v1", base_url="http://api.globo.com") api.add_resource(Resource("comments", paths=[Path("/comments", methods=[Method("GET")])])) result = self.gen(api) doc = ElementTree.fromstring(result) assert doc.tag.endswith("application") resources = doc.getchildren()[0] assert resources.tag.endswith("resources") assert resources.get("base") == "http://api.globo.com/v1" resource = resources.getchildren()[0] assert resource.tag.endswith("resource") assert resource.get("path") == "/comments" method = resource.getchildren()[0] assert method.tag.endswith("method") assert method.get("name") == "GET"
def test_gen_spec_for_put_method(self): api = APISpecification(version='v1', base_url='http://api.globo.com') api.add_resource(Resource('dogs', paths=[ Path('/dogs', methods=[ Method('PUT')])])) api.add_resource(Resource('cats', paths=[ Path('/cats', methods=[ Method('PUT')])])) result = self.apply_generation(api, 'cats') assert result['resourcePath'] == '/cats' assert result['apis'][0]['path'] == '/cats' operation = result['apis'][0]['operations'][0] assert operation['httpMethod'] == 'PUT' assert operation['nickname'] == 'put_cats'
def test_generate_with_resource(self): api = APISpecification(version='v1', base_url='http://api.globo.com') api.add_resource( Resource('comments', paths=[Path('/comments', methods=[Method('GET')])])) result = self.gen(api) doc = ElementTree.fromstring(result) assert doc.tag.endswith('application') resources = doc.getchildren()[0] assert resources.tag.endswith('resources') assert resources.get('base') == 'http://api.globo.com/v1' resource = resources.getchildren()[0] assert resource.tag.endswith('resource') assert resource.get('path') == '/comments' method = resource.getchildren()[0] assert method.tag.endswith('method') assert method.get('name') == 'GET'
def test_genetate_with_more_than_one_resource(self): api = APISpecification(version="v1", base_url="http://api.globo.com") api.add_resource(Resource("dogs", paths=[Path("/dogs", methods=[Method("PUT")])])) api.add_resource(Resource("cats", paths=[Path("/cats", methods=[Method("PUT")])])) result = self.gen(api) doc = ElementTree.fromstring(result) assert doc.tag.endswith("application") resource = doc.getchildren()[0] resources = resource.getchildren() assert len(resources) == 2 assert resources[0].tag.endswith("resource") assert resources[0].get("path") == "/dogs" method = resources[0].getchildren()[0] assert method.tag.endswith("method") assert method.get("name") == "PUT" assert resources[1].tag.endswith("resource") assert resources[1].get("path") == "/cats" method = resources[1].getchildren()[0] assert method.tag.endswith("method") assert method.get("name") == "PUT"
def test_gen_spec_for_a_resource(self): api = APISpecification(version='v1', base_url='http://api.globo.com') resource_name = 'comments' api.add_resource( Resource(resource_name, paths=[ Path('/comments', methods=[Method('GET')]) ])) result = self.apply_generation(api, resource_name) assert result['resourcePath'] == '/comments' assert len(result['apis']) == 1 assert result['apis'][0]['path'] == '/comments' assert result['apis'][0]['description'] == '' assert len(result['apis'][0]['operations']) == 1 operation = result['apis'][0]['operations'][0] assert operation['httpMethod'] == 'GET' assert operation['nickname'] == 'get_comments' assert operation['parameters'] == [] assert operation['summary'] == '' assert operation['notes'] == '' assert operation['errorResponses'] == []
def test_generate_with_params(self): api = APISpecification(version="v1", base_url="http://api.globo.com") api.add_resource( Resource( "dogs", paths=[Path("/dogs/{key}", params=[Param("key")], methods=[Method("POST"), Method("GET")])] ) ) result = self.gen(api) doc = ElementTree.fromstring(result) resources = doc.getchildren()[0] resource = resources.getchildren()[0] param = resource.getchildren()[0] assert param.tag.endswith("param") assert param.get("name") == "key" assert param.get("required") == "true" assert param.get("type") == "xsd:string" assert param.get("style") == "template" method_1 = resource.getchildren()[1] assert method_1.tag.endswith("method") assert method_1.get("name") == "POST" method_2 = resource.getchildren()[2] assert method_2.tag.endswith("method") assert method_2.get("name") == "GET"
def test_gen_spec_with_params(self): api = APISpecification(version='v1', base_url='http://api.globo.com') api.add_resource(Resource('dogs', paths=[ Path('/dogs/{key}', params=[ Param('key') ], methods=[ Method('GET') ] ), ]) ) result = self.apply_generation(api, 'dogs') assert result['apis'][0]['path'] == '/dogs/{key}' operation = result['apis'][0]['operations'][0] assert len(operation['parameters']) == 1 assert operation['parameters'][0]['name'] == 'key' assert operation['parameters'][0]['paramType'] == 'path' assert operation['parameters'][0]['description'] == '' assert operation['parameters'][0]['dataType'] == 'String' assert operation['parameters'][0]['required'] == True assert operation['parameters'][0]['allowMultiple'] == False
def test_genetate_with_more_than_one_resource(self): api = APISpecification(version='v1', base_url='http://api.globo.com') api.add_resource( Resource('dogs', paths=[Path('/dogs', methods=[Method('PUT')])])) api.add_resource( Resource('cats', paths=[Path('/cats', methods=[Method('PUT')])])) result = self.gen(api) doc = ElementTree.fromstring(result) assert doc.tag.endswith('application') resource = doc.getchildren()[0] resources = resource.getchildren() assert len(resources) == 2 assert resources[0].tag.endswith('resource') assert resources[0].get('path') == '/dogs' method = resources[0].getchildren()[0] assert method.tag.endswith('method') assert method.get('name') == 'PUT' assert resources[1].tag.endswith('resource') assert resources[1].get('path') == '/cats' method = resources[1].getchildren()[0] assert method.tag.endswith('method') assert method.get('name') == 'PUT'
class Metadata(object): def __init__(self, version=None, base_url=None): self.spec = APISpecification(version=version, base_url=base_url) def add(self, path, handler): resource = Resource(path) basic_methods = list(self.get_basic_methods(handler)) if basic_methods: resource.add_path(Path('/{0}'.format(path), methods=basic_methods)) resource.add_path( Path('/{0}.{{type}}'.format(path), params=[Param('type', style='url')], methods=basic_methods)) instance_methods = list(self.get_instance_methods(handler)) if instance_methods: resource.add_path( Path('/{0}/{{key}}'.format(path), params=[Param('key', style='url')], methods=instance_methods)) resource.add_path( Path('/{0}/{{key}}.{{type}}'.format(path), params=[ Param('key', style='url'), Param('type', style='url') ], methods=instance_methods)) self.spec.add_resource(resource) def get_basic_methods(self, handler): return self.introspect_methods(GET=handler.get_collection, POST=handler.create_model) def is_overridden(self, method): return not hasattr(method, 'original') def get_instance_methods(self, handler): return self.introspect_methods(GET=handler.get_model, PUT=handler.update_model, DELETE=handler.delete_model) def introspect_methods(self, **mapping): for method_type, implementation in mapping.items(): if self.is_overridden(implementation): params = self.introspect_params(implementation) yield Method(method_type, params=params, description=implementation.__doc__) def introspect_params(self, method): params = [] if hasattr(method, 'request_schema'): request_schema = method.request_schema if hasattr(request_schema, 'querystring'): for param in request_schema.querystring_params(): params.append( Param(param.name, required=not param.is_optional, style='querystring', description=param.description)) return params
class Metadata(object): def __init__(self, version=None, base_url=None): self.spec = APISpecification(version=version, base_url=base_url) def add(self, path, handler): resource = Resource(path) basic_methods = list(self.get_basic_methods(handler)) if basic_methods: resource.add_path( Path('/{0}'.format(path), methods=basic_methods)) resource.add_path( Path('/{0}.{{type}}'.format(path), params=[Param('type', style='url')], methods=basic_methods)) instance_methods = list(self.get_instance_methods(handler)) if instance_methods: resource.add_path( Path('/{0}/{{key}}'.format(path), params=[Param('key', style='url')], methods=instance_methods)) resource.add_path( Path('/{0}/{{key}}.{{type}}'.format(path), params=[ Param('key', style='url'), Param('type', style='url') ], methods=instance_methods)) self.spec.add_resource(resource) def get_basic_methods(self, handler): return self.introspect_methods( GET=handler.get_collection, POST=handler.create_model) def is_overridden(self, method): return not hasattr(method, 'original') def get_instance_methods(self, handler): return self.introspect_methods( GET=handler.get_model, PUT=handler.update_model, DELETE=handler.delete_model) def introspect_methods(self, **mapping): for method_type, implementation in mapping.items(): if self.is_overridden(implementation): params = self.introspect_params(implementation) yield Method(method_type, params=params, description=implementation.__doc__) def introspect_params(self, method): params = [] if hasattr(method, 'request_schema'): request_schema = method.request_schema if hasattr(request_schema, 'querystring'): for param in request_schema.querystring_params(): params.append( Param( param.name, required=not param.is_optional, style='querystring', description=param.description ) ) return params
def __init__(self, version=None, base_url=None): self.spec = APISpecification(version=version, base_url=base_url)
def test_generate_basic_spec(self): api = APISpecification(version='v1', base_url='http://api.globo.com') result = self.gen(api) assert '<application xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:apigee="http://api.apigee.com/wadl/2010/07/" xmlns="http://wadl.dev.java.net/2009/02" xsi:schemaLocation="http://wadl.dev.java.net/2009/02 http://apigee.com/schemas/wadl-schema.xsd http://api.apigee.com/wadl/2010/07/ http://apigee.com/schemas/apigee-wadl-extensions.xsd">' in result, result
def test_create_spec(self): spec = APISpecification(version='v1', base_url='http://api.glb.com') assert spec.version == 'v1' assert spec.base_url == 'http://api.glb.com' assert spec.complete_url == 'http://api.glb.com/v1' assert spec.resources == []