algolia 2.0.0.pre.alpha.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (86) hide show
  1. checksums.yaml +7 -0
  2. data/.circleci/config.yml +146 -0
  3. data/.github/ISSUE_TEMPLATE.md +20 -0
  4. data/.github/PULL_REQUEST_TEMPLATE.md +22 -0
  5. data/.gitignore +38 -0
  6. data/.rubocop.yml +186 -0
  7. data/.rubocop_todo.yml +14 -0
  8. data/CODE_OF_CONDUCT.md +74 -0
  9. data/Gemfile +18 -0
  10. data/LICENSE +21 -0
  11. data/README.md +56 -0
  12. data/Rakefile +45 -0
  13. data/Steepfile +6 -0
  14. data/algolia.gemspec +41 -0
  15. data/bin/console +21 -0
  16. data/bin/setup +8 -0
  17. data/lib/algolia.rb +42 -0
  18. data/lib/algolia/account_client.rb +65 -0
  19. data/lib/algolia/analytics_client.rb +105 -0
  20. data/lib/algolia/config/algolia_config.rb +40 -0
  21. data/lib/algolia/config/analytics_config.rb +20 -0
  22. data/lib/algolia/config/insights_config.rb +20 -0
  23. data/lib/algolia/config/recommendation_config.rb +20 -0
  24. data/lib/algolia/config/search_config.rb +40 -0
  25. data/lib/algolia/defaults.rb +35 -0
  26. data/lib/algolia/enums/call_type.rb +4 -0
  27. data/lib/algolia/enums/retry_outcome_type.rb +5 -0
  28. data/lib/algolia/error.rb +29 -0
  29. data/lib/algolia/helpers.rb +83 -0
  30. data/lib/algolia/http/http_requester.rb +84 -0
  31. data/lib/algolia/http/response.rb +23 -0
  32. data/lib/algolia/insights_client.rb +238 -0
  33. data/lib/algolia/iterators/base_iterator.rb +19 -0
  34. data/lib/algolia/iterators/object_iterator.rb +27 -0
  35. data/lib/algolia/iterators/paginator_iterator.rb +44 -0
  36. data/lib/algolia/iterators/rule_iterator.rb +9 -0
  37. data/lib/algolia/iterators/synonym_iterator.rb +9 -0
  38. data/lib/algolia/logger_helper.rb +14 -0
  39. data/lib/algolia/recommendation_client.rb +60 -0
  40. data/lib/algolia/responses/add_api_key_response.rb +38 -0
  41. data/lib/algolia/responses/base_response.rb +9 -0
  42. data/lib/algolia/responses/delete_api_key_response.rb +40 -0
  43. data/lib/algolia/responses/indexing_response.rb +28 -0
  44. data/lib/algolia/responses/multiple_batch_indexing_response.rb +29 -0
  45. data/lib/algolia/responses/multiple_response.rb +45 -0
  46. data/lib/algolia/responses/restore_api_key_response.rb +36 -0
  47. data/lib/algolia/responses/update_api_key_response.rb +39 -0
  48. data/lib/algolia/search_client.rb +614 -0
  49. data/lib/algolia/search_index.rb +1094 -0
  50. data/lib/algolia/transport/request_options.rb +94 -0
  51. data/lib/algolia/transport/retry_strategy.rb +117 -0
  52. data/lib/algolia/transport/stateful_host.rb +26 -0
  53. data/lib/algolia/transport/transport.rb +161 -0
  54. data/lib/algolia/user_agent.rb +25 -0
  55. data/lib/algolia/version.rb +3 -0
  56. data/sig/config/algolia_config.rbs +24 -0
  57. data/sig/config/analytics_config.rbs +11 -0
  58. data/sig/config/insights_config.rbs +11 -0
  59. data/sig/config/recommendation_config.rbs +11 -0
  60. data/sig/config/search_config.rbs +11 -0
  61. data/sig/enums/call_type.rbs +5 -0
  62. data/sig/helpers.rbs +12 -0
  63. data/sig/http/http_requester.rbs +17 -0
  64. data/sig/http/response.rbs +14 -0
  65. data/sig/interfaces/_connection.rbs +16 -0
  66. data/sig/iterators/base_iterator.rbs +15 -0
  67. data/sig/iterators/object_iterator.rbs +6 -0
  68. data/sig/iterators/paginator_iterator.rbs +8 -0
  69. data/sig/iterators/rule_iterator.rbs +5 -0
  70. data/sig/iterators/synonym_iterator.rbs +5 -0
  71. data/sig/transport/request_options.rbs +33 -0
  72. data/sig/transport/stateful_host.rbs +21 -0
  73. data/test/algolia/integration/account_client_test.rb +47 -0
  74. data/test/algolia/integration/analytics_client_test.rb +113 -0
  75. data/test/algolia/integration/base_test.rb +9 -0
  76. data/test/algolia/integration/insights_client_test.rb +80 -0
  77. data/test/algolia/integration/mocks/mock_requester.rb +45 -0
  78. data/test/algolia/integration/recommendation_client_test.rb +30 -0
  79. data/test/algolia/integration/search_client_test.rb +361 -0
  80. data/test/algolia/integration/search_index_test.rb +698 -0
  81. data/test/algolia/unit/helpers_test.rb +69 -0
  82. data/test/algolia/unit/retry_strategy_test.rb +139 -0
  83. data/test/algolia/unit/user_agent_test.rb +16 -0
  84. data/test/test_helper.rb +89 -0
  85. data/upgrade_guide.md +595 -0
  86. metadata +307 -0
