Beispiel #1
0
    def test_default_json_provider(self):

        @provides(MediaType.ApplicationJson)
        class MyHandler(RequestHandler):
            pass

        provider, _ = ProviderBase.map_provider(MediaType.ApplicationJson,
                                                handler=MyHandler)
        self.assertIs(provider, JsonProvider)

        with self.assertRaises(NoProviderFound):
            ProviderBase.map_provider('application/vnd.supercell-v1.1+json',
                                      handler=MyHandler)
Beispiel #2
0
    def _provide_result(self, verb, headers, result):
        """Find the correct provider for the result and call it with the final
        result."""

        if isinstance(result, ReturnInformationT):
            self.set_header('Content-Type', MediaType.ApplicationJson)
            self.set_status(result.code)
            if result.message and 'additional' in result.message:
                self.logger.info(result.message['additional'])
            if result.code != 204:
                self.write(json.dumps(result.message))

        elif not isinstance(result, Model):
            # raise an error when something else than a model has been returned
            self.logger.error('Returning a non-model is not supported')
            raise HTTPError(500)

        else:
            try:
                provider_class = ProviderBase.map_provider(headers.get(
                    'Accept', ''),
                                                           self,
                                                           allow_default=True)
            except NoProviderFound:
                raise HTTPError(406)

            provider = provider_class()
            if isinstance(result, Model):
                provider.provide(result, self)

        if not self._finished:
            self.finish()
Beispiel #3
0
    def _provide_result(self, verb, headers, result):
        """Find the correct provider for the result and call it with the final
        result."""

        if isinstance(result, ReturnInformationT):
            self.set_header('Content-Type', MediaType.ApplicationJson)
            self.set_status(result.code)
            if result.message and 'additional' in result.message:
                self.logger.info(result.message['additional'])
            if result.code != 204:
                self.write(json.dumps(result.message))

        elif not isinstance(result, Model):
            # raise an error when something else than a model has been returned
            self.logger.error('Returning a non-model is not supported')
            raise HTTPError(500)

        else:
            try:
                provider_class = ProviderBase.map_provider(
                    headers.get('Accept', ''), self, allow_default=True)
            except NoProviderFound:
                raise HTTPError(406)

            provider = provider_class()
            if isinstance(result, Model):
                provider.provide(result, self)

        if not self._finished:
            self.finish()
Beispiel #4
0
 def write_error(self, status_code, **kwargs):
     try:
         provider_class = ProviderBase.map_provider(
             self.request.headers.get('Accept', ''), self,
             allow_default=True)
         provider_class().error(status_code, self._reason, self)
     except NoProviderFound:
         super(RequestHandler, self).write_error(status_code, **kwargs)
Beispiel #5
0
    def test_json_provider_with_version(self):

        @provides(MediaType.ApplicationJson, vendor='supercell', version=1.0)
        class MyHandler(RequestHandler):

            def __init__(self, *args, **kwargs):
                # do not call super here in order to test mapping on instances
                # of this class
                pass

        provider, _ = ProviderBase.map_provider(
            'application/vnd.supercell-v1.0+json', handler=MyHandler)
        self.assertIs(provider, JsonProviderWithVendorAndVersion)

        handler = MyHandler()
        provider, _ = ProviderBase.map_provider(
            'application/vnd.supercell-v1.0+json', handler=handler)
        self.assertIs(provider, JsonProviderWithVendorAndVersion)
Beispiel #6
0
    def test_specific_json_provider(self):

        @provides(MediaType.ApplicationJson, vendor='supercell')
        class MyHandler(RequestHandler):
            pass

        provider, _ = ProviderBase.map_provider('application/vnd.supercell+json',
                                                handler=MyHandler)
        self.assertIs(provider, MoreDetailedJsonProvider)
Beispiel #7
0
    def test_json_provider_with_configuration(self):

        @provides(MediaType.ApplicationJson, partial=True)
        class MyHandler(RequestHandler):
            pass

        provider, configuration = ProviderBase.map_provider(
            MediaType.ApplicationJson, handler=MyHandler)
        self.assertIs(provider, JsonProvider)
        self.assertIs(configuration["partial"], True)