Skip to content

Commit b2c8c18

Browse files
committed
refactor(operations): format operations class based on linting rules
1 parent 35da218 commit b2c8c18

1 file changed

Lines changed: 156 additions & 21 deletions

File tree

src/typesense/operations.py

Lines changed: 156 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,21 @@
1+
"""
2+
This module provides functionality for performing various operations in the Typesense API.
3+
4+
It contains the Operations class, which handles different API operations such as
5+
health checks, snapshots, and configuration changes.
6+
7+
Classes:
8+
Operations: Manages various operations in the Typesense API.
9+
10+
Dependencies:
11+
- typesense.types.operations:
12+
Provides type definitions for operation responses and parameters.
13+
- typesense.api_call: Provides the ApiCall class for making API requests.
14+
- typesense.configuration: Provides the Configuration class.
15+
16+
Note: This module uses conditional imports to support both Python 3.11+ and earlier versions.
17+
"""
18+
119
import sys
220

321
from typesense.types.operations import (
@@ -12,67 +30,149 @@
1230
else:
1331
import typing_extensions as typing
1432

15-
from typesense import configuration
1633
from typesense.api_call import ApiCall
17-
from typesense.configuration import Configuration
1834

1935

20-
class Operations(object):
21-
RESOURCE_PATH = "/operations"
22-
HEALTH_PATH = "/health"
23-
CONFIG_PATH = "/config"
36+
class Operations:
37+
"""
38+
Manages various operations in the Typesense API.
39+
40+
This class provides methods to perform different operations such as
41+
health checks, snapshots, and configuration changes.
42+
43+
Attributes:
44+
resource_path (str): The base path for operations endpoints.
45+
healht_path (str): The path for the health check endpoint.
46+
config_path (str): The path for the configuration endpoint.
47+
api_call (ApiCall): The ApiCall instance for making API requests.
48+
"""
49+
50+
resource_path: typing.Final[str] = "/operations"
51+
healht_path: typing.Final[str] = "/health"
52+
config_path: typing.Final[str] = "/config"
2453

2554
def __init__(self, api_call: ApiCall):
26-
self.api_call = api_call
55+
"""
56+
Initialize the Operations instance.
2757
28-
@staticmethod
29-
def _endpoint_path(operation_name: str) -> str:
30-
return "{0}/{1}".format(Operations.RESOURCE_PATH, operation_name)
58+
Args:
59+
api_call (ApiCall): The ApiCall instance for making API requests.
60+
"""
61+
self.api_call = api_call
3162

3263
@typing.overload
3364
def perform(
3465
self,
3566
operation_name: typing.Literal["vote"],
3667
query_params: None = None,
37-
) -> OperationResponse: ...
68+
) -> OperationResponse:
69+
"""
70+
Perform a vote operation.
71+
72+
Args:
73+
operation_name (Literal["vote"]): The name of the operation.
74+
query_params (None, optional): Query parameters (not used for vote operation).
75+
76+
Returns:
77+
OperationResponse: The response from the vote operation.
78+
"""
3879

3980
@typing.overload
4081
def perform(
4182
self,
4283
operation_name: typing.Literal["db/compact"],
4384
query_params: None = None,
44-
) -> OperationResponse: ...
85+
) -> OperationResponse:
86+
"""
87+
Perform a database compaction operation.
88+
89+
Args:
90+
operation_name (Literal["db/compact"]): The name of the operation.
91+
query_params (None, optional): Query parameters (not used for db/compact operation).
92+
93+
Returns:
94+
OperationResponse: The response from the database compaction operation.
95+
"""
4596

4697
@typing.overload
4798
def perform(
4899
self,
49100
operation_name: typing.Literal["cache/clear"],
50101
query_params: None = None,
51-
) -> OperationResponse: ...
102+
) -> OperationResponse:
103+
"""
104+
Perform a cache clear operation.
105+
106+
Args:
107+
operation_name (Literal["cache/clear"]): The name of the operation.
108+
query_params (None, optional):
109+
Query parameters (not used for cache/clear operation).
110+
111+
Returns:
112+
OperationResponse: The response from the cache clear operation.
113+
"""
52114

