Class: Injest::HttpClient

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configuration) ⇒ HttpClient

Returns a new instance of HttpClient.



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/injest/http_client.rb', line 11

def initialize(configuration)
  @root_url = configuration.injest_root
  @client_code = configuration.injest_client
  if client_code.nil? || client_code == ''
    @push_url = [root_url, 'api/v1/logs'].join('/')
  else
    @push_url = [root_url, 'api/v1/logs', client_code].join('/')
  end
  @search_url = [root_url, 'api/v1/logs/search'].join('/')
  @raw_search_url = [root_url, 'api/v1/logs/raw_search'].join('/')
  @jwt = configuration.injest_token
end

Instance Attribute Details

#client_codeObject (readonly)

Returns the value of attribute client_code.



5
6
7
# File 'lib/injest/http_client.rb', line 5

def client_code
  @client_code
end

#jwtObject (readonly)

Returns the value of attribute jwt.



5
6
7
# File 'lib/injest/http_client.rb', line 5

def jwt
  @jwt
end

#push_urlObject (readonly)

Returns the value of attribute push_url.



5
6
7
# File 'lib/injest/http_client.rb', line 5

def push_url
  @push_url
end

#raw_search_urlObject (readonly)

Returns the value of attribute raw_search_url.



5
6
7
# File 'lib/injest/http_client.rb', line 5

def raw_search_url
  @raw_search_url
end

#root_urlObject (readonly)

Returns the value of attribute root_url.



5
6
7
# File 'lib/injest/http_client.rb', line 5

def root_url
  @root_url
end

#search_urlObject (readonly)

Returns the value of attribute search_url.



5
6
7
# File 'lib/injest/http_client.rb', line 5

def search_url
  @search_url
end

Instance Method Details

#audit(data) ⇒ Object

Push an audit log



25
26
27
# File 'lib/injest/http_client.rb', line 25

def audit(data)
  push data.merge(log_type: 'audit')
end

#json(time:, severity:, data:, correlation_id: nil, request_id: nil, partition_key: nil, owner: nil, delta: nil) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/injest/http_client.rb', line 55

def json(time:, severity:, data:,
         correlation_id: nil,
         request_id: nil,
         partition_key: nil,
         owner: nil,
         delta: nil)
  request_id ||= SecureRandom.uuid
  data = {
    request_id: request_id,
    correlation_id: correlation_id,
    time: time,
    human_time: Time.at(time).to_s,
    log_type: 'json',
    severity: severity,
    partition_key: partition_key,
    owner: owner,
    delta: delta,
    data: data_as_hash(data)
  }
  create_log data
end

#process_response!(response) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/injest/http_client.rb', line 99

def process_response!(response)
  case response.code
  when '200', '201'
    unless response.body.empty?
      JSON.parse(response.body)
    else
      {}
    end
  else
    data = JSON.parse(response.body) unless response.body.blank?
    puts "Endpoint replied with #{response.code} - #{data['error_code']}", data: data
    raise "Endpoint replied with #{response.code} - #{data['error_code']}"
  end
end

#push(data) ⇒ Object

Push a log



50
51
52
53
# File 'lib/injest/http_client.rb', line 50

def push(data)
  data = data_as_hash(data)
  create_log(data)
end

#raw_search(body:, index_postfix: nil) ⇒ Object



42
43
44
45
46
47
# File 'lib/injest/http_client.rb', line 42

def raw_search(body:, index_postfix: nil)
  payload = { body: body }
  payload[:index_postfix] = index_postfix unless index_postfix.nil?

  post_http_request raw_search_url, payload
end

#search(page: 1, per: 20, **other) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/injest/http_client.rb', line 29

def search(page: 1, per: 20, **other)
  payload = {
    page: page,
    per: per,
    logs_search_form: {}
  }
  other.each do |k, v|
    payload[:logs_search_form][k] = v
  end

  post_http_request search_url, payload
end

#text(time:, severity:, message:, correlation_id: nil, request_id: nil, partition_key: nil, owner: nil, delta: nil) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/injest/http_client.rb', line 77

def text(time:, severity:, message:,
         correlation_id: nil,
         request_id: nil,
         partition_key: nil,
         owner: nil,
         delta: nil)
  request_id ||= SecureRandom.uuid
  data = {
    request_id: request_id,
    correlation_id: correlation_id,
    time: time,
    human_time: Time.at(time).to_s,
    log_type: 'text',
    severity: severity,
    partition_key: partition_key,
    owner: owner,
    delta: delta,
    text: message
  }
  create_log data
end