
# 声音注册查询
#### 前提条件
- 确保已按照[配置Python环境](https://support.huaweicloud.com/sdkreference-sis/sis_05_0056.html)配置完毕。
- 请参考[SDK（websocket）](https://support.huaweicloud.com/sdkreference-sis/sis_05_0004.html)获取最新版本SDK包。
- 仅上海一局点支持该接口。
 
#### 初始化Client
初始化vcs_client，详见[表1]。
 表1vcs_client 
| 参数名称             | 是否必选 | 参数类型   | 描述                                                                                     |
|:---|:---|:---|:---|
| ak               | 是    | String | 用户的ak，可参考[AK/SK认证](https://support.huaweicloud.com/sdkreference-sis/sis_05_0003.html)。 |
| sk               | 是    | String | 用户的sk，可参考[AK/SK认证](https://support.huaweicloud.com/sdkreference-sis/sis_05_0003.html)。 |
| region           | 是    | String | 区域，如：cn-east-3。具体请参考[终端节点](https://support.huaweicloud.com/api-sis/sis_03_0004.html)。  |
| project_id       | 是    | String | 项目ID，同region一一对应，参考获取项目ID。                                                             |
| service_endpoint | 否    | String | 终端节点，一般使用默认即可。                                                                         |
   
#### 请求参数（声音注册）
请求类型为register_query_request，详见[表2]。
 表2register_query_request 
| 参数名称   | 是否必选 | 参数类型   | 描述                                       |
|:---|:---|:---|:---|
| path   | 是    | String | 待注册声音路径，要求采样率不低于16k。                     |
| config | 是    | Object | 详见[表3]。 |
   
 表3Config 
| 参数名称       | 是否必选 | 参数类型   | 描述                                                                         |
|:---|:---|:---|:---|
| voice_name | 是    | String | 注册声音的名称，不能以数字、下划线开头。仅包含大、小写英文字母、数字和下划线，且长度不超过20个字符。一个project_id下的声音名称不能重复。 |
| language   | 否    | String | data中语音数据的语种，取值"chinese"、"english"；默认是chinese。                             |
   
#### 请求参数（声音查询）
请求类型register_query_request，详见[表4]。
 表4register_query_request 
| 参数名称   | 是否必选 | 参数类型    | 描述                                                |
|:---|:---|:---|:---|
| limit  | 否    | Integer | 查询已注册的声音列表，每页查询显示的条目数量，默认：10。                     |
| offset | 否    | Integer | 查询已注册的声音列表，页码偏移量，表示从此页码偏移量开始查询，offset大于等于0， 默认：0。 |
   
#### 响应参数（声音注册）
Python SDK响应结果为Json格式，正确响应详见[表5]，错误响应详见[表7]。
 表5响应参数说明 
| 参数名称     | 参数类型   | 描述                                               |
|:---|:---|:---|
| trace_id | String | 服务内部的令牌，可用于在日志中追溯具体流程。                           |
| result   | Object | 注册声音响应，详见[表6]。 |
   
 表6result数据结构 
| 参数名称       | 参数类型   | 描述       |
|:---|:---|:---|
| voice_name | String | 注册声音的名称。 |
   
 表7错误响应 
| 参数名称       | 参数类型   | 描述                     |
|:---|:---|:---|
| error_code | String | 调用失败时的错误码。 调用成功时无此字段。  |
| error_msg  | String | 调用失败时的错误信息。 调用成功时无此字段。 |
   
#### 响应参数（声音查询）
Python SDK响应结果为Json格式，正确响应详见[表8]，错误响应详见[表11]。
 表8响应参数说明 
| 参数名称     | 参数类型   | 描述                                                |
|:---|:---|:---|
| trace_id | String | 服务内部的令牌，可用于在日志中追溯具体流程。                            |
| result   | Object | 注册声音响应，详见[表9]。 |
   
 表9result数据结构 
| 参数名称   | 参数类型   | 描述                                                   |
|:---|:---|:---|
| voices | String | 注册成功的声音列表，详见[表10]。 |
   
 表10voices结构 
| 参数名称       | 参数类型   | 描述         |
|:---|:---|:---|
| voice_name | String | 注册成功的声音名称。 |
| language   | String | 注册成功的声音语种。 |
   
 表11错误响应 
| 参数名称       | 参数类型   | 描述                     |
|:---|:---|:---|
| error_code | String | 调用失败时的错误码。 调用成功时无此字段。  |
| error_msg  | String | 调用失败时的错误信息。 调用成功时无此字段。 |
   
#### 请求示例
- 说明："endpoint"即调用API的请求地址，不同服务不同区域的"endpoint"不同，具体请参见终端节点，当前仅支持上海一。
- 调用API，注册查询声音。
```
import base64
import os
from huaweicloud_sis.bean.vcs_register_voice_request import RegisterVoiceRequest
from huaweicloud_sis.client.vcs_client import VcsClient
from huaweicloud_sis.bean.sis_config import SisConfig
# 鉴权参数
# 认证用的ak和sk硬编码到代码中或者明文存储都有很大的安全风险，建议在配置文件或者环境变量中密文存放，使用时解密，确保安全；
# 本示例以ak和sk保存在环境变量中来实现身份验证为例，运行本示例前请先在本地环境中设置环境变量HUAWEICLOUD_SIS_AK/HUAWEICLOUD_SIS_SK/HUAWEICLOUD_SIS_PROJECT_ID。
ak = os.getenv("HUAWEICLOUD_SIS_AK")             # 从环境变量获取ak 参考https://support.huaweicloud.com/sdkreference-sis/sis_05_0003.html
assert ak is not None, "Please add ak in your develop environment"
sk = os.getenv("HUAWEICLOUD_SIS_SK")             # 从环境变量获取sk 参考https://support.huaweicloud.com/sdkreference-sis/sis_05_0003.html
assert sk is not None, "Please add sk in your develop environment"
region = ""         # region，支持上海一region，cn-east-3
project_id = ""     # project id 同region一一对应，参考https://support.huaweicloud.com/api-sis/sis_03_0008.html
path = "" # 参考语音路径,请根据实际路径修改,要求采样率不小于16k，例如 r"D://test.wav"
voice_name = "" # 音色名称，可以使用注册音色或预置音色
service_endpoint = "" # 可选，自定义endpoint，例如"https://sis-ext.cn-east-3.myhuaweicloud.com"
def register_voice_example(path, voice_name):
    with open(path, 'rb') as f:
        data = f.read()
        data = str(base64.b64encode(data), 'utf-8')
    config = SisConfig()
    config.set_connect_timeout(10)  # 设置连接超时，单位s
    config.set_read_timeout(60)  # 设置读取超时，单位s
    vcs_client = VcsClient(ak, sk, region, project_id, sis_config=config, service_endpoint=service_endpoint)
    register_voice_request = RegisterVoiceRequest(data, voice_name)
    result = vcs_client.register_voice(register_voice_request)
    print(result)
def query_example():
    config = SisConfig()
    config.set_connect_timeout(10)  # 设置连接超时，单位s
    config.set_read_timeout(60)  # 设置读取超时，单位s
    vcs_client = VcsClient(ak, sk, region, project_id, sis_config=config, service_endpoint=service_endpoint)
    result = vcs_client.query_voice_name(10, 0) # 默认查询10条，支持分页查询
    print(result)
if __name__ == '__main__':
    # 两个接口可单独运行
    register_voice_example(path, voice_name) # 注册
    query_example() # 查询
```
#### 响应示例
状态码：200
成功响应示例（注册声音）
```
{ 
   "trace_id": "567e8537-a89c-13c3-a882-826321939651", 
   "result":{ 
     "voice_name": "test_xiaoming"
   } 
 }
```
状态码：200
成功响应示例（查询声音）
```
{ 
   "trace_id": "567e8537-a89c-13c3-a882-826321939651", 
   "result":{ 
     "voices": [
        {
          "voice_name": "test_xiaoming",
          "language": "chinese"
        }
   } 
 }
```
状态码：400
失败响应示例（注册查询声音）
```
{  
    "error_code": "SIS.***",  
    "error_msg": "***"          
}
```
