Example #1
0
    def map_consumer(content_type, handler):
        """Map a given content type to the correct provider implementation.

        If no provider matches, raise a `NoProviderFound` exception.

        :param accept_header: HTTP Accept header value
        :type accept_header: str
        :param handler: supercell request handler

        :raises: :exc:`NoConsumerFound`
        """
        accept = parse_accept_header(content_type)
        if len(accept) == 0:
            raise NoConsumerFound()

        (ctype, params, q) = accept[0]

        if ctype not in handler._CONS_CONTENT_TYPES:
            raise NoConsumerFound()

        c = ContentType(ctype,
                        vendor=params.get('vendor', None),
                        version=params.get('version', None))
        if c not in handler._CONS_CONTENT_TYPES[ctype]:
            raise NoConsumerFound()

        known_types = [
            t for t in ConsumerMeta.KNOWN_CONTENT_TYPES[ctype] if t[0] == c
        ]

        if len(known_types) == 1:
            return (handler._CONS_MODEL[c], known_types[0][1])

        raise NoConsumerFound()
Example #2
0
 def test_parse_accept_header_browser(self):
     accept = ("text/html,application/xhtml+xml,application/xml;" +
               "q=0.9,*/*;q=0.8,application/json")
     should = [('text/html', {}, 1.0), ('application/xhtml+xml', {}, 1.0),
               ('application/json', {}, 1.0), ('application/xml', {}, 0.9),
               ('*/*', {}, 0.8)]
     self.assertEquals(parse_accept_header(accept), should)
Example #3
0
 def test_parse_accept_header_smart_client(self):
     accept = "application/vnd.ficture.lightt-v1.1+json"
     should = [('application/json', {
         'version': 1.1,
         'vendor': 'ficture.lightt'
     }, 1.0)]
     self.assertEquals(parse_accept_header(accept), should)
Example #4
0
    def map_consumer(content_type, handler):
        """Map a given content type to the correct provider implementation.

        If no provider matches, raise a `NoProviderFound` exception.

        :param accept_header: HTTP Accept header value
        :type accept_header: str
        :param handler: supercell request handler

        :raises: :exc:`NoConsumerFound`
        """
        accept = parse_accept_header(content_type)
        if len(accept) == 0:
            raise NoConsumerFound()

        (ctype, params, q) = accept[0]

        if ctype not in handler._CONS_CONTENT_TYPES:
            raise NoConsumerFound()

        c = ContentType(ctype, vendor=params.get('vendor', None),
                        version=params.get('version', None))
        if c not in handler._CONS_CONTENT_TYPES[ctype]:
            raise NoConsumerFound()

        known_types = [t for t in ConsumerMeta.KNOWN_CONTENT_TYPES[ctype]
                       if t[0] == c]

        if len(known_types) == 1:
            return (handler._CONS_MODEL[c], known_types[0][1])

        raise NoConsumerFound()
Example #5
0
 def test_parse_accept_header_browser(self):
     accept = ("text/html,application/xhtml+xml,application/xml;" +
               "q=0.9,*/*;q=0.8,application/json")
     should = [('text/html', {}, 1.0),
               ('application/xhtml+xml', {}, 1.0),
               ('application/json', {}, 1.0),
               ('application/xml', {}, 0.9),
               ('*/*', {}, 0.8)]
     self.assertEquals(parse_accept_header(accept), should)
Example #6
0
    def map_provider(accept_header, handler, allow_default=False):
        """Map a given content type to the correct provider implementation.

        If no provider matches, raise a `NoProviderFound` exception.

        :param accept_header: HTTP Accept header value
        :type accept_header: str
        :param handler: supercell request handler
        :param allow_default: allow usage of default provider if no accept
                              header is set, default is False
        :type allow_default: bool
        :raises: :exc:`NoProviderFound`

        :return: A tuple of the matching provider implementation class and
                 the provide()-kwargs
        :rtype: (supercell.api.provider.ProviderBase, dict)
        """
        if not hasattr(handler, '_PROD_CONTENT_TYPES'):
            raise NoProviderFound()

        accept = parse_accept_header(accept_header)

        if len(accept) > 0:
            for (ctype, params, q) in accept:
                if ctype not in handler._PROD_CONTENT_TYPES:
                    continue

                c = ContentType(ctype, vendor=params.get('vendor', None),
                                version=params.get('version', None))
                if c not in handler._PROD_CONTENT_TYPES[ctype]:
                    continue

                known_types = [t for t in
                               ProviderMeta.KNOWN_CONTENT_TYPES[ctype]
                               if t[0] == c]

                configuration = handler._PROD_CONFIGURATION[ctype]
                if len(known_types) == 1:
                    return (known_types[0][1], configuration)

        if allow_default and 'default' in handler._PROD_CONTENT_TYPES:
            content_type = handler._PROD_CONTENT_TYPES['default']
            configuration = handler._PROD_CONFIGURATION['default']
            ctype = content_type.content_type
            default_type = [t for t in
                            ProviderMeta.KNOWN_CONTENT_TYPES[ctype]
                            if t[0] == content_type]

            if len(default_type) == 1:
                return (default_type[0][1], configuration)

        raise NoProviderFound()
