import json
from abc import ABC, abstractmethod
from lib.common import rest_client
from lib.services.api_gateway.base_client import BaseClient


class MonitoringClient(BaseClient):
    """Service client for API Gateway Monitoring resource."""

    def __init__(self, auth_provider, service, region, client=None, **kwargs):
        super(MonitoringClient, self).__init__(auth_provider, service, region, **kwargs)
        self.client = client

    def _parse_response(self, resp, body, schema):
        """Parse and validate response."""
        if body:
            body = json.loads(body)
        else:
            body = {}
        self.validate_response(schema, resp, body)
        return rest_client.ResponseBody(resp, body)

    def list_stage_metrics(self, stage_id):
        """Get stage metrics attributes for the Flava Monitoring dashboard."""
        url = self.client.list_stage_metrics_path(self.project_name, stage_id)
        resp, body = self.get(url)
        return self._parse_response(
            resp, body, self.client.get_schema().list_stage_metrics
        )


class MonitoringClientInterface(ABC):
    """Interface for cloud-specific implementations."""

    @abstractmethod
    def get_schema(self):
        """Return response schemas module."""
        pass

    @abstractmethod
    def list_stage_metrics_path(self, project, stage_id):
        """Return API endpoint path for stage metrics attributes."""
        pass
