@@ -43,8 +43,8 @@ func CreateIterable[T any](execute func(*T, error) (*T, error), validate func(*T
4343
4444// SearchForHits calls the `search` method but with certainty that we will only request Algolia records (hits) and not facets.
4545// Disclaimer: We don't assert that the parameters you pass to this method only contains `hits` requests to prevent impacting search performances, this helper is purely for typing purposes.
46- func (c *APIClient) SearchForHits(requests []SearchQuery, strategy *SearchStrategy, opts ...RequestOption) ([]SearchResponse, error) {
47- res, err := c.Search(requests, strategy, opts...)
46+ func (c *APIClient) SearchForHits(ctx context.Context, requests []SearchQuery, strategy *SearchStrategy, opts ...RequestOption) ([]SearchResponse, error) {
47+ res, err := c.Search(ctx, requests, strategy, opts...)
4848 if err != nil {
4949 return nil, err
5050 }
@@ -62,8 +62,8 @@ func (c *APIClient) SearchForHits(requests []SearchQuery, strategy *SearchStrate
6262
6363// SearchForFacets calls the `search` method but with certainty that we will only request Algolia facets and not records (hits).
6464// Disclaimer: We don't assert that the parameters you pass to this method only contains `facets` requests to prevent impacting search performances, this helper is purely for typing purposes.
65- func (c *APIClient) SearchForFacets(requests []SearchQuery, strategy *SearchStrategy, opts ...RequestOption) ([]SearchForFacetValuesResponse, error) {
66- res, err := c.Search(requests, strategy, opts...)
65+ func (c *APIClient) SearchForFacets(ctx context.Context, requests []SearchQuery, strategy *SearchStrategy, opts ...RequestOption) ([]SearchForFacetValuesResponse, error) {
66+ res, err := c.Search(ctx, requests, strategy, opts...)
6767 if err != nil {
6868 return nil, err
6969 }
@@ -83,6 +83,7 @@ func (c *APIClient) SearchForFacets(requests []SearchQuery, strategy *SearchStra
8383// It returns the task response if the operation was successful.
8484// It returns an error if the operation failed.
8585func (c *APIClient) WaitForTask(
86+ ctx context.Context,
8687 indexName string,
8788 taskID int64,
8889 opts ...IterableOption,
@@ -94,7 +95,7 @@ func (c *APIClient) WaitForTask(
9495
9596 return CreateIterable( //nolint:wrapcheck
9697 func(*GetTaskResponse, error) (*GetTaskResponse, error) {
97- return c.GetTask(indexName, taskID, toRequestOptions(opts)...)
98+ return c.GetTask(ctx, indexName, taskID, toRequestOptions(opts)...)
9899 } ,
99100 func(response *GetTaskResponse, err error) (bool, error) {
100101 if err != nil || response == nil {
@@ -111,6 +112,7 @@ func (c *APIClient) WaitForTask(
111112// It returns the task response if the operation was successful.
112113// It returns an error if the operation failed.
113114func (c *APIClient) WaitForAppTask(
115+ ctx context.Context,
114116 taskID int64,
115117 opts ...IterableOption,
116118) (*GetTaskResponse, error) {
@@ -121,7 +123,7 @@ func (c *APIClient) WaitForAppTask(
121123
122124 return CreateIterable( //nolint:wrapcheck
123125 func(*GetTaskResponse, error) (*GetTaskResponse, error) {
124- return c.GetAppTask(taskID, toRequestOptions(opts)...)
126+ return c.GetAppTask(ctx, taskID, toRequestOptions(opts)...)
125127 } ,
126128 func(response *GetTaskResponse, err error) (bool, error) {
127129 if err != nil || response == nil {
@@ -164,6 +166,7 @@ func slicesEqualUnordered[T cmp.Ordered](a []T, b []T) bool {
164166// If the operation is "update", the apiKey parameter must be set.
165167// If the operation is "delete" or "add", the apiKey parameter is not used.
166168func (c *APIClient) WaitForApiKey(
169+ ctx context.Context,
167170 key string,
168171 operation ApiKeyOperation,
169172 opts ...WaitForApiKeyOption,
@@ -247,7 +250,7 @@ func (c *APIClient) WaitForApiKey(
247250
248251 return CreateIterable( //nolint:wrapcheck
249252 func(*GetApiKeyResponse, error) (*GetApiKeyResponse, error) {
250- return c.GetApiKey(key, toRequestOptions(opts)...)
253+ return c.GetApiKey(ctx, key, toRequestOptions(opts)...)
251254 } ,
252255 validateFunc,
253256 waitForApiKeyToIterableOptions(opts)...,
@@ -257,6 +260,7 @@ func (c *APIClient) WaitForApiKey(
257260// BrowseObjects allows to aggregate all the hits returned by the API calls.
258261// Use the `WithAggregator` option to collect all the responses.
259262func (c *APIClient) BrowseObjects(
263+ ctx context.Context,
260264 indexName string,
261265 browseParams BrowseParamsObject,
262266 opts ...IterableOption,
@@ -272,7 +276,7 @@ func (c *APIClient) BrowseObjects(
272276 }
273277
274278 return c.Browse(
275- indexName, BrowseParamsObjectAsBrowseParams(&browseParams),
279+ ctx, indexName, BrowseParamsObjectAsBrowseParams(&browseParams),
276280 toRequestOptions(opts)...,
277281 )
278282 },
@@ -288,6 +292,7 @@ func (c *APIClient) BrowseObjects(
288292// BrowseRules allows to aggregate all the rules returned by the API calls.
289293// Use the `WithAggregator` option to collect all the responses.
290294func (c *APIClient) BrowseRules(
295+ ctx context.Context,
291296 indexName string,
292297 optionalParams *SearchRulesOptions,
293298 opts ...IterableOption,
@@ -312,7 +317,7 @@ func (c *APIClient) BrowseRules(
312317 optionalParams.Page = utils.ToPtr(int32(0))
313318 }
314319
315- return c.SearchRules(indexName, optionalParams, toRequestOptions(opts)...)
320+ return c.SearchRules(ctx, indexName, optionalParams, toRequestOptions(opts)...)
316321 },
317322 func(response *SearchRulesResponse, err error) (bool, error) {
318323 return err != nil || (response != nil && len(response.Hits) < int(hitsPerPage)), err
@@ -326,6 +331,7 @@ func (c *APIClient) BrowseRules(
326331// BrowseSynonyms allows to aggregate all the synonyms returned by the API calls.
327332// Use the `WithAggregator` option to collect all the responses.
328333func (c *APIClient) BrowseSynonyms(
334+ ctx context.Context,
329335 indexName string,
330336 optionalParams *SearchSynonymsOptions,
331337 opts ...IterableOption,
@@ -350,7 +356,7 @@ func (c *APIClient) BrowseSynonyms(
350356 optionalParams.Page = utils.ToPtr(*optionalParams.Page + 1)
351357 } ()
352358
353- return c.SearchSynonyms(indexName, optionalParams, toRequestOptions(opts)...)
359+ return c.SearchSynonyms(ctx, indexName, optionalParams, toRequestOptions(opts)...)
354360 },
355361 func(response *SearchSynonymsResponse, err error) (bool, error) {
356362 return err != nil || (response != nil && len(response.Hits) < int(hitsPerPage)), err
@@ -454,23 +460,23 @@ func (c *APIClient) GetSecuredApiKeyRemainingValidity(securedApiKey string) (tim
454460}
455461
456462// Helper: Saves the given array of objects in the given index. The `chunkedBatch` helper is used under the hood, which creates a `batch` requests with at most 1000 objects in it.
457- func (c *APIClient) SaveObjects(indexName string, objects []map[string]any, opts ...ChunkedBatchOption) ([]BatchResponse, error) {
458- return c.ChunkedBatch(indexName, objects, ACTION_ADD_OBJECT, opts...)
463+ func (c *APIClient) SaveObjects(ctx context.Context, indexName string, objects []map[string]any, opts ...ChunkedBatchOption) ([]BatchResponse, error) {
464+ return c.ChunkedBatch(ctx, indexName, objects, ACTION_ADD_OBJECT, opts...)
459465}
460466
461467// Helper: Deletes every records for the given objectIDs. The `chunkedBatch` helper is used under the hood, which creates a `batch` requests with at most 1000 objectIDs in it.
462- func (c *APIClient) DeleteObjects(indexName string, objectIDs []string, opts ...ChunkedBatchOption) ([]BatchResponse, error) {
468+ func (c *APIClient) DeleteObjects(ctx context.Context, indexName string, objectIDs []string, opts ...ChunkedBatchOption) ([]BatchResponse, error) {
463469 objects := make([]map[string]any, 0, len(objectIDs))
464470
465471 for _, id := range objectIDs {
466472 objects = append(objects, map[string]any{" objectID" :id} )
467473 }
468474
469- return c.ChunkedBatch(indexName, objects, ACTION_DELETE_OBJECT, opts...)
475+ return c.ChunkedBatch(ctx, indexName, objects, ACTION_DELETE_OBJECT, opts...)
470476}
471477
472478// Helper: Replaces object content of all the given objects according to their respective `objectID` field. The `chunkedBatch` helper is used under the hood, which creates a `batch` requests with at most 1000 objects in it.
473- func (c *APIClient) PartialUpdateObjects(indexName string, objects []map[string]any, opts ...PartialUpdateObjectsOption) ([]BatchResponse, error) {
479+ func (c *APIClient) PartialUpdateObjects(ctx context.Context, indexName string, objects []map[string]any, opts ...PartialUpdateObjectsOption) ([]BatchResponse, error) {
474480 conf := config{
475481 headerParams: map[string]string{} ,
476482 createIfNotExists: true,
@@ -488,11 +494,11 @@ func (c *APIClient) PartialUpdateObjects(indexName string, objects []map[string]
488494 action = ACTION_PARTIAL_UPDATE_OBJECT_NO_CREATE
489495 }
490496
491- return c.ChunkedBatch(indexName, objects, action, partialUpdateObjectsToChunkedBatchOptions(opts)...)
497+ return c.ChunkedBatch(ctx, indexName, objects, action, partialUpdateObjectsToChunkedBatchOptions(opts)...)
492498}
493499
494500// ChunkedBatch chunks the given `objects` list in subset of 1000 elements max in order to make it fit in `batch` requests.
495- func (c *APIClient) ChunkedBatch(indexName string, objects []map[string]any, action Action, opts ...ChunkedBatchOption) ([]BatchResponse, error) {
501+ func (c *APIClient) ChunkedBatch(ctx context.Context, indexName string, objects []map[string]any, action Action, opts ...ChunkedBatchOption) ([]BatchResponse, error) {
496502 conf := config{
497503 headerParams: map[string]string{} ,
498504 waitForTasks: false,
@@ -510,7 +516,7 @@ func (c *APIClient) ChunkedBatch(indexName string, objects []map[string]any, act
510516 requests = append(requests, *NewBatchRequest(action, obj))
511517
512518 if len(requests) == conf.batchSize || i == len(objects)-1 {
513- resp, err := c.Batch(indexName, requests, toRequestOptions(opts)...)
519+ resp, err := c.Batch(ctx, indexName, requests, toRequestOptions(opts)...)
514520 if err != nil {
515521 return nil, err
516522 }
@@ -522,7 +528,7 @@ func (c *APIClient) ChunkedBatch(indexName string, objects []map[string]any, act
522528
523529 if conf.waitForTasks {
524530 for _, resp := range responses {
525- _, err := c.WaitForTask(indexName, resp.TaskID, toIterableOptions(opts)...)
531+ _, err := c.WaitForTask(ctx, indexName, resp.TaskID, toIterableOptions(opts)...)
526532 if err != nil {
527533 return nil, err
528534 }
@@ -620,7 +626,7 @@ func (c *APIClient) ReplaceAllObjectsWithTransformation(indexName string, object
620626
621627// ReplaceAllObjects replaces all objects (records) in the given `indexName` with the given `objects`. A temporary index is created during this process in order to backup your data.
622628// See https://api-clients-automation.netlify.app/docs/add-new-api-client#5-helpers for implementation details.
623- func (c *APIClient) ReplaceAllObjects(indexName string, objects []map[string]any, opts ...ReplaceAllObjectsOption) (*ReplaceAllObjectsResponse, error) {
629+ func (c *APIClient) ReplaceAllObjects(ctx context.Context, indexName string, objects []map[string]any, opts ...ReplaceAllObjectsOption) (*ReplaceAllObjectsResponse, error) {
624630 tmpIndexName := fmt.Sprintf(" %s_tmp_%d" , indexName, time.Now().UnixNano())
625631
626632 conf := config{
@@ -634,49 +640,49 @@ func (c *APIClient) ReplaceAllObjects(indexName string, objects []map[string]any
634640
635641 opts = append(opts, WithWaitForTasks(true))
636642
637- copyResp, err := c.OperationIndex(indexName, OPERATION_TYPE_COPY, tmpIndexName, &conf.scopes, toRequestOptions(opts)...)
643+ copyResp, err := c.OperationIndex(ctx, indexName, OPERATION_TYPE_COPY, tmpIndexName, &conf.scopes, toRequestOptions(opts)...)
638644 if err != nil {
639645 return nil, err
640646 }
641647
642- batchResp, err := c.ChunkedBatch(tmpIndexName, objects, ACTION_ADD_OBJECT, replaceAllObjectsToChunkBatchOptions (opts)...)
648+ batchResp, err := c.ChunkedBatch(ctx, tmpIndexName, objects, ACTION_ADD_OBJECT, replaceAllObjectsToChunkBactchOptions (opts)...)
643649 if err != nil {
644- _, _ = c.DeleteIndex(tmpIndexName)
650+ _, _ = c.DeleteIndex(ctx, tmpIndexName)
645651
646652 return nil, err
647653 }
648654
649- _, err = c.WaitForTask(tmpIndexName, copyResp.TaskID, replaceAllObjectsToIterableOptions(opts)...)
655+ _, err = c.WaitForTask(ctx, tmpIndexName, copyResp.TaskID, replaceAllObjectsToIterableOptions(opts)...)
650656 if err != nil {
651- _, _ = c.DeleteIndex(tmpIndexName)
657+ _, _ = c.DeleteIndex(ctx, tmpIndexName)
652658
653659 return nil, err
654660 }
655661
656- copyResp, err = c.OperationIndex(indexName, OPERATION_TYPE_COPY, tmpIndexName, &conf.scopes, toRequestOptions(opts)...)
662+ copyResp, err = c.OperationIndex(ctx, indexName, OPERATION_TYPE_COPY, tmpIndexName, &conf.scopes, toRequestOptions(opts)...)
657663 if err != nil {
658- _, _ = c.DeleteIndex(tmpIndexName)
664+ _, _ = c.DeleteIndex(ctx, tmpIndexName)
659665
660666 return nil, err
661667 }
662668
663- _, err = c.WaitForTask(tmpIndexName, copyResp.TaskID, replaceAllObjectsToIterableOptions(opts)...)
669+ _, err = c.WaitForTask(ctx, tmpIndexName, copyResp.TaskID, replaceAllObjectsToIterableOptions(opts)...)
664670 if err != nil {
665- _, _ = c.DeleteIndex(tmpIndexName)
671+ _, _ = c.DeleteIndex(ctx, tmpIndexName)
666672
667673 return nil, err
668674 }
669675
670- moveResp, err := c.OperationIndex(tmpIndexName, OPERATION_TYPE_MOVE, indexName, nil, toRequestOptions(opts)...)
676+ moveResp, err := c.OperationIndex(ctx, tmpIndexName, OPERATION_TYPE_MOVE, indexName, nil, toRequestOptions(opts)...)
671677 if err != nil {
672- _, _ = c.DeleteIndex(tmpIndexName)
678+ _, _ = c.DeleteIndex(ctx, tmpIndexName)
673679
674680 return nil, err
675681 }
676682
677- _, err = c.WaitForTask(tmpIndexName, moveResp.TaskID, replaceAllObjectsToIterableOptions(opts)...)
683+ _, err = c.WaitForTask(ctx, tmpIndexName, moveResp.TaskID, replaceAllObjectsToIterableOptions(opts)...)
678684 if err != nil {
679- _, _ = c.DeleteIndex(tmpIndexName)
685+ _, _ = c.DeleteIndex(ctx, tmpIndexName)
680686
681687 return nil, err
682688 }
@@ -691,8 +697,8 @@ func (c *APIClient) ReplaceAllObjects(indexName string, objects []map[string]any
691697// Exists returns whether an initialized index exists or not, along with a nil
692698// error. When encountering a network error, a non-nil error is returned along
693699// with false.
694- func (c *APIClient) IndexExists(indexName string) (bool, error) {
695- _, err := c.GetSettings(indexName)
700+ func (c *APIClient) IndexExists(ctx context.Context, indexName string) (bool, error) {
701+ _, err := c.GetSettings(ctx, indexName)
696702 if err == nil {
697703 return true , nil
698704 }
0 commit comments