示例#1
0
文件: app.py 项目: globocom/impim-api
    def __init__(self, conf_file=None, *args, **kwargs):
        here = dirname(dirname(sys.argv[0]))
        self.config = Config.load(path=conf_file, conf_name="impim_api.conf",
            lookup_paths=[
                here,
                os.curdir,
                expanduser('~'),
                '/etc/',
                dirname(__file__),
            ])

        rest_api = TornadoRESTful(version='', base_url=self.config.APPLICATION_URL,
                discovery=True, cross_origin_enabled=True)
        rest_api.add_resource('alpha/images', ImagesResourceHandler)

        handlers = [
            (r'/healthcheck(?:/|\.html)?', HealthCheckHandler),
            (r'/thumbor_urls/?', JsonpEnabledThumborUrlHandler),
            (r'/alpha/images/(?P<key>.+)/.+', ImageHandler),
        ] + rest_api.get_url_mapping()

        super(ImagesApplication, self).__init__(handlers,
                thumbor_security_key=self.config.THUMBOR_SECURITY_KEY,
                thumbor_server_url=self.config.THUMBOR_SERVER_URL,
                *args,
                **kwargs)
示例#2
0
    def __init__(self, conf_file=None, *args, **kwargs):
        here = dirname(dirname(sys.argv[0]))
        self.config = Config.load(path=conf_file,
                                  conf_name="impim_api.conf",
                                  lookup_paths=[
                                      here,
                                      os.curdir,
                                      expanduser('~'),
                                      '/etc/',
                                      dirname(__file__),
                                  ])

        rest_api = TornadoRESTful(version='',
                                  base_url=self.config.APPLICATION_URL,
                                  discovery=True,
                                  cross_origin_enabled=True)
        rest_api.add_resource('alpha/images', ImagesResourceHandler)

        handlers = [
            (r'/healthcheck(?:/|\.html)?', HealthCheckHandler),
            (r'/thumbor_urls/?', JsonpEnabledThumborUrlHandler),
            (r'/alpha/images/(?P<key>.+)/.+', ImageHandler),
        ] + rest_api.get_url_mapping()

        super(ImagesApplication, self).__init__(
            handlers,
            thumbor_security_key=self.config.THUMBOR_SECURITY_KEY,
            thumbor_server_url=self.config.THUMBOR_SERVER_URL,
            *args,
            **kwargs)
示例#3
0
 def test_basic_info_about_api(self):
     api = TornadoRESTful(
             version='v1', base_url='http://api.images.globo.com')
     my_api = api.get_spec()
     assert isinstance(my_api, APISpecification)
     assert my_api.version == 'v1'
     assert my_api.base_url == 'http://api.images.globo.com'
     assert my_api.complete_url == 'http://api.images.globo.com/v1'
     assert my_api.resources == []
示例#4
0
 def test_basic_info_about_api(self):
     api = TornadoRESTful(
             version='v1', base_url='http://api.images.globo.com')
     my_api = api.get_spec()
     assert isinstance(my_api, APISpecification)
     assert my_api.version == 'v1'
     assert my_api.base_url == 'http://api.images.globo.com'
     assert my_api.complete_url == 'http://api.images.globo.com/v1'
     assert my_api.resources == []
示例#5
0
    def test_spec_with_optional_param(self):

        class HalfImplementedResource(ResourceHandler):

            @validate(querystring={optional('host'): unicode})
            def get_collection(self, *args, **kwargs):
                pass

        self.api = TornadoRESTful(
                version='v1', base_url='http://api.images.globo.com')
        self.api.add_resource('comments', HalfImplementedResource)
        my_api = self.api.get_spec()
        resource = my_api.resources[0]
        param = resource.paths[0].methods[0].params[0]
        assert param.required == False
