Skip to content

Commit

Permalink
Should resolve the issue #71 #73 (#72)
Browse files Browse the repository at this point in the history
* helper function added to collect all items of paginated response

* account services list method modified to get all paginated items

* account service retrieve method modified to fix issue #73

* param name changed to kwargs

Co-authored-by: badiuzzaman <[email protected]>
  • Loading branch information
prantoamt and badiuzzaman authored Aug 12, 2022
1 parent 7c5fe29 commit 4d4fcfe
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 5 deletions.
17 changes: 12 additions & 5 deletions fortnox/services/account_services.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from .helpers import collect_all_items_from_paginators

class AccountsService(object):
"""
:class:`fortnox.AccountsService` is used by :class:`fortnox.Client` to make
Expand Down Expand Up @@ -34,23 +36,28 @@ def list(self, **params):
:return: List of dictionaries that support attriubte-style access, which represent collection of Customers.
:rtype: list
"""

_, _, accounts = self.http_client.get("/accounts", params=params)
url = '/accounts'
if 'page' not in params:
accounts = collect_all_items_from_paginators(self, params, url, 'Accounts')
else:
_, _, accounts = self.http_client.get(url, params=params)
return accounts

def retrieve(self, id):
def retrieve(self, id, **kwargs):
"""
Retrieve a single Accounts
Returns a single Account according to the unique Account ID provided
If the specified Account does not exist, this query returns an error
:calls: ``get /accounts/{id}``
:param int id: Unique identifier of a Account.
:param int id: Unique identifier of an Account.
:param dict kwargs: (optional) Search options.
:return: Dictionary that support attriubte-style access and represent Accounts resource.
:rtype: dict
"""
_, _, account = self.http_client.get("/accounts/{id}".format(id=id))
url = f'/accounts/{id}'
_, _, account = self.http_client.get(url, params=kwargs)
return account

def create(self, *args, **kwargs):
Expand Down
28 changes: 28 additions & 0 deletions fortnox/services/helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
def collect_all_items_from_paginators(self: object, params: dict, url: str, targeted_service: str) -> list:
'''
Returns all items from paginated response and returns as list
:parameters:
self -> service class object.
params -> dict : params passed to the func of the service object.
url -> str: where the request will be performed.
targeted_service -> str: the service key we are looking for in the response.
:return: List of dictionaries that support attriubte-style access, which represent collection of Customers.
:rtype: list
'''
total_pages = 0
_, _, raw_response = self.http_client.get(
url, params=params, **{'raw': True}) ## gets the raw data from fortnox
meta_data = raw_response.get('MetaInformation')
if meta_data:
total_pages = meta_data.get('@TotalPages', 0) ## gets the total number of pages

services = raw_response.get(targeted_service) ## gets the targeted service of 1st page

if total_pages > 1:
## gets the targeted service from page 2 to last page and concat then in services
for page in range(2, total_pages+1):
params['page'] = page
_, _, response = self.http_client.get(url, params=params)
services = services + response
return services

0 comments on commit 4d4fcfe

Please sign in to comment.