import os

from huaweicloudsdkcdn.v2.cdn_client import CdnClient
from huaweicloudsdkcdn.v2.model.show_domain_location_stats_request import ShowDomainLocationStatsRequest
from huaweicloudsdkcdn.v2.model.show_domain_stats_request import ShowDomainStatsRequest
from huaweicloudsdkcdn.v2.model.show_top_url_request import ShowTopUrlRequest
from huaweicloudsdkcore.auth.credentials import GlobalCredentials
from huaweicloudsdkcore.exceptions.exceptions import ConnectionException
from huaweicloudsdkcore.exceptions.exceptions import RequestTimeoutException
from huaweicloudsdkcore.exceptions.exceptions import ServiceResponseException


class DomainStatistics:

    def __init__(self):
        pass

    @staticmethod
    def main(args):
        # 认证用的ak和sk直接写到代码中有很大的安全风险,建议在配置文件或者环境变量中密文存放,使用时解密,确保安全;
        # 本示例以ak和sk保存在环境变量中来实现身份验证为例,运行本示例前请先在本地环境中设置环境变量HUAWEICLOUD_SDK_AK和HUAWEICLOUD_SDK_SK。
        ak = os.environ["HUAWEICLOUD_SDK_AK"]
        sk = os.environ["HUAWEICLOUD_SDK_SK"]
        iam_endpoint = "<iam endpoint>"
        endpoint = "<endpoint>"
        auth = GlobalCredentials(ak, sk) \
            .with_iam_endpoint(iam_endpoint)
        client = CdnClient.new_builder() \
            .with_credentials(credentials=auth) \
            .with_endpoint(endpoint) \
            .build()

        # 按区域运营商查询域名统计数据
        DomainStatistics.show_domain_location_stats(client)

        # 查询统计域名数据
        DomainStatistics.show_domain_stats(client)

        # 查询TOP100 URL明细
        DomainStatistics.show_top_url(client)

    @staticmethod
    def show_domain_location_stats(client):
        # startTime,endTime以及interval均为请求参数,将值替换即可
        startTime = 1701849600000
        endTime = 1701856800000
        interval = 300
        domainName = "<domain_name>"
        action = "<action>"
        statType = "<stat_type>"
        country = "<country>"
        province = "<province>"
        isp = "<isp>"
        groupBy = "<group_by>"
        enterpriseProjectId = "<enterprise_project_id>"

        request = ShowDomainLocationStatsRequest()
        request.domain_name = domainName
        request.action = action
        request.start_time = startTime
        request.end_time = endTime
        request.stat_type = statType
        request.interval = interval
        request.country = country
        request.province = province
        request.isp = isp
        request.group_by = groupBy
        request.enterprise_project_id = enterpriseProjectId

        try:
            response = client.show_domain_location_stats(request)
            result_code = response.status_code
            if result_code == 200:
                print("show domain location stats success")
                print(response)
        except ConnectionException:
            print("show domain location stats ConnectionException")
        except RequestTimeoutException:
            print("show domain location stats RequestTimeoutException")
        except ServiceResponseException as e:
            print("show domain location stats ServiceResponseException")
            print(e.status_code)
            print(e.request_id)
            print(e.error_code)
            print(e.error_msg)

    @staticmethod
    def show_domain_stats(client):
        # startTime,endTime以及interval均为请求参数,将值替换即可
        startTime = 1701849600000
        endTime = 1701856800000
        interval = 300
        domainName = "<domain_name>"
        action = "<action>"
        statType = "<stat_type>"
        groupBy = "<group_by>"
        serviceArea = "<service_area>"
        enterpriseProjectId = "<enterprise_project_id>"

        request = ShowDomainStatsRequest()
        request.domain_name = domainName
        request.action = action
        request.start_time = startTime
        request.end_time = endTime
        request.stat_type = statType
        request.interval = interval
        request.group_by = groupBy
        request.service_area = serviceArea
        request.enterprise_project_id = enterpriseProjectId

        try:
            response = client.show_domain_stats(request)
            result_code = response.status_code
            if result_code == 200:
                print("show domain stats success")
                print(response)
        except ConnectionException:
            print("show domain stats ConnectionException")
        except RequestTimeoutException:
            print("show domain stats RequestTimeoutException")
        except ServiceResponseException as e:
            print("show domain stats ServiceResponseException")
            print(e.status_code)
            print(e.request_id)
            print(e.error_code)
            print(e.error_msg)

    @staticmethod
    def show_top_url(client):
        # startTime,endTime均为请求参数,将值替换即可
        startTime = 1701849600000
        endTime = 1701856800000
        domainName = "<domain_name>"
        statType = "<stat_type>"
        serviceArea = "<service_area>"
        enterpriseProjectId = "<enterprise_project_id>"

        request = ShowTopUrlRequest()
        request.domain_name = domainName
        request.start_time = startTime
        request.end_time = endTime
        request.stat_type = statType
        request.service_area = serviceArea
        request.enterprise_project_id = enterpriseProjectId

        try:
            response = client.show_top_url(request)
            result_code = response.status_code
            if result_code == 200:
                print("show top url success")
                print(response)
        except ConnectionException:
            print("show top url ConnectionException")
        except RequestTimeoutException:
            print("show top url RequestTimeoutException")
        except ServiceResponseException as e:
            print("show top url ServiceResponseException")
            print(e.status_code)
            print(e.request_id)
            print(e.error_code)
            print(e.error_msg)


if __name__ == "__main__":
    DomainStatistics().main(any)