forked from slashml/gatewayz-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexamples.py
More file actions
237 lines (190 loc) · 6.13 KB
/
Copy pathexamples.py
File metadata and controls
237 lines (190 loc) · 6.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
"""
Example usage of the GatewayzApi Python SDK.
This file demonstrates common patterns for using the SDK.
"""
import asyncio
from gatewayz import GatewayzApi, AsyncGatewayzApi
from gatewayz.types import Message
from gatewayz.core import ApiError
# Example 1: Basic synchronous client usage
def basic_example():
"""Basic synchronous client usage."""
client = GatewayzApi(
token="YOUR_API_TOKEN",
base_url="https://api.gatewayz.ai"
)
# Health check
try:
health = client.health_check_health_get()
print(f"Health check: {health}")
except ApiError as e:
print(f"API Error: {e.status_code} - {e.body}")
# Example 2: Chat completions
def chat_example():
"""Chat completions example."""
client = GatewayzApi(
token="YOUR_API_TOKEN",
base_url="https://api.gatewayz.ai"
)
# Send a chat message
response = client.chat.chat_completions(
model="gpt-4",
messages=[
Message(role="system", content="You are a helpful assistant."),
Message(role="user", content="What is the capital of France?")
],
max_tokens=100,
temperature=0.7
)
print(f"Chat response: {response}")
# Example 3: Async usage
async def async_example():
"""Async client usage example."""
client = AsyncGatewayzApi(
token="YOUR_API_TOKEN",
base_url="https://api.gatewayz.ai"
)
# Concurrent requests
health_task = client.health_check_health_get()
ping_task = client.ping_ping_get()
health, ping = await asyncio.gather(health_task, ping_task)
print(f"Health: {health}")
print(f"Ping: {ping}")
# Example 4: User registration and authentication
def auth_example():
"""Authentication example."""
from gatewayz.types import UserRegistrationRequest
client = GatewayzApi(
token="YOUR_API_TOKEN",
base_url="https://api.gatewayz.ai"
)
# Register a new user
try:
user = client.authentication.register_user(
request=UserRegistrationRequest(
email="newuser@example.com",
# Add other required fields based on your API
)
)
print(f"User registered: {user}")
# Get user profile
profile = client.authentication.get_profile()
print(f"Profile: {profile}")
except ApiError as e:
print(f"Authentication error: {e}")
# Example 5: Plans and subscriptions
def subscription_example():
"""Plans and subscription example."""
client = GatewayzApi(
token="YOUR_API_TOKEN",
base_url="https://api.gatewayz.ai"
)
# List available plans
plans = client.plans.list_plans()
print(f"Available plans: {plans}")
# Get current subscription
subscription = client.subscription.get_subscription()
print(f"Current subscription: {subscription}")
# Example 6: Coupon management
def coupon_example():
"""Coupon management example."""
client = GatewayzApi(
token="YOUR_API_TOKEN",
base_url="https://api.gatewayz.ai"
)
# List available coupons
coupons = client.coupons.list_available_coupons()
print(f"Available coupons: {coupons}")
# Redeem a coupon
try:
redemption = client.coupons.redeem_coupon(coupon_code="SAVE20")
print(f"Coupon redeemed: {redemption}")
except ApiError as e:
print(f"Coupon error: {e}")
# Example 7: Error handling
def error_handling_example():
"""Comprehensive error handling example."""
from gatewayz.errors import UnprocessableEntityError
client = GatewayzApi(
token="YOUR_API_TOKEN",
base_url="https://api.gatewayz.ai"
)
try:
response = client.chat.chat_completions(
model="gpt-4",
messages=[
Message(role="user", content="Hello!")
]
)
print(f"Success: {response}")
except UnprocessableEntityError as e:
print(f"Validation error: {e}")
except ApiError as e:
print(f"API error: {e.status_code} - {e.body}")
except Exception as e:
print(f"Unexpected error: {e}")
# Example 8: Custom configuration
def custom_config_example():
"""Custom configuration example."""
import httpx
# Custom httpx client with specific settings
custom_httpx = httpx.Client(
timeout=120.0,
follow_redirects=True,
verify=True
)
client = GatewayzApi(
token="YOUR_API_TOKEN",
base_url="https://api.gatewayz.ai",
httpx_client=custom_httpx,
headers={
"X-Custom-Header": "custom-value"
}
)
response = client.ping_ping_get()
print(f"Response: {response}")
# Example 9: Using request options
def request_options_example():
"""Request options example."""
from gatewayz.core import RequestOptions
client = GatewayzApi(
token="YOUR_API_TOKEN",
base_url="https://api.gatewayz.ai"
)
# Custom request options
response = client.ping_ping_get(
request_options=RequestOptions(
timeout_in_seconds=30,
max_retries=3,
additional_headers={"X-Request-ID": "abc123"}
)
)
print(f"Response: {response}")
# Example 10: Dynamic token provider
def dynamic_token_example():
"""Dynamic token provider example."""
def get_token():
# Your logic to fetch or refresh token
# This could read from a file, environment variable, or API
return "dynamically-fetched-token"
client = GatewayzApi(
token=get_token, # Pass callable instead of string
base_url="https://api.gatewayz.ai"
)
response = client.ping_ping_get()
print(f"Response: {response}")
if __name__ == "__main__":
print("GatewayzApi SDK Examples")
print("=" * 50)
# Uncomment the examples you want to run
# basic_example()
# chat_example()
# asyncio.run(async_example())
# auth_example()
# subscription_example()
# coupon_example()
# error_handling_example()
# custom_config_example()
# request_options_example()
# dynamic_token_example()
print("\nPlease uncomment the examples you want to run and add your API credentials.")