def update_subscription(self, subscription): """ A temp patch for bug https://github.com/OneDrive/onedrive-sdk-python/issues/95. """ self.content_type = "application/json" self.method = "PATCH" entity = onedrivesdk.Subscription( json.loads(self.send(subscription).content)) return entity
def handle(self): logging.info('Updating webhook for Drive %s.', self.repo.drive.id) item_request = self.repo.authenticator.client.item( drive=self.repo.drive.id, path='/') expiration_time = datetime.utcnow() + timedelta( seconds=self.repo.context.config['webhook_renew_interval_sec']) try: if self.subscription_id is None: subscription = od_api_helper.create_subscription( item_request, self.repo, self.webhook_worker.webhook_url, expiration_time) else: subscription = onedrivesdk.Subscription() subscription.id = self.subscription_id subscription.notification_url = self.webhook_worker.webhook_url subscription.expiration_date_time = expiration_time subscription = od_api_helper.item_request_call( self.repo, item_request.subscriptions[self.subscription_id].update, subscription) self.webhook_worker.add_subscription(subscription, self.repo) logging.info('Webhook for Drive %s updated.', self.repo.drive.id) return subscription except onedrivesdk.error.OneDriveError as e: logging.error('Error: %s', e) return None
def create_subscription(folder_item_request, repo, webhook_url, expiration_time): """ :param onedrivesdk.ItemRequestBuilder folder_item_request: :param onedrived.od_repo.OneDriveLocalRepository repo: :param str webhook_url: :param datetime.datetime.datetime expiration_time: :return onedrivesdk.Subscription: """ subscriptions_collection_req = folder_item_request.subscriptions subscription_req_builder = onedrivesdk.SubscriptionRequestBuilder( subscriptions_collection_req._request_url, subscriptions_collection_req._client) subscription_req = item_request_call(repo, subscription_req_builder.request) subscription_req.content_type = "application/json" subscription_req.method = "POST" subscription = onedrivesdk.Subscription() subscription.notification_url = webhook_url subscription.expiration_date_time = expiration_time return onedrivesdk.Subscription( json.loads(subscription_req.send(subscription).content))
def test_execution(self): self.worker.start() notification_data = json.loads(get_resource('data/webhook_notification.json', pkg_name='tests')) subscription = onedrivesdk.Subscription() subscription.id = notification_data['subscriptionId'] self.worker.add_subscription(subscription, self.repo) # Send a notification. self.worker.queue_input(json.dumps({'value': [notification_data]}).encode('utf-8')) # Duplicate notifications should be ignored. self.worker.queue_input(json.dumps(notification_data).encode('utf-8')) # Unknown subscriptions should be ignored. notification_data['subscriptionId'] = '233' self.worker.queue_input(json.dumps(notification_data).encode('utf-8')) self.assertTrue(self.callback_called_sem.acquire(timeout=3)) self.assertFalse(self.callback_called_sem.acquire(timeout=1)) self.assertEqual([self.repo], self.callback_repos) self.assertEqual(1, self.callback_count)