class Multiwoven::Integrations::Destination::FacebookCustomAudience::Client

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

def write(sync_config, records, _action = "insert")
  connection_config = sync_config.destination.connection_specification.with_indifferent_access
  access_token = connection_config[:access_token]
  url = generate_url(sync_config, connection_config)
  write_success = 0
  write_failure = 0
  records.each_slice(MAX_CHUNK_SIZE) do |chunk|
    payload = create_payload(chunk, sync_config.stream.json_schema.with_indifferent_access)
    response = Multiwoven::Integrations::Core::HttpClient.request(
      url,
      sync_config.stream.request_method,
      payload: payload,
      headers: auth_headers(access_token)
    )
    if success?(response)
      write_success += chunk.size
    else
      write_failure += chunk.size
    end
  rescue StandardError => e
    handle_exception("FACEBOOK:RECORD:WRITE:EXCEPTION", "error", e)
    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("FACEBOOK:WRITE:EXCEPTION", "error", e)
end