|
| 1 | +""" |
| 2 | +This module provides async functionality for managing analytics rules in Typesense (V1). |
| 3 | +
|
| 4 | +Classes: |
| 5 | + - AsyncAnalyticsRulesV1: Handles async operations related to analytics rules. |
| 6 | +
|
| 7 | +Methods: |
| 8 | + - __init__: Initializes the AsyncAnalyticsRulesV1 object. |
| 9 | + - __getitem__: Retrieves or creates an AsyncAnalyticsRuleV1 object for a given rule_id. |
| 10 | + - create: Creates a new analytics rule. |
| 11 | + - upsert: Creates or updates an analytics rule. |
| 12 | + - retrieve: Retrieves all analytics rules. |
| 13 | +
|
| 14 | +Attributes: |
| 15 | + - resource_path: The API resource path for analytics rules. |
| 16 | +
|
| 17 | +The AsyncAnalyticsRulesV1 class interacts with the Typesense API to manage analytics rule operations. |
| 18 | +It provides methods to create, update, and retrieve analytics rules, as well as access |
| 19 | +individual AsyncAnalyticsRuleV1 objects. |
| 20 | +
|
| 21 | +For more information on analytics, refer to the Analytics & Query Suggestion |
| 22 | +[documentation](https://typesense.org/docs/27.0/api/analytics-query-suggestions.html) |
| 23 | +
|
| 24 | +This module uses type hinting and is compatible with Python 3.11+ as well as earlier |
| 25 | +versions through the use of the typing_extensions library. |
| 26 | +""" |
| 27 | + |
| 28 | +import sys |
| 29 | + |
| 30 | +from typesense.logger import warn_deprecation |
| 31 | + |
| 32 | +if sys.version_info >= (3, 11): |
| 33 | + import typing |
| 34 | +else: |
| 35 | + import typing_extensions as typing |
| 36 | + |
| 37 | +from typesense.async_analytics_rule_v1 import AsyncAnalyticsRuleV1 |
| 38 | +from typesense.async_api_call import AsyncApiCall |
| 39 | +from typesense.types.analytics_rule_v1 import ( |
| 40 | + RuleCreateSchemaForCounters, |
| 41 | + RuleCreateSchemaForQueries, |
| 42 | + RuleSchemaForCounters, |
| 43 | + RuleSchemaForQueries, |
| 44 | + RulesRetrieveSchema, |
| 45 | +) |
| 46 | + |
| 47 | +_RuleParams = typing.Union[ |
| 48 | + typing.Dict[str, typing.Union[str, int, bool]], |
| 49 | + None, |
| 50 | +] |
| 51 | + |
| 52 | + |
| 53 | +class AsyncAnalyticsRulesV1(object): |
| 54 | + """ |
| 55 | + Class for managing analytics rules in Typesense (V1) (async). |
| 56 | +
|
| 57 | + This class provides methods to interact with analytics rules, including |
| 58 | + creating, updating, and retrieving them. |
| 59 | +
|
| 60 | + Attributes: |
| 61 | + resource_path (str): The API resource path for analytics rules. |
| 62 | + api_call (AsyncApiCall): The API call object for making requests. |
| 63 | + rules (Dict[str, AsyncAnalyticsRuleV1]): A dictionary of AsyncAnalyticsRuleV1 objects. |
| 64 | + """ |
| 65 | + |
| 66 | + resource_path: typing.Final[str] = "/analytics/rules" |
| 67 | + |
| 68 | + @warn_deprecation( # type: ignore[untyped-decorator] |
| 69 | + "AsyncAnalyticsRulesV1 is deprecated on v30+. Use client.analytics instead.", |
| 70 | + flag_name="analytics_rules_v1_deprecation", |
| 71 | + ) |
| 72 | + def __init__(self, api_call: AsyncApiCall): |
| 73 | + """ |
| 74 | + Initialize the AsyncAnalyticsRulesV1 object. |
| 75 | +
|
| 76 | + Args: |
| 77 | + api_call (AsyncApiCall): The API call object for making requests. |
| 78 | + """ |
| 79 | + self.api_call = api_call |
| 80 | + self.rules: typing.Dict[str, AsyncAnalyticsRuleV1] = {} |
| 81 | + |
| 82 | + def __getitem__(self, rule_id: str) -> AsyncAnalyticsRuleV1: |
| 83 | + """ |
| 84 | + Get or create an AsyncAnalyticsRuleV1 object for a given rule_id. |
| 85 | +
|
| 86 | + Args: |
| 87 | + rule_id (str): The ID of the analytics rule. |
| 88 | +
|
| 89 | + Returns: |
| 90 | + AsyncAnalyticsRuleV1: The AsyncAnalyticsRuleV1 object for the given ID. |
| 91 | + """ |
| 92 | + if not self.rules.get(rule_id): |
| 93 | + self.rules[rule_id] = AsyncAnalyticsRuleV1(self.api_call, rule_id) |
| 94 | + return self.rules[rule_id] |
| 95 | + |
| 96 | + async def create( |
| 97 | + self, |
| 98 | + rule: typing.Union[RuleCreateSchemaForCounters, RuleCreateSchemaForQueries], |
| 99 | + rule_parameters: _RuleParams = None, |
| 100 | + ) -> typing.Union[RuleSchemaForCounters, RuleSchemaForQueries]: |
| 101 | + """ |
| 102 | + Create a new analytics rule. |
| 103 | +
|
| 104 | + This method can create both counter rules and query rules. |
| 105 | +
|
| 106 | + Args: |
| 107 | + rule (Union[RuleCreateSchemaForCounters, RuleCreateSchemaForQueries]): |
| 108 | + The rule schema. Use RuleCreateSchemaForCounters for counter rules |
| 109 | + and RuleCreateSchemaForQueries for query rules. |
| 110 | +
|
| 111 | + rule_parameters (_RuleParams, optional): Additional rule parameters. |
| 112 | +
|
| 113 | + Returns: |
| 114 | + Union[RuleSchemaForCounters, RuleSchemaForQueries]: |
| 115 | + The created rule. Returns RuleSchemaForCounters for counter rules |
| 116 | + and RuleSchemaForQueries for query rules. |
| 117 | + """ |
| 118 | + response: typing.Union[ |
| 119 | + RuleSchemaForCounters, RuleSchemaForQueries |
| 120 | + ] = await self.api_call.post( |
| 121 | + AsyncAnalyticsRulesV1.resource_path, |
| 122 | + body=rule, |
| 123 | + params=rule_parameters, |
| 124 | + as_json=True, |
| 125 | + entity_type=typing.Union[ |
| 126 | + RuleSchemaForCounters, |
| 127 | + RuleSchemaForQueries, |
| 128 | + ], |
| 129 | + ) |
| 130 | + return response |
| 131 | + |
| 132 | + async def upsert( |
| 133 | + self, |
| 134 | + rule_id: str, |
| 135 | + rule: typing.Union[RuleCreateSchemaForQueries, RuleSchemaForCounters], |
| 136 | + ) -> typing.Union[RuleSchemaForCounters, RuleCreateSchemaForQueries]: |
| 137 | + """ |
| 138 | + Create or update an analytics rule. |
| 139 | +
|
| 140 | + Args: |
| 141 | + rule_id (str): The ID of the rule to upsert. |
| 142 | + rule (Union[RuleCreateSchemaForQueries, RuleSchemaForCounters]): The rule schema. |
| 143 | +
|
| 144 | + Returns: |
| 145 | + Union[RuleSchemaForCounters, RuleCreateSchemaForQueries]: The upserted rule. |
| 146 | + """ |
| 147 | + response = await self.api_call.put( |
| 148 | + "/".join([AsyncAnalyticsRulesV1.resource_path, rule_id]), |
| 149 | + body=rule, |
| 150 | + entity_type=typing.Union[RuleSchemaForQueries, RuleSchemaForCounters], |
| 151 | + ) |
| 152 | + return typing.cast( |
| 153 | + typing.Union[RuleSchemaForCounters, RuleCreateSchemaForQueries], |
| 154 | + response, |
| 155 | + ) |
| 156 | + |
| 157 | + async def retrieve(self) -> RulesRetrieveSchema: |
| 158 | + """ |
| 159 | + Retrieve all analytics rules. |
| 160 | +
|
| 161 | + Returns: |
| 162 | + RulesRetrieveSchema: The schema containing all analytics rules. |
| 163 | + """ |
| 164 | + response: RulesRetrieveSchema = await self.api_call.get( |
| 165 | + AsyncAnalyticsRulesV1.resource_path, |
| 166 | + as_json=True, |
| 167 | + entity_type=RulesRetrieveSchema, |
| 168 | + ) |
| 169 | + return response |
0 commit comments