@@ -169,7 +169,7 @@ class Usage:
169169 checked individually.
170170 For API Pro accounts, api_key_character, speech_to_text_minutes,
171171 speech_to_speech_minutes, products, start_time and end_time provide
172- further details; for other accounts they are invalid or None.
172+ further details; for other accounts they are None.
173173 The any_limit_reached property checks if for any usage type the amount used
174174 has reached the allowed amount.
175175 """
@@ -279,16 +279,22 @@ def __init__(self, json: Optional[dict]):
279279 self ._character = self .Detail (json , "character" )
280280 self ._document = self .Detail (json , "document" )
281281 self ._team_document = self .Detail (json , "team_document" )
282- self ._api_key_character = self .Detail (json , "api_key_character" )
283- self ._speech_to_text_minutes = self .Detail (
282+ self ._api_key_character = self ._optional_detail (
283+ json , "api_key_character"
284+ )
285+ self ._speech_to_text_minutes = self ._optional_detail (
284286 json , "speech_to_text_minutes"
285287 )
286- self ._speech_to_speech_minutes = self .Detail (
288+ self ._speech_to_speech_minutes = self ._optional_detail (
287289 json , "speech_to_speech_minutes"
288290 )
289291 products = json .get ("products" ) if json else None
290292 self ._products : Optional [List [Usage .Product ]] = (
291- [Usage .Product .from_json (p ) for p in products ]
293+ [
294+ Usage .Product .from_json (p )
295+ for p in products
296+ if isinstance (p , dict )
297+ ]
292298 if isinstance (products , list )
293299 else None
294300 )
@@ -299,6 +305,18 @@ def __init__(self, json: Optional[dict]):
299305 json .get ("end_time" ) if json else None
300306 )
301307
308+ @staticmethod
309+ def _optional_detail (
310+ json : Optional [dict ], prefix : str
311+ ) -> Optional ["Usage.Detail" ]:
312+ """Returns the Detail for prefix, or None if the response contains
313+ neither its count nor its limit."""
314+ if not json or (
315+ f"{ prefix } _count" not in json and f"{ prefix } _limit" not in json
316+ ):
317+ return None
318+ return Usage .Detail (json , prefix )
319+
302320 @property
303321 def any_limit_reached (self ) -> bool :
304322 """True if for the character, document or team document usage, the
@@ -340,22 +358,24 @@ def team_document(self) -> Detail:
340358 return self ._team_document
341359
342360 @property
343- def api_key_character (self ) -> Detail :
361+ def api_key_character (self ) -> Optional [ Detail ] :
344362 """Returns character usage details for this API key, as opposed to
345- character, which covers the whole account. Only valid for API Pro
346- accounts."""
363+ character, which covers the whole account, or None if the response
364+ does not contain them (only present for API Pro accounts) ."""
347365 return self ._api_key_character
348366
349367 @property
350- def speech_to_text_minutes (self ) -> Detail :
351- """Returns speech-to-text usage details in minutes. Only valid for API
352- Pro accounts."""
368+ def speech_to_text_minutes (self ) -> Optional [Detail ]:
369+ """Returns speech-to-text usage details in minutes, or None if the
370+ response does not contain them (only present for API Pro
371+ accounts)."""
353372 return self ._speech_to_text_minutes
354373
355374 @property
356- def speech_to_speech_minutes (self ) -> Detail :
357- """Returns speech-to-speech usage details in minutes. Only valid for
358- API Pro accounts."""
375+ def speech_to_speech_minutes (self ) -> Optional [Detail ]:
376+ """Returns speech-to-speech usage details in minutes, or None if the
377+ response does not contain them (only present for API Pro
378+ accounts)."""
359379 return self ._speech_to_speech_minutes
360380
361381 @property
0 commit comments