是否可以在Python中对API响应使用触发器函数?

是否可以在Python中对API响应使用触发器函数?,python,api,response,Python,Api,Response,我希望在收到来自API调用的有效响应时调用函数。我该怎么做呢?我想用的逻辑基本上是 function(x): api_instance.download_report(x) if valid in response: function(x) 我只是不知道如何正确设置。我已经收到一个有效的响应,但我想知道如何使用它来您需要使用回调: def domyapithing(x, functobecalled): response= api_instance.download_

我希望在收到来自API调用的有效响应时调用函数。我该怎么做呢?我想用的逻辑基本上是

function(x):
    api_instance.download_report(x)

if valid in response:
    function(x)

我只是不知道如何正确设置。我已经收到一个有效的响应,但我想知道如何使用它来

您需要使用回调:

def domyapithing(x, functobecalled):
    response= api_instance.download_report(x)
    if status(response) == 'ok':
        functobecalled(response)

def functobecalled(response):
    print(response)
这是一个近似答案,假设您有一个名为status的函数,如果一切正常,该函数将返回ok