diff --git a/examples/one_of_example.py b/examples/one_of_example.py new file mode 100644 index 000000000..7412d3ea3 --- /dev/null +++ b/examples/one_of_example.py @@ -0,0 +1,195 @@ +""" +Example demonstrating oneOf functionality in the Twilio Python SDK. + +This example shows how to use the Pet API which implements oneOf schema patterns. +The Cat schema uses allOf + oneOf, allowing two different variants: +1. Variant "One": Cat with param1, param2, and a nested dog object +2. Variant "Two": Cat with object1 and object2 + +Based on the OpenAPI spec at /v1/pets endpoint. +""" + +import os +from twilio.rest import Client +from twilio.rest.one_of.v1 import V1 +from twilio.rest.one_of.v1.pet import PetList +from twilio.base.domain import Domain + +# Initialize Twilio client credentials +ACCOUNT_SID = os.environ.get("TWILIO_ACCOUNT_SID") +AUTH_TOKEN = os.environ.get("TWILIO_AUTH_TOKEN") + + +def example_create_pet_variant_one(): + """ + Example: Create a pet using Cat variant "One" + + This variant includes: + - account_sid (from base Cat schema) + - param1, param2 (from "One" schema) + - dog (nested Dog object from "One" schema) + """ + client = Client(ACCOUNT_SID, AUTH_TOKEN) + + # Create a Dog object for the nested property + dog = PetList.Dog({ + "type": "dog", + "name": "Buddy", + "pack_size": 5 + }) + + # Create Cat with variant "One" (param1, param2, dog) + cat = PetList.Cat({ + "account_sid": ACCOUNT_SID, + "param1": "first_parameter", + "param2": "second_parameter", + "dog": dog + }) + + try: + # Initialize the OneOf domain and create the pet + one_of_domain = Domain(client, "https://oneOf.twilio.com") + v1 = V1(one_of_domain) + pet = v1.pets.create(cat=cat) + + print("Successfully created pet with variant 'One':") + print(f" Account SID: {pet.account_sid}") + print(f" Param1: {pet.param1}") + print(f" Param2: {pet.param2}") + print(f" Dog: {pet.dog}") + return pet + + except Exception as e: + print(f"Error creating pet: {e}") + return None + + +def example_create_pet_variant_two(): + """ + Example: Create a pet using Cat variant "Two" + + This variant includes: + - account_sid (from base Cat schema) + - object1, object2 (from "Two" schema) + """ + client = Client(ACCOUNT_SID, AUTH_TOKEN) + + # Create Cat with variant "Two" (object1, object2) + cat = PetList.Cat({ + "account_sid": ACCOUNT_SID, + "object1": "first_object", + "object2": "second_object" + }) + + try: + # Initialize the OneOf domain and create the pet + one_of_domain = Domain(client, "https://oneOf.twilio.com") + v1 = V1(one_of_domain) + pet = v1.pets.create(cat=cat) + + print("Successfully created pet with variant 'Two':") + print(f" Account SID: {pet.account_sid}") + print(f" Object1: {pet.object1}") + print(f" Object2: {pet.object2}") + return pet + + except Exception as e: + print(f"Error creating pet: {e}") + return None + + +def example_create_pet_inline(): + """ + Example: Create a pet with inline Dog object construction + + This shows a more concise way to create the Cat object with + nested objects defined inline. + """ + client = Client(ACCOUNT_SID, AUTH_TOKEN) + + # Create Cat with inline Dog object + cat = PetList.Cat({ + "account_sid": ACCOUNT_SID, + "param1": "inline_param1", + "param2": "inline_param2", + "dog": PetList.Dog({ + "type": "dog", + "name": "Max", + "pack_size": 3 + }) + }) + + try: + one_of_domain = Domain(client, "https://oneOf.twilio.com") + v1 = V1(one_of_domain) + pet = v1.pets.create(cat=cat) + + print("Successfully created pet with inline approach:") + print(f" Account SID: {pet.account_sid}") + print(f" Param1: {pet.param1}") + return pet + + except Exception as e: + print(f"Error creating pet: {e}") + return None + + +async def example_create_pet_async(): + """ + Example: Create a pet asynchronously + + The SDK supports async operations using the create_async method. + """ + client = Client(ACCOUNT_SID, AUTH_TOKEN) + + cat = PetList.Cat({ + "account_sid": ACCOUNT_SID, + "param1": "async_param1", + "param2": "async_param2", + "dog": PetList.Dog({ + "type": "dog", + "name": "Charlie", + "pack_size": 2 + }) + }) + + try: + one_of_domain = Domain(client, "https://oneOf.twilio.com") + v1 = V1(one_of_domain) + pet = await v1.pets.create_async(cat=cat) + + print("Asynchronously created pet:") + print(f" Account SID: {pet.account_sid}") + print(f" Param1: {pet.param1}") + return pet + + except Exception as e: + print(f"Error creating pet async: {e}") + return None + + +if __name__ == "__main__": + print("=" * 60) + print("OneOf Pattern Examples - Pet API") + print("=" * 60) + + print("\n1. Creating pet with variant 'One' (param1, param2, dog):") + print("-" * 60) + example_create_pet_variant_one() + + print("\n2. Creating pet with variant 'Two' (object1, object2):") + print("-" * 60) + example_create_pet_variant_two() + + print("\n3. Creating pet with inline Dog construction:") + print("-" * 60) + example_create_pet_inline() + + print("\n4. Creating pet asynchronously:") + print("-" * 60) + import asyncio + asyncio.run(example_create_pet_async()) + + print("\n" + "=" * 60) + print("Examples completed!") + print("=" * 60) diff --git a/twilio/rest/one_of/v1/__init__.py b/twilio/rest/one_of/v1/__init__.py new file mode 100644 index 000000000..40f7c3c3f --- /dev/null +++ b/twilio/rest/one_of/v1/__init__.py @@ -0,0 +1,43 @@ +r""" + This code was generated by + ___ _ _ _ _ _ _ ____ ____ ____ _ ____ ____ _ _ ____ ____ ____ ___ __ __ + | | | | | | | | | __ | | |__| | __ | __ |___ |\ | |___ |__/ |__| | | | |__/ + | |_|_| | |___ | |__| |__| | | | |__] |___ | \| |___ | \ | | | |__| | \ + + Number Pool Service + This service is an entry point for all Number Pool CRUD requests. + + NOTE: This class is auto generated by OpenAPI Generator. + https://openapi-generator.tech + Do not edit the class manually. +""" + +from typing import Optional +from twilio.base.version import Version +from twilio.base.domain import Domain +from twilio.rest.one_of.v1.pet import PetList + + +class V1(Version): + + def __init__(self, domain: Domain): + """ + Initialize the V1 version of OneOf + + :param domain: The Twilio.one_of domain + """ + super().__init__(domain, "v1") + self._pets: Optional[PetList] = None + + @property + def pets(self) -> PetList: + if self._pets is None: + self._pets = PetList(self) + return self._pets + + def __repr__(self) -> str: + """ + Provide a friendly representation + :returns: Machine friendly representation + """ + return "" diff --git a/twilio/rest/one_of/v1/pet.py b/twilio/rest/one_of/v1/pet.py new file mode 100644 index 000000000..7bbf3ff27 --- /dev/null +++ b/twilio/rest/one_of/v1/pet.py @@ -0,0 +1,246 @@ +r""" + This code was generated by + ___ _ _ _ _ _ _ ____ ____ ____ _ ____ ____ _ _ ____ ____ ____ ___ __ __ + | | | | | | | | | __ | | |__| | __ | __ |___ |\ | |___ |__/ |__| | | | |__/ + | |_|_| | |___ | |__| |__| | | | |__] |___ | \| |___ | \ | | | |__| | \ + + Number Pool Service + This service is an entry point for all Number Pool CRUD requests. + + NOTE: This class is auto generated by OpenAPI Generator. + https://openapi-generator.tech + Do not edit the class manually. +""" + + +from datetime import date, datetime +from typing import Any, Dict, List, Optional, Union, Iterator, AsyncIterator +from twilio.base import deserialize, serialize, values + +from twilio.base.instance_resource import InstanceResource +from twilio.base.list_resource import ListResource +from twilio.base.version import Version + + + +class PetInstance(InstanceResource): + + class Cat(object): + """ + :ivar account_sid: + :ivar param1: + :ivar param2: + :ivar dog: + :ivar object1: + :ivar object2: + """ + + def __init__(self, payload: Dict[str, Any]): + + + self.account_sid: Optional[str] = payload.get("account_sid") + self.param1: Optional[str] = payload.get("param1") + self.param2: Optional[str] = payload.get("param2") + self.dog: Optional[PetList.Dog] = payload.get("dog") + self.object1: Optional[str] = payload.get("object1") + self.object2: Optional[str] = payload.get("object2") + + def to_dict(self): + return { + + "account_sid": self.account_sid, + "param1": self.param1, + "param2": self.param2, + "dog": self.dog.to_dict() if self.dog is not None else None , + "object1": self.object1, + "object2": self.object2, + } + + class Dog(object): + """ + :ivar type: + :ivar name: + :ivar pack_size: + """ + + def __init__(self, payload: Dict[str, Any]): + + + self.type: Optional["PetInstance.str"] = payload.get("type") + self.name: Optional[str] = payload.get("name") + self.pack_size: Optional[int] = payload.get("pack_size") + + def to_dict(self): + return { + + "type": self.type, + "name": self.name, + "pack_size": self.pack_size, + } + + + + """ + :ivar account_sid: + :ivar param1: + :ivar param2: + :ivar dog: + :ivar object1: + :ivar object2: + """ + + def __init__(self, version: Version, payload: Dict[str, Any]): + super().__init__(version) + + + self.account_sid: Optional[str] = payload.get("account_sid") + self.param1: Optional[str] = payload.get("param1") + self.param2: Optional[str] = payload.get("param2") + self.dog: Optional[PetList.str] = payload.get("dog") + self.object1: Optional[str] = payload.get("object1") + self.object2: Optional[str] = payload.get("object2") + + + + + def __repr__(self) -> str: + """ + Provide a friendly representation + + :returns: Machine friendly representation + """ + + return '' + + + + +class PetList(ListResource): + + class Cat(object): + """ + :ivar account_sid: + :ivar param1: + :ivar param2: + :ivar dog: + :ivar object1: + :ivar object2: + """ + + def __init__(self, payload: Dict[str, Any]): + + + self.account_sid: Optional[str] = payload.get("account_sid") + self.param1: Optional[str] = payload.get("param1") + self.param2: Optional[str] = payload.get("param2") + self.dog: Optional[PetList.Dog] = payload.get("dog") + self.object1: Optional[str] = payload.get("object1") + self.object2: Optional[str] = payload.get("object2") + + def to_dict(self): + return { + + "account_sid": self.account_sid, + "param1": self.param1, + "param2": self.param2, + "dog": self.dog.to_dict() if self.dog is not None else None , + "object1": self.object1, + "object2": self.object2, + } + + class Dog(object): + """ + :ivar type: + :ivar name: + :ivar pack_size: + """ + + def __init__(self, payload: Dict[str, Any]): + + + self.type: Optional["PetInstance.str"] = payload.get("type") + self.name: Optional[str] = payload.get("name") + self.pack_size: Optional[int] = payload.get("pack_size") + + def to_dict(self): + return { + + "type": self.type, + "name": self.name, + "pack_size": self.pack_size, + } + + + def __init__(self, version: Version): + """ + Initialize the PetList + + :param version: Version that contains the resource + + """ + super().__init__(version) + + + self._uri = '/pets' + + + + def create(self, cat: Cat) -> PetInstance: + """ + Create the PetInstance + + :param cat: + + :returns: The created PetInstance + """ + data = cat.to_dict() + + headers = values.of({ + 'Content-Type': 'application/x-www-form-urlencoded' + }) + + headers["Content-Type"] = "application/json" + + + headers["Accept"] = "application/json" + + + payload = self._version.create(method='POST', uri=self._uri, data=data, headers=headers) + + return PetInstance(self._version, payload) + + async def create_async(self, cat: Cat) -> PetInstance: + """ + Asynchronously create the PetInstance + + :param cat: + + :returns: The created PetInstance + """ + data = cat.to_dict() + + headers = values.of({ + 'Content-Type': 'application/x-www-form-urlencoded' + }) + + headers["Content-Type"] = "application/json" + + + headers["Accept"] = "application/json" + + + payload = await self._version.create_async(method='POST', uri=self._uri, data=data, headers=headers) + + return PetInstance(self._version, payload) + + + + + def __repr__(self) -> str: + """ + Provide a friendly representation + + :returns: Machine friendly representation + """ + return '' +