|
| 1 | +### 1. Get Request and Response Types |
| 2 | + |
| 3 | +Before calling a method, you can obtain the corresponding request and response structures via `param` and `return`. |
| 4 | +Taking `ecs.run_instances` as an example: |
| 5 | + |
| 6 | +```python |
| 7 | +def run_instances(self, body, **kwargs): # noqa: E501 |
| 8 | + """run_instances |
| 9 | + :param RunInstancesRequest body: (required) |
| 10 | + :return: RunInstancesResponse |
| 11 | + """ |
| 12 | +``` |
| 13 | + |
| 14 | +### 2. Get Parameter Types for Requests and Responses |
| 15 | + |
| 16 | +In the Request or Response body, you can check specific request parameters and their types via `swagger_types`. |
| 17 | +Taking `volcenginesdkecs.RunInstancesRequest` as an example, its parameter names and definitions are as follows: |
| 18 | + |
| 19 | +```python |
| 20 | +swagger_types = { |
| 21 | + 'auto_renew': 'bool', |
| 22 | + 'auto_renew_period': 'int', |
| 23 | + 'client_token': 'str', |
| 24 | + 'count': 'int', |
| 25 | + 'network_interfaces': 'list[NetworkInterfaceForRunInstancesInput]', |
| 26 | + 'security_enhancement_strategy': 'str', |
| 27 | + 'volumes': 'list[VolumeForRunInstancesInput]', |
| 28 | + ...... |
| 29 | +} |
| 30 | +``` |
| 31 | + |
| 32 | +The corresponding request body is: |
| 33 | + |
| 34 | +```python |
| 35 | +volcenginesdkecs.RunInstancesRequest( |
| 36 | + instance_name="insname", |
| 37 | + network_interfaces=[volcenginesdkecs.NetworkInterfaceForRunInstancesInput( |
| 38 | + subnet_id="subnet-2d68bh73d858ozfekrm8fj", |
| 39 | + security_group_ids=["sg-2b3dq7v0ha0w2dx0eg0nhljv"], |
| 40 | + )], |
| 41 | + image_id="image-ybvz29l3da4ox5h0m9", |
| 42 | + volumes=[volcenginesdkecs.VolumeForRunInstancesInput( |
| 43 | + volume_type="ESSD", |
| 44 | + size=40, |
| 45 | + )], |
| 46 | + key_pair_name="vtable", |
| 47 | + instance_charge_type="PostPaid" |
| 48 | +) |
| 49 | +``` |
| 50 | + |
| 51 | +### 3. Memory Overflow Occurs |
| 52 | + |
| 53 | +Check whether `Configuration._default` has been initialized. |
| 54 | +If not, each time you create a `Configuration` instance, a new logger handler will be created and added to the global logger. |
| 55 | +It is recommended to set the default configuration via `Configuration.set_default`. |
| 56 | + |
| 57 | +```python |
| 58 | +configuration = volcenginesdkcore.Configuration() |
| 59 | +configuration.client_side_validation = False |
| 60 | +configuration.schema = "http" # https or http |
| 61 | +configuration.debug = False # Whether to enable debugging |
| 62 | + |
| 63 | +volcenginesdkcore.Configuration.set_default(configuration) |
| 64 | +``` |
| 65 | + |
| 66 | +### 4. Convert Object to Dict |
| 67 | + |
| 68 | +For request and response objects, you can convert them to `dict` using the `to_dict()` method. |
| 69 | + |
| 70 | +### 5. View Usage Examples |
| 71 | + |
| 72 | +[volcenginesdkexamples](https://github.com/volcengine/volcengine-python-sdk/tree/master/volcenginesdkexamples) |
| 73 | + |
| 74 | +### 6. Convert Response Object to CamelCase Dict |
| 75 | + |
| 76 | +After calling an API, the parameters in the received response object typically use PascalCase (CamelCase with the first letter capitalized). When coding with the Python SDK, since Python generally uses snake_case, you may want to convert between the SDK's snake_case fields and the documentation's CamelCase fields. |
| 77 | + |
| 78 | +You can convert the response object into a CamelCase dictionary with the following code. After conversion, the field names in the dictionary will be exactly the same as those in the documentation. |
| 79 | + |
| 80 | +```python |
| 81 | +import json |
| 82 | +import pprint |
| 83 | +from volcenginesdkcore.model import canonical_str |
| 84 | + |
| 85 | +try: |
| 86 | + resp = api_instance.list_users(req) |
| 87 | + pprint(resp) # Response object with snake_case fields |
| 88 | + dict_resp = json.loads(json.dumps(canonical_str(resp))) |
| 89 | + pprint(dict_resp) # Response dict with CamelCase fields |
| 90 | + |
| 91 | +except ApiException as e: |
| 92 | + print("Exception when calling IAMApi->ListUsers: %s\n" % e) |
| 93 | + |
| 94 | +``` |
| 95 | + |
| 96 | +However, this is not the recommended approach. It is suggested to directly use the snake_case parameters in the response object instead of converting. |
0 commit comments