Module: Restforce::Concerns::CompositeAPI

Extended by:
Verbs
Included in:
AbstractClient
Defined in:
lib/restforce/concerns/composite_api.rb

Defined Under Namespace

Classes: Subrequests

Instance Method Summary collapse

Methods included from Verbs

define_api_verb, define_verb, define_verbs

Instance Method Details

#composite(all_or_none: false, collate_subrequests: false) {|subrequests| ... } ⇒ Object

Yields:

  • (subrequests)


12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/restforce/concerns/composite_api.rb', line 12

def composite(all_or_none: false, collate_subrequests: false)
  subrequests = Subrequests.new(options)
  yield(subrequests)

  if subrequests.requests.length > 25
    raise ArgumentError, 'Cannot have more than 25 subrequests.'
  end

  properties = {
    compositeRequest: subrequests.requests,
    allOrNone: all_or_none,
    collateSubrequests: collate_subrequests
  }
  response = api_post('composite', properties.to_json)

  results = response.body['compositeResponse']
  has_errors = results.any? { |result| result['httpStatusCode'].digits.last == 4 }
  if all_or_none && has_errors
    last_error_index = results.rindex { |result| result['httpStatusCode'] != 412 }
    last_error = results[last_error_index]
    raise CompositeAPIError.new(last_error['body'][0]['errorCode'], response)
  end

  results
end

#composite!(collate_subrequests: false, &block) ⇒ Object



38
39
40
# File 'lib/restforce/concerns/composite_api.rb', line 38

def composite!(collate_subrequests: false, &block)
  composite(all_or_none: true, collate_subrequests: collate_subrequests, &block)
end