Skip to content

Commit

Permalink
fix: generalise the get_all_generator method for all onject types a…
Browse files Browse the repository at this point in the history
…nd create an implementation in `InvoiceProfiles`
  • Loading branch information
JustARatherRidiculouslyLongUsername committed Oct 22, 2024
1 parent 468c119 commit 26faa2a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
11 changes: 4 additions & 7 deletions connectors/travelperk/apis/api_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
class ApiBase:
"""The base class for all API classes."""

GET_INVOICE_PROFILES = '/profiles'

def __init__(self):
self.__access_token = None
self.__server_url = None
Expand Down Expand Up @@ -78,9 +76,9 @@ def _get_request(self, object_type: str, api_url: str, params: dict = {}) -> Lis
else:
raise self._get_error(response.status_code, response.text)

def get_all_generator(self):
def _get_all_generator(self, object_type: str, api_url: str):
"""
Creates a generator that contains all profiles across all pages
Creates a generator that contains all records of `object_type` across all pages
Parameters:
object_type (str): The type of object to get
Expand All @@ -93,13 +91,12 @@ def get_all_generator(self):

limit = 50
params = {'limit': limit}
total_profiles = self._get_request('total', self.GET_INVOICE_PROFILES, params=params)
total_profiles = self._get_request('total', api_url, params=params)

for offset in range(0, total_profiles, limit):
params['offset'] = offset
profiles = self._get_request('profiles', self.GET_INVOICE_PROFILES, params=params)
profiles = self._get_request(object_type, api_url, params=params)
for profile in profiles:
print('[x]', profile['name'])
yield profile

def _post_request(self, api_url: str, data: Dict) -> Dict:
Expand Down
12 changes: 11 additions & 1 deletion connectors/travelperk/apis/invoice_profiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,20 @@
class InvoiceProfiles(ApiBase):
"""Class for Invoice Profiles APIs."""

GET_INVOICE_PROFILES = '/profiles'

def get_all(self):
"""Get a list of the existing Invoice Profiles in the Organization.
Returns:
List with dicts in Invoice Profile schema.
"""
return [*self.get_all_generator()]
return [*self._get_all_generator('profiles', self.GET_INVOICE_PROFILES)]

def get_all_generator(self):
"""Create a generator with all the existing Invoice Profiles in the Organization.
Returns:
Generator with dicts in Invoice Profile schema.
"""
return self._get_all_generator('profiles', self.GET_INVOICE_PROFILES)

0 comments on commit 26faa2a

Please sign in to comment.