Skip to content

Commit 82de166

Browse files
committed
feat(analyticsV1): add async support for analytics v1 operations
- add async tests for analytics v1 functionality - add async fixtures for testing async analytics v1 operations - remove future annotations imports from test files
1 parent dd2abf2 commit 82de166

7 files changed

Lines changed: 546 additions & 2 deletions
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
"""
2+
This module provides async functionality for managing individual analytics rules in Typesense (V1).
3+
4+
Classes:
5+
- AsyncAnalyticsRuleV1: Handles async operations related to a specific analytics rule.
6+
7+
Methods:
8+
- __init__: Initializes the AsyncAnalyticsRuleV1 object.
9+
- _endpoint_path: Constructs the API endpoint path for this specific analytics rule.
10+
- retrieve: Retrieves the details of this specific analytics rule.
11+
- delete: Deletes this specific analytics rule.
12+
13+
The AsyncAnalyticsRuleV1 class interacts with the Typesense API to manage operations on a
14+
specific analytics rule. It provides methods to retrieve and delete individual rules.
15+
16+
For more information on analytics, refer to the Analytics & Query Suggestion
17+
[documentation](https://typesense.org/docs/27.0/api/analytics-query-suggestions.html)
18+
19+
This module uses type hinting and is compatible with Python 3.11+ as well as earlier
20+
versions through the use of the typing_extensions library.
21+
"""
22+
23+
import sys
24+
25+
if sys.version_info >= (3, 11):
26+
import typing
27+
else:
28+
import typing_extensions as typing
29+
30+
from typing_extensions import deprecated
31+
32+
from typesense.async_api_call import AsyncApiCall
33+
from typesense.logger import warn_deprecation
34+
from typesense.types.analytics_rule_v1 import (
35+
RuleDeleteSchema,
36+
RuleSchemaForCounters,
37+
RuleSchemaForQueries,
38+
)
39+
40+
41+
@deprecated(
42+
"AsyncAnalyticsRuleV1 is deprecated on v30+. Use client.analytics.rules[rule_id] instead."
43+
)
44+
class AsyncAnalyticsRuleV1:
45+
"""
46+
Class for managing individual analytics rules in Typesense (V1) (async).
47+
48+
This class provides methods to interact with a specific analytics rule,
49+
including retrieving and deleting it.
50+
51+
Attributes:
52+
api_call (AsyncApiCall): The API call object for making requests.
53+
rule_id (str): The ID of the analytics rule.
54+
"""
55+
56+
def __init__(self, api_call: AsyncApiCall, rule_id: str):
57+
"""
58+
Initialize the AsyncAnalyticsRuleV1 object.
59+
60+
Args:
61+
api_call (AsyncApiCall): The API call object for making requests.
62+
rule_id (str): The ID of the analytics rule.
63+
"""
64+
self.api_call = api_call
65+
self.rule_id = rule_id
66+
67+
async def retrieve(
68+
self,
69+
) -> typing.Union[RuleSchemaForQueries, RuleSchemaForCounters]:
70+
"""
71+
Retrieve this specific analytics rule.
72+
73+
Returns:
74+
Union[RuleSchemaForQueries, RuleSchemaForCounters]:
75+
The schema containing the rule details.
76+
"""
77+
response: typing.Union[
78+
RuleSchemaForQueries, RuleSchemaForCounters
79+
] = await self.api_call.get(
80+
self._endpoint_path,
81+
entity_type=typing.Union[RuleSchemaForQueries, RuleSchemaForCounters],
82+
as_json=True,
83+
)
84+
return response
85+
86+
async def delete(self) -> RuleDeleteSchema:
87+
"""
88+
Delete this specific analytics rule.
89+
90+
Returns:
91+
RuleDeleteSchema: The schema containing the deletion response.
92+
"""
93+
response: RuleDeleteSchema = await self.api_call.delete(
94+
self._endpoint_path,
95+
entity_type=RuleDeleteSchema,
96+
)
97+
98+
return response
99+
100+
@property
101+
@warn_deprecation( # type: ignore[untyped-decorator]
102+
"AsyncAnalyticsRuleV1 is deprecated on v30+. Use client.analytics.rules[rule_id] instead.",
103+
flag_name="analytics_rules_v1_deprecation",
104+
)
105+
def _endpoint_path(self) -> str:
106+
"""
107+
Construct the API endpoint path for this specific analytics rule.
108+
109+
Returns:
110+
str: The constructed endpoint path.
111+
"""
112+
from typesense.async_analytics_rules_v1 import AsyncAnalyticsRulesV1
113+
114+
return "/".join([AsyncAnalyticsRulesV1.resource_path, self.rule_id])
Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
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+
def __init__(self, api_call: AsyncApiCall):
69+
"""
70+
Initialize the AsyncAnalyticsRulesV1 object.
71+
72+
Args:
73+
api_call (AsyncApiCall): The API call object for making requests.
74+
"""
75+
self.api_call = api_call
76+
self.rules: typing.Dict[str, AsyncAnalyticsRuleV1] = {}
77+
78+
def __getitem__(self, rule_id: str) -> AsyncAnalyticsRuleV1:
79+
"""
80+
Get or create an AsyncAnalyticsRuleV1 object for a given rule_id.
81+
82+
Args:
83+
rule_id (str): The ID of the analytics rule.
84+
85+
Returns:
86+
AsyncAnalyticsRuleV1: The AsyncAnalyticsRuleV1 object for the given ID.
87+
"""
88+
if not self.rules.get(rule_id):
89+
self.rules[rule_id] = AsyncAnalyticsRuleV1(self.api_call, rule_id)
90+
return self.rules[rule_id]
91+
92+
@warn_deprecation( # type: ignore[untyped-decorator]
93+
"AsyncAnalyticsRulesV1 is deprecated on v30+. Use client.analytics instead.",
94+
flag_name="analytics_rules_v1_deprecation",
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+
@warn_deprecation( # type: ignore[untyped-decorator]
133+
"AsyncAnalyticsRulesV1 is deprecated on v30+. Use client.analytics instead.",
134+
flag_name="analytics_rules_v1_deprecation",
135+
)
136+
async def upsert(
137+
self,
138+
rule_id: str,
139+
rule: typing.Union[RuleCreateSchemaForQueries, RuleSchemaForCounters],
140+
) -> typing.Union[RuleSchemaForCounters, RuleCreateSchemaForQueries]:
141+
"""
142+
Create or update an analytics rule.
143+
144+
Args:
145+
rule_id (str): The ID of the rule to upsert.
146+
rule (Union[RuleCreateSchemaForQueries, RuleSchemaForCounters]): The rule schema.
147+
148+
Returns:
149+
Union[RuleSchemaForCounters, RuleCreateSchemaForQueries]: The upserted rule.
150+
"""
151+
response = await self.api_call.put(
152+
"/".join([self.resource_path, rule_id]),
153+
body=rule,
154+
entity_type=typing.Union[RuleSchemaForQueries, RuleSchemaForCounters],
155+
)
156+
return typing.cast(
157+
typing.Union[RuleSchemaForCounters, RuleCreateSchemaForQueries],
158+
response,
159+
)
160+
161+
@warn_deprecation( # type: ignore[untyped-decorator]
162+
"AsyncAnalyticsRulesV1 is deprecated on v30+. Use client.analytics instead.",
163+
flag_name="analytics_rules_v1_deprecation",
164+
)
165+
async def retrieve(self) -> RulesRetrieveSchema:
166+
"""
167+
Retrieve all analytics rules.
168+
169+
Returns:
170+
RulesRetrieveSchema: The schema containing all analytics rules.
171+
"""
172+
response: RulesRetrieveSchema = await self.api_call.get(
173+
AsyncAnalyticsRulesV1.resource_path,
174+
as_json=True,
175+
entity_type=RulesRetrieveSchema,
176+
)
177+
return response
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
"""
2+
This module provides async functionality for managing analytics (V1) in Typesense.
3+
4+
Classes:
5+
- AsyncAnalyticsV1: Handles async operations related to analytics, including access to analytics rules.
6+
7+
Methods:
8+
- __init__: Initializes the AsyncAnalyticsV1 object.
9+
10+
The AsyncAnalyticsV1 class serves as an entry point for analytics-related operations in Typesense,
11+
currently providing access to AsyncAnalyticsRulesV1.
12+
13+
For more information on analytics, refer to the Analytics & Query Suggestion
14+
[documentation](https://typesense.org/docs/27.0/api/analytics-query-suggestions.html)
15+
16+
This module uses type hinting and is compatible with Python 3.11+ as well as earlier
17+
versions through the use of the typing_extensions library.
18+
"""
19+
20+
from typing_extensions import deprecated
21+
22+
from typesense.async_analytics_rules_v1 import AsyncAnalyticsRulesV1
23+
from typesense.async_api_call import AsyncApiCall
24+
25+
26+
@deprecated("AsyncAnalyticsV1 is deprecated on v30+. Use client.analytics instead.")
27+
class AsyncAnalyticsV1(object):
28+
"""
29+
Class for managing analytics in Typesense (V1) (async).
30+
31+
This class provides access to analytics-related functionalities,
32+
currently including operations on analytics rules.
33+
34+
Attributes:
35+
rules (AsyncAnalyticsRulesV1): An instance of AsyncAnalyticsRulesV1 for managing analytics rules.
36+
"""
37+
38+
def __init__(self, api_call: AsyncApiCall) -> None:
39+
"""
40+
Initialize the AsyncAnalyticsV1 object.
41+
42+
Args:
43+
api_call (AsyncApiCall): The API call object for making requests.
44+
"""
45+
self._rules = AsyncAnalyticsRulesV1(api_call)
46+
47+
@property
48+
def rules(self) -> AsyncAnalyticsRulesV1:
49+
return self._rules

0 commit comments

Comments
 (0)