Python: Use of class methods to change data Could you please give me some feedback on the use of class methods in this Python script?
The code retrieves time entries from the Toggl API and works as expected. My question is, do the methods make sense? The methods first_api_call and make_subsequent_calls change self.data. Is this okay or should all the changes to self.data be made in retrieve_data?
from datetime import datetime

import requests

today = datetime.today().date()
since = datetime(today.year, 1, 1)


class Toggl:
    def __init__(self):
        self.data = []
        self.total_calls = None
        self.params = {
            'workspace_id': '5604761',
            'since': since,
            'until': today,
            'user_agent': 'john@doe.com',
            'page': 1,
        }

    def retrieve_data(self):
        r = self.first_api_call()
        self.total_calls = self.calculate_number_of_extra_calls_needed(r)
        self.make_subsequent_calls()

    def first_api_call(self):
        r = requests.get('https://toggl.com/reports/api/v2/details',
                         params=self.params,
                         auth=('APIKEY', 'api_token'))
        r.raise_for_status()
        r = r.json()
        self.data = r['data']
        return r

    def calculate_number_of_extra_calls_needed(self, r):
        &quot;&quot;&quot;
        Calculate the number of pages that need to be retrieved.
        The second part is True/False to round up.
        Finally minus 1 because we got the first page already.
        &quot;&quot;&quot;
        total_calls = r['total_count'] // 50 + (r['total_count'] % 50 &gt; 0) - 1
        return total_calls

    def make_subsequent_calls(self):
        &quot;&quot;&quot;Make the new API calls and store the data&quot;&quot;&quot;
        for i in range(self.total_calls):
            self.params['page'] = self.params['page'] + 1
            r = requests.get('https://toggl.com/reports/api/v2/details',
                             params=self.params,
                             auth=('APIKEY', 'api_token'))
            r.raise_for_status()
            r = r.json()
            for d in r['data']:
                self.data.append(d)


if __name__ == &quot;__main__&quot;:
    toggl = Toggl()
    toggl.retrieve_data()
    print(toggl.data)
It might be best to move
today = datetime.today().date()
since = datetime(today.year, 1, 1)

into the class's __init__. As it is, the code will just be run once when the module loads, whereas it would make more sense to work out the date when you construct Toggl.
I'd also say that having two methods for what is essentially the exact same task could be streamlined. Perhaps something like this?
import math

# Default page size in the Toggl API.
PAGE_SIZE = 50

class Toggl:
    def __init__(self):
        today = datetime.today().date()
        since = datetime(today.year, 1, 1)
        self.data = []
        self.total_calls = None
        self.params = {
            'workspace_id': '5604761',
            'since': since,
            'until': today,
            'user_agent': 'john@doe.com'
        }

    def fetch_page(self, page_number):
        params = self.params
        params['page'] = page_number
        r = requests.get('https://toggl.com/reports/api/v2/details',
                         params=params,
                         auth=('APIKEY', 'api_token'))
        r.raise_for_status()
        return r.json()

    def retrieve_data(self):
        r = self.fetch_page(1)
        self.data = r['data']
        pages = self.total_pages(r['total_count'])
        for i in range(2, pages + 1):
            r = self.fetch_page(i)
            self.data += r['data']

    def total_pages(self, total_count):
        return math.ceil(total_count / PAGE_SIZE)

In particular, any &quot;magic number&quot; constants should be taken out and put at the top as constants (by convention in SCREAMING_SNAKE_CASE). I tweaked the function names to be more descriptive, and replaced first_api_call and make_subsequent_calls with a function that doesn't affect the state: instead it just makes a call to fetch the required page and retreive_data decides what to do with it. I don't think there's anything inherently wrong with doing it as you did though, I just personally thought fetch_page seemed cleaner and reduced duplication.
You can simplify the calculation to work out how many pages you need using math.ceil as I demonstrate above too: for example 101 / 50 = 2.02, which is mapped to 3 by math.ceil.
To make the code even better, you could avoid storing the API key hard coded: take it as a parameter in the constructor and store it in self. You could also take since and today as parameters so the user can choose the range that they want to obtain.