@@ -43,8 +43,8 @@ func CreateIterable[T any](execute func(*T, error) (*T, error), validate func(*T
43
43
44
44
// SearchForHits calls the `search` method but with certainty that we will only request Algolia records (hits) and not facets.
45
45
// 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...)
48
48
if err != nil {
49
49
return nil, err
50
50
}
@@ -62,8 +62,8 @@ func (c *APIClient) SearchForHits(requests []SearchQuery, strategy *SearchStrate
62
62
63
63
// SearchForFacets calls the `search` method but with certainty that we will only request Algolia facets and not records (hits).
64
64
// 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...)
67
67
if err != nil {
68
68
return nil, err
69
69
}
@@ -83,6 +83,7 @@ func (c *APIClient) SearchForFacets(requests []SearchQuery, strategy *SearchStra
83
83
// It returns the task response if the operation was successful.
84
84
// It returns an error if the operation failed.
85
85
func (c *APIClient) WaitForTask(
86
+ ctx context.Context,
86
87
indexName string,
87
88
taskID int64,
88
89
opts ...IterableOption,
@@ -94,7 +95,7 @@ func (c *APIClient) WaitForTask(
94
95
95
96
return CreateIterable( //nolint:wrapcheck
96
97
func(*GetTaskResponse, error) (*GetTaskResponse, error) {
97
- return c.GetTask(indexName, taskID, toRequestOptions(opts)...)
98
+ return c.GetTask(ctx, indexName, taskID, toRequestOptions(opts)...)
98
99
} ,
99
100
func(response *GetTaskResponse, err error) (bool, error) {
100
101
if err != nil || response == nil {
@@ -111,6 +112,7 @@ func (c *APIClient) WaitForTask(
111
112
// It returns the task response if the operation was successful.
112
113
// It returns an error if the operation failed.
113
114
func (c *APIClient) WaitForAppTask(
115
+ ctx context.Context,
114
116
taskID int64,
115
117
opts ...IterableOption,
116
118
) (*GetTaskResponse, error) {
@@ -121,7 +123,7 @@ func (c *APIClient) WaitForAppTask(
121
123
122
124
return CreateIterable( //nolint:wrapcheck
123
125
func(*GetTaskResponse, error) (*GetTaskResponse, error) {
124
- return c.GetAppTask(taskID, toRequestOptions(opts)...)
126
+ return c.GetAppTask(ctx, taskID, toRequestOptions(opts)...)
125
127
} ,
126
128
func(response *GetTaskResponse, err error) (bool, error) {
127
129
if err != nil || response == nil {
@@ -164,6 +166,7 @@ func slicesEqualUnordered[T cmp.Ordered](a []T, b []T) bool {
164
166
// If the operation is "update", the apiKey parameter must be set.
165
167
// If the operation is "delete" or "add", the apiKey parameter is not used.
166
168
func (c *APIClient) WaitForApiKey(
169
+ ctx context.Context,
167
170
key string,
168
171
operation ApiKeyOperation,
169
172
opts ...WaitForApiKeyOption,
@@ -247,7 +250,7 @@ func (c *APIClient) WaitForApiKey(
247
250
248
251
return CreateIterable( //nolint:wrapcheck
249
252
func(*GetApiKeyResponse, error) (*GetApiKeyResponse, error) {
250
- return c.GetApiKey(key, toRequestOptions(opts)...)
253
+ return c.GetApiKey(ctx, key, toRequestOptions(opts)...)
251
254
} ,
252
255
validateFunc,
253
256
waitForApiKeyToIterableOptions(opts)...,
@@ -257,6 +260,7 @@ func (c *APIClient) WaitForApiKey(
257
260
// BrowseObjects allows to aggregate all the hits returned by the API calls.
258
261
// Use the `WithAggregator` option to collect all the responses.
259
262
func (c *APIClient) BrowseObjects(
263
+ ctx context.Context,
260
264
indexName string,
261
265
browseParams BrowseParamsObject,
262
266
opts ...IterableOption,
@@ -272,7 +276,7 @@ func (c *APIClient) BrowseObjects(
272
276
}
273
277
274
278
return c.Browse(
275
- indexName, BrowseParamsObjectAsBrowseParams(&browseParams),
279
+ ctx, indexName, BrowseParamsObjectAsBrowseParams(&browseParams),
276
280
toRequestOptions(opts)...,
277
281
)
278
282
},
@@ -288,6 +292,7 @@ func (c *APIClient) BrowseObjects(
288
292
// BrowseRules allows to aggregate all the rules returned by the API calls.
289
293
// Use the `WithAggregator` option to collect all the responses.
290
294
func (c *APIClient) BrowseRules(
295
+ ctx context.Context,
291
296
indexName string,
292
297
optionalParams *SearchRulesOptions,
293
298
opts ...IterableOption,
@@ -312,7 +317,7 @@ func (c *APIClient) BrowseRules(
312
317
optionalParams.Page = utils.ToPtr(int32(0))
313
318
}
314
319
315
- return c.SearchRules(indexName, optionalParams, toRequestOptions(opts)...)
320
+ return c.SearchRules(ctx, indexName, optionalParams, toRequestOptions(opts)...)
316
321
},
317
322
func(response *SearchRulesResponse, err error) (bool, error) {
318
323
return err != nil || (response != nil && len(response.Hits) < int(hitsPerPage)), err
@@ -326,6 +331,7 @@ func (c *APIClient) BrowseRules(
326
331
// BrowseSynonyms allows to aggregate all the synonyms returned by the API calls.
327
332
// Use the `WithAggregator` option to collect all the responses.
328
333
func (c *APIClient) BrowseSynonyms(
334
+ ctx context.Context,
329
335
indexName string,
330
336
optionalParams *SearchSynonymsOptions,
331
337
opts ...IterableOption,
@@ -350,7 +356,7 @@ func (c *APIClient) BrowseSynonyms(
350
356
optionalParams.Page = utils.ToPtr(*optionalParams.Page + 1)
351
357
} ()
352
358
353
- return c.SearchSynonyms(indexName, optionalParams, toRequestOptions(opts)...)
359
+ return c.SearchSynonyms(ctx, indexName, optionalParams, toRequestOptions(opts)...)
354
360
},
355
361
func(response *SearchSynonymsResponse, err error) (bool, error) {
356
362
return err != nil || (response != nil && len(response.Hits) < int(hitsPerPage)), err
@@ -454,23 +460,23 @@ func (c *APIClient) GetSecuredApiKeyRemainingValidity(securedApiKey string) (tim
454
460
}
455
461
456
462
// 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...)
459
465
}
460
466
461
467
// 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) {
463
469
objects := make([]map[string]any, 0, len(objectIDs))
464
470
465
471
for _, id := range objectIDs {
466
472
objects = append(objects, map[string]any{" objectID" :id} )
467
473
}
468
474
469
- return c.ChunkedBatch(indexName, objects, ACTION_DELETE_OBJECT, opts...)
475
+ return c.ChunkedBatch(ctx, indexName, objects, ACTION_DELETE_OBJECT, opts...)
470
476
}
471
477
472
478
// 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) {
474
480
conf := config{
475
481
headerParams: map[string]string{} ,
476
482
createIfNotExists: true,
@@ -488,11 +494,11 @@ func (c *APIClient) PartialUpdateObjects(indexName string, objects []map[string]
488
494
action = ACTION_PARTIAL_UPDATE_OBJECT_NO_CREATE
489
495
}
490
496
491
- return c.ChunkedBatch(indexName, objects, action, partialUpdateObjectsToChunkedBatchOptions(opts)...)
497
+ return c.ChunkedBatch(ctx, indexName, objects, action, partialUpdateObjectsToChunkedBatchOptions(opts)...)
492
498
}
493
499
494
500
// 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) {
496
502
conf := config{
497
503
headerParams: map[string]string{} ,
498
504
waitForTasks: false,
@@ -510,7 +516,7 @@ func (c *APIClient) ChunkedBatch(indexName string, objects []map[string]any, act
510
516
requests = append(requests, *NewBatchRequest(action, obj))
511
517
512
518
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)...)
514
520
if err != nil {
515
521
return nil, err
516
522
}
@@ -522,7 +528,7 @@ func (c *APIClient) ChunkedBatch(indexName string, objects []map[string]any, act
522
528
523
529
if conf.waitForTasks {
524
530
for _, resp := range responses {
525
- _, err := c.WaitForTask(indexName, resp.TaskID, toIterableOptions(opts)...)
531
+ _, err := c.WaitForTask(ctx, indexName, resp.TaskID, toIterableOptions(opts)...)
526
532
if err != nil {
527
533
return nil, err
528
534
}
@@ -534,7 +540,7 @@ func (c *APIClient) ChunkedBatch(indexName string, objects []map[string]any, act
534
540
535
541
// 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.
536
542
// 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) {
538
544
tmpIndexName := fmt.Sprintf(" %s_tmp_%d" , indexName, time.Now().UnixNano())
539
545
540
546
conf := config{
@@ -548,49 +554,49 @@ func (c *APIClient) ReplaceAllObjects(indexName string, objects []map[string]any
548
554
549
555
opts = append(opts, WithWaitForTasks(true))
550
556
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)...)
552
558
if err != nil {
553
559
return nil, err
554
560
}
555
561
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)...)
557
563
if err != nil {
558
- _, _ = c.DeleteIndex(tmpIndexName)
564
+ _, _ = c.DeleteIndex(ctx, tmpIndexName)
559
565
560
566
return nil, err
561
567
}
562
568
563
- _, err = c.WaitForTask(tmpIndexName, copyResp.TaskID, replaceAllObjectsToIterableOptions(opts)...)
569
+ _, err = c.WaitForTask(ctx, tmpIndexName, copyResp.TaskID, replaceAllObjectsToIterableOptions(opts)...)
564
570
if err != nil {
565
- _, _ = c.DeleteIndex(tmpIndexName)
571
+ _, _ = c.DeleteIndex(ctx, tmpIndexName)
566
572
567
573
return nil, err
568
574
}
569
575
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)...)
571
577
if err != nil {
572
- _, _ = c.DeleteIndex(tmpIndexName)
578
+ _, _ = c.DeleteIndex(ctx, tmpIndexName)
573
579
574
580
return nil, err
575
581
}
576
582
577
- _, err = c.WaitForTask(tmpIndexName, copyResp.TaskID, replaceAllObjectsToIterableOptions(opts)...)
583
+ _, err = c.WaitForTask(ctx, tmpIndexName, copyResp.TaskID, replaceAllObjectsToIterableOptions(opts)...)
578
584
if err != nil {
579
- _, _ = c.DeleteIndex(tmpIndexName)
585
+ _, _ = c.DeleteIndex(ctx, tmpIndexName)
580
586
581
587
return nil, err
582
588
}
583
589
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)...)
585
591
if err != nil {
586
- _, _ = c.DeleteIndex(tmpIndexName)
592
+ _, _ = c.DeleteIndex(ctx, tmpIndexName)
587
593
588
594
return nil, err
589
595
}
590
596
591
- _, err = c.WaitForTask(tmpIndexName, moveResp.TaskID, replaceAllObjectsToIterableOptions(opts)...)
597
+ _, err = c.WaitForTask(ctx, tmpIndexName, moveResp.TaskID, replaceAllObjectsToIterableOptions(opts)...)
592
598
if err != nil {
593
- _, _ = c.DeleteIndex(tmpIndexName)
599
+ _, _ = c.DeleteIndex(ctx, tmpIndexName)
594
600
595
601
return nil, err
596
602
}
@@ -605,8 +611,8 @@ func (c *APIClient) ReplaceAllObjects(indexName string, objects []map[string]any
605
611
// Exists returns whether an initialized index exists or not, along with a nil
606
612
// error. When encountering a network error, a non-nil error is returned along
607
613
// 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)
610
616
if err == nil {
611
617
return true , nil
612
618
}
0 commit comments