def accept_webhook(request, integration_id): """ Google Calendar webhook handler For more details see https://developers.google.com/google-apps/calendar/v3/push?hl=en_US#receiving-notifications """ try: channel_id = request.META['HTTP_X_GOOG_CHANNEL_ID'] resource_id = request.META['HTTP_X_GOOG_RESOURCE_ID'] resource_state = request.META['HTTP_X_GOOG_RESOURCE_STATE'] resource_uri = request.META['HTTP_X_GOOG_RESOURCE_URI'] token = request.META['HTTP_X_GOOG_CHANNEL_TOKEN'] except KeyError: # not a google request return HttpResponse() try: token_data = utils.validate_webhook_token(token) except PowerAppError: logger.debug("Invalid token %s. Quietly ignore", token) return HttpResponse() logger.debug('Received webhook from Google Calendar, ' 'channel_id=%s, token=%s, resource: (id=%s, state=%s, uri=%s)', channel_id, token, resource_id, resource_state, resource_uri) if Integration.objects.filter(id=integration_id).exists(): subtask = tasks.sync_gcal.s(integration_id) schedule_with_rate_limit(integration_id, 'last_sync', subtask) else: tasks.stop_channel.delay(token_data['u'], channel_id, resource_id) return HttpResponse()
def sync_now(request, integration_id): integration = get_object_or_404(Integration, id=integration_id, user_id=request.user.id) subtask = tasks.sync_evernote.s(integration.id) schedule_with_rate_limit(integration.id, 'last_sync', subtask, timeout=120) messages.info(request, 'Synchronization with Evernote scheduled') return redirect('evernote_sync:edit_integration', integration.id)
def accept_webhook(request, webhook_secret_key): """ Webhooks recipient, as described here: https://dev.evernote.com/doc/articles/polling_notification.php """ if settings.EVERNOTE_WEBHOOK_SECRET_KEY != webhook_secret_key: # not an Evernote request return HttpResponse() # "Webhook reasons" we react on: Evernote note created, Evernote note # updated. Everything else is quietly ignored reasons = {'create', 'update'} reason = request.GET.get('reason') if reason not in reasons: return HttpResponse() # We react on userId and notebookGuid. If we have an integration to # sync, schedule a new evernote synchronization, but make sure we don't # perform sync more often than once in a minute try: user_id = int(request.GET.get('userId')) except (TypeError, ValueError): return HttpResponse() notebook_guid = request.GET.get('notebookGuid') # most likely, it's going to be no more than one cache object, and no # more than one integration. Two or more Evernote accounts connected to # the same client, or more than one Evernote integration per account # is an exception for cache in (EvernoteAccountCache.objects .filter(evernote_user_id=user_id) .select_related('user')): for integration in cache.user.integration_set.filter(service_id='evernote_sync'): notebooks = integration.settings.get('evernote_notebooks') or [] if notebook_guid in notebooks: with ctx(integration=integration, user=integration.user): logger.debug('Received Evernote webhook. Schedule Sync') subtask = tasks.sync_evernote.s(integration.id) schedule_with_rate_limit(integration.id, 'last_sync', subtask, timeout=120) return HttpResponse()
def accept_webhook(request, integration_id): """ Google Calendar webhook handler For more details see https://developers.google.com/google-apps/calendar/v3/push?hl=en_US#receiving-notifications """ try: channel_id = request.META['HTTP_X_GOOG_CHANNEL_ID'] resource_id = request.META['HTTP_X_GOOG_RESOURCE_ID'] resource_state = request.META['HTTP_X_GOOG_RESOURCE_STATE'] resource_uri = request.META['HTTP_X_GOOG_RESOURCE_URI'] token = request.META['HTTP_X_GOOG_CHANNEL_TOKEN'] except KeyError: # not a google request return HttpResponse() try: token_data = utils.validate_webhook_token(token) except PowerAppError: return HttpResponse() try: integration = Integration.objects.get(id=integration_id) except Integration.DoesNotExist: tasks.stop_channel.delay(token_data['u'], channel_id, resource_id) else: with ctx(integration=integration, user=integration.user): logging_extra = { 'channel_id': channel_id, 'token': token, 'resource_id': resource_id, 'resource_state': resource_state, 'resource_uri': resource_uri } logger.debug('Received GCal webhook. Schedule Sync', extra=logging_extra) subtask = tasks.sync_gcal.s(integration_id) schedule_with_rate_limit(integration_id, 'last_sync', subtask) return HttpResponse()
def accept_webhook(request, integration_id): """ Google Calendar webhook handler For more details see https://developers.google.com/google-apps/calendar/v3/push?hl=en_US#receiving-notifications """ try: channel_id = request.META['HTTP_X_GOOG_CHANNEL_ID'] resource_id = request.META['HTTP_X_GOOG_RESOURCE_ID'] resource_state = request.META['HTTP_X_GOOG_RESOURCE_STATE'] resource_uri = request.META['HTTP_X_GOOG_RESOURCE_URI'] token = request.META['HTTP_X_GOOG_CHANNEL_TOKEN'] except KeyError: # not a google request return HttpResponse() try: token_data = utils.validate_webhook_token(token) except PowerAppError: return HttpResponse() try: integration = Integration.objects.get(id=integration_id) except Integration.DoesNotExist: tasks.stop_channel.delay(token_data['u'], channel_id, resource_id) else: with ctx(integration=integration, user=integration.user): logging_extra = {'channel_id': channel_id, 'token': token, 'resource_id': resource_id, 'resource_state': resource_state, 'resource_uri': resource_uri} logger.debug('Received GCal webhook. Schedule Sync', extra=logging_extra) subtask = tasks.sync_gcal.s(integration_id) schedule_with_rate_limit(integration_id, 'last_sync', subtask) return HttpResponse()
def sync_now(request, integration_id): get_object_or_404(Integration, id=integration_id, user_id=request.user.id) subtask = tasks.sync_gcal.s(integration_id) schedule_with_rate_limit(integration_id, 'last_sync', subtask) messages.info(request, 'Synchronization with Google Calendar scheduled') return redirect('gcal_sync:edit_integration', integration_id)
def sync_now(request, integration_id): get_object_or_404(Integration, id=integration_id, user_id=request.user.id) subtask = tasks.sync_gcal.s(integration_id) schedule_with_rate_limit(integration_id, 'last_sync', subtask) messages.info(request, 'Synchronization with Google Calendar scheduled') return redirect('gcal_sync:edit_integration', integration_id)