Skip to content

fix(clients): support dict in helpers #4254

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion playground/ruby/Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: ../../clients/algoliasearch-client-ruby
specs:
algolia (3.10.0)
algolia (3.10.1)
base64 (>= 0.2.0, < 1)
faraday (>= 1.0.1, < 3.0)
faraday-net_http_persistent (>= 0.15, < 3)
Expand Down
2 changes: 1 addition & 1 deletion playground/ruby/search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
client = Algolia::SearchClient.create(ENV['ALGOLIA_APPLICATION_ID'], ENV['ALGOLIA_ADMIN_KEY'])
# set a custom user agent
client.add_user_agent_segment('Algolia for rails', "test")
res = client.search_single_index('contacts', Algolia::Search::SearchParamsObject.new(query: 'Jimmie'))
res = client.browse_objects('qigbuery-RECORDS')
puts res

=begin
Expand Down
10 changes: 10 additions & 0 deletions templates/python/search_helpers.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@
"""
Helper: Iterate on the `browse` method of the client to allow aggregating objects of an index.
"""
if isinstance(browse_params, dict):
browse_params = BrowseParamsObject().from_dict(browse_params)

if browse_params is None:
browse_params = BrowseParamsObject(hits_per_page=1000)

Expand Down Expand Up @@ -159,6 +162,9 @@
"""
Helper: Iterate on the `search_rules` method of the client to allow aggregating rules of an index.
"""
if isinstance(search_rules_params, dict):
search_rules_params = SearchRulesParams().from_dict(search_rules_params)

if search_rules_params is None:
search_rules_params = SearchRulesParams(hits_per_page=1000)

Expand Down Expand Up @@ -191,6 +197,9 @@
"""
Helper: Iterate on the `search_synonyms` method of the client to allow aggregating synonyms of an index.
"""
if isinstance(search_synonyms_params, dict):
search_synonyms_params = SearchSynonymsParams().from_dict(search_synonyms_params)

if search_synonyms_params is None:
search_synonyms_params = SearchSynonymsParams(
hits_per_page=1000,
Expand Down Expand Up @@ -226,6 +235,7 @@
"""
if restrictions is None:
restrictions = SecuredApiKeyRestrictions()

restrictions_dict = {}
if isinstance(restrictions, SecuredApiKeyRestrictions):
restrictions_dict = restrictions.to_dict()
Expand Down
37 changes: 25 additions & 12 deletions templates/ruby/search_helpers.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,17 @@ end
# @param timeout [Proc] the function to decide how long to wait between retries.
# @param request_options [Hash] the requestOptions to send along with the query, they will be forwarded to the `getApikey` method and merged with the transporter requestOptions.
# @return [Http::Response] the last get_api_key response
def wait_for_api_key(key, operation, api_key = {}, max_retries = 50, timeout = -> (retry_count) { [retry_count * 200, 5000].min }, request_options = {})
def wait_for_api_key(key, operation, api_key = Search::ApiKey.new, max_retries = 50, timeout = -> (retry_count) { [retry_count * 200, 5000].min }, request_options = {})
api_key = api_client.object_to_hash(api_key)

retries = 0
if operation == 'update'
raise ArgumentError, '`api_key` is required when waiting for an `update` operation.' if api_key.nil?
while retries < max_retries
updated_key = get_api_key(key, request_options)
updated_key_hash = updated_key.to_hash
equals = true
api_key.to_hash.each do |k, v|
api_key.each do |k, v|
equals &&= updated_key_hash[k] == v
end

Expand Down Expand Up @@ -95,7 +97,9 @@ end
# @param request_options [Hash] the requestOptions to send along with the query, they will be forwarded to the `browse` method.
# @param block [Proc] the block to execute on each object of the index.
def browse_objects(index_name, browse_params = Search::BrowseParamsObject.new, request_options = {}, &block)
browse_params.hits_per_page ||= 1000
browse_params = api_client.object_to_hash(browse_params)

browse_params[:hitsPerPage] = 1000 unless browse_params.key?(:hitsPerPage)

hits = []
loop do
Expand All @@ -107,8 +111,9 @@ def browse_objects(index_name, browse_params = Search::BrowseParamsObject.new, r
else
hits.concat(res.hits)
end
browse_params.cursor = res.cursor
break if browse_params.cursor.nil?

browse_params[:cursor] = res.cursor
break if browse_params[:cursor].nil?
end

hits unless block_given?
Expand All @@ -120,7 +125,11 @@ end
# @param search_rules_params [SearchRulesParams] the parameters to send along with the query, they will be forwarded to the `searchRules` method.
# @param request_options [Hash] the requestOptions to send along with the query, they will be forwarded to the `searchRules` method.
# @param block [Proc] the block to execute on each rule of the index.
def browse_rules(index_name, search_rules_params = Search::SearchRulesParams.new(hits_per_page: 1000, page: 0), request_options = {}, &block)
def browse_rules(index_name, search_rules_params = Search::SearchRulesParams.new, request_options = {}, &block)
search_rules_params = api_client.object_to_hash(search_rules_params)

search_rules_params[:hitsPerPage] = 1000 unless search_rules_params.key?(:hitsPerPage)

rules = []
loop do
res = search_rules(index_name, search_rules_params, request_options)
Expand All @@ -131,8 +140,8 @@ def browse_rules(index_name, search_rules_params = Search::SearchRulesParams.new
else
rules.concat(res.hits)
end
search_rules_params.page += 1
break if res.hits.length < search_rules_params.hits_per_page
search_rules_params[:page] += 1
break if res.hits.length < search_rules_params[:hitsPerPage]
end

rules unless block_given?
Expand All @@ -144,7 +153,11 @@ end
# @param search_synonyms_params [SearchSynonymsParams] the parameters to send along with the query, they will be forwarded to the `searchSynonyms` method.
# @param request_options [Hash] the requestOptions to send along with the query, they will be forwarded to the `searchSynonyms` method.
# @param block [Proc] the block to execute on each synonym of the index.
def browse_synonyms(index_name, search_synonyms_params = Search::SearchSynonymsParams.new(hits_per_page: 1000, page: 0), request_options = {}, &block)
def browse_synonyms(index_name, search_synonyms_params = Search::SearchSynonymsParams.new, request_options = {}, &block)
search_synonyms_params = api_client.object_to_hash(search_synonyms_params)

search_synonyms_params[:hitsPerPage] = 1000 unless search_synonyms_params.key?(:hitsPerPage)

synonyms = []
loop do
res = search_synonyms(index_name, search_synonyms_params, request_options)
Expand All @@ -155,8 +168,8 @@ def browse_synonyms(index_name, search_synonyms_params = Search::SearchSynonymsP
else
synonyms.concat(res.hits)
end
search_synonyms_params.page += 1
break if res.hits.length < search_synonyms_params.hits_per_page
search_synonyms_params[:page] += 1
break if res.hits.length < search_synonyms_params[:hitsPerPage]
end

synonyms unless block_given?
Expand Down Expand Up @@ -405,4 +418,4 @@ def index_exists?(index_name)
end

true
end
end