示例#1
0
文件: _sink.py 项目: l-grebe/hammock
    def _wrapper(self, req):
        if self.dest is not None and not inspect.isgeneratorfunction(
                self.func):
            # If it's a proxy function, and also not a generator,
            # we have no need in parsing credentials
            return proxy.proxy(req, self.dest)

        if self.credentials_class and common.KW_CREDENTIALS in self.spec.args:
            credentials = self.credentials_class(req.headers)
            req.url_params[common.KW_CREDENTIALS] = credentials

        if self.dest is None:
            return self(req, **req.url_params)  # pylint: disable=not-callable
        else:
            generator = self(req, **req.url_params)
            # We now know that the function is a generator and should be treated like this:
            # The code before the first 'yield' should be executed immediately
            next(generator)
            # Now we should perform the proxy logic
            resp = proxy.proxy(req, self.dest)
            try:
                # And now, we should call the code after yield, giving it the response received by the server at 'dest'
                generator.send(resp)
            except StopIteration:
                # Here the generator ends, everything's good!
                pass
            return resp
示例#2
0
    def _wrapper(self, req):
        if self.credentials_class and common.KW_CREDENTIALS in self.spec.args:
            credentials = self.credentials_class(req.headers)
            req.url_params[common.KW_CREDENTIALS] = credentials

        if self.dest is None:
            return self(req, **req.url_params)  # pylint: disable=not-callable
        else:
            return proxy.proxy(req, self.dest)
示例#3
0
    def _wrapper(self, req):
        """
        Wraps the decorated func (self._func)
        :param req: a hammock.types.request.Request object.
        :return: response as hammock.types.response.Response object.
        """
        if self.dest is None:
            kwargs = req.collected_data
            self._convert_by_keyword_map(kwargs)
            enforcer = None
            credentials = None
            if self.credentials_class:
                credentials = self.credentials_class(req.headers)
                if self.full_policy_rule_name:
                    enforcer = self.policy.check(
                        self.full_policy_rule_name, target=kwargs, credentials=credentials)

            # Add special keyword arguments:
            if common.KW_HEADERS in self.spec.args:
                kwargs[common.KW_HEADERS] = req.headers
            if common.KW_HOST in self.spec.args:
                kwargs[common.KW_HOST] = '{}://{}'.format(req.parsed_url.scheme, req.parsed_url.netloc)
            if common.KW_CREDENTIALS in self.spec.args:
                kwargs[common.KW_CREDENTIALS] = credentials
            if common.KW_ENFORCER in self.spec.args:
                kwargs[common.KW_ENFORCER] = enforcer

            try:
                self.spec.match_and_convert(kwargs)
            except exceptions.HttpError as exc:
                raise self._error(exceptions.BadRequest, str(exc))
            except Exception as exc:  # pylint: disable=broad-except
                logging.exception('[Error parsing request kwargs %s] kwargs: %s, %r', req.uid, kwargs, exc)
                raise self._error(exceptions.BadRequest, 'Error parsing request parameters, {}'.format(exc))

            # Invoke the routing method:
            logging.debug('[kwargs %s] %s', req.uid, kwargs)
            result = self(**kwargs)

            resp = response.Response.from_result(result, self.success_code, self.response_content_type)
        else:
            resp = proxy.proxy(req, self.dest)
        return resp
示例#4
0
文件: _route.py 项目: kindsof/hammock
    def _wrapper(self, req):
        """
        Wraps the decorated func (self._func)
        :param req: a hammock.types.request.Request object.
        :return: response as hammock.types.response.Response object.
        """
        if self.dest is not None and not inspect.isgeneratorfunction(self.func):
            # If it's a proxy function, and also not a generator,
            # we have no need in parsing the kwargs
            return proxy.proxy(req, self.dest)

        kwargs = req.collected_data
        self._convert_by_keyword_map(kwargs)
        enforcer = None
        credentials = None
        if self.credentials_class:
            credentials = self.credentials_class(req.headers)
            if self.full_policy_rule_name:
                enforcer = self.policy.check(
                    self.full_policy_rule_name, target=kwargs, credentials=credentials)

        # Add special keyword arguments:
        if common.KW_HEADERS in self.spec.args:
            kwargs[common.KW_HEADERS] = req.headers
        if common.KW_HOST in self.spec.args:
            kwargs[common.KW_HOST] = '{}://{}'.format(req.parsed_url.scheme, req.parsed_url.netloc)
        if common.KW_CREDENTIALS in self.spec.args:
            kwargs[common.KW_CREDENTIALS] = credentials
        if common.KW_ENFORCER in self.spec.args:
            kwargs[common.KW_ENFORCER] = enforcer

        try:
            self.spec.match_and_convert(kwargs)
        except exceptions.HttpError as exc:
            raise self._error(exceptions.BadRequest, str(exc))
        except Exception as exc:  # pylint: disable=broad-except
            logging.debug('[Error parsing request kwargs %s] kwargs: %s, %r', req.uid,
                          common.repr_request_kwargs_for_logging(kwargs), exc)
            raise self._error(exceptions.BadRequest, 'Error parsing request parameters, {}'.format(exc))

        if self.dest is None:
            # Invoke the routing method:
            logging.debug('[kwargs %s] %s', req.uid, kwargs)
            result = self(**kwargs)

            resp = response.Response.from_result(result, self.success_code, self.response_content_type)
        else:
            generator = self(**kwargs)
            # We now know that the function is a generator and should be treated like this:
            # The code before the first 'yield' should be executed immediately
            next(generator)
            # Now we should perform the proxy logic
            resp = proxy.proxy(req, self.dest)
            try:
                # And now, we should call the code after yield, giving it the response received by the server at 'dest'
                generator.send(resp)
            except StopIteration:
                # Here the generator ends, everything's good!
                pass

        return resp