# Copyright (C) 2020-2022 HERE Global B.V. and its affiliate(s).
# All rights reserved.
#
# This software and other materials contain proprietary information
# controlled by HERE and are protected by applicable copyright legislation.
# Any use and utilization of this software and other materials and
# disclosure to any third parties is conditional upon having a separate
# agreement with HERE for the access, use, utilization or disclosure of this
# software. In the absence of such agreement, the use of the software is not
# allowed.

"""
This module contains a :class:`ServiceApi` class to perform API operations.

It is an extension of :class:`BaseApi` that provides APIs for REST calls
"""

from typing import Optional

from here.platform.api.base_api import BaseApi
from here.platform.auth import Auth
from here.platform.config import ApplicationConfig, PlatformConfig
from here.platform.exceptions import PlatformException


class ServiceApi(BaseApi):
    """
    This class provides methods that perform common service operations

    The registry service provides basic service management operations.
    It manages all platform service resources needed for different kinds
    of services and operations on them.
    """

    def __init__(
        self,
        service_base_url: str,
        platform_config: PlatformConfig,
        application_config: ApplicationConfig,
        auth: Auth,
        proxies: Optional[dict],
    ):
        """
        Instantiate ServiceApi object.

        :param service_base_url: service base url
        :param platform_config: a mandatory :class:`PlatformConfig` object to provide
        configuration information for the API.
        :param application_config: a mandatory :class:`ApplicationConfig` object to provide
        configuration information for the API.
        :param auth: an Authentication instance.
        :param proxies: an optional proxy configuration. Defaults to the environment proxy
        configuration.
        """
        super(ServiceApi, self).__init__(
            platform_config=platform_config,
            application_config=application_config,
            auth=auth,
            proxies=proxies,
        )
        self.service_base_url = service_base_url

    def health(self) -> dict:
        """
        Gets current health of the service

        :return: health status of the service
        :raises PlatformException: If platform responds with an HTTP error.
        """
        path = "/health"
        url = self.format_url(self.service_base_url, path)
        resp = self.get(url)
        if resp.status_code == 200:
            resp_json: dict = resp.json()
            return resp_json
        else:
            raise PlatformException(resp)

    def version(self) -> dict:
        """
        Gets detailed version dictionary of the service

        :return: detailed version information of the service
        :raises PlatformException: If platform responds with an HTTP error.
        """
        path = "/version"
        url = self.format_url(self.service_base_url, path)
        resp = self.get(url)
        if resp.status_code == 200:
            resp_json: dict = resp.json()
            return resp_json
        else:
            raise PlatformException(resp)
