import logging

import requests

from ..conf import settings


logger = logging.getLogger(__name__)


class WebHookNotify(object):

    CREDS_KEY = 'webhook'
    POST_KEY = 'message'

    def __init__(self, creds_key=None, value=None, post_key=None):
        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()

    @staticmethod
    def load_url(creds_key, value):
        if value:
            return value
        else:
            webhook_creds = settings().creds[creds_key]
            return webhook_creds['url']

    def post(self, report, **kwargs):
        response = self.session.post(
            url=self.url,
            data=self.payload(report),
        )
        logger.debug(response.text)
        response.raise_for_status()
        return response

    def configure_session(self):
        pass

    def payload(self, report):
        return {self.post_key: report}

    __call__ = post


def webhook_factory(klass):
    def notify_factory(conf, value):
        return klass(value=value)
    return notify_factory
