Esempio n. 1
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)
Esempio n. 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)
Esempio n. 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 == []
Esempio n. 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 == []
Esempio n. 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
Esempio n. 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}']
Esempio n. 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'
Esempio n. 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'
Esempio n. 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'
Esempio n. 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
Esempio n. 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}']
Esempio n. 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'
Esempio n. 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'
Esempio n. 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'
Esempio n. 15
0
 def get_app(self):
     api = TornadoRESTful()
     api.add_resource('projects', ProjectsResource)
     application = tornado.web.Application(api.get_url_mapping())
     return application
Esempio n. 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()
	
	
Esempio n. 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()
Esempio n. 18
0
 def get_app(self):
     api = TornadoRESTful()
     api.add_resource('api', ResourceHandler)
     application = tornado.web.Application(api.get_url_mapping())
     return application
Esempio n. 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
Esempio n. 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()
Esempio n. 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
Esempio n. 22
0
 def get_app(self):
     api = TornadoRESTful()
     api.add_resource('always_valid', NoAccessValuesInQuerystring)
     application = tornado.web.Application(api.get_url_mapping())
     return application
Esempio n. 23
0
 def setUp(self):
     self.api = TornadoRESTful()
Esempio n. 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())
Esempio n. 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
Esempio n. 26
0
 def get_app(self):
     api = TornadoRESTful()
     api.add_resource('api', ResourceHandler)
     application = tornado.web.Application(api.get_url_mapping())
     return application
Esempio n. 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())
Esempio n. 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
Esempio n. 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()
Esempio n. 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
Esempio n. 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
Esempio n. 32
0
 def get_app(self):
     api = TornadoRESTful()
     api.add_resource('projects', ProjectsResource)
     application = tornado.web.Application(api.get_url_mapping())
     return application