Class: Outhad::Integrations::Destination::Airtable::Client

Inherits:
DestinationConnector
  • Object
show all
Includes:
Core::RateLimiter
Defined in:
lib/outhad/integrations/destination/airtable/client.rb

Constant Summary collapse

MAX_CHUNK_SIZE =
10

Instance Method Summary collapse

Instance Method Details

#check_connection(connection_config) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/outhad/integrations/destination/airtable/client.rb', line 12

def check_connection(connection_config)
  connection_config = connection_config.with_indifferent_access
  bases = Outhad::Integrations::Core::HttpClient.request(
    AIRTABLE_BASES_ENDPOINT,
    HTTP_GET,
    headers: auth_headers(connection_config[:api_key])
  )
  if success?(bases)
    base_id_exists?(bases, connection_config[:base_id])
    success_status
  else
    failure_status(nil)
  end
rescue StandardError => e
  failure_status(e)
end

#discover(connection_config) ⇒ Object



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

def discover(connection_config)
  connection_config = connection_config.with_indifferent_access
  base_id = connection_config[:base_id]
  api_key = connection_config[:api_key]

  bases = Outhad::Integrations::Core::HttpClient.request(
    AIRTABLE_BASES_ENDPOINT,
    HTTP_GET,
    headers: auth_headers(api_key)
  )

  base = extract_bases(bases).find { |b| b["id"] == base_id }
  base_name = base["name"]

  schema = Outhad::Integrations::Core::HttpClient.request(
    AIRTABLE_GET_BASE_SCHEMA_ENDPOINT.gsub("{baseId}", base_id),
    HTTP_GET,
    headers: auth_headers(api_key)
  )

  catalog = build_catalog_from_schema(extract_body(schema), base_id, base_name)
  catalog.to_outhad_message
rescue StandardError => e
  handle_exception(e, {
                     context: "AIRTABLE:DISCOVER:EXCEPTION",
                     type: "error"
                   })
end

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



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
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/outhad/integrations/destination/airtable/client.rb', line 58

def write(sync_config, records, _action = "create")
  connection_config = sync_config.destination.connection_specification.with_indifferent_access
  api_key = connection_config[:api_key]
  url = sync_config.stream.url
  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: auth_headers(api_key)
    )
    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: "AIRTABLE: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: "AIRTABLE:RECORD:WRITE:EXCEPTION",
                     type: "error",
                     sync_id: sync_config.sync_id,
                     sync_run_id: sync_config.sync_run_id
                   })
end