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, 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 query(table_name, record_id): """ Gets a single record (handles GET traffic) """ try: return make_response(jsonify(Interactions.get(table_name, record_id)), client.OK) except RqlRuntimeError as runtime_err: return make_response(jsonify({'Error': runtime_err.message}), client.INTERNAL_SERVER_ERROR) except RqlDriverError as rql_err: return make_response(jsonify({'Error': rql_err.message}), client.INTERNAL_SERVER_ERROR) except TypeError: return make_response(jsonify({'Error': 'Invalid id parameter'}), client.BAD_REQUEST)
def query(table_name, record_id): """ Gets a single record (handles GET traffic) """ try: return make_response( jsonify(Interactions.get(table_name, record_id)), client.OK) except RqlRuntimeError as runtime_err: return make_response(jsonify({'Error': runtime_err.message}), client.INTERNAL_SERVER_ERROR) except RqlDriverError as rql_err: return make_response(jsonify({'Error': rql_err.message}), client.INTERNAL_SERVER_ERROR) except TypeError: return make_response( jsonify({'Error': 'Invalid id parameter'}), client.BAD_REQUEST)
def update_failed_count(account_id=None, increment_failed_count=False): """ Update the failed_count field on the user's account """ try: # Get the failed_count value account_record = Interactions.get( DEFAULT_ACCOUNTS_TABLE, record_id=account_id) failed_count = int(account_record['failed_count']) if increment_failed_count: failed_count += 1 else: failed_count = 0 Interactions.update(DEFAULT_ACCOUNTS_TABLE, record_id=account_id, updates={'failed_count': failed_count}) except (RqlRuntimeError, RqlDriverError, Exception): pass
def update_failed_count(account_id=None, increment_failed_count=False): """ Update the failed_count field on the user's account """ try: # Get the failed_count value account_record = Interactions.get(DEFAULT_ACCOUNTS_TABLE, record_id=account_id) failed_count = int(account_record['failed_count']) if increment_failed_count: failed_count += 1 else: failed_count = 0 Interactions.update(DEFAULT_ACCOUNTS_TABLE, record_id=account_id, updates={'failed_count': failed_count}) except (RqlRuntimeError, RqlDriverError, Exception): pass