Class: Antbird::Client
- Inherits:
-
Object
- Object
- Antbird::Client
- Defined in:
- lib/antbird/client.rb,
lib/antbird/client/errors.rb
Defined Under Namespace
Classes: ApiError, Error, RequestError, ServerError
Instance Attribute Summary collapse
-
#adapter ⇒ Object
readonly
Returns the value of attribute adapter.
-
#api_specs ⇒ Object
readonly
Returns the value of attribute api_specs.
-
#distribution ⇒ Object
readonly
Returns the value of attribute distribution.
-
#last_request ⇒ Object
readonly
Returns the value of attribute last_request.
-
#open_timeout ⇒ Object
readonly
Returns the value of attribute open_timeout.
-
#read_timeout ⇒ Object
readonly
Returns the value of attribute read_timeout.
-
#scope ⇒ Object
readonly
Returns the value of attribute scope.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Instance Method Summary collapse
- #connection ⇒ Object
- #elasticsearch? ⇒ Boolean
- #elasticsearch_v7_0_compatible? ⇒ Boolean
- #elasticsearch_v7_6_compatible? ⇒ Boolean
- #extract_body(params) ⇒ Object
- #extract_method(params) ⇒ Object
- #extract_scopes(params) ⇒ Object
-
#initialize(scope: {}, url: "http://localhost:9200", version: nil, distribution: nil, read_timeout: 5, open_timeout: 2, adapter: ::Faraday.default_adapter, &block) ⇒ Client
constructor
A new instance of Client.
- #opensearch? ⇒ Boolean
- #request(api_name, api_spec, params) ⇒ Object
- #scoped(new_scope = {}) ⇒ Object
- #version ⇒ Object
Constructor Details
#initialize(scope: {}, url: "http://localhost:9200", version: nil, distribution: nil, read_timeout: 5, open_timeout: 2, adapter: ::Faraday.default_adapter, &block) ⇒ Client
Returns a new instance of Client.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/antbird/client.rb', line 6 def initialize( scope: {}, url: "http://localhost:9200", version: nil, distribution: nil, read_timeout: 5, open_timeout: 2, adapter: ::Faraday.default_adapter, &block) @read_timeout = read_timeout @open_timeout = open_timeout @adapter = adapter @block = block @url = url @scope = scope.transform_keys(&:to_sym) if version @version = version @distribution = distribution end @api_specs = {} end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object (private)
295 296 297 298 299 300 301 302 |
# File 'lib/antbird/client.rb', line 295 def method_missing(name, *args, &block) return super if api_specs_loaded? ensure_api_spec_loaded return super unless respond_to?(name) __send__(name, *args, &block) end |
Instance Attribute Details
#adapter ⇒ Object (readonly)
Returns the value of attribute adapter.
32 33 34 |
# File 'lib/antbird/client.rb', line 32 def adapter @adapter end |
#api_specs ⇒ Object (readonly)
Returns the value of attribute api_specs.
33 34 35 |
# File 'lib/antbird/client.rb', line 33 def api_specs @api_specs end |
#distribution ⇒ Object (readonly)
Returns the value of attribute distribution.
31 32 33 |
# File 'lib/antbird/client.rb', line 31 def distribution @distribution end |
#last_request ⇒ Object (readonly)
Returns the value of attribute last_request.
33 34 35 |
# File 'lib/antbird/client.rb', line 33 def last_request @last_request end |
#open_timeout ⇒ Object (readonly)
Returns the value of attribute open_timeout.
32 33 34 |
# File 'lib/antbird/client.rb', line 32 def open_timeout @open_timeout end |
#read_timeout ⇒ Object (readonly)
Returns the value of attribute read_timeout.
32 33 34 |
# File 'lib/antbird/client.rb', line 32 def read_timeout @read_timeout end |
#scope ⇒ Object (readonly)
Returns the value of attribute scope.
31 32 33 |
# File 'lib/antbird/client.rb', line 31 def scope @scope end |
#url ⇒ Object (readonly)
Returns the value of attribute url.
31 32 33 |
# File 'lib/antbird/client.rb', line 31 def url @url end |
Instance Method Details
#connection ⇒ Object
184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/antbird/client.rb', line 184 def connection @connection ||= Faraday.new(url) do |conn| @block&.call(conn) conn.request :json conn.response :json, content_type: /\bjson$/ conn.[:timeout] = read_timeout conn.[:open_timeout] = open_timeout conn.adapter adapter end end |
#elasticsearch? ⇒ Boolean
198 199 200 |
# File 'lib/antbird/client.rb', line 198 def elasticsearch? !opensearch? end |
#elasticsearch_v7_0_compatible? ⇒ Boolean
208 209 210 211 212 |
# File 'lib/antbird/client.rb', line 208 def elasticsearch_v7_0_compatible? return true if opensearch? elasticsearch? && Gem::Version.new(version) >= Gem::Version.new('7.0.0') end |
#elasticsearch_v7_6_compatible? ⇒ Boolean
214 215 216 217 218 |
# File 'lib/antbird/client.rb', line 214 def elasticsearch_v7_6_compatible? return true if opensearch? elasticsearch? && Gem::Version.new(version) >= Gem::Version.new('7.6.0') end |
#extract_body(params) ⇒ Object
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/antbird/client.rb', line 161 def extract_body(params) body = params.delete :body return if body.nil? body = case body when String body when Array if body.all? { |b| b.is_a?(Hash) } body.map { |b| JSON.dump(b) }.join("\n") + "\n" else body << nil unless body.last.nil? body.join "\n" end else JSON.dump(body) end # Prevent excon from changing the encoding (see https://github.com/github/elastomer-client/issues/138) body.freeze end |
#extract_method(params) ⇒ Object
147 148 149 |
# File 'lib/antbird/client.rb', line 147 def extract_method(params) params.delete(:method)&.to_sym end |
#extract_scopes(params) ⇒ Object
151 152 153 154 155 156 157 158 159 |
# File 'lib/antbird/client.rb', line 151 def extract_scopes(params) scopes = {} [:index, :type, :id].each do |s| scope = params.delete(s) next unless scope scopes[s] = scope end scopes end |
#opensearch? ⇒ Boolean
202 203 204 205 206 |
# File 'lib/antbird/client.rb', line 202 def opensearch? ensure_version_and_distribution distribution == 'opensearch' end |
#request(api_name, api_spec, params) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 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 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/antbird/client.rb', line 46 def request(api_name, api_spec, params) validate_params(api_spec, params) body = extract_body(params) scopes = extract_scopes(params) forced_method = extract_method(params) # Greedy match api_path = nil api_path_template = nil path_methods = nil sort_url_paths(api_spec['url']['paths']).each do |path| if path.is_a?(Hash) path_methods = path['methods'] path = path['path'] end = path.gsub(/{([a-z_\-}]+)}/) do |match| scopes[$1.to_sym] || @scope[$1.to_sym] || match end unless .include?('{') api_path_template = path api_path = break end end unless api_path raise "API path not found: paths: #{api_spec['url']['paths']}, scope: #{@scope}" end methods = (path_methods || api_spec['methods']).map { |m| m.downcase.to_sym } method = if forced_method forced_method elsif methods.include?(:put) :put elsif methods.include?(:post) :post else methods.first end read_timeout = params.delete(:read_timeout) params.reject! { |_, v| v.nil? } @last_request = { method: method, forced_method: forced_method, api_path: api_path, api_path_template: api_path_template, params: params } response = case method when :head connection.head(api_path) do |req| req.params = params unless params.empty? req.[:timeout] = read_timeout if read_timeout end when :get connection.get(api_path) do |req| req.params = params unless params.empty? req.body = body if body req.[:timeout] = read_timeout if read_timeout end when :put connection.put(api_path, body) do |req| req.params = params unless params.empty? req.[:timeout] = read_timeout if read_timeout end when :post connection.post(api_path, body) do |req| req.params = params unless params.empty? req.[:timeout] = read_timeout if read_timeout end when :delete connection.delete(api_path) do |req| req.params = params unless params.empty? req.body = body if body req.[:timeout] = read_timeout if read_timeout end else raise ArgumentError, "Unknown HTTP request method: #{method.inspect}" end if method == :head case response.status when 200 return true when 404 return false end end handle_errors!(response) response.body end |
#scoped(new_scope = {}) ⇒ Object
35 36 37 38 39 40 41 42 43 44 |
# File 'lib/antbird/client.rb', line 35 def scoped(new_scope = {}) Client.new( scope: new_scope, url: url, version: version, distribution: distribution, read_timeout: read_timeout, open_timeout: open_timeout ) end |
#version ⇒ Object
220 221 222 223 224 225 |
# File 'lib/antbird/client.rb', line 220 def version return @version if @version ensure_version_and_distribution @version end |