Class: Outhad::Integrations::Destination::Http::Client

Inherits:
DestinationConnector
  • Object
show all
Defined in:
lib/outhad/integrations/destination/http/client.rb

Constant Summary collapse

MAX_CHUNK_SIZE =
10

Instance Method Summary collapse

Instance Method Details

#check_connection(connection_config) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/outhad/integrations/destination/http/client.rb', line 10

def check_connection(connection_config)
  connection_config = connection_config.with_indifferent_access
  destination_url = connection_config[:destination_url]
  headers = connection_config[:headers]
  request = Outhad::Integrations::Core::HttpClient.request(
    destination_url,
    HTTP_POST,
    payload: {},
    headers: headers
  )
  if success?(request)
    success_status
  else
    failure_status(nil)
  end
rescue StandardError => e
  handle_exception(e, {
                     context: "HTTP:CHECK_CONNECTION:EXCEPTION",
                     type: "error"
                   })
  failure_status(e)
end

#discover(_connection_config = nil) ⇒ Object



33
34
35
36
37
38
39
40
41
42
# File 'lib/outhad/integrations/destination/http/client.rb', line 33

def discover(_connection_config = nil)
  catalog_json = read_json(CATALOG_SPEC_PATH)
  catalog = build_catalog(catalog_json)
  catalog.to_outhad_message
rescue StandardError => e
  handle_exception(e, {
                     context: "HTTP:DISCOVER:EXCEPTION",
                     type: "error"
                   })
end

#write(sync_config, records, _action = "create") ⇒ Object



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
# File 'lib/outhad/integrations/destination/http/client.rb', line 44

def write(sync_config, records, _action = "create")
  connection_config = sync_config.destination.connection_specification.with_indifferent_access
  url = connection_config[:destination_url]
  headers = connection_config[:headers]
  log_message_array = []
  write_success = 0
  write_failure = 0
  records.each_slice(MAX_CHUNK_SIZE) do |chunk|
    payload = create_payload(chunk)
    args = [sync_config.stream.request_method, url, payload]
    response = Outhad::Integrations::Core::HttpClient.request(
      url,
      sync_config.stream.request_method,
      payload: payload,
      headers: headers
    )
    if success?(response)
      write_success += chunk.size
    else
      write_failure += chunk.size
    end
    log_message_array << log_request_response("info", args, response)
  rescue StandardError => e
    handle_exception(e, {
                       context: "HTTP:RECORD:WRITE:EXCEPTION",
                       type: "error",
                       sync_id: sync_config.sync_id,
                       sync_run_id: sync_config.sync_run_id
                     })
    write_failure += chunk.size
    log_message_array << log_request_response("error", args, e.message)
  end
  tracking_message(write_success, write_failure, log_message_array)
rescue StandardError => e
  handle_exception(e, {
                     context: "HTTP:RECORD:WRITE:EXCEPTION",
                     type: "error",
                     sync_id: sync_config.sync_id,
                     sync_run_id: sync_config.sync_run_id
                   })
end