示例#6
0
    def test_complete_resource_mapping(self):
        self.api = TornadoRESTful(
                version='v1', base_url='http://api.images.globo.com')
        self.api.add_resource('comments', ResourceWithDocumentation)
        my_api = self.api.get_spec()
        resource = my_api.resources[0]

        assert resource.name == 'comments'
        assert len(resource.paths) == 4

        path_names = [p.name for p in resource.paths]

        assert '/comments' in path_names
        assert '/comments.{type}' in path_names
        assert '/comments/{key}' in path_names
        assert '/comments/{key}.{type}' in path_names

        method_names = {}
        param_names = {}
        for path in resource.paths:
            method_names[path.name] = [m.name for m in path.methods]
            param_names[path.name] = [p.name for p in path.params]

        assert len(method_names['/comments']) == 2
        assert 'GET' in method_names['/comments']
        assert 'POST' in method_names['/comments']

        assert len(method_names['/comments.{type}']) == 2
        assert 'GET' in method_names['/comments.{type}']
        assert 'POST' in method_names['/comments.{type}']
        assert len(list(param_names['/comments.{type}'])) == 1
        assert 'type' in param_names['/comments.{type}']

        assert len(method_names['/comments/{key}']) == 3
        assert 'GET' in method_names['/comments/{key}']
        assert 'PUT' in method_names['/comments/{key}']
        assert 'DELETE' in method_names['/comments/{key}']
        assert len(list(param_names['/comments/{key}'])) == 1
        assert 'key' in param_names['/comments/{key}']

        assert len(method_names['/comments/{key}.{type}']) == 3
        assert 'GET' in method_names['/comments/{key}.{type}']
        assert 'PUT' in method_names['/comments/{key}.{type}']
        assert 'DELETE' in method_names['/comments/{key}.{type}']
        assert len(list(param_names['/comments/{key}.{type}'])) == 2
        assert 'type' in param_names['/comments/{key}.{type}']
        assert 'key' in param_names['/comments/{key}.{type}']
示例#7
0
    def test_spec_that_only_impl_create_model(self):

        class HalfImplementedResource(ResourceHandler):
            def create_model(self, *args, **kwargs):
                pass

        self.api = TornadoRESTful(
                version='v1', base_url='http://api.images.globo.com')
        self.api.add_resource('comments', HalfImplementedResource)
        my_api = self.api.get_spec()
        resource = my_api.resources[0]
        assert len(resource.paths) == 2
        assert resource.paths[0].name == '/comments'
        assert len(resource.paths[0].methods) == 1
        assert resource.paths[0].methods[0].name == 'POST'
        assert resource.paths[1].name == '/comments.{type}'
        assert len(resource.paths[1].methods) == 1
        assert resource.paths[1].methods[0].name == 'POST'
示例#8
0
    def test_spec_that_only_impl_get_collection(self):

        class HalfImplementedResource(ResourceHandler):
            def get_collection(self, callback):
                callback([])

        self.api = TornadoRESTful(
                version='v1', base_url='http://api.images.globo.com')
        self.api.add_resource('comments', HalfImplementedResource)
        my_api = self.api.get_spec()
        resource = my_api.resources[0]
        assert resource.name == 'comments'
        assert len(resource.paths) == 2
        assert resource.paths[0].name == '/comments'
        assert len(resource.paths[0].methods) == 1
        assert resource.paths[0].methods[0].name == 'GET'
        assert resource.paths[1].name == '/comments.{type}'
        assert len(resource.paths[1].methods) == 1
        assert resource.paths[1].methods[0].name == 'GET'
示例#9
0
    def test_spec_with_params(self):

        class HalfImplementedResource(ResourceHandler):

            @validate(querystring={'host': unicode})
            def get_collection(self, *args, **kwargs):
                pass

        self.api = TornadoRESTful(
                version='v1', base_url='http://api.images.globo.com')
        self.api.add_resource('comments', HalfImplementedResource)
        my_api = self.api.get_spec()
        resource = my_api.resources[0]
        assert resource.paths[0].methods[0].name == 'GET'
        assert len(resource.paths[0].methods[0].params) == 1
        param = resource.paths[0].methods[0].params[0]
        assert isinstance(param, Param)
        assert param.name == 'host'
        assert param.description == ''
        assert param.style == 'querystring'
示例#10
0
    def test_spec_with_optional_param(self):

        class HalfImplementedResource(ResourceHandler):

            @validate(querystring={Optional('host'): unicode})
            def get_collection(self, *args, **kwargs):
                pass

        self.api = TornadoRESTful(
                version='v1', base_url='http://api.images.globo.com')
        self.api.add_resource('comments', HalfImplementedResource)
        my_api = self.api.get_spec()
        resource = my_api.resources[0]
        param = resource.paths[0].methods[0].params[0]
        assert param.required == False
