Class: AnyQuery::Adapters::Http Private

Inherits:
Base
  • Object
show all
Defined in:
lib/any_query/adapters/http.rb

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Defined Under Namespace

Classes: Config

Constant Summary collapse

MAX_ITERATIONS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

1000

Instance Method Summary collapse

Methods inherited from Base

#fallback_where, #group_join_data, #initialize, #instantiate_model, #map_multi_threaded, #parse_field_type, #parse_field_type_boolean, #parse_field_type_date, #parse_field_type_datetime, #parse_field_type_decimal, #parse_field_type_float, #parse_field_type_integer, #parse_field_type_string, #resolve_join, #resolve_path, #resolve_select, #run_external_join

Constructor Details

This class inherits a constructor from AnyQuery::Adapters::Base

Instance Method Details

#build_filters(where) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



72
73
74
75
76
77
78
# File 'lib/any_query/adapters/http.rb', line 72

def build_filters(where)
  {
    query: (where || {}).inject({}) do |memo, object|
             memo.merge(object)
           end
  }
end

#build_url(endpoint, params, id: nil) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/any_query/adapters/http.rb', line 128

def build_url(endpoint, params, id: nil)
  output = (@config[:url] + endpoint[:path])

  output.gsub!('{id}', id.to_s) if id

  if output.include?('{')
    output.gsub!(/\{([^}]+)\}/) do |match|
      key = Regexp.last_match(1).to_sym
      hash = params.find { |h| h[Regexp.last_match(1).to_sym] }
      hash&.delete(key) || match
    end
  end

  output
end

#handle_pagination(endpoint, index, previous_response = nil) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



180
181
182
183
184
185
186
187
188
189
# File 'lib/any_query/adapters/http.rb', line 180

def handle_pagination(endpoint, index, previous_response = nil)
  pagination = endpoint.dig(:options, :pagination) || {}
  method_name = "handle_pagination_#{pagination[:type]}"
  if respond_to?(method_name, true)
    send(method_name, pagination, index, previous_response)
  else
    AnyQuery::Config.logger.warn "Unknown pagination type #{pagination[:type]}"
    { query: { page: index } }
  end
end

#handle_pagination_cursor(pagination, _index, previous_response) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



200
201
202
203
204
205
206
# File 'lib/any_query/adapters/http.rb', line 200

def handle_pagination_cursor(pagination, _index, previous_response)
  return {} unless previous_response

  cursor_parameter = pagination.dig(:params, :cursor) || :cursor
  cursor = previous_response[cursor_parameter]
  { query: { (pagination.dig(:params, :cursor) || :cursor) => cursor } }
end

#handle_pagination_none(_pagination, _index, _previous_response) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



208
209
210
# File 'lib/any_query/adapters/http.rb', line 208

def handle_pagination_none(_pagination, _index, _previous_response)
  {}
end

#handle_pagination_page(pagination, index, _previous_response) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



191
192
193
194
# File 'lib/any_query/adapters/http.rb', line 191

def handle_pagination_page(pagination, index, _previous_response)
  starts_from = pagination[:starts_from] || 0
  { query: { (pagination.dig(:params, :number) || :page) => starts_from + index } }
end

#handle_pagination_skip(_pagination, _index, _previous_response) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



196
197
198
# File 'lib/any_query/adapters/http.rb', line 196

def handle_pagination_skip(_pagination, _index, _previous_response)
  raise 'TODO: Implement skip pagination'
end

#load(model, select:, joins:, where:, limit:) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



25
26
27
28
29
30
31
32
# File 'lib/any_query/adapters/http.rb', line 25

def load(model, select:, joins:, where:, limit:)
  data = run_http_list_query(where)

  data = resolve_joins(data, joins) if joins.present?
  data = data.first(limit) if limit.present?

  parse_response(model, select, data)
end

#load_single(model, id, joins) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



34
35
36
37
38
39
40
# File 'lib/any_query/adapters/http.rb', line 34

def load_single(model, id, joins)
  data = run_http_single_query(id, {})

  data = resolve_joins(data, joins) if joins.present?

  instantiate_model(model, data)
end

#load_single_from_list(data) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

FIXME: Use common method



52
53
54
55
56
57
58
59
# File 'lib/any_query/adapters/http.rb', line 52

