Skip to content

Commit 5f594dc

Browse files
author
fengyikai
committed
支持自定义Header
1 parent b812983 commit 5f594dc

10 files changed

Lines changed: 29 additions & 275 deletions

File tree

README.md

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -37,38 +37,35 @@ pip install --upgrade kingsoftcloud-sdk-python
3737

3838
```python
3939
import os
40+
4041
from ksyun.common import credential
4142
from ksyun.common.profile.client_profile import ClientProfile
4243
from ksyun.common.profile.http_profile import HttpProfile
4344
from ksyun.common.exception.ksyun_sdk_exception import KsyunSDKException
4445
from ksyun.client.iam.v20151101 import client as iam_client, models as iam_models
4546

4647
try:
47-
# 实例化一个证书对象,入参需要传入secretId,secretKey
4848
cred = credential.Credential(
4949
os.environ.get("KSYUN_SECRET_ID", "KSYUN_SECRET_ID_HERE"),
5050
os.environ.get("KSYUN_SECRET_KEY", "KSYUN_SECRET_KEY_HERE")
5151
)
52+
5253
httpProfile = HttpProfile()
5354
httpProfile.endpoint = "iam.api.ksyun.com"
54-
httpProfile.reqMethod = "GET" # get请求(默认为post请求)
55-
httpProfile.reqTimeout = 30 # 请求超时时间,单位为秒(默认60秒)
56-
55+
httpProfile.reqMethod = "POST"
56+
httpProfile.reqTimeout = 60
57+
httpProfile.scheme = "http"
5758
clientProfile = ClientProfile()
58-
# clientProfile.signMethod = "HMAC-SHA256" # 指定签名算法
5959
clientProfile.httpProfile = httpProfile
60-
iamClient = iam_client.IamClient(cred, "cn-beijing-6", clientProfile)
61-
62-
# 实例化一个请求对象, 每个接口都会对应一个request对象。
63-
iamReq = iam_models.CreateUserRequest()
64-
# 传参
65-
iamReq.UserName = "test2_user_name"
66-
# 调用接口返回
67-
iamResp = iamClient.CreateUser(iamReq)
68-
print(iamResp)
60+
61+
iamClient = iam_client.IamClient(cred, "cn-beijing-6", profile=clientProfile)
62+
63+
print(iamClient.call("ListUsers", {
64+
"MaxItems": 100,
65+
}))
66+
6967
except KsyunSDKException as err:
7068
print(err)
71-
7269
```
7370

7471
**注意,您必须明确知道您调用的接口所需参数,否则可能会调用失败。**

example/common_client/binary.data

Lines changed: 0 additions & 4 deletions
This file was deleted.

example/common_client/cls_upload_log.py

Lines changed: 0 additions & 54 deletions
This file was deleted.

example/common_client/iam_batch_attach_resource_dir_policy.py

Lines changed: 0 additions & 51 deletions
This file was deleted.

example/common_client/iam_create_project.py

Lines changed: 0 additions & 46 deletions
This file was deleted.

example/common_client/iam_list_users.py

Lines changed: 0 additions & 44 deletions
This file was deleted.

example/iam/v20151101/list_users.py

Lines changed: 0 additions & 45 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,30 @@
1-
# 调用子用户列表
2-
31
import os
2+
43
from ksyun.common import credential
54
from ksyun.common.profile.client_profile import ClientProfile
65
from ksyun.common.profile.http_profile import HttpProfile
76
from ksyun.common.exception.ksyun_sdk_exception import KsyunSDKException
87
from ksyun.client.iam.v20151101 import client as iam_client, models as iam_models
98

109
try:
11-
# 实例化一个证书对象,入参需要传入secretId,secretKey
1210
cred = credential.Credential(
1311
os.environ.get("KSYUN_SECRET_ID", "KSYUN_SECRET_ID_HERE"),
1412
os.environ.get("KSYUN_SECRET_KEY", "KSYUN_SECRET_KEY_HERE")
1513
)
14+
1615
httpProfile = HttpProfile()
1716
httpProfile.endpoint = "iam.api.ksyun.com"
18-
httpProfile.reqMethod = "GET" # get请求(默认为post请求)
19-
httpProfile.reqTimeout = 30 # 请求超时时间,单位为秒(默认60秒)
20-
17+
httpProfile.reqMethod = "POST"
18+
httpProfile.reqTimeout = 60
19+
httpProfile.scheme = "http"
2120
clientProfile = ClientProfile()
22-
# clientProfile.signMethod = "HMAC-SHA256" # 指定签名算法
2321
clientProfile.httpProfile = httpProfile
24-
iamClient = iam_client.IamClient(cred, "cn-beijing-6", clientProfile)
2522

26-
# 实例化一个请求对象, 每个接口都会对应一个request对象。
27-
iamReq = iam_models.CreateUserRequest()
28-
# 传参
29-
iamReq.UserName = "test2_user_name"
30-
# 调用接口返回
31-
iamResp = iamClient.CreateUser(iamReq)
32-
print(iamResp)
23+
iamClient = iam_client.IamClient(cred, "cn-beijing-6", profile=clientProfile)
24+
25+
print(iamClient.call("ListUsers", {
26+
"MaxItems": 100,
27+
}))
28+
3329
except KsyunSDKException as err:
3430
print(err)

ksyun/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@
1414
# limitations under the License.
1515

1616

17-
__version__ = '1.5.8.42'
17+
__version__ = '1.5.8.43'

ksyun/common/abstract_client.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,11 @@ def _build_req_with_signature(self, action, params, req, options=None):
163163
content_type = _x_protobuf
164164
req.header["Content-Type"] = content_type
165165

166+
# 支持自定义 header
167+
if options.get("CustomHeaders"):
168+
for key, value in options["CustomHeaders"].items():
169+
req.header[key] = value
170+
166171
# GET上传文件报错
167172
if req.method == "GET" and content_type == _multipart_content:
168173
raise SDKError("ClientError", "Invalid request method GET for multipart.")

0 commit comments

Comments
 (0)