import os

from huaweicloudsdkcore.exceptions import exceptions
from huaweicloudsdkcore.auth.credentials import BasicCredentials
from huaweicloudsdkgsl.v3.gsl_client import GslClient
from huaweicloudsdkgsl.v3.model.list_pro_price_plans_request import ListProPricePlansRequest
from huaweicloudsdkgsl.v3.region.gsl_region import GslRegion


class PricePlansApplication:
    # 认证用的ak和sk直接写到代码中有很大的安全风险,建议在配置文件或者环境变量中密文存放,使用时解密,确保安全;
    # 本示例以ak和sk保存在环境变量中来实现身份验证为例,运行本示例前请先在本地环境中设置环境变量HUAWEICLOUD_SDK_AK和HUAWEICLOUD_SDK_SK。
    __AK = os.environ["HUAWEICLOUD_SDK_AK"]
    __SK = os.environ["HUAWEICLOUD_SDK_SK"]

    # 华为云IAM服务访问终端地址
    __IAM_ENDPOINT = "https://<IamEndpoint>"

    def __init__(self):
        pass

    @staticmethod
    def main(args):
        # 创建认证
        auth = BasicCredentials(
            ak=PricePlansApplication.__AK,
            sk=PricePlansApplication.__SK,
        ).with_iam_endpoint(endpoint=PricePlansApplication.__IAM_ENDPOINT)

        gsl_client = GslClient.new_builder() \
            .with_credentials(credentials=auth) \
            .with_region(region=GslRegion.CN_NORTH_4) \
            .build()
        # 查询套餐列表
        PricePlansApplication.__get_price_plans_list(gsl_client)

    @staticmethod
    def __get_price_plans_list(gsl_client):
        offset = 1
        limit = 10
        # 运营商类型
        carrier_type = 1
        try:
            response = gsl_client.list_pro_price_plans(
                ListProPricePlansRequest(
                    offset=offset,
                    limit=limit,
                    carrier_type=carrier_type
                )
            )
            print(response)
        except exceptions.ClientRequestException as e:
            print(e.status_code)
            print(e.request_id)
            print(e.error_code)
            print(e.error_msg)


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