Example #7
0
    def map_provider(accept_header, handler, allow_default=False):
        """Map a given content type to the correct provider implementation.

        If no provider matches, raise a `NoProviderFound` exception.

        :param accept_header: HTTP Accept header value
        :type accept_header: str
        :param handler: supercell request handler
        :raises: :exc:`NoProviderFound`
        """
        if not hasattr(handler, '_PROD_CONTENT_TYPES'):
            raise NoProviderFound()

        accept = parse_accept_header(accept_header)

        if len(accept) > 0:
            for (ctype, params, q) in accept:
                if ctype not in handler._PROD_CONTENT_TYPES:
                    continue

                c = ContentType(ctype,
                                vendor=params.get('vendor', None),
                                version=params.get('version', None))
                if c not in handler._PROD_CONTENT_TYPES[ctype]:
                    continue

                known_types = [
                    t for t in ProviderMeta.KNOWN_CONTENT_TYPES[ctype]
                    if t[0] == c
                ]

                if len(known_types) == 1:
                    return known_types[0][1]

        if 'default' in handler._PROD_CONTENT_TYPES:
            content_type = handler._PROD_CONTENT_TYPES['default']
            ctype = content_type.content_type
            default_type = [
                t for t in ProviderMeta.KNOWN_CONTENT_TYPES[ctype]
                if t[0] == content_type
            ]

            if len(default_type) == 1:
                return default_type[0][1]

        raise NoProviderFound()
Example #8
0
    def map_provider(accept_header, handler, allow_default=False):
        """Map a given content type to the correct provider implementation.

        If no provider matches, raise a `NoProviderFound` exception.

        :param accept_header: HTTP Accept header value
        :type accept_header: str
        :param handler: supercell request handler
        :param allow_default: allow usage of default provider if no accept
                              header is set, default is False
        :type allow_default: bool
        :raises: :exc:`NoProviderFound`

        :return: A tuple of the matching provider implementation class and
                 the provide()-kwargs
        :rtype: (supercell.api.provider.ProviderBase, dict)
        """
        if not hasattr(handler, '_PROD_CONTENT_TYPES'):
            raise NoProviderFound()

        accept = parse_accept_header(accept_header)

        for (ctype, params, q) in accept:
            if ctype not in handler._PROD_CONTENT_TYPES:
                continue

            if ctype == '*/*':
                if not allow_default:
                    continue
                c = handler._PROD_CONTENT_TYPES[ctype][0]
            else:
                c = ContentType(ctype, vendor=params.get('vendor', None),
                                version=params.get('version', None))

            if c not in handler._PROD_CONTENT_TYPES[c.content_type]:
                continue

            known_types = [t for t in
                           ProviderMeta.KNOWN_CONTENT_TYPES[c.content_type]
                           if t[0] == c]

            configuration = handler._PROD_CONFIGURATION[ctype]
            if len(known_types) == 1:
                return (known_types[0][1], configuration)

        raise NoProviderFound()
Example #9
0
    def map_provider(accept_header, handler, allow_default=False):
        """Map a given content type to the correct provider implementation.

        If no provider matches, raise a `NoProviderFound` exception.

        :param accept_header: HTTP Accept header value
        :type accept_header: str
        :param handler: supercell request handler
        :raises: :exc:`NoProviderFound`
        """
        if not hasattr(handler, '_PROD_CONTENT_TYPES'):
            raise NoProviderFound()

        accept = parse_accept_header(accept_header)

        if len(accept) > 0:
            for (ctype, params, q) in accept:
                if ctype not in handler._PROD_CONTENT_TYPES:
                    continue

                c = ContentType(ctype, vendor=params.get('vendor', None),
                                version=params.get('version', None))
                if c not in handler._PROD_CONTENT_TYPES[ctype]:
                    continue

                known_types = [t for t in
                               ProviderMeta.KNOWN_CONTENT_TYPES[ctype]
                               if t[0] == c]

                if len(known_types) == 1:
                    return known_types[0][1]

        if 'default' in handler._PROD_CONTENT_TYPES:
            content_type = handler._PROD_CONTENT_TYPES['default']
            ctype = content_type.content_type
            default_type = [t for t in
                            ProviderMeta.KNOWN_CONTENT_TYPES[ctype]
                            if t[0] == content_type]

            if len(default_type) == 1:
                return default_type[0][1]

        raise NoProviderFound()
Example #10
0
 def test_parse_accept_header_also_dumb_client(self):
     accept = "application/vnd.ficture.lightt"
     should = [('application/vnd.ficture.lightt', {}, 1.0)]
     self.assertEquals(parse_accept_header(accept), should)
Example #11
0
 def test_iesix_bad_accept_header(self):
     accept = 'text/*,image/*;application/*;*/*;'
     should = [('', {}, 1.0)]
     self.assertEquals(parse_accept_header(accept), should)
Example #12
0
 def test_parse_accept_header_really_dumb_client(self):
     accept = ""
     should = [('', {}, 1.0)]
     self.assertEquals(parse_accept_header(accept), should)
Example #13
0
 def test_parse_accept_header_dumb_client(self):
     accept = "application/json"
     should = [('application/json', {}, 1.0)]
     self.assertEquals(parse_accept_header(accept), should)
Example #14
0
 def test_parse_accept_header_also_dumb_client(self):
     accept = "application/vnd.ficture.lightt"
     should = [('application/vnd.ficture.lightt', {}, 1.0)]
     self.assertEquals(parse_accept_header(accept), should)
Example #15
0
 def test_parse_accept_header_smart_client_without_version(self):
     accept = "application/vnd.ficture.lightt+json"
     should = [('application/json', {'vendor': 'ficture.lightt'}, 1.0)]
     self.assertEquals(parse_accept_header(accept), should)
Example #16
0
 def test_parse_accept_header_dumb_client(self):
     accept = "application/json"
     should = [('application/json', {}, 1.0)]
     self.assertEquals(parse_accept_header(accept), should)
Example #17
0
 def test_parse_accept_header_really_dumb_client(self):
     accept = ""
     should = [('', {}, 1.0)]
     self.assertEquals(parse_accept_header(accept), should)
Example #18
0
 def test_iesix_bad_accept_header(self):
     accept = 'text/*,image/*;application/*;*/*;'
     should = [('', {}, 1.0)]
     self.assertEquals(parse_accept_header(accept), should)