def get(self): """ Get the user's registered webhooks """ account_id = lookup_account_id(request.headers['username']) return paginate(request, DEFAULT_REGISTRATIONS_TABLE, 'registrations', filters={'account_id': account_id})
def get(self, account_id): """ Gets a user account. Users can only see their own account """ if lookup_account_id(request.headers['username']) == account_id: return query(DEFAULT_ACCOUNTS_TABLE, account_id) else: return make_response(jsonify( {'Error': 'Not authorized'}), client.UNAUTHORIZED)
def test_lookup_account_id(self): return_value = [{"id": "123"}] filters = {"username": "******"} with patch.object(Interactions, "query", return_value=return_value) as query_method: ret = lookup_account_id("johndoe") self.assertEqual(ret, "123") query_method.assert_called_with(DEFAULT_ACCOUNTS_TABLE, filters=filters)
def post(self, registration_id): """ Creates new triggered webhook event """ registration = Interactions.query(DEFAULT_REGISTRATIONS_TABLE, filters={'id': registration_id}) if not registration: return make_response( jsonify({'Error': 'Registration id not found'}), client.NOT_FOUND) # Other users cannot trigger webhooks they didn't create calling_account_id = lookup_account_id(request.headers['username']) if not lookup_registration_id(calling_account_id, registration_id): return make_response( jsonify({ 'Error': 'You don\'t have access ' 'to this registration record or it no ' 'longer exists' }), client.UNAUTHORIZED) # Notify subscribed endpoints (send the webhooks out) subscriptions = Interactions.list_all( DEFAULT_SUBSCRIPTIONS_TABLE, order_by='epoch', filters={'registration_id': registration_id}) if subscriptions: for record in subscriptions: account = Interactions.get(DEFAULT_ACCOUNTS_TABLE, record['account_id']) # Only hit the endpoint if their failed count is low enough if int(account['failed_count']) < MAX_FAILED_COUNT: # This import is required to be here so the flask-restful # piece works properly with Celery from pywebhooks.tasks.webhook_notification import \ notify_subscribed_accounts notify_subscribed_accounts.delay( event=registration[0]['event'], event_data=registration[0]['event_data'], secret_key=account['secret_key'], endpoint=account['endpoint'], account_id=record['account_id']) return insert(DEFAULT_TRIGGERED_TABLE, **{'registration_id': registration_id})
def get(self): """ Get the user's webhook subscriptions """ try: account_id = lookup_account_id(request.headers['username']) # pylint: disable=W0703 except Exception: return make_response( jsonify({'Error': 'Invalid username or account'}), client.BAD_REQUEST) return paginate(request, DEFAULT_SUBSCRIPTIONS_TABLE, 'subscriptions', filters={'account_id': account_id})
def post(self): """ Creates a new registration """ json_data = request.get_json() # Look up account id based on username, username will be valid since # the api_key_restricted_resource decorator runs first account_id = lookup_account_id(request.headers['username']) return insert(DEFAULT_REGISTRATIONS_TABLE, **{'account_id': account_id, 'event': json_data['event'], 'description': json_data['description'], 'event_data': json_data['event_data']})
def post(self, registration_id): """ Creates new triggered webhook event """ registration = Interactions.query( DEFAULT_REGISTRATIONS_TABLE, filters={'id': registration_id}) if not registration: return make_response( jsonify( {'Error': 'Registration id not found'} ), client.NOT_FOUND) # Other users cannot trigger webhooks they didn't create calling_account_id = lookup_account_id(request.headers['username']) if not lookup_registration_id(calling_account_id, registration_id): return make_response( jsonify({'Error': 'You don\'t have access ' 'to this registration record or it no ' 'longer exists'}), client.UNAUTHORIZED) # Notify subscribed endpoints (send the webhooks out) subscriptions = Interactions.list_all( DEFAULT_SUBSCRIPTIONS_TABLE, order_by='epoch', filters={'registration_id': registration_id}) if subscriptions: for record in subscriptions: account = Interactions.get(DEFAULT_ACCOUNTS_TABLE, record['account_id']) # Only hit the endpoint if their failed count is low enough if int(account['failed_count']) < MAX_FAILED_COUNT: # This import is required to be here so the flask-restful # piece works properly with Celery from pywebhooks.tasks.webhook_notification import \ notify_subscribed_accounts notify_subscribed_accounts.delay( event=registration[0]['event'], event_data=registration[0]['event_data'], secret_key=account['secret_key'], endpoint=account['endpoint'], account_id=record['account_id']) return insert(DEFAULT_TRIGGERED_TABLE, **{'registration_id': registration_id})
def post(self): """ Creates a new registration """ json_data = request.get_json() # Look up account id based on username, username will be valid since # the api_key_restricted_resource decorator runs first account_id = lookup_account_id(request.headers['username']) return insert( DEFAULT_REGISTRATIONS_TABLE, **{ 'account_id': account_id, 'event': json_data['event'], 'description': json_data['description'], 'event_data': json_data['event_data'] })
def post(self, subscription_id): """ Creates new subscription """ # subscription_id is actually the registration_id registration_id = subscription_id account_id = lookup_account_id(request.headers['username']) if not registration_id_exists(registration_id): return make_response( jsonify({'Error': 'The registration id does not exist'}), client.NOT_FOUND) return insert(DEFAULT_SUBSCRIPTIONS_TABLE, **{'account_id': account_id, 'registration_id': registration_id})
def post(self, subscription_id): """ Creates new subscription """ # subscription_id is actually the registration_id registration_id = subscription_id account_id = lookup_account_id(request.headers['username']) if not registration_id_exists(registration_id): return make_response( jsonify({'Error': 'The registration id does not exist'}), client.NOT_FOUND) return insert( DEFAULT_SUBSCRIPTIONS_TABLE, **{ 'account_id': account_id, 'registration_id': registration_id })
def test_lookup_account_id(self): return_value = [ { 'id': '123' } ] filters = {'username': '******'} with patch.object(Interactions, 'query', return_value=return_value) as query_method: ret = lookup_account_id('johndoe') self.assertEqual(ret, '123') query_method.assert_called_with( DEFAULT_ACCOUNTS_TABLE, filters=filters )