|
| 1 | +""" |
| 2 | +Example demonstrating oneOf functionality in the Twilio Python SDK. |
| 3 | +
|
| 4 | +This example shows how to use the Pet API which implements oneOf schema patterns. |
| 5 | +The Cat schema uses allOf + oneOf, allowing two different variants: |
| 6 | +1. Variant "One": Cat with param1, param2, and a nested dog object |
| 7 | +2. Variant "Two": Cat with object1 and object2 |
| 8 | +
|
| 9 | +Based on the OpenAPI spec at /v1/pets endpoint. |
| 10 | +""" |
| 11 | + |
| 12 | +import os |
| 13 | +from twilio.rest import Client |
| 14 | +from twilio.rest.one_of.v1 import V1 |
| 15 | +from twilio.rest.one_of.v1.pet import PetList |
| 16 | +from twilio.base.domain import Domain |
| 17 | + |
| 18 | +# Initialize Twilio client credentials |
| 19 | +ACCOUNT_SID = os.environ.get("TWILIO_ACCOUNT_SID") |
| 20 | +AUTH_TOKEN = os.environ.get("TWILIO_AUTH_TOKEN") |
| 21 | + |
| 22 | + |
| 23 | +def example_create_pet_variant_one(): |
| 24 | + """ |
| 25 | + Example: Create a pet using Cat variant "One" |
| 26 | +
|
| 27 | + This variant includes: |
| 28 | + - account_sid (from base Cat schema) |
| 29 | + - param1, param2 (from "One" schema) |
| 30 | + - dog (nested Dog object from "One" schema) |
| 31 | + """ |
| 32 | + client = Client(ACCOUNT_SID, AUTH_TOKEN) |
| 33 | + |
| 34 | + # Create a Dog object for the nested property |
| 35 | + dog = PetList.Dog({ |
| 36 | + "type": "dog", |
| 37 | + "name": "Buddy", |
| 38 | + "pack_size": 5 |
| 39 | + }) |
| 40 | + |
| 41 | + # Create Cat with variant "One" (param1, param2, dog) |
| 42 | + cat = PetList.Cat({ |
| 43 | + "account_sid": ACCOUNT_SID, |
| 44 | + "param1": "first_parameter", |
| 45 | + "param2": "second_parameter", |
| 46 | + "dog": dog |
| 47 | + }) |
| 48 | + |
| 49 | + try: |
| 50 | + # Initialize the OneOf domain and create the pet |
| 51 | + one_of_domain = Domain(client, "https://oneOf.twilio.com") |
| 52 | + v1 = V1(one_of_domain) |
| 53 | + pet = v1.pets.create(cat=cat) |
| 54 | + |
| 55 | + print("Successfully created pet with variant 'One':") |
| 56 | + print(f" Account SID: {pet.account_sid}") |
| 57 | + print(f" Param1: {pet.param1}") |
| 58 | + print(f" Param2: {pet.param2}") |
| 59 | + print(f" Dog: {pet.dog}") |
| 60 | + return pet |
| 61 | + |
| 62 | + except Exception as e: |
| 63 | + print(f"Error creating pet: {e}") |
| 64 | + return None |
| 65 | + |
| 66 | + |
| 67 | +def example_create_pet_variant_two(): |
| 68 | + """ |
| 69 | + Example: Create a pet using Cat variant "Two" |
| 70 | +
|
| 71 | + This variant includes: |
| 72 | + - account_sid (from base Cat schema) |
| 73 | + - object1, object2 (from "Two" schema) |
| 74 | + """ |
| 75 | + client = Client(ACCOUNT_SID, AUTH_TOKEN) |
| 76 | + |
| 77 | + # Create Cat with variant "Two" (object1, object2) |
| 78 | + cat = PetList.Cat({ |
| 79 | + "account_sid": ACCOUNT_SID, |
| 80 | + "object1": "first_object", |
| 81 | + "object2": "second_object" |
| 82 | + }) |
| 83 | + |
| 84 | + try: |
| 85 | + # Initialize the OneOf domain and create the pet |
| 86 | + one_of_domain = Domain(client, "https://oneOf.twilio.com") |
| 87 | + v1 = V1(one_of_domain) |
| 88 | + pet = v1.pets.create(cat=cat) |
| 89 | + |
| 90 | + print("Successfully created pet with variant 'Two':") |
| 91 | + print(f" Account SID: {pet.account_sid}") |
| 92 | + print(f" Object1: {pet.object1}") |
| 93 | + print(f" Object2: {pet.object2}") |
| 94 | + return pet |
| 95 | + |
| 96 | + except Exception as e: |
| 97 | + print(f"Error creating pet: {e}") |
| 98 | + return None |
| 99 | + |
| 100 | + |
| 101 | +def example_create_pet_inline(): |
| 102 | + """ |
| 103 | + Example: Create a pet with inline Dog object construction |
| 104 | +
|
| 105 | + This shows a more concise way to create the Cat object with |
| 106 | + nested objects defined inline. |
| 107 | + """ |
| 108 | + client = Client(ACCOUNT_SID, AUTH_TOKEN) |
| 109 | + |
| 110 | + # Create Cat with inline Dog object |
| 111 | + cat = PetList.Cat({ |
| 112 | + "account_sid": ACCOUNT_SID, |
| 113 | + "param1": "inline_param1", |
| 114 | + "param2": "inline_param2", |
| 115 | + "dog": PetList.Dog({ |
| 116 | + "type": "dog", |
| 117 | + "name": "Max", |
| 118 | + "pack_size": 3 |
| 119 | + }) |
| 120 | + }) |
| 121 | + |
| 122 | + try: |
| 123 | + one_of_domain = Domain(client, "https://oneOf.twilio.com") |
| 124 | + v1 = V1(one_of_domain) |
| 125 | + pet = v1.pets.create(cat=cat) |
| 126 | + |
| 127 | + print("Successfully created pet with inline approach:") |
| 128 | + print(f" Account SID: {pet.account_sid}") |
| 129 | + print(f" Param1: {pet.param1}") |
| 130 | + return pet |
| 131 | + |
| 132 | + except Exception as e: |
| 133 | + print(f"Error creating pet: {e}") |
| 134 | + return None |
| 135 | + |
| 136 | + |
| 137 | +async def example_create_pet_async(): |
| 138 | + """ |
| 139 | + Example: Create a pet asynchronously |
| 140 | +
|
| 141 | + The SDK supports async operations using the create_async method. |
| 142 | + """ |
| 143 | + client = Client(ACCOUNT_SID, AUTH_TOKEN) |
| 144 | + |
| 145 | + cat = PetList.Cat({ |
| 146 | + "account_sid": ACCOUNT_SID, |
| 147 | + "param1": "async_param1", |
| 148 | + "param2": "async_param2", |
| 149 | + "dog": PetList.Dog({ |
| 150 | + "type": "dog", |
| 151 | + "name": "Charlie", |
| 152 | + "pack_size": 2 |
| 153 | + }) |
| 154 | + }) |
| 155 | + |
| 156 | + try: |
| 157 | + one_of_domain = Domain(client, "https://oneOf.twilio.com") |
| 158 | + v1 = V1(one_of_domain) |
| 159 | + pet = await v1.pets.create_async(cat=cat) |
| 160 | + |
| 161 | + print("Asynchronously created pet:") |
| 162 | + print(f" Account SID: {pet.account_sid}") |
| 163 | + print(f" Param1: {pet.param1}") |
| 164 | + return pet |
| 165 | + |
| 166 | + except Exception as e: |
| 167 | + print(f"Error creating pet async: {e}") |
| 168 | + return None |
| 169 | + |
| 170 | + |
| 171 | +if __name__ == "__main__": |
| 172 | + print("=" * 60) |
| 173 | + print("OneOf Pattern Examples - Pet API") |
| 174 | + print("=" * 60) |
| 175 | + |
| 176 | + print("\n1. Creating pet with variant 'One' (param1, param2, dog):") |
| 177 | + print("-" * 60) |
| 178 | + example_create_pet_variant_one() |
| 179 | + |
| 180 | + print("\n2. Creating pet with variant 'Two' (object1, object2):") |
| 181 | + print("-" * 60) |
| 182 | + example_create_pet_variant_two() |
| 183 | + |
| 184 | + print("\n3. Creating pet with inline Dog construction:") |
| 185 | + print("-" * 60) |
| 186 | + example_create_pet_inline() |
| 187 | + |
| 188 | + print("\n4. Creating pet asynchronously:") |
| 189 | + print("-" * 60) |
| 190 | + import asyncio |
| 191 | + asyncio.run(example_create_pet_async()) |
| 192 | + |
| 193 | + print("\n" + "=" * 60) |
| 194 | + print("Examples completed!") |
| 195 | + print("=" * 60) |
0 commit comments