Alibaba Cloud Tea for Python

0.4.3 · active · verified Sat Apr 11

Alibaba Cloud Tea for Python is the core `tea` module of the Alibaba Cloud Python SDK ecosystem. It acts as a low-level library primarily designed to support Darabonba OpenAPI DSL, facilitating HTTP requests and foundational utilities for other higher-level Alibaba Cloud SDKs. Its current version is 0.4.3, and it receives frequent updates, often tied to releases of dependent SDK components.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates the basic usage of `TeaModel` for defining structured request/response objects and `TeaException` for handling errors. `alibabacloud-tea` itself is a foundational library; most direct interaction occurs through other Alibaba Cloud SDKs built upon it.

import os
from Tea.model import TeaModel
from Tea.exceptions import TeaException

class MyRequest(TeaModel):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.my_param = kwargs.get('my_param', None)

    def validate(self):
        # Example validation: my_param is required
        if self.my_param is None:
            raise TeaException({'code': 'MissingParameter', 'message': 'my_param is required'})


def main():
    print("Demonstrating TeaModel and TeaException...")
    try:
        # Valid request
        req1 = MyRequest(my_param='test_value')
        req1.validate()
        print(f"Request 1 (valid): {req1.my_param}")

        # Invalid request (missing my_param)
        req2 = MyRequest()
        req2.validate()

    except TeaException as e:
        print(f"Caught TeaException: Code={e.code}, Message={e.message}")
    except Exception as e:
        print(f"Caught unexpected exception: {e}")

if __name__ == '__main__':
    main()

view raw JSON →