Class: EventHookClientServer::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/event_hook_client_server/client.rb,
lib/event_hook_client_server/client/response.rb,
lib/event_hook_client_server/client/models/error.rb,
lib/event_hook_client_server/client/models/integration.rb,
lib/event_hook_client_server/client/models/integrations/binding.rb

Defined Under Namespace

Modules: Integrations Classes: Error, Integration, Response

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Client

Returns a new instance of Client.



77
78
79
80
81
82
# File 'lib/event_hook_client_server/client.rb', line 77

def initialize(options)
  @options = self.class.default_config.merge(options)

  refresh_hosts!
  recreate_grpc_client!
end

Instance Attribute Details

#grpc_clientObject (readonly)

Returns the value of attribute grpc_client.



75
76
77
# File 'lib/event_hook_client_server/client.rb', line 75

def grpc_client
  @grpc_client
end

#hostsObject (readonly)

Returns the value of attribute hosts.



72
73
74
# File 'lib/event_hook_client_server/client.rb', line 72

def hosts
  @hosts
end

#hosts_expire_atObject (readonly)

Returns the value of attribute hosts_expire_at.



73
74
75
# File 'lib/event_hook_client_server/client.rb', line 73

def hosts_expire_at
  @hosts_expire_at
end

#optionsObject (readonly)

Returns the value of attribute options.



74
75
76
# File 'lib/event_hook_client_server/client.rb', line 74

def options
  @options
end

Class Method Details

.defaultObject



60
61
62
# File 'lib/event_hook_client_server/client.rb', line 60

def self.default
  @default ||= new(default_config)
end

.default_configObject



64
65
66
67
68
69
70
# File 'lib/event_hook_client_server/client.rb', line 64

def self.default_config
  @default_config ||= DEFAULT_CONFIG.dup.tap do |config|
    if defined?(ElasticAPM::GRPC::ClientInterceptor)
      config[:interceptors] << ElasticAPM::GRPC::ClientInterceptor.new
    end
  end
end

Instance Method Details

#create_integration(params: {}) ⇒ Object

CREATE



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/event_hook_client_server/client.rb', line 173

def create_integration(params: {})
  params = symbolize_keys(params)
  params.transmute(EventHookClientServer::Grpc::StructEncoder,
                   %I[configuration params])

  request = V1::Messages::IntegrationCreateRequest.new(input: params)

  response = retry_on_failure { grpc_client.create_integration(request) }

  if response.errors.any?
    Response.fail!(response.errors.map { |e| Error.new(e.to_h) })
  else
    Response.success!(data: Integration.new(response.integration.to_h))
  end
end

#destroy_integration(id:) ⇒ Object

DESTROY



207
208
209
210
211
212
213
214
215
216
217
# File 'lib/event_hook_client_server/client.rb', line 207

def destroy_integration(id:)
  request = V1::Messages::IntegrationDestroyRequest.new(id: id.to_s)

  response = retry_on_failure { grpc_client.destroy_integration(request) }

  if response.errors.any?
    Response.fail!([])
  else
    Response.success!(data: true)
  end
end

#find_integration_from_headers(headers: nil, cursor: nil) ⇒ Object

FIND INTEGRATION FOR HEADERS



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/event_hook_client_server/client.rb', line 112

def find_integration_from_headers(headers: nil, cursor: nil)
  request = V1::Messages::FindIntegrationsFromHeadersRequest.new(
    headers: EventHookClientServer::Grpc::StructEncoder.call(headers),
    pagination: { cursor: cursor }
  )

  response = retry_on_failure do
    grpc_client.find_integration_from_headers(request)
  end

  integrations = response.integrations.map do |integration|
    Integration.new(integration.to_h)
  end

  Response.success!(data: integrations,
                    pagination: response.pagination.to_h)
end

#integration(id) ⇒ Object

SHOW



131
132
133
134
135
136
137
138
139
140
141
# File 'lib/event_hook_client_server/client.rb', line 131

def integration(id)
  request = V1::Messages::GetIntegrationRequest.new(id: id.to_s)

  response = retry_on_failure { grpc_client.integration(request) }

  if response.errors.any?
    Response.fail!(response.errors.map { |e| Error.new(e.to_h) })
  else
    Response.success!(data: Integration.new(response.integration.to_h))
  end
end

#integrations(query_params: nil, cursor: nil, per_page: nil) ⇒ Object

INDEX



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/event_hook_client_server/client.rb', line 144

def integrations(query_params: nil, cursor: nil, per_page: nil)
  per_page =
    if per_page.to_i.zero? || per_page.to_i.negative?
      10
    else
      per_page.to_i
    end

  pagination = { cursor: cursor.to_s, per_page: per_page }

  query_params&.each_with_object(query_params) do |(key, value), hash|
    hash[key] = { value: value.to_s } unless value.nil?
  end

  request =
    V1::Messages::ListIntegrationsRequest.new(query_params: query_params,
                                              pagination: pagination)

  response = retry_on_failure { grpc_client.integrations(request) }

  integrations = response.integrations.map do |integration|
    Integration.new(integration.to_h)
  end

  Response.success!(data: integrations,
                    pagination: response.pagination.to_h)
end

#recreate_grpc_client!Object



101
102
103
104
105
106
107
108
109
# File 'lib/event_hook_client_server/client.rb', line 101

def recreate_grpc_client!
  grpc_options = options.reject { |k, _| NON_STUB_OPTIONS.include?(k) }

  @grpc_client = GRPC_CLIENT_CLASS.new(
    hosts.next,
    options[:channel_mode],
    **grpc_options
  )
end

#refresh_hosts!Object



89
90
91
92
93
94
95
96
97
98
99
# File 'lib/event_hook_client_server/client.rb', line 89

def refresh_hosts!
  return if !hosts_expire_at.nil? && Time.now.to_i < hosts_expire_at

  result = fetch_srv_host_record
  @hosts_expire_at = Time.now.to_i + (result.map(&:ttl).min || 60)

  hosts = result.map { |r| [r.target&.to_s, r.port].compact.join(':') }
  hosts = [options[:host]].compact if hosts.empty?

  @hosts = hosts.each.cycle
end

#rotate_host!Object



84
85
86
87
# File 'lib/event_hook_client_server/client.rb', line 84

def rotate_host!
  refresh_hosts!
  recreate_grpc_client!
end

#update_integration(id:, params: {}) ⇒ Object

UPDATE



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/event_hook_client_server/client.rb', line 190

def update_integration(id:, params: {})
  params = symbolize_keys(params)
  params.transmute(EventHookClientServer::Grpc::StructEncoder,
                   %I[configuration params])

  request = V1::Messages::IntegrationUpdateRequest.new(id: id.to_s,
                                                       input: params)
  response = retry_on_failure { grpc_client.update_integration(request) }

  if response.errors.any?
    Response.fail!(response.errors.map { |e| Error.new(e.to_h) })
  else
    Response.success!(data: Integration.new(response.integration.to_h))
  end
end