文件最后提交记录最后更新时间
2 年前
2 年前
2 年前
README

0. 版本说明

本示例基于华为云Python SDK开发

1. 功能介绍

基于华为云Python SDK, 对现有套餐列表进行查询

2. 开发前准备

已注册华为云, 并完成实名认证; 已获取华为云账号对应的Access Key(AK)和Secret Access Key(SK); 具备开发环境,支持Python3及以上版本

3. 安装SDK

您可以通过加入相应的依赖项获取和安装SDK。

安裝核心库:

pip install huaweicloudsdkcore

安装sdk:

pip install huaweicloudsdkgsl

4. 开始使用

4.1 导入依赖模块

from huaweicloudsdkcore.auth.credentials import BasicCredentials
from huaweicloudsdkcore.exceptions import exceptions
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

4.2.1 初始化认证信息

auth = BasicCredentials(
     ak=PricePlansApplication.__AK,
     sk=PricePlansApplication.__SK,
     ).with_iam_endpoint(endpoint=PricePlansApplication.__IAM_ENDPOINT)

__IAM_ENDPOINT: 华为云IAM服务访问终端地址,详情见https://developer.huaweicloud.com/endpoint?IAM

4.2.2 初始化客户端

gsl_client = GslClient.new_builder()
    .with_credentials(credentials=auth)
    .with_region(region=GslRegion.CN_NORTH_4)
    .build()

5. 示例代码

使用如下代码进行套餐列表查询,调用前请根据实际情况替换变量:

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)

6. 运行结果

调用list_pro_price_plans查询运营商类型为2(中国电信)的套餐,返回第一页,每一页10条数据。

GET https://{endpoint}/v1/price-plans?offset=1&limit=10&carrier_type=2

返回结果如下:

{
  "limit" : 10,
  "offset" : 1,
  "count" : 1,
  "price_plans" : [ {
    "price_plan_id" : "100000",
    "price_plan_name" : "中国电信消费级每月30M联接服务(1年)",
    "description" : "中国电信消费级每月30M联接服务(1年)",
    "flow_total" : 10,
    "package_type" : 1,
    "period" : 1,
    "period_type" : 20,
    "effect_type" : 1,
    "silent_period_day" : 6,
    "silent_period_unit" : 2,
    "auto_renew" : false,
    "location_desc" : "中国",
    "location_type" : 1,
    "sim_type" : 3,
    "carrier_type" : 2,
    "price" : 1
  } ]
}

7. 参考

更多信息请参考全球SIM联接 GSL

8. 修订记录

发布日期 文档版本 修订说明
2021-09-28 1.0.1 文档首次发布
2022-04-12 1.0.2 更新依赖,修改部分接口
2022-06-12 1.0.3 更新依赖,新增标签管理,自定义属性管理接口说明
2022-08-02 1.0.4 更新依赖
2023-11-08 1.0.5 更新文档