Skip to content

Commit 34a78f3

Browse files
committed
feat(python): add bridge to transformation on search
1 parent 736998a commit 34a78f3

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
lines changed

templates/python/api.mustache

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ from algoliasearch.search.models.operation_type import OperationType
99
from algoliasearch.search.models.replace_all_objects_response import ReplaceAllObjectsResponse
1010
from algoliasearch.search.models.scope_type import ScopeType
1111
from algoliasearch.search.models.secured_api_key_restrictions import SecuredApiKeyRestrictions
12+
13+
from algoliasearch.ingestion.models.watch_response import WatchResponse
14+
from algoliasearch.ingestion.config import IngestionConfig
15+
from algoliasearch.ingestion.client import (IngestionClient, IngestionClientSync)
1216
{{/isSearchClient}}
1317

1418
{{#operations}}{{#operation}}{{#imports}}
@@ -37,6 +41,9 @@ class {{classname}}{{#isSyncClient}}Sync{{/isSyncClient}}:
3741
"""
3842

3943
_transporter: Transporter{{#isSyncClient}}Sync{{/isSyncClient}}
44+
{{#isSearchClient}}
45+
_ingestion_transporter: Optional[IngestionClient{{#isSyncClient}}Sync{{/isSyncClient}}]
46+
{{/isSearchClient}}
4047
_config: BaseConfig
4148
_request_options: RequestOptions
4249

@@ -49,6 +56,9 @@ class {{classname}}{{#isSyncClient}}Sync{{/isSyncClient}}:
4956
self._config = config
5057
self._request_options = RequestOptions(config)
5158

59+
if config.region is not None:
60+
self._ingestion_transporter = IngestionClient{{#isSyncClient}}Sync{{/isSyncClient}}.create_with_config(IngestionConfig(config.app_id, config.api_key, config.region))
61+
5262
if transporter is None:
5363
transporter = Transporter{{#isSyncClient}}Sync{{/isSyncClient}}(config)
5464
self._transporter = transporter

templates/python/config.mustache

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,9 @@ class {{#lambda.pascalcase}}{{client}}{{/lambda.pascalcase}}Config(BaseConfig):
7777
{{/hostWithAppID}}
7878
)
7979
{{/hasRegionalHost}}
80+
81+
{{#isSearchClient}}
82+
def with_transformation(self, region: str = ""):
83+
"This method is required to be called with the appropriate region of your Algolia application if you wish to leverage the *_with_transformation methods."
84+
self.region = region
85+
{{/isSearchClient}}

templates/python/search_helpers.mustache

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,31 @@
292292
"""
293293
return {{^isSyncClient}}await {{/isSyncClient}}self.chunked_batch(index_name=index_name, objects=objects, action=Action.ADDOBJECT, wait_for_tasks=wait_for_tasks, batch_size=batch_size, request_options=request_options)
294294
295+
{{^isSyncClient}}async {{/isSyncClient}}def save_objects_with_transformation(
296+
self,
297+
index_name: str,
298+
objects: List[Dict[str, Any]],
299+
wait_for_tasks: bool = False,
300+
batch_size: int = 1000,
301+
request_options: Optional[Union[dict, RequestOptions]] = None,
302+
) -> WatchResponse:
303+
"""
304+
Helper: Similar to the `save_objects` method but requires a Push connector (https://www.algolia.com/doc/guides/sending-and-managing-data/send-and-update-your-data/connectors/push/) to be created first, in order to transform records before indexing them to Algolia. The `region` must've been passed to the client's config at instantiation.
305+
"""
306+
if self._ingestion_transporter is None:
307+
raise ValueError("`region` must be provided at client instantiation before calling this method.")
308+
309+
return {{^isSyncClient}}await {{/isSyncClient}}self._ingestion_transporter.push(
310+
index_name=index_name,
311+
push_task_payload={
312+
"action": Action.ADDOBJECT,
313+
"records": objects,
314+
},
315+
watch=wait_for_tasks,
316+
request_options=request_options,
317+
)
318+
319+
295320
{{^isSyncClient}}async {{/isSyncClient}}def delete_objects(
296321
self,
297322
index_name: str,
@@ -319,6 +344,32 @@
319344
"""
320345
return {{^isSyncClient}}await {{/isSyncClient}}self.chunked_batch(index_name=index_name, objects=objects, action=Action.PARTIALUPDATEOBJECT if create_if_not_exists else Action.PARTIALUPDATEOBJECTNOCREATE, wait_for_tasks=wait_for_tasks, batch_size=batch_size, request_options=request_options)
321346
347+
{{^isSyncClient}}async {{/isSyncClient}}def partial_update_objects_with_transformation(
348+
self,
349+
index_name: str,
350+
objects: List[Dict[str, Any]],
351+
create_if_not_exists: bool = False,
352+
wait_for_tasks: bool = False,
353+
batch_size: int = 1000,
354+
request_options: Optional[Union[dict, RequestOptions]] = None,
355+
) -> WatchResponse:
356+
"""
357+
Helper: Similar to the `partial_update_objects` method but requires a Push connector (https://www.algolia.com/doc/guides/sending-and-managing-data/send-and-update-your-data/connectors/push/) to be created first, in order to transform records before indexing them to Algolia. The `region` must've been passed to the client instantiation method.
358+
"""
359+
if self._ingestion_transporter is None:
360+
raise ValueError("`region` must be provided at client instantiation before calling this method.")
361+
362+
return {{^isSyncClient}}await {{/isSyncClient}}self._ingestion_transporter.push(
363+
index_name=index_name,
364+
push_task_payload={
365+
"action": Action.PARTIALUPDATEOBJECT if create_if_not_exists else Action.PARTIALUPDATEOBJECTNOCREATE,
366+
"records": objects,
367+
},
368+
watch=wait_for_tasks,
369+
request_options=request_options,
370+
)
371+
372+
322373
{{^isSyncClient}}async {{/isSyncClient}}def chunked_batch(
323374
self,
324375
index_name: str,

0 commit comments

Comments
 (0)