Skip to content

Commit 469e6d6

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 469e6d6

7 files changed

Lines changed: 538 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+
@warn_deprecation( # type: ignore[untyped-decorator]
57+
"AsyncAnalyticsRuleV1 is deprecated on v30+. Use client.analytics.rules[rule_id] instead.",
58+
flag_name="analytics_rules_v1_deprecation",
59+
)
60+
def __init__(self, api_call: AsyncApiCall, rule_id: str):
61+
"""
62+
Initialize the AsyncAnalyticsRuleV1 object.
63+
64+
Args:
65+
api_call (AsyncApiCall): The API call object for making requests.
66+
rule_id (str): The ID of the analytics rule.
67+
"""
68+
self.api_call = api_call
69+
self.rule_id = rule_id
70+
71+
async def retrieve(
72+
self,
73+
) -> typing.Union[RuleSchemaForQueries, RuleSchemaForCounters]:
74+
"""
75+
Retrieve this specific analytics rule.
76+
77+
Returns:
78+
Union[RuleSchemaForQueries, RuleSchemaForCounters]:
79+
The schema containing the rule details.
80+
"""
81+
response: typing.Union[
82+
RuleSchemaForQueries, RuleSchemaForCounters
83+
] = await self.api_call.get(
84+
self._endpoint_path,
85+
entity_type=typing.Union[RuleSchemaForQueries, RuleSchemaForCounters],
86+
as_json=True,
87+
)
88+
return response
89+
90+
async def delete(self) -> RuleDeleteSchema:
91+
"""
92+
Delete this specific analytics rule.
93+
94+
Returns:
95+
RuleDeleteSchema: The schema containing the deletion response.
96+
"""
97+
response: RuleDeleteSchema = await self.api_call.delete(
98+
self._endpoint_path,
99+
entity_type=RuleDeleteSchema,
100+
)
101+
102+
return response
103+
104+
@property
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: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
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
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)