def load_single_from_list(data)
  data.each_slice(50).flat_map do |slice|
    slice
      .map { |data| Thread.new { run_http_single_query(data[:id], {}) } }
      .each(&:join)
      .map(&:value)
  end
end

#merge_params(endpoint, params, iteration, previous_response) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



122
123
124
125
126
# File 'lib/any_query/adapters/http.rb', line 122

def merge_params(endpoint, params, iteration, previous_response)
  (endpoint[:options][:default_params] || {})
    .deep_merge(params)
    .deep_merge(handle_pagination(endpoint, iteration, previous_response))
end

#parse_response(model, select, data) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



42
43
44
45
46
47
48
49
# File 'lib/any_query/adapters/http.rb', line 42

def parse_response(model, select, data)
  data = data.map do |record|
    instantiate_model(model, record)
  end
  data = resolve_select(data, select) if select.present?

  data
end

#resolve_joins(data, joins) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



61
62
63
64
65
66
67
68
69
70
# File 'lib/any_query/adapters/http.rb', line 61

def resolve_joins(data, joins)
  data = load_single_from_list(data) if joins.any? { |j| j[:model] == :show }

  joins.each do |join|
    next if join[:model] == :show

    resolve_join(data, join)
  end
  data
end

#run_http_list_query(raw_params) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/any_query/adapters/http.rb', line 92

def run_http_list_query(raw_params)
  endpoint = @config[:endpoints][:list]
  url = build_url(endpoint, raw_params)
  params = build_filters(raw_params)
  results = Set.new
  previous_response = nil
  MAX_ITERATIONS.times do |i|
    params = merge_params(endpoint, params, i, previous_response)

    AnyQuery::Config.logger.debug "Starting request to #{url} with params #{params.inspect}"

    data = run_http_request(endpoint, url, params)
    break if previous_response == data

    previous_response = data

    data = unwrap(endpoint, data)

    AnyQuery::Config.logger.debug "Responded with #{data&.size || 0} records"
    break if !data || data.empty?

    previous_count = results.size
    results += data
    break if results.size == previous_count

    break if endpoint.dig(:options, :pagination, :type) == :none
  end
  results.to_a
end

#run_http_request(endpoint, url, params) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



168
169
170
171
172
173
174
175
176
177
178
# File 'lib/any_query/adapters/http.rb', line 168

def run_http_request(endpoint, url, params)
  response = HTTParty.public_send(endpoint[:method], url, params)

  raise response.inspect unless response.success?

  if response.parsed_response.is_a?(Array)
    response.parsed_response.map(&:deep_symbolize_keys)
  else
    response.parsed_response&.deep_symbolize_keys
  end
end

#run_http_single_query(id, params) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



80
81
82
83
84
85
86
87
88
89
90
# File 'lib/any_query/adapters/http.rb', line 80

def run_http_single_query(id, params)
  endpoint = @config[:endpoints][:show]
  url = build_url(endpoint, params, id:)
  params = (endpoint[:options][:default_params] || {}).merge(params)
  AnyQuery::Config.logger.debug "Starting request to #{url} with params #{params.inspect}"

  data = run_http_request(endpoint, url, params)
  data = data.dig(*endpoint[:options][:wrapper]) if endpoint[:options][:wrapper]
  AnyQuery::Config.logger.debug 'Responded with single record.'
  data
end

#unwrap(endpoint, data) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



144
145
146
147
# File 'lib/any_query/adapters/http.rb', line 144

def unwrap(endpoint, data)
  data = unwrap_list(endpoint, data)
  unwrap_single(endpoint, data)
end

#unwrap_list(endpoint, data) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



149
150
151
152
153
154
155
156
157
158
# File 'lib/any_query/adapters/http.rb', line 149

def unwrap_list(endpoint, data)
  wrapper = endpoint[:options][:wrapper]
  return data unless wrapper

  if wrapper.is_a?(Proc)
    wrapper.call(data)
  else
    data.dig(*wrapper)
  end
end

#unwrap_single(endpoint, data) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



160
161
162
163
164
165
166
# File 'lib/any_query/adapters/http.rb', line 160

def unwrap_single(endpoint, data)
  return data unless endpoint[:options][:single_wrapper]

  data.map! do |row|
    row.dig(*endpoint[:options][:single_wrapper])
  end
end