1
- import requests
1
+ import httpx
2
2
from fastapi import HTTPException , status
3
3
import logging
4
4
from app .core .resource_limits import DEFAULT_KEY_DURATION , DEFAULT_MAX_SPEND , DEFAULT_RPM_PER_KEY
@@ -57,20 +57,21 @@ async def create_key(self, email: str, name: str, user_id: int, team_id: str, du
57
57
if user_id is not None :
58
58
request_data ["user_id" ] = str (user_id )
59
59
60
- logger . info ( f"Making request to LiteLLM API to generate key with data: { request_data } " )
61
- response = requests .post (
62
- f"{ self .api_url } /key/generate" ,
63
- json = request_data ,
64
- headers = {
65
- "Authorization" : f"Bearer { self .master_key } "
66
- }
67
- )
60
+ async with httpx . AsyncClient () as client :
61
+ response = await client .post (
62
+ f"{ self .api_url } /key/generate" ,
63
+ json = request_data ,
64
+ headers = {
65
+ "Authorization" : f"Bearer { self .master_key } "
66
+ }
67
+ )
68
68
69
- response .raise_for_status ()
70
- key = response .json ()["key" ]
71
- logger .info ("Successfully generated new LiteLLM API key" )
72
- return key
73
- except requests .exceptions .RequestException as e :
69
+ response .raise_for_status ()
70
+ response_data = response .json ()
71
+ key = response_data ["key" ]
72
+ logger .info ("Successfully generated new LiteLLM API key" )
73
+ return key
74
+ except httpx .HTTPStatusError as e :
74
75
error_msg = str (e )
75
76
if hasattr (e , 'response' ) and e .response is not None :
76
77
try :
@@ -87,21 +88,22 @@ async def create_key(self, email: str, name: str, user_id: int, team_id: str, du
87
88
async def delete_key (self , key : str ) -> bool :
88
89
"""Delete a LiteLLM API key"""
89
90
try :
90
- response = requests .post (
91
- f"{ self .api_url } /key/delete" ,
92
- json = {"keys" : [key ]}, # API expects an array of keys
93
- headers = {
94
- "Authorization" : f"Bearer { self .master_key } "
95
- }
96
- )
91
+ async with httpx .AsyncClient () as client :
92
+ response = await client .post (
93
+ f"{ self .api_url } /key/delete" ,
94
+ json = {"keys" : [key ]}, # API expects an array of keys
95
+ headers = {
96
+ "Authorization" : f"Bearer { self .master_key } "
97
+ }
98
+ )
97
99
98
- # Treat 404 (key not found) as success
99
- if response .status_code == 404 :
100
- return True
100
+ # Treat 404 (key not found) as success
101
+ if response .status_code == 404 :
102
+ return True
101
103
102
- response .raise_for_status ()
103
- return True
104
- except requests . exceptions . RequestException as e :
104
+ response .raise_for_status ()
105
+ return True
106
+ except httpx . HTTPStatusError as e :
105
107
error_msg = str (e )
106
108
if hasattr (e , 'response' ) and e .response is not None :
107
109
try :
@@ -118,19 +120,21 @@ async def delete_key(self, key: str) -> bool:
118
120
async def get_key_info (self , litellm_token : str ) -> dict :
119
121
"""Get information about a LiteLLM API key"""
120
122
try :
121
- response = requests .get (
122
- f"{ self .api_url } /key/info" ,
123
- headers = {
124
- "Authorization" : f"Bearer { self .master_key } "
125
- },
126
- params = {
127
- "key" : litellm_token
128
- }
129
- )
130
- response .raise_for_status ()
131
- logger .info (f"LiteLLM key information: { response .json ()} " )
132
- return response .json ()
133
- except requests .exceptions .RequestException as e :
123
+ async with httpx .AsyncClient () as client :
124
+ response = await client .get (
125
+ f"{ self .api_url } /key/info" ,
126
+ headers = {
127
+ "Authorization" : f"Bearer { self .master_key } "
128
+ },
129
+ params = {
130
+ "key" : litellm_token
131
+ }
132
+ )
133
+ response .raise_for_status ()
134
+ response_data = response .json ()
135
+ logger .info (f"LiteLLM key information: { response_data } " )
136
+ return response_data
137
+ except httpx .HTTPStatusError as e :
134
138
error_msg = str (e )
135
139
logger .error (f"Error getting LiteLLM key information: { error_msg } " )
136
140
if hasattr (e , 'response' ) and e .response is not None :
@@ -156,15 +160,16 @@ async def update_budget(self, litellm_token: str, budget_duration: str, budget_a
156
160
if budget_amount :
157
161
request_data ["max_budget" ] = budget_amount
158
162
159
- response = requests .post (
160
- f"{ self .api_url } /key/update" ,
161
- headers = {
162
- "Authorization" : f"Bearer { self .master_key } "
163
- },
164
- json = request_data
165
- )
166
- response .raise_for_status ()
167
- except requests .exceptions .RequestException as e :
163
+ async with httpx .AsyncClient () as client :
164
+ response = await client .post (
165
+ f"{ self .api_url } /key/update" ,
166
+ headers = {
167
+ "Authorization" : f"Bearer { self .master_key } "
168
+ },
169
+ json = request_data
170
+ )
171
+ response .raise_for_status ()
172
+ except httpx .HTTPStatusError as e :
168
173
error_msg = str (e )
169
174
if hasattr (e , 'response' ) and e .response is not None :
170
175
try :
@@ -180,18 +185,19 @@ async def update_budget(self, litellm_token: str, budget_duration: str, budget_a
180
185
async def update_key_duration (self , litellm_token : str , duration : str ):
181
186
"""Update the duration for a LiteLLM API key"""
182
187
try :
183
- response = requests .post (
184
- f"{ self .api_url } /key/update" ,
185
- headers = {
186
- "Authorization" : f"Bearer { self .master_key } "
187
- },
188
- json = {
189
- "key" : litellm_token ,
190
- "duration" : duration
191
- }
192
- )
193
- response .raise_for_status ()
194
- except requests .exceptions .RequestException as e :
188
+ async with httpx .AsyncClient () as client :
189
+ response = await client .post (
190
+ f"{ self .api_url } /key/update" ,
191
+ headers = {
192
+ "Authorization" : f"Bearer { self .master_key } "
193
+ },
194
+ json = {
195
+ "key" : litellm_token ,
196
+ "duration" : duration
197
+ }
198
+ )
199
+ response .raise_for_status ()
200
+ except httpx .HTTPStatusError as e :
195
201
error_msg = str (e )
196
202
if hasattr (e , 'response' ) and e .response is not None :
197
203
try :
@@ -207,21 +213,22 @@ async def update_key_duration(self, litellm_token: str, duration: str):
207
213
async def set_key_restrictions (self , litellm_token : str , duration : str , budget_amount : float , rpm_limit : int , budget_duration : Optional [str ] = None ):
208
214
"""Set the restrictions for a LiteLLM API key"""
209
215
try :
210
- response = requests .post (
211
- f"{ self .api_url } /key/update" ,
212
- headers = {
213
- "Authorization" : f"Bearer { self .master_key } "
214
- },
215
- json = {
216
- "key" : litellm_token ,
217
- "duration" : duration ,
218
- "budget_duration" : budget_duration ,
219
- "max_budget" : budget_amount ,
220
- "rpm_limit" : rpm_limit
221
- }
222
- )
223
- response .raise_for_status ()
224
- except requests .exceptions .RequestException as e :
216
+ async with httpx .AsyncClient () as client :
217
+ response = await client .post (
218
+ f"{ self .api_url } /key/update" ,
219
+ headers = {
220
+ "Authorization" : f"Bearer { self .master_key } "
221
+ },
222
+ json = {
223
+ "key" : litellm_token ,
224
+ "duration" : duration ,
225
+ "budget_duration" : budget_duration ,
226
+ "max_budget" : budget_amount ,
227
+ "rpm_limit" : rpm_limit
228
+ }
229
+ )
230
+ response .raise_for_status ()
231
+ except httpx .HTTPStatusError as e :
225
232
error_msg = str (e )
226
233
if hasattr (e , 'response' ) and e .response is not None :
227
234
try :
@@ -237,18 +244,19 @@ async def set_key_restrictions(self, litellm_token: str, duration: str, budget_a
237
244
async def update_key_team_association (self , litellm_token : str , new_team_id : str ):
238
245
"""Update the team association for a LiteLLM API key"""
239
246
try :
240
- response = requests .post (
241
- f"{ self .api_url } /key/update" ,
242
- headers = {
243
- "Authorization" : f"Bearer { self .master_key } "
244
- },
245
- json = {
246
- "key" : litellm_token ,
247
- "team_id" : new_team_id
248
- }
249
- )
250
- response .raise_for_status ()
251
- except requests .exceptions .RequestException as e :
247
+ async with httpx .AsyncClient () as client :
248
+ response = await client .post (
249
+ f"{ self .api_url } /key/update" ,
250
+ headers = {
251
+ "Authorization" : f"Bearer { self .master_key } "
252
+ },
253
+ json = {
254
+ "key" : litellm_token ,
255
+ "team_id" : new_team_id
256
+ }
257
+ )
258
+ response .raise_for_status ()
259
+ except httpx .HTTPStatusError as e :
252
260
error_msg = str (e )
253
261
if hasattr (e , 'response' ) and e .response is not None :
254
262
try :
0 commit comments