# Path: onedrived/od_models/webhook_notification.py
# class WebhookNotification:
# 
#     """ https://dev.onedrive.com/resources/webhookNotifiation.htm """
# 
#     def __init__(self, prop_dict):
#         self._prop_dict = prop_dict
# 
#     @property
#     def context(self):
#         """
#         :return str | None:
#             An optional string value that is passed back in the notification message for this subscription.
#         """
#         try:
#             return self._prop_dict['context']
#         except KeyError:
#             return None
# 
#     @property
#     def expiration_datetime(self):
#         """
#         :return arrow.Arrow: The date and time when the subscription will expire if not updated or renewed.
#         """
#         return od_dateutils.str_to_datetime(self._prop_dict['expirationDateTime'])
# 
#     @property
#     def resource(self):
#         """
#         :return str: URL to the item where the subscription is registered.
#         """
#         return self._prop_dict['resource']
# 
#     @property
#     def subscription_id(self):
#         """
#         :return str: The unique identifier for the subscription resource.
#         """
#         return self._prop_dict['subscriptionId']
# 
#     @property
#     def tenant_id(self):
#         """
#         :return str:
#             Unique identifier for the tenant which generated this notification.
#             This is only returned for OneDrive for Business and SharePoint.
#         """
#         try:
#             return self._prop_dict['tenantId']
#         except KeyError:
#             return None
# 
#     @property
#     def user_id(self):
#         """
#         :return str: Unique identifier for the drive which generated this notification.
#         """
#         return self._prop_dict['userId']


import http.server
import json
import logging
import os
import threading
import queue
import urllib.parse
from .od_models.webhook_notification import WebhookNotification
        from .od_webhooks.http_server import WebhookConfig, WebhookListener
        from .od_webhooks.ngrok_server import WebhookConfig, WebhookListener



def get_webhook_server(context):
    """
    :param onedrived.od_context.UserContext context:
    """
    if context.config['webhook_type'] == 'direct':
        wh_config = WebhookConfig(host=context.config['webhook_host'], port=context.config['webhook_port'])
    elif context.config['webhook_type'] == 'ngrok':
        ngrok_config_file = context.config_dir + '/' + context.DEFAULT_NGROK_CONF_FILENAME
        if not os.path.isfile(ngrok_config_file):
            ngrok_config_file = None
        wh_config = WebhookConfig(port=context.config['webhook_port'], ngrok_config_path=ngrok_config_file)
    else:
        raise ValueError('Unsupported webhook type: "%s".' % context.config['webhook_type'])
    return WebhookListener(wh_config, OneDriveWebhookHandler)


def parse_notification_body(body):
    try:
        data = json.loads(body.decode('utf-8'))
        try:
            subscription_ids = set([WebhookNotification(v).subscription_id for v in data['value']])
        except KeyError:

# Path: onedrived/od_models/webhook_notification.py
# class WebhookNotification:
# 
#     """ https://dev.onedrive.com/resources/webhookNotifiation.htm """
# 
#     def __init__(self, prop_dict):
#         self._prop_dict = prop_dict
# 
#     @property
#     def context(self):
#         """
#         :return str | None:
#             An optional string value that is passed back in the notification message for this subscription.
#         """
#         try:
#             return self._prop_dict['context']
#         except KeyError:
#             return None
# 
#     @property
#     def expiration_datetime(self):
#         """
#         :return arrow.Arrow: The date and time when the subscription will expire if not updated or renewed.
#         """
#         return od_dateutils.str_to_datetime(self._prop_dict['expirationDateTime'])
# 
#     @property
#     def resource(self):
#         """
#         :return str: URL to the item where the subscription is registered.
#         """
#         return self._prop_dict['resource']
# 
#     @property
#     def subscription_id(self):
#         """
#         :return str: The unique identifier for the subscription resource.
#         """
#         return self._prop_dict['subscriptionId']
# 
#     @property
#     def tenant_id(self):
#         """
#         :return str:
#             Unique identifier for the tenant which generated this notification.
#             This is only returned for OneDrive for Business and SharePoint.
#         """
#         try:
#             return self._prop_dict['tenantId']
#         except KeyError:
#             return None
# 
#     @property
#     def user_id(self):
#         """
#         :return str: Unique identifier for the drive which generated this notification.
#         """
#         return self._prop_dict['userId']


# Path: onedrived/od_webhook.py
import http.server
import json
import logging
import os
import threading
import queue
import urllib.parse
from .od_models.webhook_notification import WebhookNotification
        from .od_webhooks.http_server import WebhookConfig, WebhookListener
        from .od_webhooks.ngrok_server import WebhookConfig, WebhookListener




def get_webhook_server(context):
    """
    :param onedrived.od_context.UserContext context:
    """
    if context.config['webhook_type'] == 'direct':
        wh_config = WebhookConfig(host=context.config['webhook_host'], port=context.config['webhook_port'])
    elif context.config['webhook_type'] == 'ngrok':
        ngrok_config_file = context.config_dir + '/' + context.DEFAULT_NGROK_CONF_FILENAME
        if not os.path.isfile(ngrok_config_file):
            ngrok_config_file = None
        wh_config = WebhookConfig(port=context.config['webhook_port'], ngrok_config_path=ngrok_config_file)
    else:
        raise ValueError('Unsupported webhook type: "%s".' % context.config['webhook_type'])
    return WebhookListener(wh_config, OneDriveWebhookHandler)


def parse_notification_body(body):
    try:
        data = json.loads(body.decode('utf-8'))
        try:
            subscription_ids = set([WebhookNotification(v).subscription_id for v in data['value']])
        except KeyError:

            subscription_ids = (WebhookNotification(data).subscription_id,)