# Path: isserviceup/services/models/service.py
# class Service(object):
# 
#     @property
#     def id(self):
#         return self.__class__.__name__
# 
#     @property
#     def status_url(self):
#         raise NotImplemented()
# 
#     @property
#     def icon_url(self):
#         raise NotImplemented()
# 
#     @property
#     def name(self):
#         raise NotImplemented()
# 
#     def get_status(self):
#         raise NotImplemented()
# 
# class Status(Enum):
#     ok = 1              # green
#     maintenance = 2     # blue
#     minor = 3           # yellow
#     major = 4           # orange
#     critical = 5        # red
#     unavailable = 6     # gray


import requests
from decouple import config
from isserviceup.services.models.service import Service, Status
        # Will check if the Weblate JSON API is OK.
        self.check_json = config('WEBLATE_CHECK_JSON', default=True, cast=bool)
        # If you need to monitor multiple Weblate instances and need each one
        # to have a different behaviour: extend this class, override the
        # __init__ function, call super, and then override these properties.

    def _check_http(self):
        request = requests.get(self.status_url)
        return request.ok

    def _check_json(self):
        request = requests.get('{base}/api/projects'.format(
            base=self.status_url.strip('/')
        ))
        try:
            if request.json():
                return True
        except ValueError as error:
            print('Value error: {msg}'.format(msg=error.message))
        return False

    def get_status(self):
        status = [
            not self.check_http or self._check_http(),
            not self.check_json or self._check_json(),
        ]
        if all(status):
            return Status.ok
        elif any(status):
            return Status.major

# Path: isserviceup/services/models/service.py
# class Service(object):
# 
#     @property
#     def id(self):
#         return self.__class__.__name__
# 
#     @property
#     def status_url(self):
#         raise NotImplemented()
# 
#     @property
#     def icon_url(self):
#         raise NotImplemented()
# 
#     @property
#     def name(self):
#         raise NotImplemented()
# 
#     def get_status(self):
#         raise NotImplemented()
# 
# class Status(Enum):
#     ok = 1              # green
#     maintenance = 2     # blue
#     minor = 3           # yellow
#     major = 4           # orange
#     critical = 5        # red
#     unavailable = 6     # gray


# Path: isserviceup/services/models/weblate.py
import requests
from decouple import config
from isserviceup.services.models.service import Service, Status

        # Will check if the Weblate JSON API is OK.
        self.check_json = config('WEBLATE_CHECK_JSON', default=True, cast=bool)
        # If you need to monitor multiple Weblate instances and need each one
        # to have a different behaviour: extend this class, override the
        # __init__ function, call super, and then override these properties.

    def _check_http(self):
        request = requests.get(self.status_url)
        return request.ok

    def _check_json(self):
        request = requests.get('{base}/api/projects'.format(
            base=self.status_url.strip('/')
        ))
        try:
            if request.json():
                return True
        except ValueError as error:
            print('Value error: {msg}'.format(msg=error.message))
        return False

    def get_status(self):
        status = [
            not self.check_http or self._check_http(),
            not self.check_json or self._check_json(),
        ]
        if all(status):
            return Status.ok
        elif any(status):
            return Status.major

        return Status.critical