Class: Attio::Resources::Bulk Private

Inherits:
Base
  • Object
show all
Defined in:
lib/attio/resources/bulk.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Bulk operations for efficient batch processing

Examples:

Bulk create records

client.bulk.create_records(
  object: "companies",
  records: [
    { name: "Acme Corp", domain: "acme.com" },
    { name: "Tech Co", domain: "techco.com" }
  ]
)

Bulk update records

client.bulk.update_records(
  object: "people",
  updates: [
    { id: "person_123", data: { title: "CEO" } },
    { id: "person_456", data: { title: "CTO" } }
  ]
)

Since:

  • 1.0.0

API:

  • private

Constant Summary collapse

MAX_BATCH_SIZE =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Maximum number of records per bulk operation

Since:

  • 1.0.0

API:

  • private

100

Instance Method Summary collapse

Constructor Details

This class inherits a constructor from Attio::Resources::Base

Instance Method Details

#add_list_entries(list_id:, entries:, options: {}) ⇒ Hash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Bulk add entries to a list

Parameters:

  • The list ID

  • Array of entries to add

  • (defaults to: {})

    Additional options

Returns:

  • Results including added entries

Since:

  • 1.0.0

API:

  • private



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/attio/resources/bulk.rb', line 149

def add_list_entries(list_id:, entries:, options: {})
  validate_id!(list_id, "List")
  validate_bulk_records!(entries, "add to list")

  batches = entries.each_slice(MAX_BATCH_SIZE).to_a
  results = []

  batches.each_with_index do |batch, index|
    body = {
      entries: batch,
      partial_success: options.fetch(:partial_success, false),
    }

    result = request(:post, "lists/#{list_id}/entries/bulk", body)
    results << result.merge("batch" => index + 1)
  end

  merge_batch_results(results)
end

#create_records(object:, records:, options: {}) ⇒ Hash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Bulk create multiple records

Parameters:

  • The object type (companies, people, etc.)

  • Array of record data to create

  • (defaults to: {})

    Additional options

Options Hash (options:):

  • :partial_success (Boolean)

    Allow partial success (default: false)

  • :return_records (Boolean)

    Return created records (default: true)

Returns:

  • Results including created records and any errors

Since:

  • 1.0.0

API:

  • private



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/attio/resources/bulk.rb', line 36

def create_records(object:, records:, options: {})
  validate_required_string!(object, "Object type")
  validate_bulk_records!(records, "create")

  batches = records.each_slice(MAX_BATCH_SIZE).to_a
  results = []

  batches.each_with_index do |batch, index|
    body = {
      records: batch.map { |record| { data: record } },
      partial_success: options.fetch(:partial_success, false),
      return_records: options.fetch(:return_records, true),
    }

    result = request(:post, "objects/#{object}/records/bulk", body)
    results << result.merge("batch" => index + 1)
  end

  merge_batch_results(results)
end

#delete_records(object:, ids:, options: {}) ⇒ Hash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Bulk delete multiple records

Parameters:

  • The object type

  • Array of record IDs to delete

  • (defaults to: {})

    Additional options

Options Hash (options:):

  • :partial_success (Boolean)

    Allow partial success (default: false)

Returns:

  • Results including deletion confirmations and any errors

Since:

  • 1.0.0

API:

  • private



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/attio/resources/bulk.rb', line 93

def delete_records(object:, ids:, options: {})
  validate_required_string!(object, "Object type")
  validate_bulk_ids!(ids)

  batches = ids.each_slice(MAX_BATCH_SIZE).to_a
  results = []

  batches.each_with_index do |batch, index|
    body = {
      ids: batch,
      partial_success: options.fetch(:partial_success, false),
    }

    result = request(:delete, "objects/#{object}/records/bulk", body)
    results << result.merge("batch" => index + 1)
  end

  merge_batch_results(results)
end

#initialize_merged_result(batch_count) ⇒ Object (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 1.0.0

API:

  • private



259
260
261
262
263
264
265
266
267
268
269
270
271
272
# File 'lib/attio/resources/bulk.rb', line 259