@@ -0,0 +1,614 @@
1
+ require 'faraday'
2
+ require 'openssl'
3
+ require 'base64'
4
+
5
+ module Algolia
6
+ module Search
7
+ # Class Client
8
+ class Client
9
+ include CallType
10
+ include Helpers
11
+
12
+ # Initialize a client to connect to Algolia
13
+ #
14
+ # @param search_config [Search::Config] a Search::Config object which contains your APP_ID and API_KEY
15
+ # @option adapter [Object] adapter object used for the connection
16
+ # @option logger [Object]
17
+ # @option http_requester [Object] http_requester object used for the connection
18
+ #
19
+ def initialize(search_config, opts = {})
20
+ @config = search_config
21
+ adapter = opts[:adapter] || Defaults::ADAPTER
22
+ logger = opts[:logger] || LoggerHelper.create('debug.log')
23
+ requester = opts[:http_requester] || Defaults::REQUESTER_CLASS.new(adapter, logger)
24
+ @transporter = Transport::Transport.new(@config, requester)
25
+ end
26
+
27
+ # Create a new client providing only app ID and API key
28
+ #
29
+ # @param app_id [String] Algolia application ID
30
+ # @param api_key [String] Algolia API key
31
+ #
32
+ # @return self
33
+ #
34
+ def self.create(app_id, api_key)
35
+ config = Search::Config.new(app_id: app_id, api_key: api_key)
36
+ create_with_config(config)
37
+ end
38
+
39
+ # Create a new client providing only the search config
40
+ #
41
+ # @param config [Search::Config]
42
+ #
43
+ # @return self
44
+ #
45
+ def self.create_with_config(config)
46
+ new(config)
47
+ end
48
+
49
+ # Fetch the task status until it returns as "published", meaning the operation is done
50
+ #
51
+ # @param index_name [String]
52
+ # @param task_id [Integer]
53
+ # @param time_before_retry [Integer] time before retrying the call, in ms
54
+ # @param opts [Hash] contains extra parameters to send with your query
55
+ #
56
+ # @return nil
57
+ #
58
+ def wait_task(index_name, task_id, time_before_retry = WAIT_TASK_DEFAULT_TIME_BEFORE_RETRY, opts = {})
59
+ loop do
60
+ status = get_task_status(index_name, task_id, opts)
61
+ if status == 'published'
62
+ return
63
+ end
64
+ sleep(time_before_retry.to_f / 1000)
65
+ end
66
+ end
67
+
68
+ # Check the status of a task on the server.
69
+ # All server task are asynchronous and you can check the status of a task with this method.
70
+ #
71
+ # @param index_name [String] index used for the calls
72
+ # @param task_id [Integer] the id of the task returned by server
73
+ # @param opts [Hash] contains extra parameters to send with your query
74
+ #
75
+ # @return [String]
76
+ #
77
+ def get_task_status(index_name, task_id, opts = {})
78
+ res = @transporter.read(:GET, path_encode('/1/indexes/%s/task/%s', index_name, task_id), {}, opts)
79
+ get_option(res, 'status')
80
+ end
81
+
82
+ # # # # # # # # # # # # # # # # # # # # #
83
+ # MISC
84
+ # # # # # # # # # # # # # # # # # # # # #
85
+
86
+ # Initialize an index with a given name
87
+ #
88
+ # @param index_name [String] name of the index to init
89
+ #
90
+ # @return [Index] new Index instance
91
+ #
92
+ def init_index(index_name)
93
+ index_name.strip!
94
+ if index_name.empty?
95
+ raise AlgoliaError, 'Please provide a valid index name'
96
+ end
97
+ Index.new(index_name, @transporter, @config)
98
+ end
99
+
100
+ # List all indexes of the client
101
+ #
102
+ # @param opts [Hash] contains extra parameters to send with your query
103
+ #
104
+ # @return [Hash]
105
+ #
106
+ def list_indexes(opts = {})
107
+ @transporter.read(:GET, '/1/indexes', {}, opts)
108
+ end
109
+
110
+ # Retrieve the client logs
111
+ #
112
+ # @param opts [Hash] contains extra parameters to send with your query
113
+ #
114
+ # @return [Hash]
115
+ #
116
+ def get_logs(opts = {})
117
+ @transporter.read(:GET, '/1/logs', {}, opts)
118
+ end
119
+
120
+ # # # # # # # # # # # # # # # # # # # # #
121
+ # COPY OPERATIONS
122
+ # # # # # # # # # # # # # # # # # # # # #
123
+
124
+ # Copy the rules from source index to destination index
125
+ #
126
+ # @param src_index_name [String] Name of the source index
127
+ # @param dest_index_name [String] Name of the destination index
128
+ # @param opts [Hash] contains extra parameters to send with your query
129
+ #
130
+ # @return [IndexingResponse]
131
+ #
132
+ def copy_rules(src_index_name, dest_index_name, opts = {})
133
+ request_options = symbolize_hash(opts)
134
+ request_options[:scope] = ['rules']
135
+ copy_index(src_index_name, dest_index_name, request_options)
136
+ end
137
+
138
+ # Copy the rules from source index to destination index and wait for the task to complete
139
+ #
140
+ # @param src_index_name [String] Name of the source index
141
+ # @param dest_index_name [String] Name of the destination index
142
+ # @param opts [Hash] contains extra parameters to send with your query
143
+ #
144
+ # @return [IndexingResponse]
145
+ #
146
+ def copy_rules!(src_index_name, dest_index_name, opts = {})
147
+ request_options = symbolize_hash(opts)
148
+ request_options[:scope] = ['rules']
149
+ copy_index!(src_index_name, dest_index_name, request_options)
150
+ end
151
+
152
+ # Copy the settings from source index to destination index
153
+ #
154
+ # @param src_index_name [String] Name of the source index
155
+ # @param dest_index_name [String] Name of the destination index
156
+ # @param opts [Hash] contains extra parameters to send with your query
157
+ #
158
+ # @return [IndexingResponse]
159
+ #
160
+ def copy_settings(src_index_name, dest_index_name, opts = {})
161
+ request_options = symbolize_hash(opts)
162
+ request_options[:scope] = ['settings']
163
+ copy_index(src_index_name, dest_index_name, request_options)
164
+ end
165
+
166
+ # Copy the settings from source index to destination index and wait for the task to complete
167
+ #
168
+ # @param src_index_name [String] Name of the source index
169
+ # @param dest_index_name [String] Name of the destination index
170
+ # @param opts [Hash] contains extra parameters to send with your query
171
+ #
172
+ # @return [IndexingResponse]
173
+ #
174
+ def copy_settings!(src_index_name, dest_index_name, opts = {})
175
+ request_options = symbolize_hash(opts)
176
+ request_options[:scope] = ['settings']
177
+ copy_index!(src_index_name, dest_index_name, request_options)
178
+ end
179
+
180
+ # Copy the synonyms from source index to destination index
181
+ #
182
+ # @param src_index_name [String] Name of the source index
183
+ # @param dest_index_name [String] Name of the destination index
184
+ # @param opts [Hash] contains extra parameters to send with your query
185
+ #
186
+ # @return [IndexingResponse]
187
+ #
188
+ def copy_synonyms(src_index_name, dest_index_name, opts = {})
189
+ request_options = symbolize_hash(opts)
190
+ request_options[:scope] = ['synonyms']
191
+ copy_index(src_index_name, dest_index_name, request_options)
192
+ end
193
+
194
+ # Copy the synonyms from source index to destination index and wait for the task to complete
195
+ #
196
+ # @param src_index_name [String] Name of the source index
197
+ # @param dest_index_name [String] Name of the destination index
198
+ # @param opts [Hash] contains extra parameters to send with your query
199
+ #
200
+ # @return [IndexingResponse]
201
+ #
202
+ def copy_synonyms!(src_index_name, dest_index_name, opts = {})
203
+ request_options = symbolize_hash(opts)
204
+ request_options[:scope] = ['synonyms']
205
+ copy_index!(src_index_name, dest_index_name, request_options)
206
+ end
207
+
208
+ # Copy the source index to the destination index
209
+ #
210
+ # @param src_index_name [String] Name of the source index
211
+ # @param dest_index_name [String] Name of the destination index
212
+ # @param opts [Hash] contains extra parameters to send with your query
213
+ #
214
+ # @return [IndexingResponse]
215
+ #
216
+ def copy_index(src_index_name, dest_index_name, opts = {})
217
+ response = @transporter.write(:POST, path_encode('/1/indexes/%s/operation', src_index_name), { operation: 'copy', destination: dest_index_name }, opts)
218
+
219
+ IndexingResponse.new(init_index(src_index_name), response)
220
+ end
221
+
222
+ # Copy the source index to the destination index and wait for the task to complete
223
+ #
224
+ # @param src_index_name [String] Name of the source index
225
+ # @param dest_index_name [String] Name of the destination index
226
+ # @param opts [Hash] contains extra parameters to send with your query
227
+ #
228
+ # @return [IndexingResponse]
229
+ #
230
+ def copy_index!(src_index_name, dest_index_name, opts = {})
231
+ response = copy_index(src_index_name, dest_index_name, opts)
232
+
233
+ response.wait(opts)
234
+ end
235
+
236
+ # # # # # # # # # # # # # # # # # # # # #
237
+ # MOVE OPERATIONS
238
+ # # # # # # # # # # # # # # # # # # # # #
239
+
240
+ # Move the source index to the destination index
241
+ #
242
+ # @param src_index_name [String] Name of the source index
243
+ # @param dest_index_name [String] Name of the destination index
244
+ # @param opts [Hash] contains extra parameters to send with your query
245
+ #
246
+ # @return [IndexingResponse]
247
+ #
248
+ def move_index(src_index_name, dest_index_name, opts = {})
249
+ response = @transporter.write(:POST, path_encode('/1/indexes/%s/operation', src_index_name), { operation: 'move', destination: dest_index_name }, opts)
250
+
251
+ IndexingResponse.new(init_index(src_index_name), response)
252
+ end
253
+
254
+ # Move the source index to the destination index and wait for the task to complete
255
+ #
256
+ # @param src_index_name [String] Name of the source index
257
+ # @param dest_index_name [String] Name of the destination index
258
+ # @param opts [Hash] contains extra parameters to send with your query
259
+ #
260
+ # @return [IndexingResponse]
261
+ #
262
+ def move_index!(src_index_name, dest_index_name, opts = {})
263
+ response = move_index(src_index_name, dest_index_name, opts)
264
+
265
+ response.wait(opts)
266
+ end
267
+
268
+ # # # # # # # # # # # # # # # # # # # # #
269
+ # API KEY METHODS
270
+ # # # # # # # # # # # # # # # # # # # # #
271
+
272
+ # Get the designated API key
273
+ #
274
+ # @param key_id [String] API key to retrieve
275
+ #
276
+ # @return [Hash]
277
+ #
278
+ def get_api_key(key_id, opts = {})
279
+ @transporter.read(:GET, path_encode('/1/keys/%s', key_id), {}, opts)
280
+ end
281
+
282
+ # Add an API key with the given ACL
283
+ #
284
+ # @param acl [Array] API key to retrieve
285
+ # @param opts [Hash] contains extra parameters to send with your query used for the key
286
+ #
287
+ # @return [AddApiKeyResponse]
288
+ #
289
+ def add_api_key(acl, opts = {})
290
+ response = @transporter.write(:POST, '/1/keys', { acl: acl }, opts)
291
+
292
+ AddApiKeyResponse.new(self, response)
293
+ end
294
+
295
+ # Add an API key with the given ACL and wait for the task to complete
296
+ #
297
+ # @param acl [Array] API key to retrieve
298
+ # @param opts [Hash] contains extra parameters to send with your query used for the key
299
+ #
300
+ # @return [AddApiKeyResponse]
301
+ #
302
+ def add_api_key!(acl, opts = {})
303
+ response = add_api_key(acl)
304
+
305
+ response.wait(opts)
306
+ end
307
+
308
+ # Update an API key with the optional parameters
309
+ #
310
+ # @param key [String] API key to update
311
+ # @param opts [Hash] contains extra parameters to send with your query used to update the key
312
+ #
313
+ # @return [UpdateApiKeyResponse]
314
+ #
315
+ def update_api_key(key, opts = {})
316
+ request_options = symbolize_hash(opts)
317
+
318
+ response = @transporter.write(:PUT, path_encode('/1/keys/%s', key), {}, request_options)
319
+
320
+ UpdateApiKeyResponse.new(self, response, request_options)
321
+ end
322
+
323
+ # Update an API key with the optional parameters and wait for the task to complete
324
+ #
325
+ # @param key [String] API key to update
326
+ # @param opts [Hash] contains extra parameters to send with your query used to update the key
327
+ #
328
+ # @return [UpdateApiKeyResponse]
329
+ #
330
+ def update_api_key!(key, opts = {})
331
+ response = update_api_key(key, opts)
332
+
333
+ response.wait(opts)
334
+ end
335
+
336
+ # Delete the given API key
337
+ #
338
+ # @param key [String] API key to delete
339
+ # @param opts [Hash] contains extra parameters to send with your query
340
+ #
341
+ # @return [DeleteApiKeyResponse]
342
+ #
343
+ def delete_api_key(key, opts = {})
344
+ response = @transporter.write(:DELETE, path_encode('/1/keys/%s', key), {}, opts)
345
+
346
+ DeleteApiKeyResponse.new(self, response, key)
347
+ end
348
+
349
+ # Delete the given API key and wait for the task to complete
350
+ #
351
+ # @param key [String] API key to delete
352
+ # @param opts [Hash] contains extra parameters to send with your query
353
+ #
354
+ # @return [DeleteApiKeyResponse]
355
+ #
356
+ def delete_api_key!(key, opts = {})
357
+ response = delete_api_key(key, opts)
358
+
359
+ response.wait(opts)
360
+ end
361
+
362
+ # Restore the given API key
363
+ #
364
+ # @param key [String] API key to restore
365
+ # @param opts [Hash] contains extra parameters to send with your query
366
+ #
367
+ # @return [RestoreApiKeyResponse]
368
+ #
369
+ def restore_api_key(key, opts = {})
370
+ @transporter.write(:POST, path_encode('/1/keys/%s/restore', key), {}, opts)
371
+
372
+ RestoreApiKeyResponse.new(self, key)
373
+ end
374
+
375
+ # Restore the given API key and wait for the task to complete
376
+ #
377
+ # @param key [String] API key to restore
378
+ # @param opts [Hash] contains extra parameters to send with your query
379
+ #
380
+ # @return [RestoreApiKeyResponse]
381
+ #
382
+ def restore_api_key!(key, opts = {})
383
+ response = restore_api_key(key, opts)
384
+
385
+ response.wait(opts)
386
+ end
387
+
388
+ # List all keys associated with the current client
389
+ #
390
+ # @param opts [Hash] contains extra parameters to send with your query
391
+ #
392
+ # @return [Hash]
393
+ #
394
+ def list_api_keys(opts = {})
395
+ @transporter.read(:GET, '/1/keys', {}, opts)
396
+ end
397
+
398
+ # Generate a secured API key from the given parent key with the given restrictions
399
+ #
400
+ # @param parent_key [String] Parent API key used the generate the secured key
401
+ # @param restrictions [Hash] Restrictions to apply on the secured key
402
+ #
403
+ # @return [String]
404
+ #
405
+ def self.generate_secured_api_key(parent_key, restrictions)
406
+ url_encoded_restrictions = to_query_string(symbolize_hash(restrictions))
407
+ hmac = OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('sha256'), parent_key, url_encoded_restrictions)
408
+ Base64.encode64("#{hmac}#{url_encoded_restrictions}").gsub("\n", '')
409
+ end
410
+
411
+ # Returns the time the given securedAPIKey remains valid in seconds
412
+ #
413
+ # @param secured_api_key [String]
414
+ #
415
+ # @return [Integer]
416
+ #
417
+ def self.get_secured_api_key_remaining_validity(secured_api_key)
418
+ now = Time.now.to_i
419
+ decoded_key = Base64.decode64(secured_api_key)
420
+ regex = 'validUntil=(\d+)'
421
+ matches = decoded_key.match(regex)
422
+
423
+ if matches.nil?
424
+ raise AlgoliaError, 'The SecuredAPIKey doesn\'t have a validUntil parameter.'
425
+ end
426
+
427
+ valid_until = matches[1].to_i
428
+
429
+ valid_until - now
430
+ end
431
+
432
+ # # # # # # # # # # # # # # # # # # # # #
433
+ # MULTIPLE* METHODS
434
+ # # # # # # # # # # # # # # # # # # # # #
435
+
436
+ # Batch multiple operations
437
+ #
438
+ # @param operations [Array] array of operations (addObject, updateObject, ...)
439
+ # @param opts [Hash] contains extra parameters to send with your query
440
+ #
441
+ # @return [MultipleIndexBatchIndexingResponse]
442
+ #
443
+ def multiple_batch(operations, opts = {})
444
+ response = @transporter.write(:POST, '/1/indexes/*/batch', { requests: operations }, opts)
445
+
446
+ MultipleIndexBatchIndexingResponse.new(self, response)
447
+ end
448
+
449
+ # Batch multiple operations and wait for the task to complete
450
+ #
451
+ # @param operations [Array] array of operations (addObject, updateObject, ...)
452
+ # @param opts [Hash] contains extra parameters to send with your query
453
+ #
454
+ # @return [MultipleIndexBatchIndexingResponse]
455
+ #
456
+ def multiple_batch!(operations, opts = {})
457
+ response = multiple_batch(operations, opts)
458
+
459
+ response.wait(opts)
460
+ end
461
+
462
+ # Retrieve multiple objects in one batch request
463
+ #
464
+ # @param requests [Array] array of requests
465
+ # @param opts [Hash] contains extra parameters to send with your query
466
+ #
467
+ # @return [Hash]
468
+ #
469
+ def multiple_get_objects(requests, opts = {})
470
+ @transporter.read(:POST, '/1/indexes/*/objects', { requests: requests }, opts)
471
+ end
472
+
473
+ # Search multiple indices
474
+ #
475
+ # @param queries [Array] array of queries
476
+ # @param opts [Hash] contains extra parameters to send with your query
477
+ #
478
+ # @return [Hash]
479
+ #
480
+ def multiple_queries(queries, opts = {})
481
+ @transporter.read(:POST, '/1/indexes/*/queries', { requests: queries }, opts)
482
+ end
483
+
484
+ # # # # # # # # # # # # # # # # # # # # #
485
+ # MCM METHODS
486
+ # # # # # # # # # # # # # # # # # # # # #
487
+
488
+ # Assign or Move a userID to a cluster.
489
+ #
490
+ # @param user_id [String]
491
+ # @param cluster_name [String]
492
+ #
493
+ # @return [Hash]
494
+ #
495
+ def assign_user_id(user_id, cluster_name, opts = {})
496
+ request_options = symbolize_hash(opts)
497
+ request_options[:headers] = { 'X-Algolia-User-ID': user_id }
498
+
499
+ @transporter.write(:POST, '/1/clusters/mapping', { cluster: cluster_name }, request_options)
500
+ end
501
+
502
+ # Assign multiple userIDs to a cluster.
503
+ #
504
+ # @param user_ids [Array]
505
+ # @param cluster_name [String]
506
+ #
507
+ # @return [Hash]
508
+ #
509
+ def assign_user_ids(user_ids, cluster_name, opts = {})
510
+ @transporter.write(:POST, '/1/clusters/mapping/batch', { cluster: cluster_name, users: user_ids }, opts)
511
+ end
512
+
513
+ # Get the top 10 userIDs with the highest number of records per cluster.
514
+ #
515
+ # @param opts [Hash] contains extra parameters to send with your query
516
+ #
517
+ # @return [Hash]
518
+ #
519
+ def get_top_user_ids(opts = {})
520
+ @transporter.read(:GET, '/1/clusters/mapping/top', {}, opts)
521
+ end
522
+
523
+ # Returns the userID data stored in the mapping.
524
+ #
525
+ # @param user_id [String]
526
+ # @param opts [Hash] contains extra parameters to send with your query
527
+ #
528
+ # @return [Hash]
529
+ #
530
+ def get_user_id(user_id, opts = {})
531
+ @transporter.read(:GET, path_encode('/1/clusters/mapping/%s', user_id), {}, opts)
532
+ end
533
+
534
+ # List the clusters available in a multi-clusters setup for a single appID
535
+ #
536
+ # @param opts [Hash] contains extra parameters to send with your query
537
+ #
538
+ # @return [Hash]
539
+ #
540
+ def list_clusters(opts = {})
541
+ @transporter.read(:GET, '/1/clusters', {}, opts)
542
+ end
543
+
544
+ # List the userIDs assigned to a multi-clusters appID
545
+ #
546
+ # @param opts [Hash] contains extra parameters to send with your query
547
+ #
548
+ # @return [Hash]
549
+ #
550
+ def list_user_ids(opts = {})
551
+ @transporter.read(:GET, '/1/clusters/mapping', {}, opts)
552
+ end
553
+
554
+ # Remove a userID and its associated data from the multi-clusters
555
+ #
556
+ # @param user_id [String]
557
+ # @param opts [Hash] contains extra parameters to send with your query
558
+ #
559
+ # @return [Hash]
560
+ #
561
+ def remove_user_id(user_id, opts = {})
562
+ request_options = symbolize_hash(opts)
563
+ request_options[:headers] = { 'X-Algolia-User-ID': user_id }
564
+
565
+ @transporter.write(:DELETE, '/1/clusters/mapping', {}, request_options)
566
+ end
567
+
568
+ # Search for userIDs
569
+ #
570
+ # @param query [String]
571
+ # @param opts [Hash] contains extra parameters to send with your query
572
+ #
573
+ # @return [Hash]
574
+ #
575
+ def search_user_ids(query, opts = {})
576
+ @transporter.read(:POST, '/1/clusters/mapping/search', { query: query }, opts)
577
+ end
578
+
579
+ # Get the status of your clusters' migrations or user creations
580
+ #
581
+ # @param opts [Hash] contains extra parameters to send with your query
582
+ #
583
+ # @return [Hash]
584
+ #
585
+ def pending_mappings?(opts = {})
586
+ retrieve_mappings = false
587
+
588
+ request_options = symbolize_hash(opts)
589
+ if request_options.has_key?(:retrieveMappings)
590
+ retrieve_mappings = request_options[:retrieveMappings]
591
+ request_options.delete(:retrieveMappings)
592
+ end
593
+
594
+ @transporter.read(:GET, '/1/clusters/mapping/pending' + handle_params({ getClusters: retrieve_mappings }), {}, request_options)
595
+ end
596
+
597
+ #
598
+ # Aliases the pending_mappings? method
599
+ #
600
+ alias_method :has_pending_mappings, :pending_mappings?
601
+
602
+ #
603
+ # Method available to make custom requests to the API
604
+ #
605
+ def custom_request(data, uri, method, call_type, opts = {})
606
+ if call_type == WRITE
607
+ @transporter.write(method.to_sym, uri, data, opts)
608
+ elsif call_type == READ
609
+ @transporter.read(method.to_sym, uri, data, opts)
610
+ end
611
+ end
612
+ end
613
+ end
614
+ end