Skip to content

Refactor that call_api function in the Client (controller) object #227

@heartsucker

Description

@heartsucker

Right now calling the API from the controller is done by

def call_api(self, function, callback, timeout, *args, current_object=None, **kwargs):
    ...

The timeout function takes no arguments, the callback takes one (result) which is either and exception or whatever the API call returns (Reply, List[Source], etc.).

If want to send a message and then on timeout emit a signal self.reply_failure(reply_uuid) so that the UI element can change the related elements to signify failure, I need to know in the callback what the Reply's uuid is. I can do this:

self.call_api(
    self.api.reply_source,
    self.on_reply,
    functools.partial(self.on_reply_timeout, reply.uuid), # <----- partial
    ... # moar args
)

This would lead to us using partial function application everywhere, which is annoying af because it also means in the tests we can't use checks like mock_thingy.assert_called_once_with(...) because of this:

>>> partial(foo, 123) == partial(foo, 123)
False

The proposal would be something like this:

def call_api(self, function, success_callback, failure_callback, *args, **kwargs):
   ...

Where the failure callback is any failure, not just timeouts. Additionally, both callbacks will be passed *nargs and **kwargs to allow them to make additional decisions about what to do on success/failure.

I do wonder if we might end up wanting even more control in the future over behavior or want additional args available to the callbacks. I can't think of a case right now, but it might be worth removing this all together and having a pattern like

def reply_to_source(self, source_uuid: str, reply_uuid: str, message: str) -> None:
    def func():
        try:
            reply_obj = self.api.reply_source(source_uuid, message, reply_uuid)
        except TimeoutError:
            # foo
        except NotFound:
            # bar
        else:
            # baz

    thread = QThread()
    thread.started.connect(func)
    thread.run()    

I generally feel that we might have abstracted the call_api functionality into a single function too early.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions