def __call__(self, message: ffd.Message, next_: Callable) -> ffd.Message: if not self._initialized: self._initialize() not_for_this_context = message.get_context() != 'firefly' and \ self._context_map.get_context(message.get_context()) is None is_async = hasattr(message, '_async') and getattr(message, '_async') is True if not_for_this_context or is_async: return self._transfer_message(message) args = message.to_dict() args['_message'] = message if str(message) not in self._command_handlers: raise ffd.ConfigurationError(f'No command handler registered for {message}') service = self._command_handlers[str(message)] if self._service_is_batch_capable(service) and self._batch_service.is_registered(service.__class__): self.debug('Deferring to batch service') return self._batch_service.handle(service, args) else: parsed_args = ffd.build_argument_list(args, service) self.debug('Calling service %s with arguments: %s', service, parsed_args) return service(**parsed_args)
def __call__(self, message: ffd.Message, next_: Callable) -> ffd.Message: if not self._initialized: self._initialize() # TODO Fix the local dev server to work with multiple contexts if self._context is not None and message.get_context() != 'firefly' and message.get_context() != self._context: return self._transfer_message(message) args = message.to_dict() args['_message'] = message for service, query_type in self._query_handlers.items(): if message.is_this(query_type): return service(**ffd.build_argument_list(args, service)) raise ffd.ConfigurationError(f'No query handler registered for {message}')
def __call__(self, message: ffd.Message, next_: Callable) -> ffd.Message: if not self._initialized: self._initialize() if message.get_context() != 'firefly' and self._context_map.get_context(message.get_context()) is None: return self._transfer_message(message) args = message.to_dict() args['_message'] = message for service, query_type in self._query_handlers.items(): if message.is_this(query_type): parsed_args = ffd.build_argument_list(args, service) self.debug('Calling service with arguments: %s', parsed_args) return service(**parsed_args) raise ffd.ConfigurationError(f'No query handler registered for {message}')
def __call__(self, message: ffd.Message, next_: Callable) -> ffd.Message: return m.request({ 'method': 'GET', 'url': f'{process.env.FF_HOST}/{message.get_context()}', 'params': { 'query': JSON.stringify(message.to_dict()), }, })
async def _marshal_request(message: ffd.Message, request: web.Request, endpoint: ffd.HttpEndpoint): # TODO make a class message.headers = { 'http_request': { 'headers': dict(request.headers), 'method': request.method, 'path': request.path, 'content_type': request.content_type, 'content_length': request.content_length, 'query': dict(request.query), 'url': request._message.url, } } if endpoint is not None: message.headers['secured'] = endpoint.secured message.headers['scopes'] = endpoint.scopes
def __call__(self, message: ffd.Message, next_: Callable) -> ffd.Message: if not self._initialized: self._initialize() # TODO Fix the local dev server to work with multiple contexts if self._context is not None and message.get_context( ) != 'firefly' and message.get_context() != self._context: return self._transfer_message(message) args = message.to_dict() args['_message'] = message if str(message) not in self._command_handlers: raise ffd.ConfigurationError( f'No command handler registered for {message}') service = self._command_handlers[str(message)] return service(**ffd.build_argument_list(args, service))
def __call__(self, message: ffd.Message, next_: Callable) -> ffd.Message: if not self._initialized: self._initialize() self.debug('Message context: %s', message.get_context()) self.debug('This context: %s', self._context) self.debug('Environment: %s', self._ff_environment) if message.get_context() != 'firefly' and \ message.get_context() == self._context and \ not message.headers.get('external', False) and \ self._ff_environment != 'test': self.debug( 'EventResolvingMiddleware - event originated from this context. Dispatching.' ) self._publish_message(message) return next_(message) args = message.to_dict(recursive=False) args['_message'] = message if str(message) in self._event_listeners: services = self._event_listeners[str(message)] for service in services: try: if self._service_is_batch_capable( service) and self._batch_service.is_registered( service.__class__): self.debug('Deferring to batch service') return self._batch_service.handle(service, args) else: parsed_args = ffd.build_argument_list(args, service) self.debug('Calling service %s with arguments: %s', service.__class__.__name__, parsed_args) service(**parsed_args) except TypeError as e: self.exception(e) raise ffd.FrameworkError( f'Error calling {service.__class__.__name__}:\n\n{str(e)}' ) else: self.info('No event listener found for message %s', message) return next_(message)
async def _marshal_request(message: ffd.Message, request: web.Request): # TODO make a class message.headers['http_request'] = { 'headers': dict(request.headers), 'method': request.method, 'path': request.path, 'content_type': request.content_type, 'content_length': request.content_length, 'query': dict(request.query), 'post': dict(await request.post()), 'url': request._message.url, }
def __call__(self, message: ffd.Message, next_: Callable) -> ffd.Message: if not self._initialized: self._initialize() if message.get_context() != 'firefly' and \ message.get_context() == self._context and \ not message.headers.get('external', False): self._publish_message(message) return next_(message) args = message.to_dict(recursive=False) args['_message'] = message if str(message) in self._event_listeners: services = self._event_listeners[str(message)] for service in services: try: service(**ffd.build_argument_list(args, service)) except TypeError as e: raise ffd.FrameworkError( f'Error calling {service.__class__.__name__}:\n\n{str(e)}' ) return next_(message)
def __call__(self, message: ffd.Message, next_: Callable) -> ffd.Message: if 'http_request' in message.headers and message.headers.get( 'secured', True): token = None for k, v in message.headers['http_request']['headers'].items(): if k.lower() == 'authorization': if not v.startswith('Bearer'): raise ff.UnauthenticatedError() token = v.split(' ')[-1] if token is None: raise ff.UnauthenticatedError() self.debug('Decoding token') claims = self._jwt_decoder.decode(token) self.debug('Got sub: %s', claims['sub']) message.headers['sub'] = claims['sub'] return next_(message)
def __call__(self, message: ffd.Message, next_: Callable) -> ffd.Message: original_log_level = None if ('DEBUG' in os.environ and os.environ['DEBUG']) or ('debug' in message.headers and message.headers['debug']): original_log_level = self._logger.get_level() self._logger.set_level_to_debug() self.info(self._message, message) try: self.debug('Message properties: %s', message.to_dict()) except AttributeError: pass ret = next_(message) self.debug('Response: %s', str(ret)) if original_log_level is not None: self._logger.set_level(original_log_level) return ret
def __call__(self, message: ffd.Message, next_: Callable) -> ffd.Message: while True: try: if self._level == 0: self.debug('Level 0 - Resetting repositories') self.debug(message) self._reset() elif self._level > 0: message.headers['nested_request'] = True if isinstance(message, ffd.Event): self.debug('Buffering message') self._event_buffer.append(message) return message self._level += 1 self.debug('Level incremented: %d', self._level) ret = next_(message) self._level -= 1 if self._level < 0: self._level = 0 self.debug('Level decremented: %d', self._level) if self._level == 0: self.debug('Level 0 - Committing changes') self._commit() return ret except ffd.ConcurrentUpdateDetected: self.info('Concurrent update detected. Retrying the operation.') self.reset_level() self._reset() except Exception as e: self.exception(str(e)) self._level -= 1 if self._level < 0: self._level = 0 self.debug('Level decremented: %d', self._level) if self._level == 0: self.debug('Level 0 - Resetting repositories') self._reset() raise
def _broadcast(self, message: ffd.Message): if message.get_context() != 'firefly': self.debug('Broadcasting message %s', message) m = self._serializer.serialize(message) for queue in self.queues.values(): queue.put_nowait(m)
def __call__(self, message: ffd.Message, next_: Callable) -> ffd.Message: return m.request({ 'method': 'POST', 'url': f'{process.env.FF_HOST}/{message.get_context()}', 'body': message.to_dict(), })