facetoe / zenpy

Python wrapper for the Zendesk API
GNU General Public License v3.0
340 stars 162 forks source link

`SearchExportResultGenerator.next()` raises `IndexError` #609

Closed cbini closed 4 months ago

cbini commented 9 months ago

https://github.com/facetoe/zenpy/blob/e3013a963d9f2a3e68f25442d0ed0d58ebe61646/zenpy/lib/generator.py#L51

I'm iterating over a SearchExportResultGenerator, and for some reason it's producing IndexError: list index out of range. This has worked without issue for every other search I've done, but somehow these parameters are breaking it:

Here's my function call:

archived_tickets = zendesk.search_export(
    type="ticket", status="closed", updated_between=[start_date, end_date]
)

I'm pretty stumped so maybe there's a bug somewhere?

cryptomail commented 9 months ago

It could be that the range is too big? I'll have to look at the documentation to see how wide the date range you specified is "legal" for. :)

cbini commented 9 months ago

Maybe? It's only a month, and the strange part is that I've done this for every month going back to 2012, but that's the only one that fails with the out of range error.

Perhaps there's a cap on number of records search export will return and for some reason that month is just ridiculously high?

cryptomail commented 9 months ago

Yes, that could be it @cbini :I Also, at the risk of too much information, we have "protectionst" measures for queries that are too onerous and taxing on our systems. Can you try to winnow down the data to maybe 15 days (or bisect thereof).

cbini commented 9 months ago

Yeah, it's not too big a deal. I threw it into a try/except block so that it stops iterating if it hits that error, but I figured you might want to know.

ben-lorisai commented 8 months ago

I've hit the same issue. The problem occurs when the number of items in results is a multiple of 100 i.e. of the page-size. The StopIterration condition here: https://github.com/facetoe/zenpy/blob/40222cc450e84aaf499d3d4f98e9b77446923056/zenpy/lib/generator.py#L49 will never be True since the values list is always extended rather than replaced. A possible fix would be to replace lines 47-50:

        if self.position >= len(self.values):
            self.handle_pagination()
        if len(self.values) < 1:
            raise StopIteration()

by

        if self.position >= len(self.values):
            self.handle_pagination()
            ifself.position >= len(self.values):
                raise StopIteration()

Until this is done, you can catch the IndexError at the application level and ignore it if the number of fetched items is a multiple of 100.

cryptomail commented 4 months ago

Wait I think I fixed this from another issue :)