Example #1
0
    def test_register(self):
        """ Test register method. """
        from adrest.api import Api

        api = Api('1.0.0')

        class TestResource(ResourceView):
            class Meta:
                name = 'test1'

        api.register(TestResource, name='test2')
        resource = api.resources.get('test2')
        self.assertEqual(resource._meta.name, 'test2')

        @api.register
        class TestResource(ResourceView):
            class Meta:
                name = 'test3'

        self.assertTrue('test3' in api.resources)

        @api.register()
        class TestResource(ResourceView):
            class Meta:
                name = 'test4'

        self.assertTrue('test4' in api.resources)

        @api.register(name='test6')
        class TestResource(ResourceView):
            class Meta:
                name = 'test5'

        self.assertFalse('test5' in api.resources)
        self.assertTrue('test6' in api.resources)
Example #2
0
    def test_base(self):
        """ Test main functionality. """
        from adrest.api import Api

        api = Api('1.0.0')
        self.assertEqual(api.version, '1.0.0')
        self.assertTrue(api.urls)

        class Resource(ResourceView):
            class Meta:
                model = 'core.pirate'

        api.register(Resource)
        self.assertEqual(len(api.urls), 2)
        self.assertTrue(api.resources.get('pirate'))

        class TreasureResource(ResourceView):
            class Meta:
                parent = Resource
                model = 'core.treasure'

        api.register(TreasureResource)
        self.assertEqual(len(api.urls), 3)
        self.assertTrue(api.resources.get('pirate-treasure'))

        class PrefixResource(TreasureResource):
            class Meta:
                prefix = 'more'

        api.register(PrefixResource)
        self.assertEqual(len(api.urls), 4)
        self.assertTrue(api.resources.get('pirate-more-treasure'))

        class Resource2(ResourceView):
            class Meta:
                model = 'core.pirate'
                emitters = XMLEmitter

        api.register(Resource2, name='wow')
        resource = api.resources.get('wow')
        self.assertEqual(resource._meta.emitters, (XMLEmitter, ))
        self.assertEqual(resource._meta.name, 'wow')
Example #3
0
    def test_register(self):
        """ Test register method. """
        from adrest.api import Api

        api = Api('1.0.0')

        class TestResource(ResourceView):

            class Meta:
                name = 'test1'

        resource = api.register(TestResource, name='test2')
        self.assertEqual(resource._meta.name, 'test2')

        @api.register
        class TestResource(ResourceView):

            class Meta:
                name = 'test3'

        self.assertTrue('test3' in api.resources)

        @api.register()
        class TestResource(ResourceView):

            class Meta:
                name = 'test4'

        self.assertTrue('test4' in api.resources)

        @api.register(name='test6')
        class TestResource(ResourceView):

            class Meta:
                name = 'test5'

        self.assertFalse('test5' in api.resources)
        self.assertTrue('test6' in api.resources)
Example #4
0
    def test_base(self):
        """ Test main functionality. """
        from adrest.api import Api

        api = Api('1.0.0')
        self.assertEqual(api.version, '1.0.0')
        self.assertTrue(api.urls)

        class Resource(ResourceView):

            class Meta:
                model = 'core.pirate'

        api.register(Resource)
        self.assertEqual(len(api.urls), 2)
        self.assertTrue(api.resources.get('pirate'))

        class TreasureResource(ResourceView):

            class Meta:
                parent = Resource
                model = 'core.treasure'

        api.register(TreasureResource)
        self.assertEqual(len(api.urls), 3)
        self.assertTrue(api.resources.get('pirate-treasure'))

        class PrefixResource(TreasureResource):
            class Meta:
                prefix = 'more'

        api.register(PrefixResource)
        self.assertEqual(len(api.urls), 4)
        self.assertTrue(api.resources.get('pirate-more-treasure'))

        class Resource2(ResourceView):

            class Meta:
                model = 'core.pirate'
                emitters = XMLEmitter

        resource = api.register(Resource2, name='wow')
        self.assertEqual(resource._meta.emitters, (XMLEmitter,))
        self.assertEqual(resource._meta.name, 'wow')
Example #5
0
    def test_fabric(self):
        from adrest.api import Api

        api = Api('1.0.0')

        @api.register
        class PirateResource(ResourceView):
            class Meta:
                model = 'core.pirate'

            def get_collection(self, request, **resources):
                return super(PirateResource,
                             self).get_collection(request, **resources)

        resource = api.resources['pirate']
        rf = RequestFactory()
        response = resource().dispatch(rf.get('/'))
        self.assertContains(response, 'resources')
