How to use the socketshark.events.Event function in socketshark

To help you get started, we’ve selected a few socketshark examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github closeio / socketshark / socketshark / events.py View on Github external
if not ticket:
            raise EventError(c.ERR_NEEDS_TICKET)

        auth_url = auth_method_config['validation_url']
        auth_fields = auth_method_config['auth_fields']
        result = await http_post(self.shark, auth_url, {'ticket': ticket})
        if result.get('status') != 'ok':
            raise EventError(result.get('error', c.ERR_AUTH_FAILED))
        auth_info = {field: result[field] for field in auth_fields}
        self.session.auth_info = auth_info
        self.session.log.debug('auth info', auth_info=auth_info)
        await self.send_ok()
        return True


class SubscriptionEvent(Event):
    def __init__(self, session, data):
        super().__init__(session, data)
        subscription_name = data.get('subscription') or None
        self.subscription_name = subscription_name
        if subscription_name in self.session.subscriptions:
            self.subscription = self.session.subscriptions[subscription_name]
        else:
            self.subscription = Subscription(self.config, session, data)
        self.extra_data = self.subscription.extra_data

    async def send_error(self, error, data=None):
        await super().send_error(error, data=data, extra_data={
            'subscription': self.subscription_name,
        } if self.subscription_name else {})

    async def send_ok(self, data=None):
github closeio / socketshark / socketshark / events.py View on Github external
class InvalidEvent(Event):
    def __init__(self, session):
        self.session = session
        self.event = None
        self.extra_data = {}

    async def full_process(self):
        msg = {
            'status': 'error',
            'error': c.ERR_INVALID_EVENT,
        }
        await self.session.send(msg)
        return False


class UnknownEvent(Event):
    async def process(self):
        raise EventError(c.ERR_EVENT_NOT_FOUND)


class AuthEvent(Event):
    def __init__(self, session, data):
        super().__init__(session, data)
        self.auth_config = self.config['AUTHENTICATION']
        self.method = data.get('method', c.DEFAULT_AUTH_METHOD)

    async def process(self):
        if self.method not in self.auth_config:
            raise EventError(c.ERR_AUTH_UNSUPPORTED)

        # The only supported method.
        assert self.method == 'ticket'
github closeio / socketshark / socketshark / session.py View on Github external
async def on_client_event(self, data):
        """
        Called by the WebSocket backend when a new client messages comes in.
        Expects a JSON dict.
        """
        if not self.active:
            # Event was received while the WebSocket is about to close.
            self.log.warn('inactive client event ignored', data=data)
            return

        self.log.debug('client event', data=data)
        event = Event.from_data(self, data)
        try:
            result = await event.full_process()

            # Don't log invalid/unknown event names
            if isinstance(event, InvalidEvent):
                event_name = 'invalid'
            elif isinstance(event, UnknownEvent):
                event_name = 'unknown'
            else:
                event_name = event.event
            self.shark.metrics.log_event(event_name, result)
        except:
            self.shark.log.exception('unhandled event processing exception')
            await event.send_error(c.ERR_UNHANDLED_EXCEPTION)
            await self.close()
github closeio / socketshark / socketshark / events.py View on Github external
async def full_process(self):
        msg = {
            'status': 'error',
            'error': c.ERR_INVALID_EVENT,
        }
        await self.session.send(msg)
        return False


class UnknownEvent(Event):
    async def process(self):
        raise EventError(c.ERR_EVENT_NOT_FOUND)


class AuthEvent(Event):
    def __init__(self, session, data):
        super().__init__(session, data)
        self.auth_config = self.config['AUTHENTICATION']
        self.method = data.get('method', c.DEFAULT_AUTH_METHOD)

    async def process(self):
        if self.method not in self.auth_config:
            raise EventError(c.ERR_AUTH_UNSUPPORTED)

        # The only supported method.
        assert self.method == 'ticket'

        auth_method_config = self.auth_config[self.method]

        ticket = self.data.get('ticket')
        if not ticket:
github closeio / socketshark / socketshark / events.py View on Github external
msg['data'] = data
        msg.update(extra_data)
        await self.session.send(msg)

    async def full_process(self):
        """
        Fully process an event and return whether it was successful.
        """
        try:
            return await self.process()
        except EventError as e:
            await self.send_error(e.error, data=e.data)
            return False


class InvalidEvent(Event):
    def __init__(self, session):
        self.session = session
        self.event = None
        self.extra_data = {}

    async def full_process(self):
        msg = {
            'status': 'error',
            'error': c.ERR_INVALID_EVENT,
        }
        await self.session.send(msg)
        return False


class UnknownEvent(Event):
    async def process(self):