Class: DruidClient::Api::RestClient

Inherits:
Object
  • Object
show all
Defined in:
lib/druid_client/api/rest_client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ RestClient



12
13
14
15
16
# File 'lib/druid_client/api/rest_client.rb', line 12

def initialize(options = {})
  @options = options
  @url = options[:url]
  @log = options[:log]
end

Instance Attribute Details

#logObject (readonly)

Returns the value of attribute log.



10
11
12
# File 'lib/druid_client/api/rest_client.rb', line 10

def log
  @log
end

#optionsObject (readonly)

Returns the value of attribute options.



10
11
12
# File 'lib/druid_client/api/rest_client.rb', line 10

def options
  @options
end

#urlObject (readonly)

Returns the value of attribute url.



10
11
12
# File 'lib/druid_client/api/rest_client.rb', line 10

def url
  @url
end

Instance Method Details

#base_headersObject



82
83
84
# File 'lib/druid_client/api/rest_client.rb', line 82

def base_headers
  options[:headers] || {}
end

#basic_auth?Boolean



86
87
88
# File 'lib/druid_client/api/rest_client.rb', line 86

def basic_auth?
  basic_username && basic_password
end

#basic_passwordObject



94
95
96
# File 'lib/druid_client/api/rest_client.rb', line 94

def basic_password
  options[:password]
end

#basic_usernameObject



90
91
92
# File 'lib/druid_client/api/rest_client.rb', line 90

def basic_username
  options[:username]
end

#ca_fileObject



106
107
108
# File 'lib/druid_client/api/rest_client.rb', line 106

def ca_file
  options[:ca_file]
end

#ca_pathObject



110
111
112
# File 'lib/druid_client/api/rest_client.rb', line 110

def ca_path
  options[:ca_path]
end

#clientObject



69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/druid_client/api/rest_client.rb', line 69

def client
  @client ||= Faraday.new(client_options) do |conn|
    conn.request :authorization, :basic, basic_username, basic_password if basic_auth?
    conn.request :json
    conn.response :json
    if log
      conn.response :logger, log, headers: options[:log_headers], bodies: options[:log_bodies],
                                  log_level: :debug
    end
    conn.adapter Faraday.default_adapter
  end
end

#client_optionsObject



58
59
60
61
62
63
64
65
66
67
# File 'lib/druid_client/api/rest_client.rb', line 58

def client_options
  {
    url: url,
    headers: { 'User-Agent' => user_agent }.compact,
    request: { timeout: timeout }.compact,
    ssl: { ca_file: ca_file,
           ca_path: ca_path,
           verify: verify_ssl }.compact
  }
end

#delete(path, params: {}, headers: {}) ⇒ Object



34
35
36
# File 'lib/druid_client/api/rest_client.rb', line 34

def delete(path, params: {}, headers: {})
  request(:delete, path, params: params, headers: headers)
end

#get(path, params: {}, headers: {}) ⇒ Object



18
19
20
# File 'lib/druid_client/api/rest_client.rb', line 18

def get(path, params: {}, headers: {})
  request(:get, path, params: params, headers: headers)
end

#patch(path, body: nil, headers: {}) ⇒ Object



30
31
32
# File 'lib/druid_client/api/rest_client.rb', line 30

def patch(path, body: nil, headers: {})
  request(:patch, path, body: body, headers: headers)
end

#post(path, body: nil, headers: {}) ⇒ Object



22
23
24
# File 'lib/druid_client/api/rest_client.rb', line 22

def post(path, body: nil, headers: {})
  request(:post, path, body: body, headers: headers)
end

#put(path, body: nil, headers: {}) ⇒ Object



26
27
28
# File 'lib/druid_client/api/rest_client.rb', line 26

def put(path, body: nil, headers: {})
  request(:put, path, body: body, headers: headers)
end

#request(method, path, body: nil, params: {}, headers: {}) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/druid_client/api/rest_client.rb', line 38

def request(method, path, body: nil, params: {}, headers: {})
  File.join(url, path)
  start_time = Time.now
  response = client.send(method) do |req|
    req.url path
    req.headers.merge!(headers)
    req.body = body.to_json if body
  end
  duration = Time.now - start_time
  Response.new(
    status_code: response.status,
    headers: response.headers,
    body: response.body,
    duration: duration
  )
rescue Faraday::Error => e
  duration = Time.now - start_time
  Response.new(status_code: 0, body: e.message, duration: duration)
end

#timeoutObject



102
103
104
# File 'lib/druid_client/api/rest_client.rb', line 102

def timeout
  options[:timeout]
end

#user_agentObject



98
99
100
# File 'lib/druid_client/api/rest_client.rb', line 98

def user_agent
  options[:user_agent]
end

#verify_sslObject



114
115
116
# File 'lib/druid_client/api/rest_client.rb', line 114

def verify_ssl
  options[:verify_ssl]
end