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.add_or_modify_tag_req import AddOrModifyTagReq
from huaweicloudsdkgsl.v3.model.batch_set_tags_req import BatchSetTagsReq
from huaweicloudsdkgsl.v3.model.batch_set_tags_request import BatchSetTagsRequest
from huaweicloudsdkgsl.v3.model.create_tag_request import CreateTagRequest
from huaweicloudsdkgsl.v3.model.delete_tag_request import DeleteTagRequest
from huaweicloudsdkgsl.v3.model.list_tags_request import ListTagsRequest
from huaweicloudsdkgsl.v3.region.gsl_region import GslRegion
4.2.1 初始化认证信息
auth = BasicCredentials(
ak=SimTagApplication.__AK,
sk=SimTagApplication.__SK,
).with_iam_endpoint(endpoint=SimTagApplication.__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 SimTagApplication:
# 认证用的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=SimTagApplication.__AK,
sk=SimTagApplication.__SK,
).with_iam_endpoint(endpoint=SimTagApplication.__IAM_ENDPOINT)
gsl_client = GslClient.new_builder()
.with_credentials(credentials=auth)
.with_region(region=GslRegion.CN_NORTH_4)
.build()
# 创建SIM卡标签
SimTagApplication.__create_tag(gsl_client)
# 查询SIM卡标签列表
SimTagApplication.__list_tags(gsl_client)
# 批量绑定/取消SIM卡
SimTagApplication.__batch_set_tags(gsl_client)
# 删除SIM卡标签
SimTagApplication.__delete_tag(gsl_client)
@staticmethod
def __create_tag(gsl_client):
tag_name = "SDK测试标签"
try:
response = gsl_client.create_tag(
CreateTagRequest(
body=AddOrModifyTagReq(
tag_name=tag_name
)
)
)
print(response)
except exceptions.ClientRequestException as e:
SimTagApplication.__handle_exception(e)
@staticmethod
def __list_tags(gsl_client):
# 需要查询的标签名,如果为空则查询全部标签
tag_name = "SDK测试标签"
limit = 10
offset = 1
# 标签状态,0表示未使用, 1表示使用中
status = 0
try:
response = gsl_client.list_tags(
ListTagsRequest(
tag_name=tag_name,
limit=limit,
offset=offset,
status=status
)
)
print(response)
except exceptions.ClientRequestException as e:
SimTagApplication.__handle_exception(e)
@staticmethod
def __batch_set_tags(gsl_client):
# 需要绑定标签的SIM卡列表
sim_card_ids = []
sim_card_ids.append(3449878744318976)
sim_card_ids.append(3449878744318977)
# SIM卡对应的标签列表,会清除SIM上原有标签,绑定新的标签。如果标签列表为空,表示清空sim卡绑定的标签。
tag_ids = []
tag_ids.append(12)
tag_ids.append(13)
try:
response = gsl_client.batch_set_tags(
BatchSetTagsRequest(
body=BatchSetTagsReq(
sim_card_ids=sim_card_ids,
tag_ids=tag_ids
)
)
)
print(response)
except exceptions.ClientRequestException as e:
SimTagApplication.__handle_exception(e)
@staticmethod
def __delete_tag(gsl_client):
tag_id = 12
try:
response = gsl_client.delete_tag(
DeleteTagRequest(
tag_id=tag_id
)
)
print(response)
except exceptions.ClientRequestException as e:
SimTagApplication.__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__":
SimTagApplication().main(any)
6. 运行结果
调用list_tags接口查询标签列表:
GET https://{endpoint}/v1/tags?offset=1&limit=10
返回结果:
{
"limit": 10,
"offset": 1,
"count": 1,
"tags": [
{
"id": 5510069767624000,
"tag_name": "测试标签",
"status": 0
}
]
}
对应返回的标签id可以用于其他SIM卡标签操作的入参。
7. 参考
更多信息请参考全球SIM联接 GSL
8. 修订记录
| 发布日期 | 文档版本 | 修订说明 |
|---|---|---|
| 2022-06-12 | 1.0.1 | 文档首次发布 |
| 2022-08-02 | 1.0.2 | 更新依赖 |
| 2023-11-08 | 1.0.3 | 更新文档 |