Skip to content

Commit 95929db

Browse files
committed
chore: generate sync version of the client
1 parent d42637e commit 95929db

38 files changed

+5088
-0
lines changed

src/typesense/sync/alias.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
"""
2+
This module provides async functionality for managing individual aliases in Typesense.
3+
4+
It contains the Alias class, which allows for retrieving and deleting
5+
aliases asynchronously.
6+
7+
Classes:
8+
Alias: Manages async operations on a single alias in the Typesense API.
9+
10+
Dependencies:
11+
- typesense.async_api_call: Provides the ApiCall class for making async API requests.
12+
- typesense.types.alias: Provides AliasSchema type.
13+
14+
Note: This module uses conditional imports to support both Python 3.11+ and earlier versions.
15+
"""
16+
17+
from .api_call import ApiCall
18+
from typesense.types.alias import AliasSchema
19+
20+
21+
class Alias:
22+
"""
23+
Manages async operations on a single alias in the Typesense API.
24+
25+
This class provides async methods to retrieve and delete an alias.
26+
27+
Attributes:
28+
api_call (ApiCall): The ApiCall instance for making async API requests.
29+
name (str): The name of the alias.
30+
"""
31+
32+
def __init__(self, api_call: ApiCall, name: str):
33+
"""
34+
Initialize the Alias instance.
35+
36+
Args:
37+
api_call (ApiCall): The ApiCall instance for making async API requests.
38+
name (str): The name of the alias.
39+
"""
40+
self.api_call = api_call
41+
self.name = name
42+
43+
def retrieve(self) -> AliasSchema:
44+
"""
45+
Retrieve this specific alias.
46+
47+
Returns:
48+
AliasSchema: The schema containing the alias details.
49+
"""
50+
response: AliasSchema = self.api_call.get(
51+
self._endpoint_path,
52+
entity_type=AliasSchema,
53+
as_json=True,
54+
)
55+
return response
56+
57+
def delete(self) -> AliasSchema:
58+
"""
59+
Delete this specific alias.
60+
61+
Returns:
62+
AliasSchema: The schema containing the deletion response.
63+
"""
64+
response: AliasSchema = self.api_call.delete(
65+
self._endpoint_path,
66+
entity_type=AliasSchema,
67+
)
68+
return response
69+
70+
@property
71+
def _endpoint_path(self) -> str:
72+
"""
73+
Construct the API endpoint path for this specific alias.
74+
75+
Returns:
76+
str: The constructed endpoint path.
77+
"""
78+
from .aliases import Aliases
79+
80+
return "/".join([Aliases.resource_path, self.name])

src/typesense/sync/aliases.py

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
"""
2+
This module provides async functionality for managing aliases in Typesense.
3+
4+
It contains the Aliases class, which allows for creating, updating, retrieving, and
5+
accessing individual aliases asynchronously.
6+
7+
Classes:
8+
Aliases: Manages aliases in the Typesense API (async).
9+
10+
Dependencies:
11+
- typesense.async_api_call: Provides the ApiCall class for making async API requests.
12+
- typesense.async_alias: Provides the Alias class for individual alias operations.
13+
- typesense.types.alias: Provides AliasCreateSchema, AliasSchema, and AliasesResponseSchema types.
14+
15+
Note: This module uses conditional imports to support both Python 3.11+ and earlier versions.
16+
"""
17+
18+
import sys
19+
20+
from .api_call import ApiCall
21+
from .alias import Alias
22+
from typesense.types.alias import AliasCreateSchema, AliasSchema, AliasesResponseSchema
23+
24+
if sys.version_info >= (3, 11):
25+
import typing
26+
else:
27+
import typing_extensions as typing
28+
29+
30+
class Aliases:
31+
"""
32+
Manages aliases in the Typesense API (async).
33+
34+
This class provides async methods to create, update, retrieve, and access individual aliases.
35+
36+
Attributes:
37+
resource_path (str): The API endpoint path for alias operations.
38+
api_call (ApiCall): The ApiCall instance for making async API requests.
39+
aliases (Dict[str, Alias]): A dictionary of Alias instances, keyed by alias name.
40+
"""
41+
42+
resource_path: typing.Final[str] = "/aliases"
43+
44+
def __init__(self, api_call: ApiCall):
45+
"""
46+
Initialize the Aliases instance.
47+
48+
Args:
49+
api_call (ApiCall): The ApiCall instance for making async API requests.
50+
"""
51+
self.api_call = api_call
52+
self.aliases: typing.Dict[str, Alias] = {}
53+
54+
def __getitem__(self, name: str) -> Alias:
55+
"""
56+
Get or create an Alias instance for a given alias name.
57+
58+
This method allows accessing aliases using dictionary-like syntax.
59+
If the Alias instance doesn't exist, it creates a new one.
60+
61+
Args:
62+
name (str): The name of the alias.
63+
64+
Returns:
65+
Alias: The Alias instance for the specified alias name.
66+
67+
Example:
68+
>>> aliases = Aliases(async_api_call)
69+
>>> company_alias = aliases["company_alias"]
70+
"""
71+
if not self.aliases.get(name):
72+
self.aliases[name] = Alias(self.api_call, name)
73+
return self.aliases[name]
74+
75+
def upsert(self, name: str, mapping: AliasCreateSchema) -> AliasSchema:
76+
"""
77+
Create or update an alias.
78+
79+
Args:
80+
name (str): The name of the alias.
81+
mapping (AliasCreateSchema): The schema for creating or updating the alias.
82+
83+
Returns:
84+
AliasSchema: The created or updated alias.
85+
86+
Example:
87+
>>> aliases = Aliases(async_api_call)
88+
>>> alias = await aliases.upsert(
89+
... "company_alias", {"collection_name": "companies"}
90+
... )
91+
"""
92+
response: AliasSchema = self.api_call.put(
93+
self._endpoint_path(name),
94+
body=mapping,
95+
entity_type=AliasSchema,
96+
)
97+
return response
98+
99+
def retrieve(self) -> AliasesResponseSchema:
100+
"""
101+
Retrieve all aliases.
102+
103+
Returns:
104+
AliasesResponseSchema: The schema containing all aliases.
105+
106+
Example:
107+
>>> aliases = Aliases(async_api_call)
108+
>>> all_aliases = await aliases.retrieve()
109+
>>> for alias in all_aliases["aliases"]:
110+
... print(alias["name"])
111+
"""
112+
response: AliasesResponseSchema = self.api_call.get(
113+
Aliases.resource_path,
114+
as_json=True,
115+
entity_type=AliasesResponseSchema,
116+
)
117+
return response
118+
119+
def _endpoint_path(self, alias_name: str) -> str:
120+
"""
121+
Construct the API endpoint path for alias operations.
122+
123+
Args:
124+
alias_name (str): The name of the alias.
125+
126+
Returns:
127+
str: The constructed endpoint path.
128+
"""
129+
return "/".join([Aliases.resource_path, alias_name])