Example #6
0
from adrest.api import Api
from adrest.utils.auth import AnonimousAuthenticator
from adrest.utils.emitter import XMLEmitter, JSONTemplateEmitter
from adrest.views import ResourceView
from adrest.resources.rpc import RPCResource, JSONEmitter, JSONPEmitter


API = Api('1.0.0', api_rpc=True, emitters=XMLEmitter)


class TestAuth(AnonimousAuthenticator):

    def authenticate(self, request):
        return request.META.get('HTTP_AUTHORIZATION')

    def configure(self, request):
        self.resource.identifier = request.META.get('HTTP_AUTHORIZATION')


class TestResource(ResourceView):

    class Meta:
        allowed_methods = 'GET', 'POST', 'PUT'
        model = 'rpc.test'

    def get(self, request, **resources):
        assert not 'error' in request.GET, "Custom error"
        return True


class RootResource(ResourceView):
Example #7
0
from adrest.views import ResourceView
from adrest.api import Api


api = Api(api_rpc=True)


@api.register
class PirateResource(ResourceView):

    """ Part of simple API for tests. """

    class Meta:
        allowed_methods = 'get', 'POST', 'pUt', 'delete', 'Patch'
        model = 'core.pirate'


@api.register
class BoatResource(ResourceView):

    """ Part of simple API for tests. """

    class Meta:
        allowed_methods = 'get', 'post', 'put', 'delete'
        model = 'core.boat'
        parent = PirateResource


api2 = Api('1.0.0')
api2.register(PirateResource)
Example #8
0
""" Simple API for tests. """
from adrest.views import ResourceView
from adrest.api import Api
from adrest.utils.transformers import SmartDjangoTransformer

api = Api(api_rpc=True)


@api.register
class PirateResource(ResourceView):
    """ Part of simple API for tests. """
    class Meta:
        allowed_methods = 'get', 'POST', 'pUt', 'delete', 'Patch'
        model = 'core.pirate'
        transformers = SmartDjangoTransformer

    def put(self, *args, **kwargs):
        return super(PirateResource, self).put(*args, **kwargs)


@api.register
class BoatResource(ResourceView):
    """ Part of simple API for tests. """
    class Meta:
        allowed_methods = 'get', 'post', 'put', 'delete'
        model = 'core.boat'
        parent = PirateResource
        transformers = SmartDjangoTransformer


api2 = Api('1.0.0')
Example #9
0
File: api.py Project: intaxi/adrest
from .models import Task
from adrest.api import Api
from adrest.utils import emitter
from adrest.views import ResourceView


class TaskResource(ResourceView):
    emitters = emitter.XMLTemplateEmitter
    model = Task


class Task2Resource(ResourceView):
    allowed_methods = 'GET', 'POST'
    emitters = emitter.JSONEmitter
    model = Task


API = Api(version='1.0b')
API.register(TaskResource)
API.register(Task2Resource, url_name='task2', url_regex='task2')
Example #10
0
File: api.py Project: denya/adrest
from .models import Task
from adrest.api import Api
from adrest.utils import emitter
from adrest.views import ResourceView


class TaskResource(ResourceView):
    model = Task
    emitters = emitter.XMLTemplateEmitter


API = Api()
API.register(TaskResource)
Example #11
0
from .resources import (
    AuthorResource, BookPrefixResource, ArticleResource, SomeOtherResource,
    CustomResource, BSONResource, CSVResource)
from adrest.api import Api
from adrest.utils.auth import AnonimousAuthenticator, AccessKeyAuthenticator, \
    UserAuthenticator
from adrest.utils.emitter import XMLTemplateEmitter, JSONEmitter, BSONEmitter
from adrest.utils.parser import BSONParser
from adrest.utils.throttle import CacheThrottle


class CustomUserAuth(UserAuthenticator):
    username_fieldname = 'nickname'


API = Api(
    version=(1, 0, 0), emitters=(XMLTemplateEmitter, JSONEmitter),
    throttle=CacheThrottle, api_prefix='main')

API.register(AuthorResource,
             authenticators=(CustomUserAuth, AnonimousAuthenticator))
API.register(BookPrefixResource)
API.register(CustomResource)
API.register(ArticleResource, authenticators=AccessKeyAuthenticator)
API.register(SomeOtherResource, url_name='test', url_regex='test/mem/$')
API.register(BSONResource, parsers=(BSONParser,), emitters=(BSONEmitter,))
API.register(CSVResource)

# lint_ignore=C
Example #12
0
from .resources import (AuthorResource, BookPrefixResource, ArticleResource,
                        SomeOtherResource, CustomResource, CSVResource)