示例#11
0
    def test_complete_resource_mapping(self):
        self.api = TornadoRESTful(
                version='v1', base_url='http://api.images.globo.com')
        self.api.add_resource('comments', ResourceWithDocumentation)
        my_api = self.api.get_spec()
        resource = my_api.resources[0]

        assert resource.name == 'comments'
        assert len(resource.paths) == 4

        path_names = [p.name for p in resource.paths]

        assert '/comments' in path_names
        assert '/comments.{type}' in path_names
        assert '/comments/{key}' in path_names
        assert '/comments/{key}.{type}' in path_names

        method_names = {}
        param_names = {}
        for path in resource.paths:
            method_names[path.name] = [m.name for m in path.methods]
            param_names[path.name] = [p.name for p in path.params]

        assert len(method_names['/comments']) == 2
        assert 'GET' in method_names['/comments']
        assert 'POST' in method_names['/comments']

        assert len(method_names['/comments.{type}']) == 2
        assert 'GET' in method_names['/comments.{type}']
        assert 'POST' in method_names['/comments.{type}']
        assert len(list(param_names['/comments.{type}'])) == 1
        assert 'type' in param_names['/comments.{type}']

        assert len(method_names['/comments/{key}']) == 3
        assert 'GET' in method_names['/comments/{key}']
        assert 'PUT' in method_names['/comments/{key}']
        assert 'DELETE' in method_names['/comments/{key}']
        assert len(list(param_names['/comments/{key}'])) == 1
        assert 'key' in param_names['/comments/{key}']

        assert len(method_names['/comments/{key}.{type}']) == 3
        assert 'GET' in method_names['/comments/{key}.{type}']
        assert 'PUT' in method_names['/comments/{key}.{type}']
        assert 'DELETE' in method_names['/comments/{key}.{type}']
        assert len(list(param_names['/comments/{key}.{type}'])) == 2
        assert 'type' in param_names['/comments/{key}.{type}']
        assert 'key' in param_names['/comments/{key}.{type}']
示例#12
0
    def test_spec_that_only_impl_create_model(self):

        class HalfImplementedResource(ResourceHandler):
            def create_model(self, *args, **kwargs):
                pass

        self.api = TornadoRESTful(
                version='v1', base_url='http://api.images.globo.com')
        self.api.add_resource('comments', HalfImplementedResource)
        my_api = self.api.get_spec()
        resource = my_api.resources[0]
        assert len(resource.paths) == 2
        assert resource.paths[0].name == '/comments'
        assert len(resource.paths[0].methods) == 1
        assert resource.paths[0].methods[0].name == 'POST'
        assert resource.paths[1].name == '/comments.{type}'
        assert len(resource.paths[1].methods) == 1
        assert resource.paths[1].methods[0].name == 'POST'
示例#13
0
    def test_spec_that_only_impl_get_collection(self):

        class HalfImplementedResource(ResourceHandler):
            def get_collection(self, callback):
                callback([])

        self.api = TornadoRESTful(
                version='v1', base_url='http://api.images.globo.com')
        self.api.add_resource('comments', HalfImplementedResource)
        my_api = self.api.get_spec()
        resource = my_api.resources[0]
        assert resource.name == 'comments'
        assert len(resource.paths) == 2
        assert resource.paths[0].name == '/comments'
        assert len(resource.paths[0].methods) == 1
        assert resource.paths[0].methods[0].name == 'GET'
        assert resource.paths[1].name == '/comments.{type}'
        assert len(resource.paths[1].methods) == 1
        assert resource.paths[1].methods[0].name == 'GET'
示例#14
0
    def test_spec_with_params(self):

        class HalfImplementedResource(ResourceHandler):

            @validate(querystring={'host': unicode})
            def get_collection(self, *args, **kwargs):
                pass

        self.api = TornadoRESTful(
                version='v1', base_url='http://api.images.globo.com')
        self.api.add_resource('comments', HalfImplementedResource)
        my_api = self.api.get_spec()
        resource = my_api.resources[0]
        assert resource.paths[0].methods[0].name == 'GET'
        assert len(resource.paths[0].methods[0].params) == 1
        param = resource.paths[0].methods[0].params[0]
        assert isinstance(param, Param)
        assert param.name == 'host'
        assert param.description == ''
        assert param.style == 'querystring'
