Class: Conductor::Http::RestClient
- Inherits:
-
Object
- Object
- Conductor::Http::RestClient
- Defined in:
- lib/conductor/http/rest_client.rb
Overview
Faraday-based REST client with HTTP/2 support
Instance Attribute Summary collapse
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
Instance Method Summary collapse
- #close ⇒ Object
- #delete(url, body: nil, query: nil, headers: nil) ⇒ Object
-
#get(url, query: nil, headers: nil) ⇒ Object
Convenience methods.
- #head(url, query: nil, headers: nil) ⇒ Object
-
#initialize(configuration = nil, logger: nil) ⇒ RestClient
constructor
A new instance of RestClient.
- #options(url, body: nil, query: nil, headers: nil) ⇒ Object
- #patch(url, body: nil, query: nil, headers: nil) ⇒ Object
- #post(url, body: nil, query: nil, headers: nil) ⇒ Object
- #put(url, body: nil, query: nil, headers: nil) ⇒ Object
-
#request(method, url, query: nil, headers: nil, body: nil, metric_uri: nil) ⇒ Object
Main request method.
Constructor Details
#initialize(configuration = nil, logger: nil) ⇒ RestClient
Returns a new instance of RestClient.
16 17 18 19 20 |
# File 'lib/conductor/http/rest_client.rb', line 16 def initialize(configuration = nil, logger: nil) @configuration = configuration @logger = logger || Logger.new(File::NULL) @connection = build_connection end |
Instance Attribute Details
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
14 15 16 |
# File 'lib/conductor/http/rest_client.rb', line 14 def connection @connection end |
Instance Method Details
#close ⇒ Object
85 86 87 |
# File 'lib/conductor/http/rest_client.rb', line 85 def close @connection&.close end |
#delete(url, body: nil, query: nil, headers: nil) ⇒ Object
73 74 75 |
# File 'lib/conductor/http/rest_client.rb', line 73 def delete(url, body: nil, query: nil, headers: nil) request('DELETE', url, query: query, headers: headers, body: body) end |
#get(url, query: nil, headers: nil) ⇒ Object
Convenience methods
57 58 59 |
# File 'lib/conductor/http/rest_client.rb', line 57 def get(url, query: nil, headers: nil) request('GET', url, query: query, headers: headers) end |
#head(url, query: nil, headers: nil) ⇒ Object
77 78 79 |
# File 'lib/conductor/http/rest_client.rb', line 77 def head(url, query: nil, headers: nil) request('HEAD', url, query: query, headers: headers) end |
#options(url, body: nil, query: nil, headers: nil) ⇒ Object
81 82 83 |
# File 'lib/conductor/http/rest_client.rb', line 81 def (url, body: nil, query: nil, headers: nil) request('OPTIONS', url, query: query, headers: headers, body: body) end |
#patch(url, body: nil, query: nil, headers: nil) ⇒ Object
69 70 71 |
# File 'lib/conductor/http/rest_client.rb', line 69 def patch(url, body: nil, query: nil, headers: nil) request('PATCH', url, query: query, headers: headers, body: body) end |
#post(url, body: nil, query: nil, headers: nil) ⇒ Object
61 62 63 |
# File 'lib/conductor/http/rest_client.rb', line 61 def post(url, body: nil, query: nil, headers: nil) request('POST', url, query: query, headers: headers, body: body) end |
#put(url, body: nil, query: nil, headers: nil) ⇒ Object
65 66 67 |
# File 'lib/conductor/http/rest_client.rb', line 65 def put(url, body: nil, query: nil, headers: nil) request('PUT', url, query: query, headers: headers, body: body) end |
#request(method, url, query: nil, headers: nil, body: nil, metric_uri: nil) ⇒ Object
Main request method
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/conductor/http/rest_client.rb', line 23 def request(method, url, query: nil, headers: nil, body: nil, metric_uri: nil) method = method.to_s.upcase raise ArgumentError, "Invalid HTTP method: #{method}" unless valid_method?(method) headers ||= {} headers['Content-Type'] ||= 'application/json' if %w[POST PUT PATCH DELETE OPTIONS].include?(method) timing = http_metrics_enabled? start_time = Time.now if timing status_code = '0' begin response = @connection.run_request(method.downcase.to_sym, url, nil, headers) do |req| req.params = query if query req.body = serialize_body(body, headers['Content-Type']) if body end status_code = response.status.to_s result = handle_response(response) emit_http_event(method, url, status_code, start_time, metric_uri: metric_uri) if timing result rescue Faraday::TimeoutError => e emit_http_event(method, url, '0', start_time, metric_uri: metric_uri) if timing raise ApiError.new("Request timeout: #{e.}", status: 0, reason: 'Timeout') rescue Faraday::ConnectionFailed => e emit_http_event(method, url, '0', start_time, metric_uri: metric_uri) if timing raise ApiError.new("Connection error: #{e.}", status: 0, reason: 'ConnectionFailed') rescue ApiError, AuthorizationError emit_http_event(method, url, status_code, start_time, metric_uri: metric_uri) if timing raise end end |