from adrest.api import Api
from adrest.utils.auth import AnonimousAuthenticator, AccessKeyAuthenticator, \
    UserAuthenticator
from adrest.utils.emitter import XMLTemplateEmitter, JSONEmitter
from adrest.utils.throttle import CacheThrottle


class CustomUserAuth(UserAuthenticator):
    username_fieldname = 'nickname'


API = Api(version=(1, 0, 0),
          emitters=(XMLTemplateEmitter, JSONEmitter),
          throttle=CacheThrottle,
          api_prefix='main')

API.register(AuthorResource,
             authenticators=(CustomUserAuth, AnonimousAuthenticator))
API.register(BookPrefixResource)
API.register(CustomResource)
API.register(ArticleResource, authenticators=AccessKeyAuthenticator)
API.register(SomeOtherResource, url_name='test', url_regex='test/mem/$')
API.register(CSVResource)

# lint_ignore=C
Example #13
0
from adrest.api import Api
from adrest.utils.auth import AnonimousAuthenticator
from adrest.utils.emitter import XMLEmitter, JSONTemplateEmitter
from adrest.views import ResourceView
from adrest.resources.rpc import RPCResource, JSONEmitter, JSONPEmitter

API = Api('1.0.0', api_rpc=True, emitters=XMLEmitter)


class TestAuth(AnonimousAuthenticator):
    def authenticate(self, request):
        return request.META.get('HTTP_AUTHORIZATION')

    def configure(self, request):
        self.resource.identifier = request.META.get('HTTP_AUTHORIZATION')


class TestResource(ResourceView):
    class Meta:
        allowed_methods = 'GET', 'POST', 'PUT'
        model = 'rpc.test'

    def get(self, request, **resources):
        assert not 'error' in request.GET, "Custom error"
        return True


class RootResource(ResourceView):
    class Meta:
        allowed_methods = 'GET', 'POST', 'PUT'
        model = 'rpc.root'
Example #14
0
File: api.py Project: klen/adrest
""" Create Api and resources. """
from adrest.api import Api
from adrest.views import ResourceView
from adrest.resources.rpc import RPCResource

from . import rpc


api = Api(version="0.1")
api.register(RPCResource, scheme=rpc)


@api.register
class AuthorResource(ResourceView):

    """ Get authors from db. """

    class Meta:
        model = "main.author"


@api.register
class BookResource(ResourceView):

    """ Works with books. Nested resource. """

    class Meta:
        allowed_methods = "get", "post", "put"
        model = "main.book"
        parent = AuthorResource
Example #15
0
    CustomResource, BSONResource, CSVResource)
from adrest.api import Api
from adrest.utils.auth import AnonimousAuthenticator, AccessKeyAuthenticator, \
    UserAuthenticator
from adrest.utils.emitter import XMLTemplateEmitter, JSONEmitter, BSONEmitter
from adrest.utils.transformers import SmartDjangoTransformer, BaseTransformer
from adrest.utils.parser import BSONParser
from adrest.utils.throttle import CacheThrottle


class CustomUserAuth(UserAuthenticator):
    username_fieldname = 'nickname'


API = Api(
    version=(1, 0, 0), emitters=(XMLTemplateEmitter, JSONEmitter),
    throttle=CacheThrottle, api_prefix='main',
    transformers=SmartDjangoTransformer)

API.register(AuthorResource,
             authenticators=(CustomUserAuth, AnonimousAuthenticator),
             transformers=BaseTransformer)
API.register(BookPrefixResource, transformers=BaseTransformer)
API.register(CustomResource, transformers=BaseTransformer)
API.register(ArticleResource, authenticators=AccessKeyAuthenticator, transformers=BaseTransformer)
API.register(SomeOtherResource, url_name='test', url_regex='test/mem/$',
             transformers=BaseTransformer)
API.register(BSONResource, parsers=(BSONParser,), emitters=(BSONEmitter,))
API.register(CSVResource, transformers=BaseTransformer)

# lint_ignore=C
Example #16
0
""" Create Api and resources. """
from adrest.api import Api
from adrest.views import ResourceView
from adrest.resources.rpc import RPCResource

from . import rpc


api = Api(version='0.1')
api.register(RPCResource, scheme=rpc)


@api.register
class AuthorResource(ResourceView):

    """ Get authors from db. """

    class Meta:
        model = 'main.author'


@api.register
class BookResource(ResourceView):

    """ Works with books. Nested resource. """

    class Meta:
        allowed_methods = 'get', 'post', 'put'
        model = 'main.book'
        parent = AuthorResource