class Multiwoven::Integrations::Destination::Airtable::Client

def write(sync_config, records, _action = "create")

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 = Multiwoven::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(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
  end
  tracker = Multiwoven::Integrations::Protocol::TrackingMessage.new(
    success: write_success,
    failed: write_failure
  )
  tracker.to_multiwoven_message
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