Class: SecureNative::HttpClient

Inherits:
Object
  • Object
show all
Defined in:
lib/securenative/http_client.rb

Constant Summary collapse

AUTHORIZATION_HEADER =
'Authorization'
VERSION_HEADER =
'SN-Version'
USER_AGENT_HEADER =
'User-Agent'
USER_AGENT_HEADER_VALUE =
'SecureNative-ruby'
CONTENT_TYPE_HEADER =
'Content-Type'
CONTENT_TYPE_HEADER_VALUE =
'application/json'

Instance Method Summary collapse

Constructor Details

#initialize(securenative_options) ⇒ HttpClient

Returns a new instance of HttpClient.



12
13
14
# File 'lib/securenative/http_client.rb', line 12

def initialize(securenative_options)
  @options = securenative_options
end

Instance Method Details

#_headersObject



16
17
18
19
20
21
22
23
# File 'lib/securenative/http_client.rb', line 16

def _headers
  {
    CONTENT_TYPE_HEADER => CONTENT_TYPE_HEADER_VALUE,
    USER_AGENT_HEADER => USER_AGENT_HEADER_VALUE,
    VERSION_HEADER => SecureNative::Utils::VersionUtils.version,
    AUTHORIZATION_HEADER => @options.api_key
  }
end

#post(path, body) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/securenative/http_client.rb', line 25

def post(path, body)
  uri = URI.parse("#{@options.api_url}/#{path}")
  headers = _headers

  client = Net::HTTP.new(uri.host, uri.port)
  client.read_timeout = @options.timeout / 1000
  client.use_ssl = true
  client.verify_mode = OpenSSL::SSL::VERIFY_NONE

  request = Net::HTTP::Post.new(uri.request_uri, headers)
  request.body = body

  res = nil
  begin
    res = client.request(request)
  rescue StandardError => e
    SecureNative::Log.error("Failed to send request; #{e}")
    return res
  end
  res
end