-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathapi_client.rb
More file actions
48 lines (36 loc) · 1.21 KB
/
api_client.rb
File metadata and controls
48 lines (36 loc) · 1.21 KB
1
2
3
4
5
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# frozen_string_literal: true
require 'logger'
module Flagsmith
# Ruby client for flagsmith.com
class ApiClient
extend Forwardable
HTTP_METHODS_ALLOW_LIST = %i[get post].freeze
delegate HTTP_METHODS_ALLOW_LIST => :@conn
def initialize(config)
@conn = Faraday.new(url: config.api_url) do |f|
build_headers(f, config)
f.response :json, parser_options: { symbolize_names: true }
f.adapter Faraday.default_adapter
f.options.timeout = config.request_timeout_seconds
configure_logger(f, config)
configure_retries(f, config)
end
freeze
end
private
def build_headers(faraday, config)
faraday.headers['Accept'] = 'application/json'
faraday.headers['Content-Type'] = 'application/json'
faraday.headers['X-Environment-Key'] = config.environment_key
faraday.headers['User-Agent'] = Flagsmith::SDK::Utils.user_agent
faraday.headers.merge(config.custom_headers)
end
def configure_logger(faraday, config)
faraday.response :logger, config.logger
end
def configure_retries(faraday, config)
return unless config.retries
faraday.request :retry, { max: config.retries }
end
end
end