private def initialize_merged_result(batch_count)
  {
    "success" => true,
    "total_batches" => batch_count,
    "records" => [],
    "errors" => [],
    "statistics" => {
      "created" => 0,
      "updated" => 0,
      "deleted" => 0,
      "failed" => 0,
    },
  }
end

#merge_batch_results(results) ⇒ Object (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 1.0.0

API:

  • private



249
250
251
252
253
254
255
256
257
# File 'lib/attio/resources/bulk.rb', line 249

private def merge_batch_results(results)
  merged = initialize_merged_result(results.size)

  results.each do |result|
    merge_single_result!(merged, result)
  end

  merged
end

#merge_single_result!(merged, result) ⇒ Object (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 1.0.0

API:

  • private



274
275
276
277
278
279
# File 'lib/attio/resources/bulk.rb', line 274

private def merge_single_result!(merged, result)
  merged["records"].concat(result["records"] || [])
  merged["errors"].concat(result["errors"] || [])
  merge_statistics!(merged["statistics"], result["statistics"])
  merged["success"] &&= result["success"] != false
end

#merge_statistics!(target, source) ⇒ Object (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 1.0.0

API:

  • private



281
282
283
284
285
286
287
# File 'lib/attio/resources/bulk.rb', line 281

private def merge_statistics!(target, source)
  return unless source

  %w[created updated deleted failed].each do |key|
    target[key] += source[key] || 0
  end
end

#remove_list_entries(list_id:, entry_ids:, options: {}) ⇒ Hash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Bulk remove entries from a list

Parameters:

  • The list ID

  • Array of entry IDs to remove

  • (defaults to: {})

    Additional options

Returns:

  • Results including removal confirmations

Since:

  • 1.0.0

API:

  • private



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/attio/resources/bulk.rb', line 175

def remove_list_entries(list_id:, entry_ids:, options: {})
  validate_id!(list_id, "List")
  validate_bulk_ids!(entry_ids)

  batches = entry_ids.each_slice(MAX_BATCH_SIZE).to_a
  results = []

  batches.each_with_index do |batch, index|
    body = {
      entry_ids: batch,
      partial_success: options.fetch(:partial_success, false),
    }

    result = request(:delete, "lists/#{list_id}/entries/bulk", body)
    results << result.merge("batch" => index + 1)
  end

  merge_batch_results(results)
end

#update_records(object:, updates:, options: {}) ⇒ Hash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Bulk update multiple records

Parameters:

  • The object type

  • Array of updates with :id and :data keys

  • (defaults to: {})

    Additional options

Options Hash (options:):

  • :partial_success (Boolean)

    Allow partial success (default: false)

  • :return_records (Boolean)

    Return updated records (default: true)

Returns:

  • Results including updated records and any errors

Since:

  • 1.0.0

API:

  • private



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/attio/resources/bulk.rb', line 65

def update_records(object:, updates:, options: {})
  validate_required_string!(object, "Object type")
  validate_bulk_updates!(updates)

  batches = updates.each_slice(MAX_BATCH_SIZE).to_a
  results = []

  batches.each_with_index do |batch, index|
    body = {
      updates: batch,
      partial_success: options.fetch(:partial_success, false),
      return_records: options.fetch(:return_records, true),
    }

    result = request(:patch, "objects/#{object}/records/bulk", body)
    results << result.merge("batch" => index + 1)
  end

  merge_batch_results(results)
end

#upsert_records(object:, records:, match_attribute:, options: {}) ⇒ Hash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Bulk upsert records (create or update based on matching criteria)

Parameters:

  • The object type

  • Records to upsert

  • Attribute to match on (e.g., "email", "domain")

  • (defaults to: {})

    Additional options

Returns:

  • Results including created/updated records

Since:

  • 1.0.0

API:

  • private



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/attio/resources/bulk.rb', line 120