示例#15
0
 def get_app(self):
     api = TornadoRESTful()
     api.add_resource('projects', ProjectsResource)
     application = tornado.web.Application(api.get_url_mapping())
     return application
示例#16
0
#!/usr/local/bin/python2.7
# encoding: utf-8


import tornado.ioloop
import tornado.web

from tapioca import TornadoRESTful, ResourceHandler

class HelloResource(ResourceHandler):

    def get_collection(self, callback):
        callback("Hello, world!")

api = TornadoRESTful(discovery=True)
api.add_resource('hello', HelloResource)
application = tornado.web.Application(
    api.get_url_mapping()
)

if __name__ == "__main__":
    application.listen(8888)
    tornado.ioloop.IOLoop.instance().start()
	
	
示例#17
0
import tornado.ioloop
from tornado.httpserver import HTTPServer

from tapioca import TornadoRESTful, ResourceHandler, validate


class ProjectsHandler(ResourceHandler):

    @validate(querystring={'name': (unicode, 'name of project that do you want to search')})
    def get_collection(self, callback, *args, **kwargs):
        callback([{'params': self.values['querystring']}])


if __name__ == '__main__':
    import os
    port = int(os.environ.get('PORT', 5000))
    main_loop = tornado.ioloop.IOLoop.instance()

    api = TornadoRESTful(version='', \
            base_url='http://tapioqueiro.herokuapp.com', discovery=True, \
            cross_origin_enabled=True)
    api.add_resource('projects', ProjectsHandler)
    application = tornado.web.Application(api.get_url_mapping())

    server = HTTPServer(application)
    server.bind(port, '0.0.0.0')
    server.start(1)
    main_loop.start()
示例#18
0
 def get_app(self):
     api = TornadoRESTful()
     api.add_resource('api', ResourceHandler)
     application = tornado.web.Application(api.get_url_mapping())
     return application
示例#19
0
 def get_app(self):
     api = TornadoRESTful(cross_origin_enabled=True)
     api.add_resource('api', WithDefaultCallbackHandler)
     application = tornado.web.Application(api.get_url_mapping())
     return application
示例#20
0
#!/usr/local/bin/python2.7
# encoding: utf-8

import tornado.ioloop
import tornado.web

from tapioca import TornadoRESTful, ResourceHandler


class HelloResource(ResourceHandler):
    def get_collection(self, callback):
        callback("Hello, world!")


api = TornadoRESTful(discovery=True)
api.add_resource('hello', HelloResource)
application = tornado.web.Application(api.get_url_mapping())

if __name__ == "__main__":
    application.listen(8888)
    tornado.ioloop.IOLoop.instance().start()
