def create(self, batch_id, client_id, client_secret, max_calls=None, **kwargs): """Retrieves queued calls, sequentially sends /batch/create API calls in chunks of up to `max_calls`, which defaults to `WEPAY_BATCH_CALLS_NUMBER` setting and then processes any callbacks set. Consider raising `timeout` kwarg, since batch calls can take a while, also if you start getting HTTP 404 errors, try lowering `max_calls` value. """ max_calls = max_calls or BATCH_CALLS_NUMBER assert 0 < max_calls and max_calls <= 50, \ """max_calls should be a positive number no greater then 50, it is also WePay's limitation""" batch_key = make_batch_key(batch_id) calls = BATCH_CALLS_CACHE.get(batch_key) calls_response = [] while calls: cur_calls = calls[:max_calls] response = super(Batch, self).create( client_id, client_secret, cur_calls, **kwargs)[1] calls_response.extend(response['calls']) calls = calls[max_calls:] response = (None, {'calls': self.process_calls(batch_key, calls_response)}) self.del_calls(batch_id) return response
def del_calls(self, batch_id): batch_key = make_batch_key(batch_id) try: del BATCH_CALLBACKS[batch_key] except KeyError: pass try: del BATCH_CALLS_CACHE[batch_key] except KeyError: pass
def create(self, batch_id, **kwargs): """Retrieves calls from cache, sequentially send /batch/create API calls in chunks of up to 50 and then processes any callbacks set. """ batch_key = make_batch_key(batch_id) calls = BATCH_CALLS_CACHE.get(batch_key) calls_response = [] while calls: cur_calls = calls[:50] response = super(Batch, self).create(calls=cur_calls, **kwargs)[1] calls_response.extend(response['calls']) calls = calls[50:] response = (None, {'calls': self.process_calls(batch_key, calls_response)}) self.del_calls(batch_id) return response
def create(self, batch_id, **kwargs): """Retrieves calls from cache, sequentially send /batch/create API calls in chunks of up to 50 and then processes any callbacks set. """ batch_key = make_batch_key(batch_id) calls = BATCH_CALLS_CACHE.get(batch_key) calls_response = [] while calls: cur_calls = calls[:50] response = super(Batch, self).create(calls=cur_calls, **kwargs)[1] calls_response.extend(response['calls']) calls = calls[50:] response = (None, { 'calls': self.process_calls(batch_key, calls_response) }) self.del_calls(batch_id) return response
def make_call(self, func, params, extra_kwargs): callback = extra_kwargs.pop('callback', None) if extra_kwargs.get('batch_mode', False): batch_key = make_batch_key(extra_kwargs.pop('batch_id')) reference_id = extra_kwargs.get('batch_reference_id', None) call = super(Call, self).make_call(func, params, extra_kwargs) if not callback is None and callable(callback): # put callback in the cache assert not reference_id is None, \ "'batch_reference_id' is required when 'callback' is provided" callbacks = BATCH_CALLBACKS.get(batch_key, {}) callbacks[reference_id] = callback BATCH_CALLBACKS[batch_key] = callbacks # put the actual call in the cache calls = BATCH_CALLS_CACHE.get(batch_key, []) calls.append(call) BATCH_CALLS_CACHE[batch_key] = calls return None else: response = super(Call, self).make_call(func, params, extra_kwargs) processed = None if not callback is None and callable(callback): processed = callback(response) return (processed, response)
def set_calls(self, batch_id, calls): BATCH_CALLS_CACHE[make_batch_key(batch_id)] = calls
def get_calls(self, batch_id): return BATCH_CALLS_CACHE.get(make_batch_key(batch_id))