I have a mapped task that will make an api call. ...
# prefect-community
s
I have a mapped task that will make an api call. What is the best way to count successful api responses? I could check the response message in the task and return an int along with the response. It seems like
send_tasks
will be a list of the responses and I could iterate over it in a subsequent task, but I’m hoping there is a more efficient method. Can I force a task to fail if the api indicates a failed call? If i do, is there a way to count those?
Copy code
@task
def make_post(creds, task):
   ...
   response = requests.request('POST', ...)
   return response

with Flow(stuff, stuff):
   send_tasks = make_post.map(creds=unmapped(creds), task=data)
k
So you can make a task fail with
raise FAIL
inside it where
FAIL
is the Prefect signal. And then you can have a reduce task
Copy code
@task
def reduce(x: List):
    total = len(x)
    fail = len[1 for _ in x if isinstance(_, BaseException)]
    success = total - fail
because the FAIL is an Exception so you can look for it like that.