Return Next Pages of Results Based on Tag in API

Viewed 39

I am trying to come up with a script that loops and returns all results from an API. The max transactions per call is 500, and there is a tag 'MoreFlag' that is 0 when there are less than or equal to 500 transactions and 1 when there are more than 500 transactions (per page). How can I write the code so that when 'MoreFlag' is 1 go to the next page until the tag changes to 0?

The API requires a license key and password, but here's a piece of the output.

r = 0
station_name = 'ORANGE'
usageSearchQuery = {
    'stationName': station_name,
    'startRecord': 1 + r,
    'numTransactions': 500
}
trans_data = client.service.getTransactionData(usageSearchQuery)
for c in enumerate(trans_data):
    print(c)

This returns the following:

(0, 'responseCode')
(1, 'responseText')
(2, 'transactions')
(3, 'MoreFlag')

Next, if I use this code:

for c in enumerate(trans_data.transactions):
    print(trans_data)
    # add 500 to startRecord

The API returns:

{
'responseCode': '100',
'responseText': 'API input request executed successfully.',
'transactions': {
    'transactionData': [
        {
            'stationID': '1’,
            'stationName': 'ORANGE',
            'transactionID': 178543,
            'Revenue': 1.38,
            'companyID': ‘ABC’,
            'recordNumber': 1
        },
        {
            'stationID': '1’,
            'stationName': 'ORANGE',
            'transactionID': 195325,
            'Revenue': 1.63,
            'companyID': ‘ABC’,
            'recordNumber': 2
        },
        {
            'stationID': '1’,
            'stationName': 'ORANGE',
            'transactionID': 287006,
            'Revenue': 8.05,
            'companyID': ‘ABC’,
            'recordNumber': 500
        }
    ]
},
'MoreFlag': 1
}

The idea is to pull data from trans_data.transactions.transactionData, but I'm getting tripped up when I need more than 500 results, i.e. subsequent pages.

1 Answers

I figured it out. I guess my only question: is there a cleaner way to do this? It seems kind of repetitive.

i = 1
y = []
lr = 0
station_name = 'ORANGE'
usageSearchQuery = {
    'stationName': station_name,
}
trans_data = client.service.getTransactionData(usageSearchQuery)
for c in enumerate(trans_data):
        while trans_data.MoreFlag == 1:
            usageSearchQuery = {
                        'stationName': station_name,
                        'startRecord': 1 + lr,
                        'numTransactions': 500
                    }
            trans_data = client.service.getTransactionData(usageSearchQuery)
            for (d) in trans_data.transactions.transactionData:
                td = [i, str(d.stationName), d.transactionID,
                      d.transactionTime.strftime('%Y-%m-%d %H:%M:%S'),
                      d.Revenue]
                i = i + 1
                y.append(td)
            lr = lr + len(trans_data.transactions.transactionData)
            
Related