'Fetches the response for this transaction, or executes the given function
to produce a response for this transaction.
Args:
txn_key (str): A key to ensure idempotency should fetch_or_execute be
called again at a later point in time.
fn (function): A function which returns a tuple of
(response_code, response_dict).
*args: Arguments to pass to fn.
**kwargs: Keyword arguments to pass to fn.
Returns:
Deferred which resolves to a tuple of (response_code, response_dict).'

def fetch_or_execute(self, txn_key, fn, *args, **kwargs):

    try:
        return self.transactions[txn_key][0].observe()
    except (KeyError, IndexError):
        pass
    deferred = fn(*args, **kwargs)
    def remove_from_map(err):
        self.transactions.pop(txn_key, None)
        return err
    deferred.addErrback(remove_from_map)
    observable = ObservableDeferred(deferred, consumeErrors=True)
    self.transactions[txn_key] = (observable, self.clock.time_msec())
    return observable.observe()
