Module: Parse::API::Batch

Included in:
Client
Defined in:
lib/parse/api/batch.rb

Overview

Defines the Batch interface for the Parse REST API

Instance Method Summary collapse

Instance Method Details

#batch_request(requests) ⇒ Array<Parse::Response>, Parse::Response #batch_request(operation) ⇒ Array<Parse::Response>, Parse::Response

Note:

You cannot use batch_requests with User instances that need to be created.

Note:

Every request is sent through this client, as Client#request sends a single request through the client it is called on, even when the request was built for another object's class client.

Note:

Each Request's own credentials (its session_token: / use_master_key: options and its session-token or master-key suppression headers) are honored. When every request resolves to this client and one set of credentials, the batch is one call. A transaction that would mix credentials raises BatchOperation::MixedAuthorityError before anything is sent. A non-transactional batch with mixed credentials is sent as one call per set of credentials, through BatchOperation#submit, with responses in request order. Elements that are not Request instances are ignored, as BatchOperation#add ignores them.

Overloads:

Parameters:

  • opts (Hash) —

    request options for the POST /batch call itself, such as session_token: or use_master_key:. Parse Server runs every sub-request under the credentials of this one call (it ignores per-sub-request headers), so the batch's authority is set here. A request that names no credentials of its own runs under these. Narrowing options always win: use_master_key: false here keeps the master key off even for a request that set use_master_key: true. An option here never widens a request.

Returns:

Raises:



49
50
51
52
53
54
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
# File 'lib/parse/api/batch.rb', line 49

def batch_request(batch_operations, **opts)
  unless batch_operations.is_a?(Parse::BatchOperation)
    batch_operations = Parse::BatchOperation.new batch_operations
  end
  call_auth = opts.slice(:session_token, :use_master_key, :suppress_master_key)
  groups = batch_operations.send(:authority_groups, self, call_auth, force_client: true)
  if groups.empty?
    return post_batch_operation(batch_operations, opts.except(:suppress_master_key))
  end
  if groups.size == 1 &&
     Parse::BatchOperation.credential_fingerprint(groups.first[:client]) ==
     Parse::BatchOperation.credential_fingerprint(self)
    send_opts = opts.except(:session_token, :use_master_key, :suppress_master_key)
                    .merge(groups.first[:opts])
    return post_batch_operation(batch_operations, send_opts)
  end
  if batch_operations.transaction
    raise Parse::BatchOperation::MixedAuthorityError,
          "A transaction cannot mix requests built for different credentials or " \
          "another client. Parse Server runs a batch under one credential, so it " \
          "cannot honor each request's own. Save these objects in separate transactions."
  end
  # Mixed credentials, not a transaction: send one call per set of
  # credentials, keeping responses in request order.
  # Assigned, not re-added: `add` would drop tagged duplicates and leave
  # fewer responses than requests.
  routed = Parse::BatchOperation.new
  routed.requests = batch_operations.requests.dup
  routed.client = self
  routed.batch_defaults = call_auth
  routed.submit
end