Class: Etl::Integrations::Destination::Airtable::Client

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

Overview

rubocop:disable Metrics/ClassLength

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/etl/integrations/destination/airtable/client.rb', line 12

def check_connection(connection_config)
  connection_config = connection_config.with_indifferent_access
  bases = Etl::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
# File 'lib/etl/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 = Etl::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 = Etl::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_multiwoven_message
rescue StandardError => e
  handle_exception("AIRTABLE:DISCOVER:EXCEPTION", "error", e)
end

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



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
85
86
# File 'lib/etl/integrations/destination/airtable/client.rb', line 55

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
  write_success = 0
  write_failure = 0
  records.each_slice(MAX_CHUNK_SIZE) do |chunk|
    payload = create_payload(chunk)
    response = Etl::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
  rescue StandardError => e
    handle_exception("AIRTABLE:RECORD:WRITE:EXCEPTION", "error", e)
    write_failure += chunk.size
  end

  tracker = Etl::Integrations::Protocol::TrackingMessage.new(
    success: write_success,
    failed: write_failure
  )
  tracker.to_multiwoven_message
rescue StandardError => e
  handle_exception("AIRTABLE:WRITE:EXCEPTION", "error", e)
end