示例#21
0
class ExtractInfoFromAPITestCase(TestCase):

    def test_basic_info_about_api(self):
        api = TornadoRESTful(
                version='v1', base_url='http://api.images.globo.com')
        my_api = api.get_spec()
        assert isinstance(my_api, APISpecification)
        assert my_api.version == 'v1'
        assert my_api.base_url == 'http://api.images.globo.com'
        assert my_api.complete_url == 'http://api.images.globo.com/v1'
        assert my_api.resources == []

    def test_complete_resource_mapping(self):
        self.api = TornadoRESTful(
                version='v1', base_url='http://api.images.globo.com')
        self.api.add_resource('comments', ResourceWithDocumentation)
        my_api = self.api.get_spec()
        resource = my_api.resources[0]

        assert resource.name == 'comments'
        assert len(resource.paths) == 4

        path_names = [p.name for p in resource.paths]

        assert '/comments' in path_names
        assert '/comments.{type}' in path_names
        assert '/comments/{key}' in path_names
        assert '/comments/{key}.{type}' in path_names

        method_names = {}
        param_names = {}
        for path in resource.paths:
            method_names[path.name] = [m.name for m in path.methods]
            param_names[path.name] = [p.name for p in path.params]

        assert len(method_names['/comments']) == 2
        assert 'GET' in method_names['/comments']
        assert 'POST' in method_names['/comments']

        assert len(method_names['/comments.{type}']) == 2
        assert 'GET' in method_names['/comments.{type}']
        assert 'POST' in method_names['/comments.{type}']
        assert len(list(param_names['/comments.{type}'])) == 1
        assert 'type' in param_names['/comments.{type}']

        assert len(method_names['/comments/{key}']) == 3
        assert 'GET' in method_names['/comments/{key}']
        assert 'PUT' in method_names['/comments/{key}']
        assert 'DELETE' in method_names['/comments/{key}']
        assert len(list(param_names['/comments/{key}'])) == 1
        assert 'key' in param_names['/comments/{key}']

        assert len(method_names['/comments/{key}.{type}']) == 3
        assert 'GET' in method_names['/comments/{key}.{type}']
        assert 'PUT' in method_names['/comments/{key}.{type}']
        assert 'DELETE' in method_names['/comments/{key}.{type}']
        assert len(list(param_names['/comments/{key}.{type}'])) == 2
        assert 'type' in param_names['/comments/{key}.{type}']
        assert 'key' in param_names['/comments/{key}.{type}']

    def test_spec_that_only_impl_get_collection(self):

        class HalfImplementedResource(ResourceHandler):
            def get_collection(self, callback):
                callback([])

        self.api = TornadoRESTful(
                version='v1', base_url='http://api.images.globo.com')
        self.api.add_resource('comments', HalfImplementedResource)
        my_api = self.api.get_spec()
        resource = my_api.resources[0]
        assert resource.name == 'comments'
        assert len(resource.paths) == 2
        assert resource.paths[0].name == '/comments'
        assert len(resource.paths[0].methods) == 1
        assert resource.paths[0].methods[0].name == 'GET'
        assert resource.paths[1].name == '/comments.{type}'
        assert len(resource.paths[1].methods) == 1
        assert resource.paths[1].methods[0].name == 'GET'

    def test_spec_that_only_impl_create_model(self):

        class HalfImplementedResource(ResourceHandler):
            def create_model(self, *args, **kwargs):
                pass

        self.api = TornadoRESTful(
                version='v1', base_url='http://api.images.globo.com')
        self.api.add_resource('comments', HalfImplementedResource)
        my_api = self.api.get_spec()
        resource = my_api.resources[0]
        assert len(resource.paths) == 2
        assert resource.paths[0].name == '/comments'
        assert len(resource.paths[0].methods) == 1
        assert resource.paths[0].methods[0].name == 'POST'
        assert resource.paths[1].name == '/comments.{type}'
        assert len(resource.paths[1].methods) == 1
        assert resource.paths[1].methods[0].name == 'POST'

    def test_spec_that_only_impl_get_model(self):

        class HalfImplementedResource(ResourceHandler):
            def get_model(self, *args, **kwargs):
                pass

        self.api = TornadoRESTful(
                version='v1', base_url='http://api.images.globo.com')
        self.api.add_resource('comments', HalfImplementedResource)
        my_api = self.api.get_spec()
        resource = my_api.resources[0]
        assert len(resource.paths) == 2
        assert resource.paths[0].name == '/comments/{key}'
        assert len(resource.paths[0].methods) == 1
        assert resource.paths[0].methods[0].name == 'GET'
        assert resource.paths[1].name == '/comments/{key}.{type}'
        assert len(resource.paths[1].methods) == 1
        assert resource.paths[1].methods[0].name == 'GET'

    def test_spec_that_only_impl_update_model(self):

        class HalfImplementedResource(ResourceHandler):
            def update_model(self, *args, **kwargs):
                """Updates the resource."""
                pass

        self.api = TornadoRESTful(
                version='v1', base_url='http://api.images.globo.com')
        self.api.add_resource('comments', HalfImplementedResource)
        my_api = self.api.get_spec()
        resource = my_api.resources[0]
        assert len(resource.paths) == 2
        assert resource.paths[0].name == '/comments/{key}'
        assert len(resource.paths[0].methods) == 1
        assert resource.paths[0].methods[0].name == 'PUT'
        assert resource.paths[0].methods[0].description == 'Updates the resource.'
        assert resource.paths[1].name == '/comments/{key}.{type}'
        assert len(resource.paths[1].methods) == 1
        assert resource.paths[1].methods[0].name == 'PUT'

    def test_spec_that_only_impl_delete_model(self):

        class HalfImplementedResource(ResourceHandler):
            def delete_model(self, *args, **kwargs):
                pass

        self.api = TornadoRESTful(
                version='v1', base_url='http://api.images.globo.com')
        self.api.add_resource('comments', HalfImplementedResource)
        my_api = self.api.get_spec()
        resource = my_api.resources[0]
        assert len(resource.paths) == 2
        assert resource.paths[0].name == '/comments/{key}'
        assert len(resource.paths[0].methods) == 1
        assert resource.paths[0].methods[0].name == 'DELETE'
        assert resource.paths[1].name == '/comments/{key}.{type}'
        assert len(resource.paths[1].methods) == 1
        assert resource.paths[1].methods[0].name == 'DELETE'

    def test_spec_with_params(self):

        class HalfImplementedResource(ResourceHandler):

            @validate(querystring={'host': unicode})
            def get_collection(self, *args, **kwargs):
                pass

        self.api = TornadoRESTful(
                version='v1', base_url='http://api.images.globo.com')
        self.api.add_resource('comments', HalfImplementedResource)
        my_api = self.api.get_spec()
        resource = my_api.resources[0]
        assert resource.paths[0].methods[0].name == 'GET'
        assert len(resource.paths[0].methods[0].params) == 1
        param = resource.paths[0].methods[0].params[0]
        assert isinstance(param, Param)
        assert param.name == 'host'
        assert param.description == ''
        assert param.style == 'querystring'

    def test_spec_with_optional_param(self):

        class HalfImplementedResource(ResourceHandler):

            @validate(querystring={Optional('host'): unicode})
            def get_collection(self, *args, **kwargs):
                pass

        self.api = TornadoRESTful(
                version='v1', base_url='http://api.images.globo.com')
        self.api.add_resource('comments', HalfImplementedResource)
        my_api = self.api.get_spec()
        resource = my_api.resources[0]
        param = resource.paths[0].methods[0].params[0]
        assert param.required == False
