synapse/rest/client/transactions.py
def fetch_or_execute(self, txn_key, fn, *args, **kwargs):
        """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).
        """
        try:
            return self.transactions[txn_key][0].observe()
        except (KeyError, IndexError):
            pass  # execute the function instead.

        deferred = fn(*args, **kwargs)

        # if the request fails with a Twisted failure, remove it
        # from the transaction map. This is done to ensure that we don't
        # cache transient errors like rate-limiting errors, etc.
        def remove_from_map(err):
            self.transactions.pop(txn_key, None)
            return err
        deferred.addErrback(remove_from_map)

        # We don't add any other errbacks to the raw deferred, so we ask
        # ObservableDeferred to swallow the error. This is fine as the error will
        # still be reported to the observers.
        observable = ObservableDeferred(deferred, consumeErrors=True)
        self.transactions[txn_key] = (observable, self.clock.time_msec())
        return observable.observe()
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).
        
Fetches the response for this transaction, or executes the given function
to produce a response for this transaction.

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).
Fetches the response for this transaction or executes the given function to produce a response for this transaction . 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()
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 ( )
Fetches the response for this transaction, or executes the given function
to produce a response for this transaction.
Fetches the response for this transaction or executes the given function to produce a response for this transaction .
"""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).
        """ # execute the function instead. # if the request fails with a Twisted failure, remove it # from the transaction map. This is done to ensure that we don't # cache transient errors like rate-limiting errors, etc. # We don't add any other errbacks to the raw deferred, so we ask # ObservableDeferred to swallow the error. This is fine as the error will # still be reported to the observers.
self
txn_key
fn