@@ -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 }
@@ -534,7 +540,7 @@ func (c *APIClient) ChunkedBatch(indexName string, objects []map[string]any, act
534540
535541// 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.
536542// See https://api-clients-automation.netlify.app/docs/add-new-api-client#5-helpers for implementation details.
537- func (c *APIClient) ReplaceAllObjects(indexName string, objects []map[string]any, opts ...ReplaceAllObjectsOption) (*ReplaceAllObjectsResponse, error) {
543+ func (c *APIClient) ReplaceAllObjects(ctx context.Context, indexName string, objects []map[string]any, opts ...ReplaceAllObjectsOption) (*ReplaceAllObjectsResponse, error) {
538544 tmpIndexName := fmt.Sprintf(" %s_tmp_%d" , indexName, time.Now().UnixNano())
539545
540546 conf := config{
@@ -548,49 +554,49 @@ func (c *APIClient) ReplaceAllObjects(indexName string, objects []map[string]any
548554
549555 opts = append(opts, WithWaitForTasks(true))
550556
551- copyResp, err := c.OperationIndex(indexName, OPERATION_TYPE_COPY, tmpIndexName, &conf.scopes, toRequestOptions(opts)...)
557+ copyResp, err := c.OperationIndex(ctx, indexName, OPERATION_TYPE_COPY, tmpIndexName, &conf.scopes, toRequestOptions(opts)...)
552558 if err != nil {
553559 return nil, err
554560 }
555561
556- batchResp, err := c.ChunkedBatch(tmpIndexName, objects, ACTION_ADD_OBJECT, replaceAllObjectsToChunkBactchOptions(opts)...)
562+ batchResp, err := c.ChunkedBatch(ctx, tmpIndexName, objects, ACTION_ADD_OBJECT, replaceAllObjectsToChunkBactchOptions(opts)...)
557563 if err != nil {
558- _, _ = c.DeleteIndex(tmpIndexName)
564+ _, _ = c.DeleteIndex(ctx, tmpIndexName)
559565
560566 return nil, err
561567 }
562568
563- _, err = c.WaitForTask(tmpIndexName, copyResp.TaskID, replaceAllObjectsToIterableOptions(opts)...)
569+ _, err = c.WaitForTask(ctx, tmpIndexName, copyResp.TaskID, replaceAllObjectsToIterableOptions(opts)...)
564570 if err != nil {
565- _, _ = c.DeleteIndex(tmpIndexName)
571+ _, _ = c.DeleteIndex(ctx, tmpIndexName)
566572
567573 return nil, err
568574 }
569575
570- copyResp, err = c.OperationIndex(indexName, OPERATION_TYPE_COPY, tmpIndexName, &conf.scopes, toRequestOptions(opts)...)
576+ copyResp, err = c.OperationIndex(ctx, indexName, OPERATION_TYPE_COPY, tmpIndexName, &conf.scopes, toRequestOptions(opts)...)
571577 if err != nil {
572- _, _ = c.DeleteIndex(tmpIndexName)
578+ _, _ = c.DeleteIndex(ctx, tmpIndexName)
573579
574580 return nil, err
575581 }
576582
577- _, err = c.WaitForTask(tmpIndexName, copyResp.TaskID, replaceAllObjectsToIterableOptions(opts)...)
583+ _, err = c.WaitForTask(ctx, tmpIndexName, copyResp.TaskID, replaceAllObjectsToIterableOptions(opts)...)
578584 if err != nil {
579- _, _ = c.DeleteIndex(tmpIndexName)
585+ _, _ = c.DeleteIndex(ctx, tmpIndexName)
580586
581587 return nil, err
582588 }
583589
584- moveResp, err := c.OperationIndex(tmpIndexName, OPERATION_TYPE_MOVE, indexName, nil, toRequestOptions(opts)...)
590+ moveResp, err := c.OperationIndex(ctx, tmpIndexName, OPERATION_TYPE_MOVE, indexName, nil, toRequestOptions(opts)...)
585591 if err != nil {
586- _, _ = c.DeleteIndex(tmpIndexName)
592+ _, _ = c.DeleteIndex(ctx, tmpIndexName)
587593
588594 return nil, err
589595 }
590596
591- _, err = c.WaitForTask(tmpIndexName, moveResp.TaskID, replaceAllObjectsToIterableOptions(opts)...)
597+ _, err = c.WaitForTask(ctx, tmpIndexName, moveResp.TaskID, replaceAllObjectsToIterableOptions(opts)...)
592598 if err != nil {
593- _, _ = c.DeleteIndex(tmpIndexName)
599+ _, _ = c.DeleteIndex(ctx, tmpIndexName)
594600
595601 return nil, err
596602 }
@@ -605,8 +611,8 @@ func (c *APIClient) ReplaceAllObjects(indexName string, objects []map[string]any
605611// Exists returns whether an initialized index exists or not, along with a nil
606612// error. When encountering a network error, a non-nil error is returned along
607613// with false.
608- func (c *APIClient) IndexExists(indexName string) (bool, error) {
609- _, err := c.GetSettings(indexName)
614+ func (c *APIClient) IndexExists(ctx context.Context, indexName string) (bool, error) {
615+ _, err := c.GetSettings(ctx, indexName)
610616 if err == nil {
611617 return true , nil
612618 }
0 commit comments