示例#22
0
 def get_app(self):
     api = TornadoRESTful()
     api.add_resource('always_valid', NoAccessValuesInQuerystring)
     application = tornado.web.Application(api.get_url_mapping())
     return application
示例#23
0
 def setUp(self):
     self.api = TornadoRESTful()
示例#24
0
 def get_app(self):
     api = TornadoRESTful(version='v1',
                          base_url='http://api.tapioca.com',
                          discovery=True)
     api.add_resource('comments', ResourceWithDocumentation)
     return tornado.web.Application(api.get_url_mapping())
示例#25
0
 def get_app(self):
     api = TornadoRESTful(cross_origin_enabled=True)
     api.add_resource('api', WithDefaultCallbackHandler)
     application = tornado.web.Application(api.get_url_mapping())
     return application
示例#26
0
 def get_app(self):
     api = TornadoRESTful()
     api.add_resource('api', ResourceHandler)
     application = tornado.web.Application(api.get_url_mapping())
     return application
示例#27
0
 def get_app(self):
     api = TornadoRESTful(
             version='v1', base_url='http://api.tapioca.com', discovery=True)
     api.add_resource('comments', ResourceWithDocumentation)
     return tornado.web.Application(api.get_url_mapping())
示例#28
0
 def get_app(self):
     api = TornadoRESTful(version='v1', base_url='http://api.tapioca.com')
     api.add_resource('api', FullTestHandler)
     application = tornado.web.Application(api.get_url_mapping())
     return application
示例#29
0
class TestApiManager(TestCase):

    def setUp(self):
        self.api = TornadoRESTful()

    def test_should_be_possible_to_add_a_handler(self):
        self.api.add_resource('api', ResourceHandler)
        assert ('/api/?', ResourceHandler) in self.api.get_url_mapping()

    def test_should_generate_a_path_for_access_direcly_an_instance(self):
        self.api.add_resource('comment', ResourceHandler)
        assert ('/comment/(?P<key>.+)/?', ResourceHandler) in \
                self.api.get_url_mapping()

    def test_should_generate_path_to_handler_return_type_specification(self):
        self.api.add_resource('comment', ResourceHandler)
        assert ('/comment\.(?P<force_return_type>\w+)', ResourceHandler) in \
                self.api.get_url_mapping()

    def test_should_generate_path_to_handler_return_type_specification(self):
        self.api.add_resource('comment', ResourceHandler)
        assert ('/comment/(?P<key>[^.]+)\.(?P<force_return_type>\w+)', ResourceHandler) in \
                self.api.get_url_mapping()
