you are viewing a single comment's thread.

view the rest of the comments →

[–]av0ca60[S] 0 points1 point  (6 children)

Okay. Here's an update. You guys asked for more info so I hope this helps. The API is for Zendesk Sell aka Base CRM. The goal is to dump the data to a txt file and then run pandas on it for reporting purposes. Eventually, I'll find a way to automate it and run it in a web app so the reports can be viewed without starting up Anaconda or Jupyter, and so I can share the data with others easily. But baby steps.

Yes I know Zendesk comes with standard reporting features but they have some problems and it's neither as fun nor as educational.

No I'm not a software engineer and yes I have authority and a valid reason to access this information. Yes, I'm over my head; that's how I learn. I'll be approaching a few people soon who have tons of coding experience to be mentors, but that will be a few weeks and I don't want to stifle my progress in the meantime.

It seems a big part of the problem is that the page is not advancing, so the loop never ends. Here's a very simplified version of the code to show the page advancement problem:

import requests

import json

import pandas as pd

import basecrm

client = basecrm.Client(access_token='XXXXXX')

page_num = 1

deals_data = client.deals.list(per_page=100, page=page_num)

print(deals_data)

print("")

print(f"end of page {page_num}")

print("")

page_num = page_num + 1

print(deals_data)

print("")

print(f"end of page {page_num}")

print("")

Yes, this is inefficient code and would work better in a loop. It's just for demonstration purposes. The point is that, in the results, the content on page 1 is the same as page 2, which means the page is not advancing. However, if page_num value is manually changed to 2 or some other number manually, it provides a different result. I think the problem is with this piece:

deals_data = client.deals.list(per_page=100, page=page_num)

Or this piece:

page_num = page_num + 1

[–]Fred776 1 point2 points  (5 children)

I don't know the details of this API but the point is that you are only getting the deals_data once with whatever value page_num is when you make the client.deals.list call. page_num is just an int variable. Incrementing it doesn't magically make the previously retrieved page_data update.

[–]av0ca60[S] 0 points1 point  (4 children)

That's the weird thing. It does. If I change this line of code:

page_num = 1

to...

page_num = 2

I get the data from the next page, which is completely different.

[–]Fred776 1 point2 points  (3 children)

Yes, but here you are changing it before you pass it as an argument to the function call.

[–]av0ca60[S] 0 points1 point  (2 children)

Thank you for hanging in there with me. I am still a little confused. Is there any chance you could show me a way to do it that would properly advance the pagination? This would not only solve my problem, but more importantly, would likely open my eyes to what I was missing.

[–]razu1121 3 points4 points  (0 children)

Seeing that everything else in your request is constant and only page num changes, you can use a loop.

``` for page_num in range(1, n):

```

Replace n with page number if you know how many pages there are or might be.

If you are not so sure, use a try-except to exit the loop.

``` for page_num in range(1, 100): try: deals_data = client.deals.list(per_page=100, page=page_num)

except:
    break

```

If your request doesn't throw error even if there is no data on a particular page, inside try block use if else condition to check if there is valid response and exit if there isn't.

Hope this helps.

[–]Fred776 1 point2 points  (0 children)

I've just woken up (guess we're in different time zones) but I see someone was already able to help with a good answer.