sjkingo / python-freshdesk

An API for the Freshdesk helpdesk
BSD 2-Clause "Simplified" License
85 stars 67 forks source link

No way to fetch articles when folder has >30 articles (needs pagination support) #94

Open sglebs opened 1 week ago

sglebs commented 1 week ago

The code below can't fetch all the articles in a folder if it has more than 30.


freshdesk_api = API(f"{freshdesk_domain}.freshdesk.com", token)
for category in freshdesk_api.solutions.categories.list_categories():
    for folder in freshdesk_api.solutions.folders.list_from_category(category.id):
        for article in freshdesk_api.solutions.articles.list_from_folder(folder.id):
            print(f"{article.id}:{category.name}/{folder.name}/{article.title}")

How can pagination be used?

sglebs commented 1 week ago

Pagination: https://developer.freshdesk.com/api/#pagination

sglebs commented 1 week ago

Here's my patch:

    def list_from_folder(self, id):
        page_number = 1
        while True:
            url = "solutions/folders/%d/articles?page=%d" % (id, page_number)
            articles = self._api._get(url)
            for a in articles:
                yield SolutionArticle(**a)
            page_number += 1
            if len(articles)== 0:
                break
sglebs commented 1 week ago

If a category can have more than 30 folders, the same bug will happen there. Here'w what else to patch:

    def list_from_category(self, category_id):
        page_number = 1
        while True:
            url = "solutions/categories/%d/folders?page=%d" % (category_id, page_number)
            folders = self._api._get(url)
            for r in folders:
                yield SolutionFolder(**r)
            page_number += 1
            if len(folders)== 0:
                break
sglebs commented 1 week ago

If you need the fix, you can (for now) use this:

pip install https://github.com/sglebs/python-freshdesk/archive/pagination.zip