# Path: kibitzr/notifier/webhook.py
# class WebHookNotify(object):
# 
#     CREDS_KEY = 'webhook'
#     POST_KEY = 'message'
# 
#     def __init__(self, creds_key=None, conf=None, value=None, post_key=None):
#         import requests
#         self.session = requests.Session()
#         self.url = self.load_url(creds_key or self.CREDS_KEY, value)
#         self.post_key = post_key or self.POST_KEY
#         self.configure_session()
# 
#     def load_url(self, creds_key, value):
#         if value:
#             return value
#         else:
#             webhook_creds = settings().creds[creds_key]
#             return webhook_creds['url']
# 
#     def post(self, report):
#         response = self.session.post(
#             self.url,
#             data=self.payload(report),
#         )
#         logger.debug(response.text)
#         response.raise_for_status()
#         return response
#     __call__ = post
# 
#     def configure_session(self):
#         pass
# 
#     def payload(self, report):
#         return {self.post_key: report}
# 
# def webhook_factory(klass):
#     def notify_factory(conf, value):
#         return klass(conf=conf, value=value)
#     return notify_factory
# 
# Path: kibitzr/conf.py
# def settings():
#     """
#     Returns singleton instance of settings
#     """
#     return ReloadableSettings.instance()


from .webhook import WebHookNotify, webhook_factory
from ..conf import settings

class MailgunNotify(WebHookNotify):

    CREDS_KEY = 'mailgun'

    def __init__(self, conf, value, **kwargs):
        self.mailgun_creds = settings().creds.get('mailgun', {})
        self.mailgun_creds.update(value or {})
        domain = self.mailgun_creds['domain']
        self.context = {
            'subject': 'Kibitzr update for ' + conf['name'],
            'from': 'Kibitzr <mailgun@{0}>'.format(domain),
            'to': [self.mailgun_creds['to']],
        }
        super(MailgunNotify, self).__init__(conf=conf, value=value, **kwargs)

    def load_url(self, creds_key, value):
        return ('https://api.mailgun.net/v3/{0}/messages'
                .format(self.mailgun_creds['domain']))

    def configure_session(self):
        self.session.auth = ('api', self.mailgun_creds['key'])

    def payload(self, report):
        return dict(
            self.context,
            text=report,
        )



# Path: kibitzr/notifier/webhook.py
# class WebHookNotify(object):
# 
#     CREDS_KEY = 'webhook'
#     POST_KEY = 'message'
# 
#     def __init__(self, creds_key=None, conf=None, value=None, post_key=None):
#         import requests
#         self.session = requests.Session()
#         self.url = self.load_url(creds_key or self.CREDS_KEY, value)
#         self.post_key = post_key or self.POST_KEY
#         self.configure_session()
# 
#     def load_url(self, creds_key, value):
#         if value:
#             return value
#         else:
#             webhook_creds = settings().creds[creds_key]
#             return webhook_creds['url']
# 
#     def post(self, report):
#         response = self.session.post(
#             self.url,
#             data=self.payload(report),
#         )
#         logger.debug(response.text)
#         response.raise_for_status()
#         return response
#     __call__ = post
# 
#     def configure_session(self):
#         pass
# 
#     def payload(self, report):
#         return {self.post_key: report}
# 
# def webhook_factory(klass):
#     def notify_factory(conf, value):
#         return klass(conf=conf, value=value)
#     return notify_factory
# 
# Path: kibitzr/conf.py
# def settings():
#     """
#     Returns singleton instance of settings
#     """
#     return ReloadableSettings.instance()


# Path: kibitzr/notifier/mailgun.py
from .webhook import WebHookNotify, webhook_factory
from ..conf import settings


class MailgunNotify(WebHookNotify):

    CREDS_KEY = 'mailgun'

    def __init__(self, conf, value, **kwargs):
        self.mailgun_creds = settings().creds.get('mailgun', {})
        self.mailgun_creds.update(value or {})
        domain = self.mailgun_creds['domain']
        self.context = {
            'subject': 'Kibitzr update for ' + conf['name'],
            'from': 'Kibitzr <mailgun@{0}>'.format(domain),
            'to': [self.mailgun_creds['to']],
        }
        super(MailgunNotify, self).__init__(conf=conf, value=value, **kwargs)

    def load_url(self, creds_key, value):
        return ('https://api.mailgun.net/v3/{0}/messages'
                .format(self.mailgun_creds['domain']))

    def configure_session(self):
        self.session.auth = ('api', self.mailgun_creds['key'])

    def payload(self, report):
        return dict(
            self.context,
            text=report,
        )



notify_factory = webhook_factory(MailgunNotify)