def upsert_records(object:, records:, match_attribute:, options: {})
  validate_required_string!(object, "Object type")
  validate_required_string!(match_attribute, "Match attribute")
  validate_bulk_records!(records, "upsert")

  batches = records.each_slice(MAX_BATCH_SIZE).to_a
  results = []

  batches.each_with_index do |batch, index|
    body = {
      records: batch.map { |record| { data: record } },
      match_attribute: match_attribute,
      partial_success: options.fetch(:partial_success, false),
      return_records: options.fetch(:return_records, true),
    }

    result = request(:put, "objects/#{object}/records/bulk", body)
    results << result.merge("batch" => index + 1)
  end

  merge_batch_results(results)
end

#validate_array!(array, name, operation) ⇒ Object (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Raises:

Since:

  • 1.0.0

API:

  • private



236
237
238
239
240
# File 'lib/attio/resources/bulk.rb', line 236

private def validate_array!(array, name, operation)
  raise ArgumentError, "#{name} array is required for #{operation}" if array.nil?
  raise ArgumentError, "#{name} must be an array for #{operation}" unless array.is_a?(Array)
  raise ArgumentError, "#{name} array cannot be empty for #{operation}" if array.empty?
end

#validate_bulk_ids!(ids) ⇒ Object (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 1.0.0

API:

  • private



221
222
223
224
225
226
227
228
# File 'lib/attio/resources/bulk.rb', line 221

private def validate_bulk_ids!(ids)
  validate_array!(ids, "IDs", "bulk operation")
  validate_max_size!(ids, "IDs")

  ids.each_with_index do |id, index|
    validate_id_item!(id, index)
  end
end

#validate_bulk_records!(records, operation) ⇒ Object (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Raises:

Since:

  • 1.0.0

API:

  • private



195
196
197
198
199
200
201
202
203
204
# File 'lib/attio/resources/bulk.rb', line 195

private def validate_bulk_records!(records, operation)
  raise ArgumentError, "Records array is required for bulk #{operation}" if records.nil?
  raise ArgumentError, "Records must be an array for bulk #{operation}" unless records.is_a?(Array)
  raise ArgumentError, "Records array cannot be empty for bulk #{operation}" if records.empty?
  raise ArgumentError, "Too many records (max 1000)" if records.size > MAX_BATCH_SIZE * 10

  records.each_with_index do |record, index|
    raise ArgumentError, "Record at index #{index} must be a hash" unless record.is_a?(Hash)
  end
end

#validate_bulk_updates!(updates) ⇒ Object (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 1.0.0

API:

  • private



206
207
208
209
210
211
212
213
# File 'lib/attio/resources/bulk.rb', line 206

private def validate_bulk_updates!(updates)
  validate_array!(updates, "Updates", "bulk update")
  validate_max_size!(updates, "updates")

  updates.each_with_index do |update, index|
    validate_update_item!(update, index)
  end
end

#validate_id_item!(id, index) ⇒ Object (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Raises:

Since:

  • 1.0.0

API:

  • private



230
231
232
233
234
# File 'lib/attio/resources/bulk.rb', line 230

private def validate_id_item!(id, index)
  return unless id.nil? || id.to_s.strip.empty?

  raise ArgumentError, "ID at index #{index} cannot be nil or empty"
end

#validate_max_size!(array, name) ⇒ Object (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Raises:

Since:

  • 1.0.0

API:

  • private



242
243
244
245
246
247
# File 'lib/attio/resources/bulk.rb', line 242

private def validate_max_size!(array, name)
  max = MAX_BATCH_SIZE * 10
  return unless array.size > max

  raise ArgumentError, "Too many #{name} (max #{max})"
end

#validate_update_item!(update, index) ⇒ Object (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Raises:

Since:

  • 1.0.0

API:

  • private



215
216
217
218
219
# File 'lib/attio/resources/bulk.rb', line 215

private def validate_update_item!(update, index)
  raise ArgumentError, "Update at index #{index} must be a hash" unless update.is_a?(Hash)
  raise ArgumentError, "Update at index #{index} must have an :id" unless update[:id]
  raise ArgumentError, "Update at index #{index} must have :data" unless update[:data]
end