@@ -2396,6 +2396,24 @@ def _str_or_none(value: Any) -> Optional[str]:
23962396 return value if isinstance (value , str ) else None
23972397
23982398
2399+ def _group_by_or_none (value : Any ) -> Union [AdminUsageGroupBy , str , None ]:
2400+ # The live API answers group_by=key_and_day with "keyAndDay".
2401+ if value == "keyAndDay" :
2402+ return AdminUsageGroupBy .KEY_AND_DAY
2403+ return _open_enum (AdminUsageGroupBy , value )
2404+
2405+
2406+ def _open_enum (enum_type : Any , value : Any ) -> Any :
2407+ """Returns the enum member for a known value, an unknown string as is,
2408+ and None for anything else."""
2409+ if not isinstance (value , str ):
2410+ return None
2411+ try :
2412+ return enum_type (value )
2413+ except ValueError :
2414+ return value
2415+
2416+
23992417def _limit_or_none (json : dict , key : str ) -> Optional [int ]:
24002418 return None if isinstance (json .get (key ), bool ) else get_int_safe (json , key )
24012419
@@ -2747,7 +2765,8 @@ class AdminUsageReport:
27472765 :param total_usage: Usage totals over the whole date range.
27482766 :param start_date: Start of the report period.
27492767 :param end_date: End of the report period.
2750- :param group_by: Grouping used for the report, if any.
2768+ :param group_by: Grouping used for the report: an AdminUsageGroupBy for a
2769+ known value, the string for an unknown one, or None.
27512770 :param key_usages: Usage per key, present when grouped by key.
27522771 :param key_and_day_usages: Usage per key and day, present when grouped by
27532772 key and day.
@@ -2758,7 +2777,7 @@ def __init__(
27582777 total_usage : AdminUsageBreakdown ,
27592778 start_date : Optional [datetime .datetime ],
27602779 end_date : Optional [datetime .datetime ],
2761- group_by : Optional [ str ] = None ,
2780+ group_by : Union [ AdminUsageGroupBy , str , None ] = None ,
27622781 key_usages : Optional [List [KeyUsage ]] = None ,
27632782 key_and_day_usages : Optional [List [KeyUsage ]] = None ,
27642783 ):
@@ -2779,7 +2798,7 @@ def from_json(json: Any) -> "AdminUsageReport":
27792798 ),
27802799 start_date = _parse_utc_timestamp (report .get ("start_date" )),
27812800 end_date = _parse_utc_timestamp (report .get ("end_date" )),
2782- group_by = _str_or_none (report .get ("group_by" )),
2801+ group_by = _group_by_or_none (report .get ("group_by" )),
27832802 key_usages = [
27842803 KeyUsage .from_json (item )
27852804 for item in _dict_items (report .get ("key_usages" ))
@@ -2806,9 +2825,9 @@ def end_date(self) -> Optional[datetime.datetime]:
28062825 return self ._end_date
28072826
28082827 @property
2809- def group_by (self ) -> Optional [ str ]:
2810- """Returns the grouping used for the report as the API returns it
2811- (currently "key" or "keyAndDay") , or None."""
2828+ def group_by (self ) -> Union [ AdminUsageGroupBy , str , None ]:
2829+ """Returns the grouping used for the report as an AdminUsageGroupBy,
2830+ the string the API sent if it is not a known value , or None."""
28122831 return self ._group_by
28132832
28142833 @property
@@ -2932,7 +2951,9 @@ def usage_date(self) -> Optional[datetime.datetime]:
29322951class CustomTagUsageReport :
29332952 """Admin custom tag usage analytics report for a date range.
29342953
2935- :param aggregate_by: Aggregation used for the report.
2954+ :param aggregate_by: Aggregation used for the report: a
2955+ CustomTagUsageAggregateBy for a known value, the string for an
2956+ unknown one, or None.
29362957 :param start_date: Start of the report period.
29372958 :param end_date: End of the report period.
29382959 :param next_page: Page number to request the next page, or None if this
@@ -2942,7 +2963,7 @@ class CustomTagUsageReport:
29422963
29432964 def __init__ (
29442965 self ,
2945- aggregate_by : Optional [ str ],
2966+ aggregate_by : Union [ CustomTagUsageAggregateBy , str , None ],
29462967 start_date : Optional [datetime .datetime ],
29472968 end_date : Optional [datetime .datetime ],
29482969 next_page : Optional [int ],
@@ -2959,7 +2980,9 @@ def from_json(json: Any) -> "CustomTagUsageReport":
29592980 """Create CustomTagUsageReport from the given API JSON object."""
29602981 report = _as_dict (_as_dict (json ).get ("custom_tag_usage_report" ))
29612982 return CustomTagUsageReport (
2962- aggregate_by = _str_or_none (report .get ("aggregate_by" )),
2983+ aggregate_by = _open_enum (
2984+ CustomTagUsageAggregateBy , report .get ("aggregate_by" )
2985+ ),
29632986 start_date = _parse_utc_timestamp (report .get ("start_date" )),
29642987 end_date = _parse_utc_timestamp (report .get ("end_date" )),
29652988 next_page = get_int_safe (report , "next_page" ),
@@ -2970,9 +2993,10 @@ def from_json(json: Any) -> "CustomTagUsageReport":
29702993 )
29712994
29722995 @property
2973- def aggregate_by (self ) -> Optional [str ]:
2974- """Returns the aggregation used for the report as the API returns it
2975- (e.g. "period" or "day"), or None."""
2996+ def aggregate_by (self ) -> Union [CustomTagUsageAggregateBy , str , None ]:
2997+ """Returns the aggregation used for the report as a
2998+ CustomTagUsageAggregateBy, the string the API sent if it is not a
2999+ known value, or None."""
29763000 return self ._aggregate_by
29773001
29783002 @property
0 commit comments