Skip to content

Commit 8eb0d3c

Browse files
committed
chore: showcase oneof python
1 parent fbec376 commit 8eb0d3c

File tree

3 files changed

+484
-0
lines changed

3 files changed

+484
-0
lines changed

examples/one_of_example.py

Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
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)

twilio/rest/one_of/v1/__init__.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
r"""
2+
This code was generated by
3+
___ _ _ _ _ _ _ ____ ____ ____ _ ____ ____ _ _ ____ ____ ____ ___ __ __
4+
| | | | | | | | | __ | | |__| | __ | __ |___ |\ | |___ |__/ |__| | | | |__/
5+
| |_|_| | |___ | |__| |__| | | | |__] |___ | \| |___ | \ | | | |__| | \
6+
7+
Number Pool Service
8+
This service is an entry point for all Number Pool CRUD requests.
9+
10+
NOTE: This class is auto generated by OpenAPI Generator.
11+
https://openapi-generator.tech
12+
Do not edit the class manually.
13+
"""
14+
15+
from typing import Optional
16+
from twilio.base.version import Version
17+
from twilio.base.domain import Domain
18+
from twilio.rest.one_of.v1.pet import PetList
19+
20+
21+
class V1(Version):
22+
23+
def __init__(self, domain: Domain):
24+
"""
25+
Initialize the V1 version of OneOf
26+
27+
:param domain: The Twilio.one_of domain
28+
"""
29+
super().__init__(domain, "v1")
30+
self._pets: Optional[PetList] = None
31+
32+
@property
33+
def pets(self) -> PetList:
34+
if self._pets is None:
35+
self._pets = PetList(self)
36+
return self._pets
37+
38+
def __repr__(self) -> str:
39+
"""
40+
Provide a friendly representation
41+
:returns: Machine friendly representation
42+
"""
43+
return "<Twilio.OneOf.V1>"

0 commit comments

Comments
 (0)