Search code examples
pythonzendesk-api

ZenPy search calls with max results


I am using ZenPy to search for a few tickets in ZenDesk:

open_tickets = zenpy_client.search(type='ticket', status=['new', 'open'], subject=subject, group=group_name, created_between=[two_weeks_ago_date, current_date])

The problem is when I have too many results from this Search call (more than 1.000 since it's the new query limit for ZenDesk API). I get the following exception:

<Invalid search: Requested response size was greater than Search Response Limits>

I'm trying to look into ZenPy documentation but couldn't find any parameter that I could use to limit the search call for 10 pages (in this case, 1.000 records since we get 100 tickets per request).

I ended up putting a try-catch in the call but I'm sure that's not the best solution:

from zenpy.lib.exception import APIException
try:
    open_tickets = zenpy_client.search(type='ticket', status=['new', 'open'], subject=subject, group=group_name, created_between=[two_weeks_ago_date, current_date])
except APIException as ex:
    ... 

What is the best solution to limit this search?

I also know that I can limit even more the dates but we are creating a lot of tickets in one specific day of the week so there's no way to filter more, I just need to go until the limit.

Reference:

  1. https://developer.zendesk.com/rest_api/docs/support/search
  2. https://develop.zendesk.com/hc/en-us/articles/360022563994--BREAKING-New-Search-API-Result-Limits

Thanks!


Solution

  • The generator returned by search supports Python slices, so the following code will pull results up to 1000 and prevent exceeding the new limit:

    ticket_generator = zenpy_client.search(type="ticket")
    print(ticket_generator[:1000])