def query(self, po_name, method_params, api_key):
        if not hasattr(self, "client_connection"):
            if self.certificate_name:
                schema = "https://"
            else:
                schema = "http://"

            endpoint = schema + self.load_balancer_dns_name
            verify_cert = not self.certificate_is_self_signed
            self.client_connection = PredictiveServiceClient(
                endpoint=endpoint, api_key=api_key, should_verify_certificate=verify_cert
            )

        return self.client_connection.query(po_name, method_params)
class PredictiveServiceEnvironment:
    _PORT_NUM = 9005

    def _poke_host(self, host_name):
        _logger.info("Notifying: %s" % host_name)
        data = json.dumps({"admin key": self.admin_key})
        url = "http://%s:%d/control/poke" % (host_name, PredictiveServiceEnvironment._PORT_NUM)
        self._post(url, data=data)

    def __init__(self, admin_key):
        self.admin_key = admin_key
        self.certificate_name = None

        self._request_schema = None
        self._should_verify_certificate = None

    def get_status(self):
        pass

    def launch(self):
        pass

    def poke(self):
        pass

    def query(self, po_name, method_params, api_key):
        if not hasattr(self, "client_connection"):
            if self.certificate_name:
                schema = "https://"
            else:
                schema = "http://"

            endpoint = schema + self.load_balancer_dns_name
            verify_cert = not self.certificate_is_self_signed
            self.client_connection = PredictiveServiceClient(
                endpoint=endpoint, api_key=api_key, should_verify_certificate=verify_cert
            )

        return self.client_connection.query(po_name, method_params)

    def get_metrics_url(self):
        request_data = {"admin key": self.admin_key}
        request_url = "%s://%s/control/cloudwatch_url" % (self.__get_schema(), self.load_balancer_dns_name)
        resp = self._post(request_url, data=json.dumps(request_data))
        return resp.text

    def get_metrics(self, start_date=None, end_date=None):
        if start_date:
            assert isinstance(start_date, datetime)

        if end_date:
            assert isinstance(end_date, datetime)

        if (start_date and not end_date) or (end_date and not start_date):
            raise Exception("Getting metrics for a date range requires a " "start and end date")

        request_data = {"admin key": self.admin_key}

        if start_date:
            if (start_date > datetime.utcnow()) or (end_date > datetime.utcnow()):
                raise Exception("Date ranges may not extend past the current " "datetime")

            request_data.update({"start_date": start_date.isoformat(), "end_date": end_date.isoformat()})

        request_url = "%s://%s/control/metrics" % (self.__get_schema(), self.load_balancer_dns_name)
        resp = self._post(request_url, data=json.dumps(request_data))

        try:
            return resp.json()
        except Exception as e:
            raise RuntimeError("Cannot get metrics from service, error: %s" % resp.text)

    def _get_metrics(self, name, start_time, end_time, period):
        pass

    def terminate(self, del_flag):
        pass

    def _post(self, url, data):
        if not self._should_verify_certificate:
            self._should_verify_certificate = self.__should_verify_certificate()

        headers = {"content-type": "application/json"}
        return post(url=url, data=data, headers=headers, verify=self._should_verify_certificate, timeout=10)

    def _get_model_versions_for_host(self, host_dns_name):
        data = json.dumps({"admin key": self.admin_key})
        url = "http://%s:%d/control/list_objects" % (host_dns_name, PredictiveServiceEnvironment._PORT_NUM)
        resp = self._post(url, data=data)
        try:
            response = resp.text
            return json.loads(response)
        except Exception as e:
            return {"error": "Cannot get model info from host, error: %s" % resp.text}

    def __is_using_certificates(self):
        return hasattr(self, "certificate_name") and self.certificate_name

    def __get_schema(self):
        if not self._request_schema:
            self._request_schema = "https" if self.__is_using_certificates() else "http"
        return self._request_schema

    def __should_verify_certificate(self):
        return self.__is_using_certificates() and not (
            hasattr(self, "certificate_is_self_signed") and self.certificate_is_self_signed
        )