-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathclient.rb
More file actions
87 lines (69 loc) · 3.03 KB
/
client.rb
File metadata and controls
87 lines (69 loc) · 3.03 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
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
# frozen_string_literal: true
module SplitIoClient
module Api
class Client
def initialize(config)
@config = config
check_faraday_compatibility
end
def get_api(url, api_key, params = {}, cache_control_headers = false)
api_client.options.params_encoder.sort_params = false
api_client.get(url, params) do |req|
req.headers = common_headers(api_key).merge('Accept-Encoding' => 'gzip')
req.headers = req.headers.merge('Cache-Control' => 'no-cache') if cache_control_headers
req.options[:timeout] = @config.read_timeout
req.options[:open_timeout] = @config.connection_timeout
@config.split_logger.log_if_debug("GET #{url} proxy: #{api_client.proxy}")
end
rescue Zlib::GzipFile::Error => e
@config.logger.warn("Incorrect formatted response exception: #{e}\n")
SplitIoClient::Engine::Models::SplitHttpResponse.new(400, '', true)
rescue StandardError => e
@config.logger.warn("#{e}\nURL:#{url}\nparams:#{params}")
raise e, 'Split SDK failed to connect to backend to retrieve information', e.backtrace
end
def post_api(url, api_key, data, headers = {}, params = {})
api_client.post(url) do |req|
req.headers = common_headers(api_key)
.merge('Content-Type' => 'application/json')
.merge(headers)
machine_ip = @config.machine_ip
machine_name = @config.machine_name
req.headers = req.headers.merge('SplitSDKMachineIP' => machine_ip) unless machine_ip.empty? || machine_ip == 'unknown'
req.headers = req.headers.merge('SplitSDKMachineName' => machine_name) unless machine_name.empty? || machine_name == 'unknown'
req.body = data.to_json
req.options[:timeout] = @config.read_timeout
req.options[:open_timeout] = @config.connection_timeout
@config.split_logger.log_if_transport("POST #{url} #{req.body}")
@config.split_logger.log_if_debug("POST #{url}")
end
rescue StandardError => e
@config.logger.warn("#{e}\nURL:#{url}\ndata:#{data}\nparams:#{params}")
raise e, 'Split SDK failed to connect to backend to post information', e.backtrace
end
def sdk_url_overriden?
@config.sdk_url_overriden?
end
private
def api_client
@api_client ||= Faraday.new do |builder|
builder.use SplitIoClient::FaradayMiddleware::Gzip
builder.adapter :net_http_persistent
builder.options.params_encoder = Faraday::FlatParamsEncoder
end
end
def common_headers(api_key)
{
'Authorization' => "Bearer #{api_key}",
'SplitSDKVersion' => "#{@config.language}-#{@config.version}",
}
end
def check_faraday_compatibility
version = Faraday::VERSION.split('.')[0]
require 'faraday/net_http_persistent' if version.to_i >= 2
rescue StandardError => e
@config.logger.warn(e)
end
end
end
end