Skip to content

Commit d1c3d17

Browse files
committed
Merge branch 'doc/lsx/endpoint' into 'master'
docs(readme): add endpoint See merge request iaasng/volcengine-python-sdk!568
2 parents a77032d + 4786b3c commit d1c3d17

File tree

2 files changed

+87
-23
lines changed

2 files changed

+87
-23
lines changed

FAQ.md

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
### 1. 获取请求和响应类型
2+
23
在方法调用前,通过 param 和 return 获取请求和响应对应的结构体
34
以 ecs.run_instances 为例
5+
46
```python
57
def run_instances(self, body, **kwargs): # noqa: E501
68
"""run_instances
@@ -9,10 +11,11 @@ def run_instances(self, body, **kwargs): # noqa: E501
911
"""
1012
```
1113

12-
1314
### 2. 获取请求和响应的参数类型
15+
1416
在 Request 或者 Response 请求体中,通过 swagger_types 查看具体的请求参数和类型
1517
以 volcenginesdkecs.RunInstancesRequest 为例,其参数名称和定义如下所示
18+
1619
```python
1720
swagger_types = {
1821
'auto_renew': 'bool',
@@ -25,7 +28,9 @@ swagger_types = {
2528
......
2629
}
2730
```
31+
2832
对应的请求体为
33+
2934
```python
3035
volcenginesdkecs.RunInstancesRequest(
3136
instance_name="insname",
@@ -42,10 +47,13 @@ volcenginesdkecs.RunInstancesRequest(
4247
instance_charge_type="PostPaid"
4348
)
4449
```
50+
4551
### 3. 出现内存溢出
52+
4653
检查 Configuration._default 是否被初始化
4754
如果没有初始化,每一次创建 Configuration 的时候,会创建新的 logger handler 并加入到全局的 logger 中
4855
建议通过 Configuration.set_default 方法对 Configuration 进行赋值
56+
4957
```python
5058
configuration = volcenginesdkcore.Configuration()
5159
configuration.client_side_validation = False
@@ -56,7 +64,34 @@ volcenginesdkcore.Configuration.set_default(configuration)
5664
```
5765

5866
### 4. 对象转 Dict
67+
5968
对于请求和响应的对象,可以通过 to_dict() 方法将其转换成 dict 类型
6069