示例#30
0
 def get_app(self):
     api = TornadoRESTful(version='v1', base_url='http://api.tapioca.com')
     api.add_resource('api', FullTestHandler)
     application = tornado.web.Application(api.get_url_mapping())
     return application
示例#31
0
class ExtractInfoFromAPITestCase(TestCase):

    def test_basic_info_about_api(self):
        api = TornadoRESTful(
                version='v1', base_url='http://api.images.globo.com')
        my_api = api.get_spec()
        assert isinstance(my_api, APISpecification)
        assert my_api.version == 'v1'
        assert my_api.base_url == 'http://api.images.globo.com'
        assert my_api.complete_url == 'http://api.images.globo.com/v1'
        assert my_api.resources == []

    def test_complete_resource_mapping(self):
        self.api = TornadoRESTful(
                version='v1', base_url='http://api.images.globo.com')
        self.api.add_resource('comments', ResourceWithDocumentation)
        my_api = self.api.get_spec()
        resource = my_api.resources[0]

        assert resource.name == 'comments'
        assert len(resource.paths) == 4

        path_names = [p.name for p in resource.paths]

        assert '/comments' in path_names
        assert '/comments.{type}' in path_names
        assert '/comments/{key}' in path_names
        assert '/comments/{key}.{type}' in path_names

        method_names = {}
        param_names = {}
        for path in resource.paths:
            method_names[path.name] = [m.name for m in path.methods]
            param_names[path.name] = [p.name for p in path.params]

        assert len(method_names['/comments']) == 2
        assert 'GET' in method_names['/comments']
        assert 'POST' in method_names['/comments']

        assert len(method_names['/comments.{type}']) == 2
        assert 'GET' in method_names['/comments.{type}']
        assert 'POST' in method_names['/comments.{type}']
        assert len(list(param_names['/comments.{type}'])) == 1
        assert 'type' in param_names['/comments.{type}']

        assert len(method_names['/comments/{key}']) == 3
        assert 'GET' in method_names['/comments/{key}']
        assert 'PUT' in method_names['/comments/{key}']
        assert 'DELETE' in method_names['/comments/{key}']
        assert len(list(param_names['/comments/{key}'])) == 1
        assert 'key' in param_names['/comments/{key}']

        assert len(method_names['/comments/{key}.{type}']) == 3
        assert 'GET' in method_names['/comments/{key}.{type}']
        assert 'PUT' in method_names['/comments/{key}.{type}']
        assert 'DELETE' in method_names['/comments/{key}.{type}']
        assert len(list(param_names['/comments/{key}.{type}'])) == 2
        assert 'type' in param_names['/comments/{key}.{type}']
        assert 'key' in param_names['/comments/{key}.{type}']

    def test_spec_that_only_impl_get_collection(self):

        class HalfImplementedResource(ResourceHandler):
            def get_collection(self, callback):
                callback([])

        self.api = TornadoRESTful(
                version='v1', base_url='http://api.images.globo.com')
        self.api.add_resource('comments', HalfImplementedResource)
        my_api = self.api.get_spec()
        resource = my_api.resources[0]
        assert resource.name == 'comments'
        assert len(resource.paths) == 2
        assert resource.paths[0].name == '/comments'
        assert len(resource.paths[0].methods) == 1
        assert resource.paths[0].methods[0].name == 'GET'
        assert resource.paths[1].name == '/comments.{type}'
        assert len(resource.paths[1].methods) == 1
        assert resource.paths[1].methods[0].name == 'GET'

    def test_spec_that_only_impl_create_model(self):

        class HalfImplementedResource(ResourceHandler):
            def create_model(self, *args, **kwargs):
                pass

        self.api = TornadoRESTful(
                version='v1', base_url='http://api.images.globo.com')
        self.api.add_resource('comments', HalfImplementedResource)
        my_api = self.api.get_spec()
        resource = my_api.resources[0]
        assert len(resource.paths) == 2
        assert resource.paths[0].name == '/comments'
        assert len(resource.paths[0].methods) == 1
        assert resource.paths[0].methods[0].name == 'POST'
        assert resource.paths[1].name == '/comments.{type}'
        assert len(resource.paths[1].methods) == 1
        assert resource.paths[1].methods[0].name == 'POST'

    def test_spec_that_only_impl_get_model(self):

        class HalfImplementedResource(ResourceHandler):
            def get_model(self, *args, **kwargs):
                pass

        self.api = TornadoRESTful(
                version='v1', base_url='http://api.images.globo.com')
        self.api.add_resource('comments', HalfImplementedResource)
        my_api = self.api.get_spec()
        resource = my_api.resources[0]
        assert len(resource.paths) == 2
        assert resource.paths[0].name == '/comments/{key}'
        assert len(resource.paths[0].methods) == 1
        assert resource.paths[0].methods[0].name == 'GET'
        assert resource.paths[1].name == '/comments/{key}.{type}'
        assert len(resource.paths[1].methods) == 1
        assert resource.paths[1].methods[0].name == 'GET'

    def test_spec_that_only_impl_update_model(self):

        class HalfImplementedResource(ResourceHandler):
            def update_model(self, *args, **kwargs):
                """Updates the resource."""
                pass

        self.api = TornadoRESTful(
                version='v1', base_url='http://api.images.globo.com')
        self.api.add_resource('comments', HalfImplementedResource)
        my_api = self.api.get_spec()
        resource = my_api.resources[0]
        assert len(resource.paths) == 2
        assert resource.paths[0].name == '/comments/{key}'
        assert len(resource.paths[0].methods) == 1
        assert resource.paths[0].methods[0].name == 'PUT'
        assert resource.paths[0].methods[0].description == 'Updates the resource.'
        assert resource.paths[1].name == '/comments/{key}.{type}'
        assert len(resource.paths[1].methods) == 1
        assert resource.paths[1].methods[0].name == 'PUT'

    def test_spec_that_only_impl_delete_model(self):

        class HalfImplementedResource(ResourceHandler):
            def delete_model(self, *args, **kwargs):
                pass

        self.api = TornadoRESTful(
                version='v1', base_url='http://api.images.globo.com')
        self.api.add_resource('comments', HalfImplementedResource)
        my_api = self.api.get_spec()
        resource = my_api.resources[0]
        assert len(resource.paths) == 2
        assert resource.paths[0].name == '/comments/{key}'
        assert len(resource.paths[0].methods) == 1
        assert resource.paths[0].methods[0].name == 'DELETE'
        assert resource.paths[1].name == '/comments/{key}.{type}'
        assert len(resource.paths[1].methods) == 1
        assert resource.paths[1].methods[0].name == 'DELETE'

    def test_spec_with_params(self):

        class HalfImplementedResource(ResourceHandler):

            @validate(querystring={'host': unicode})
            def get_collection(self, *args, **kwargs):
                pass

        self.api = TornadoRESTful(
                version='v1', base_url='http://api.images.globo.com')
        self.api.add_resource('comments', HalfImplementedResource)
        my_api = self.api.get_spec()
        resource = my_api.resources[0]
        assert resource.paths[0].methods[0].name == 'GET'
        assert len(resource.paths[0].methods[0].params) == 1
        param = resource.paths[0].methods[0].params[0]
        assert isinstance(param, Param)
        assert param.name == 'host'
        assert param.description == ''
        assert param.style == 'querystring'

    def test_spec_with_optional_param(self):

        class HalfImplementedResource(ResourceHandler):

            @validate(querystring={optional('host'): unicode})
            def get_collection(self, *args, **kwargs):
                pass

        self.api = TornadoRESTful(
                version='v1', base_url='http://api.images.globo.com')
        self.api.add_resource('comments', HalfImplementedResource)
        my_api = self.api.get_spec()
        resource = my_api.resources[0]
        param = resource.paths[0].methods[0].params[0]
        assert param.required == False
示例#32
0
 def get_app(self):
     api = TornadoRESTful()
     api.add_resource('projects', ProjectsResource)
     application = tornado.web.Application(api.get_url_mapping())
     return application