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

0. 版本说明

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

1. 简介

基于华为云Python SDK, 与SIM卡自定义属性相关的操作

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=SimAttributesApplication.__AK,
     sk=SimAttributesApplication.__SK,
     ).with_iam_endpoint(endpoint=SimAttributesApplication.__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. 示例代码

使用如下代码进行SIM卡自定义属性相关的操作,调用前请根据实际情况替换变量:

class SimAttributesApplication:
   # 认证用的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=SimAttributesApplication.__AK,
         sk=SimAttributesApplication.__SK,
      ).with_iam_endpoint(endpoint=SimAttributesApplication.__IAM_ENDPOINT)

      gsl_client = GslClient.new_builder()
         .with_credentials(credentials=auth)
         .with_region(region=GslRegion.CN_NORTH_4)
         .build()
      # 创建自定义属性
      SimAttributesApplication.__create_attribute(gsl_client)
      # 查询自定义属性列表
      SimAttributesApplication.__list_attributes(gsl_client)
      # 批量设置SIM卡自定义属性
      SimAttributesApplication.__batch_set_attributes(gsl_client)
      # 启用自定义属性
      SimAttributesApplication.__enable_attribute(gsl_client)
      # 修改自定义属性名称
      SimAttributesApplication.__update_attribute(gsl_client)
      # 禁用自定义属性
      SimAttributesApplication.__disable_attribute(gsl_client)

   @staticmethod
   def __create_attribute(gsl_client):
      cust_attr_name = "SDK测试自定义属性"
      try:
         response = gsl_client.create_attribute(
            CreateAttributeRequest(
               body=AddOrModifyAttributeReq(
                  cust_attr_name=cust_attr_name
               )
            )
         )
         print(response)
      except exceptions.ClientRequestException as e:
         SimAttributesApplication.__handle_exception(e)

   @staticmethod
   def __list_attributes(gsl_client):
      # 需要查询的自定义属性名,如果为空则查询全部自定义属性
      cust_attr_name = "SDK测试自定义属性"
      limit = 10
      offset = 1
      # 自定义属性状态,0表示未启用, 1表示已启用
      status = 0
      try:
         response = gsl_client.list_attributes(
            ListAttributesRequest(
               cust_attr_name=cust_attr_name,
               limit=limit,
               offset=offset,
               status=status
            )
         )
         print(response)
      except exceptions.ClientRequestException as e:
         SimAttributesApplication.__handle_exception(e)

   @staticmethod
   def __batch_set_attributes(gsl_client):
      # 需要修改自定义属性的列表请求
      attributes = []
      attribute_req1 = AttributeReq()
      attribute_req1.sim_card_id = 1234567
      attribute_req1.customer_attribute1 = "自定义属性1测试"
      attribute_req1.customer_attribute2 = "自定义属性2测试"
      attribute_req1.customer_attribute3 = "自定义属性3测试"
      attribute_req1.customer_attribute4 = "自定义属性4测试"
      attribute_req1.customer_attribute5 = "自定义属性5测试"
      attribute_req1.customer_attribute6 = "自定义属性6测试"
      attributes.append(attribute_req1)
      try:
         response = gsl_client.batch_set_attributes(
            BatchSetAttributesRequest(
               body=BatchSetAttributesReq(
                  attributes=attributes
               )
            )
         )
         print(response)
      except exceptions.ClientRequestException as e:
         SimAttributesApplication.__handle_exception(e)

   @staticmethod
   def __enable_attribute(gsl_client):
      attribute_id = 12
      try:
         response = gsl_client.enable_attribute(
            EnableAttributeRequest(
               attribute_id=attribute_id
            )
         )
         print(response)
      except exceptions.ClientRequestException as e:
         SimAttributesApplication.__handle_exception(e)

   @staticmethod
   def __update_attribute(gsl_client):
      attribute_id = 12
      cust_attr_name = "SDK测试自定义属性新"
      try:
         response = gsl_client.update_attribute(
            UpdateAttributeRequest(
               attribute_id=attribute_id,
               body=AddOrModifyAttributeReq(
                  cust_attr_name=cust_attr_name
               )
            )
         )
         print(response)
      except exceptions.ClientRequestException as e:
         SimAttributesApplication.__handle_exception(e)

   @staticmethod
   def __disable_attribute(gsl_client):
      attribute_id = 12
      try:
         response = gsl_client.disable_attribute(
            DisableAttributeRequest(
               attribute_id=attribute_id
            )
         )
         print(response)
      except exceptions.ClientRequestException as e:
         SimAttributesApplication.__handle_exception(e)

   @staticmethod
   def __handle_exception(e):
      print(e.status_code)
      print(e.request_id)
      print(e.error_code)
      print(e.error_msg)


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

6. 运行结果

通过调用list_attributes接口,获取当前自定义属性信息: 请求参数:

GET https://{endpoint}/v1/attributes?offset=1&limit=10&status=1

返回响应:

{
   "limit": 10,
   "offset": 1,
   "count": 1,
   "attributes": [
      {
         "id": 5229570883266944,
         "default_attr_name_cn": "自定义属性一",
         "default_attr_name_en": "Custom attribute 1",
         "cust_attr_name": "333444",
         "status": 1,
         "create_time": "2023-04-08T08:10:18.000+00:00",
         "modify_time": "2023-10-31T01:28:03.000+00:00"
      }
   ]
}

其中,列表返回的id可以用来做之后例如自定义属性停用,启用等操作的入参。

7. 参考

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

8. 修订记录

发布日期 文档版本 修订说明
2022-06-12 1.0.1 文档首次发布
2022-08-02 1.0.2 更新依赖
2023-11-08 1.0.3 更新文档