6170
### 5. 查看使用示例
71+
6272
[volcenginesdkexamples](https://github.com/volcengine/volcengine-python-sdk/tree/master/volcenginesdkexamples)
73+
74+
### 6. 响应对象转驼峰风格 Dict
75+
76+
在实际调用 API 后,所接收的响应对象参数通常采用首字母大写的驼峰命名法。而在使用 Python SDK 进行编码时,由于 Python
77+
的整体命名规范为下划线命名法,当使用响应参数并对照文档时,您可能想要在 Python SDK 所采用的下划线命名法与文档中的驼峰命名法之间进行转换。
78+
79+
当然,可通过调用以下代码将响应对象转换为驼峰格式的字典,转换后字典中的字段名与文档中的字段名完全一致
80+
81+
```python
82+
import json
83+
import pprint
84+
from volcenginesdkcore.model import canonical_str
85+
86+
try:
87+
resp = api_instance.list_users(req)
88+
pprint(resp) # 字段为下划线的响应对象
89+
dict_resp = json.loads(json.dumps(canonical_str(resp)))
90+
pprint(dict_resp) # 字段为驼峰的响应字典
91+
92+
except ApiException as e:
93+
print("Exception when calling IAMApi->ListUsers: %s\n" % e)
94+
95+
```
96+
97+
不过,这种方法并非推荐的使用方式。建议直接使用响应对象中以下划线命名的参数,而非进行上述转换后使用。

README.md

Lines changed: 51 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
* Python版本需要不低于2.7。
1313
* 由于 Windows 系统有最长路径限制,可能会导致安装失败,请按照以下方式设置:
14+
1415
```
1516
1. 按下 Win+R ,输入 regedit 打开注册表编辑器。
1617
2. 设置 \HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\FileSystem 路径下的变量 LongPathsEnabled 为 1 即可。
@@ -19,6 +20,7 @@
1920
### Install ###
2021

2122
Install via pip
23+
2224
```sh
2325
pip install volcengine-python-sdk
2426
```
@@ -31,7 +33,54 @@ python setup.py install --user
3133

3234
(or `sudo python setup.py install` to install the package for all users)
3335

34-
### Basic Usage ###
36+
### Configuration Usage ###
37+
38+
步骤一:启动时初始化,配置 Configuration 全局默认参数
39+
40+
```python
41+
configuration = volcenginesdkcore.Configuration()
42+
configuration.client_side_validation = True # 客户端是否进行参数校验
43+
configuration.schema = "http" # https or http
44+
configuration.debug = False # 是否开启调试
45+
configuration.logger_file = "sdk.log"
46+
47+
volcenginesdkcore.Configuration.set_default(configuration)
48+
```
49+
50+
步骤二:获取 Client
51+
52+
```python
53+
def get_client(ak, sk, region):
54+
# 包含默认属性
55+
configuration = volcenginesdkcore.Configuration()
56+
configuration.ak = ak
57+
configuration.sk = sk
58+
configuration.region = region
59+
client = volcenginesdkautoscaling.AUTOSCALINGApi(volcenginesdkcore.ApiClient(configuration))
60+
return client
61+
```
62+
63+
### Endpoint 设置 ###
64+
65+
如果您要自定义SDK的Endpoint,可以按照以下示例代码设置:
66+
67+
```python
68+
configuration = volcenginesdkcore.Configuration()
69+
configuration.host = 'ecs.cn-beijing-autodriving.volcengineapi.com'
70+
```
71+
72+
火山引擎标准的Endpoint规则说明:
73+
74+
| Regional 服务 | Global 服务 |
75+
|----------------------------------------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------|
76+
| `{service}.{region}.volcengineapi.com` <br> 例如:云服务ecs在cn-beijing-autodriving Region域名为: `ecs.cn-beijing-autodriving.volcengineapi.com` | `{service}.volcengineapi.com` <br> 例如:访问控制iam为Global服务,域名为:`iam.volcengineapi.com` |
77+
78+
注:
79+
80+
- Service中存在_符号时,Endpoint时需转为-符号。存在大写字母时需转成小写。
81+
- 并非所有云产品和Region都支持标准域名,具体请前往您所使用的产品-API参考中查看。
82+
83+
### SDK 示例 ###
3584

3685
```python
3786
from __future__ import print_function
@@ -77,27 +126,7 @@ if __name__ == '__main__':
77126
print("Exception when calling ECSApi->run_instances: %s\n" % e)
78127

79128
```
80-
### Configuration Usage ###
81-
步骤一:启动时初始化,配置 Configuration 全局默认参数
82-
```python
83-
configuration = volcenginesdkcore.Configuration()
84-
configuration.client_side_validation = True # 客户端是否进行参数校验
85-
configuration.schema = "http" # https or http
86-
configuration.debug = False # 是否开启调试
87-
configuration.logger_file = "sdk.log"
88129

89-
volcenginesdkcore.Configuration.set_default(configuration)
90-
```
91-
步骤二:获取 Client
92-
```python
93-
def get_client(ak, sk, region):
94-
# 包含默认属性
95-
configuration = volcenginesdkcore.Configuration()
96-
configuration.ak = ak
97-
configuration.sk = sk
98-
configuration.region = region
99-
client = volcenginesdkautoscaling.AUTOSCALINGApi(volcenginesdkcore.ApiClient(configuration))
100-
return client
101-
```
102130
### FAQ ###
131+
103132
关于 SDK 使用时碰到的常见问题,请查看 [FAQ](FAQ.md)

0 commit comments

Comments
 (0)