53115
@typing.overload
54116
def perform(
55117
self,
56118
operation_name: str,
57119
query_params: typing.Union[typing.Dict[str, str], None] = None,
58-
) -> OperationResponse: ...
120+
) -> OperationResponse:
121+
"""
122+
Perform a generic operation.
123+
124+
Args:
125+
operation_name (str): The name of the operation.
126+
query_params (Union[Dict[str, str], None], optional):
127+
Query parameters for the operation.
128+
129+
Returns:
130+
OperationResponse: The response from the operation.
131+
"""
59132

60133
@typing.overload
61134
def perform(
62135
self,
63136
operation_name: typing.Literal["snapshot"],
64137
query_params: SnapshotParameters,
65-
) -> OperationResponse: ...
138+
) -> OperationResponse:
139+
"""
140+
Perform a snapshot operation.
141+
142+
Args:
143+
operation_name (Literal["snapshot"]): The name of the operation.
144+
query_params (SnapshotParameters): Query parameters for the snapshot operation.
145+
146+
Returns:
147+
OperationResponse: The response from the snapshot operation.
148+
"""
66149

67150
def perform(
68151
self,
69152
operation_name: typing.Union[
70-
typing.Literal["snapshot, vote, db/compact, cache/clear"], str
153+
typing.Literal["snapshot, vote, db/compact, cache/clear"],
154+
str,
71155
],
72156
query_params: typing.Union[
73-
SnapshotParameters, typing.Dict[str, str], None
157+
SnapshotParameters,
158+
typing.Dict[str, str],
159+
None,
74160
] = None,
75161
) -> OperationResponse:
162+
"""
163+
Perform an operation on the Typesense API.
164+
165+
This method is the actual implementation for all the overloaded perform methods.
166+
167+
Args:
168+
operation_name (Literal["snapshot, vote, db/compact, cache/clear"]):
169+
The name of the operation to perform.
170+
query_params (Union[SnapshotParameters, None], optional):
171+
Query parameters for the operation.
172+
173+
Returns:
174+
OperationResponse: The response from the performed operation.
175+
"""
76176
response: OperationResponse = self.api_call.post(
77177
self._endpoint_path(operation_name),
78178
params=query_params,
@@ -82,8 +182,16 @@ def perform(
82182
return response
83183

84184
def is_healthy(self) -> bool:
185+
"""
186+
Check if the Typesense server is healthy.
187+
188+
Returns:
189+
bool: True if the server is healthy, False otherwise.
190+
"""
85191
call_resp = self.api_call.get(
86-
Operations.HEALTH_PATH, as_json=True, entity_type=HealthCheckResponse
192+
Operations.healht_path,
193+
as_json=True,
194+
entity_type=HealthCheckResponse,
87195
)
88196
if isinstance(call_resp, typing.Dict):
89197
is_ok: bool = call_resp.get("ok", False)
@@ -92,13 +200,40 @@ def is_healthy(self) -> bool:
92200
return is_ok
93201

94202
def toggle_slow_request_log(
95-
self, data: LogSlowRequestsTimeParams
203+
self,
204+
log_slow_requests_time_params: LogSlowRequestsTimeParams,
96205
) -> typing.Dict[str, typing.Union[str, bool]]:
97-
data_dashed = {key.replace("_", "-"): value for key, value in data.items()}
206+
"""
207+
Toggle the slow request log configuration.
208+
209+
Args:
210+
log_slow_requests_time_params (LogSlowRequestsTimeParams):
211+
Parameters for configuring slow request logging.
212+
213+
Returns:
214+
Dict[str, Union[str, bool]]: The response from the configuration change operation.
215+
"""
216+
data_dashed = {
217+
key.replace("_", "-"): dashed_value
218+
for key, dashed_value in log_slow_requests_time_params.items()
219+
}
98220
response: typing.Dict[str, typing.Union[str, bool]] = self.api_call.post(
99-
Operations.CONFIG_PATH,
221+
Operations.config_path,
100222
as_json=True,
101223
entity_type=typing.Dict[str, typing.Union[str, bool]],
102224
body=data_dashed,
103225
)
104226
return response
227+
228+
@staticmethod
229+
def _endpoint_path(operation_name: str) -> str:
230+
"""
231+
Generate the endpoint path for a given operation.
232+
233+
Args:
234+
operation_name (str): The name of the operation.
235+
236+
Returns:
237+
str: The full endpoint path for the operation.
238+
"""
239+
return "/".join([Operations.resource_path, operation_name])

0 commit comments

Comments
 (0)