src/typesense/sync/analytics.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
"""Client for Typesense Analytics module (async)."""
2+
3+
from .analytics_events import AnalyticsEvents
4+
from .analytics_rules import AnalyticsRules
5+
from .api_call import ApiCall
6+
7+
8+
class Analytics:
9+
"""Client for v30 Analytics endpoints (async)."""
10+
11+
def __init__(self, api_call: ApiCall) -> None:
12+
self.api_call = api_call
13+
self.rules = AnalyticsRules(api_call)
14+
self.events = AnalyticsEvents(api_call)
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
"""Client for Analytics events and status operations (async)."""
2+
3+
import sys
4+
5+
if sys.version_info >= (3, 11):
6+
import typing
7+
else:
8+
import typing_extensions as typing
9+
10+
from .api_call import ApiCall
11+
from typesense.types.analytics import (
12+
AnalyticsEvent as AnalyticsEventSchema,
13+
AnalyticsEventCreateResponse,
14+
AnalyticsEventsResponse,
15+
AnalyticsStatus,
16+
)
17+
18+
19+
class AnalyticsEvents:
20+
events_path: typing.Final[str] = "/analytics/events"
21+
flush_path: typing.Final[str] = "/analytics/flush"
22+
status_path: typing.Final[str] = "/analytics/status"
23+
24+
def __init__(self, api_call: ApiCall) -> None:
25+
self.api_call = api_call
26+
27+
def create(self, event: AnalyticsEventSchema) -> AnalyticsEventCreateResponse:
28+
response: AnalyticsEventCreateResponse = self.api_call.post(
29+
AnalyticsEvents.events_path,
30+
body=event,
31+
as_json=True,
32+
entity_type=AnalyticsEventCreateResponse,
33+
)
34+
return response
35+
36+
def retrieve(
37+
self,
38+
*,
39+
user_id: str,
40+
name: str,
41+
n: int,
42+
) -> AnalyticsEventsResponse:
43+
params: typing.Dict[str, typing.Union[str, int]] = {
44+
"user_id": user_id,
45+
"name": name,
46+
"n": n,
47+
}
48+
response: AnalyticsEventsResponse = self.api_call.get(
49+
AnalyticsEvents.events_path,
50+
params=params,
51+
as_json=True,
52+
entity_type=AnalyticsEventsResponse,
53+
)
54+
return response
55+
56+
def flush(self) -> AnalyticsEventCreateResponse:
57+
response: AnalyticsEventCreateResponse = self.api_call.post(
58+
AnalyticsEvents.flush_path,
59+
body={},
60+
as_json=True,
61+
entity_type=AnalyticsEventCreateResponse,
62+
)
63+
return response
64+
65+
def status(self) -> AnalyticsStatus:
66+
response: AnalyticsStatus = self.api_call.get(
67+
AnalyticsEvents.status_path,
68+
as_json=True,
69+
entity_type=AnalyticsStatus,
70+
)
71+
return response
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
"""Per-rule client for Analytics rules operations (async)."""
2+
3+
from .api_call import ApiCall
4+
from typesense.types.analytics import AnalyticsRuleSchema
5+
6+
7+
class AnalyticsRule:
8+
def __init__(self, api_call: ApiCall, rule_name: str) -> None:
9+
self.api_call = api_call
10+
self.rule_name = rule_name
11+
12+
@property
13+
def _endpoint_path(self) -> str:
14+
from .analytics_rules import AnalyticsRules
15+
16+
return "/".join([AnalyticsRules.resource_path, self.rule_name])
17+
18+
def retrieve(self) -> AnalyticsRuleSchema:
19+
response: AnalyticsRuleSchema = self.api_call.get(
20+
self._endpoint_path,
21+
as_json=True,
22+
entity_type=AnalyticsRuleSchema,
23+
)
24+
return response
25+
26+
def delete(self) -> AnalyticsRuleSchema:
27+
response: AnalyticsRuleSchema = self.api_call.delete(
28+
self._endpoint_path,
29+
entity_type=AnalyticsRuleSchema,
30+
)
31+
return response

0 commit comments

Comments
 (0)