Exemplo n.º 1
0
    def create(self):
        subscriber = request.json

        try:
            callback_url = urlparse(subscriber['callbackURL'])

            collection, event_category = current_app.match_resource(subscriber['eventCategory'])
            assert collection == 'api.event_categories'

            threshold = Decimal(subscriber['threshold'])
            assert 0 <= threshold <= 1
        except KeyError as e:
            raise APIError(
                "Incomplete JSON payload",
                {'key': e.message}
            )

        id = uuid4()
        subscribers.insert({
            '_id': Binary(id.bytes, UUID_SUBTYPE),
            # This would ideally be in a different collection...
            'callbackURL': urlunparse(callback_url),
            'eventCategory': event_category,
            'threshold': str(threshold)
        })
        return self.resource_created(id)
Exemplo n.º 2
0
    def create(self):
        event = request.json

        try:
            collection, category = current_app.match_resource(event['category'])
            assert collection == 'api.event_categories'
        except KeyError as e:
            raise APIError(
                "Incomplete JSON payload",
                {'key': e.message}
            )

        try:
            time = parse_date(event['time'])
        except KeyError:
            time = datetime.utcnow()

        event_queue.set(EVENT_QUEUE_NAME, json.dumps({
            'category': category.hex,
            'time': time.isoformat()
        }))
        return self.resource_created()