Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added generator for the get calls #23

Merged
merged 2 commits into from
Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions qbosdk/apis/accounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,11 @@ def get(self):
List with dicts in Accounts schema.
"""
return self._query_get_all('Account', Accounts.GET_ACCOUNTS)

def get_all_generator(self):
"""Get a generator of all the existing Accounts in the Organization.

Returns:
Generator with dicts in Accounts schema.
"""
return self._query_get_all_generator('Account', Accounts.GET_ACCOUNTS)
54 changes: 53 additions & 1 deletion qbosdk/apis/api_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
API Base class with util functions
"""
import json
from typing import List, Dict
from typing import List, Dict, Generator

import requests

Expand Down Expand Up @@ -86,6 +86,58 @@ def _query_get_all(self, object_type: str, url: str) -> List[Dict]:

raise QuickbooksOnlineSDKError('Error: {0}'.format(response.status_code), response.text)

def _query_get_all_generator(self, object_type: str, url: str) -> Generator[Dict, None, None]:
"""
Gets all the objects of a particular type for query type GET calls
:param url: GET URL of object
:param object_type: type of object
:return: list of objects
"""
start_position = 1

request_url = '{0}{1}'.format(self.__server_url, url)

api_headers = {
'Authorization': 'Bearer {0}'.format(self.__access_token),
'Accept': 'application/json'
}

while True:
try:
response = requests.get(url=request_url.format(start_position), headers=api_headers)
response.raise_for_status()

data = json.loads(response.text)
query_response = data['QueryResponse']

if not query_response[object_type]:
break

for obj in query_response[object_type]:
yield obj

start_position += 1000

except requests.exceptions.HTTPError as err:
if err.response.status_code == 400:
raise WrongParamsError('Some of the parameters are wrong', err.response.text)

if err.response.status_code == 401:
raise InvalidTokenError('Invalid token, try to refresh it', err.response.text)

if err.response.status_code == 403:
raise NoPrivilegeError('Forbidden, the user has insufficient privilege', err.response.text)

if err.response.status_code == 404:
raise NotFoundItemError('Not found item with ID', err.response.text)

if err.response.status_code == 498:
raise ExpiredTokenError('Expired token, try to refresh it', err.responseesponse.text)

if err.response.status_code == 500:
raise InternalServerError('Internal server error', err.response.text)

raise QuickbooksOnlineSDKError('Error: {0}'.format(err.response.status_code), err.response.text)

def _query(self, url: str) -> List[Dict]:
"""
Expand Down
7 changes: 7 additions & 0 deletions qbosdk/apis/attachments.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ def get(self):
"""
return self._query_get_all('Attachable', Attachments.GET_ATTACHABLES)

def get_all_generator(self):
"""
Get all Attachables
:return: Generator with Dicts in Attachable Schema
"""
return self._query_get_all_generator('Attachable', Attachments.GET_ATTACHABLES)

def post(self, ref_id: str, ref_type: str, content, file_name):
"""
Post Attachable (check, etc) to Quickbooks Online
Expand Down
7 changes: 7 additions & 0 deletions qbosdk/apis/bill_payments.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ def get(self):
"""
return self._query_get_all('BillPayment', BillPayments.GET_BILLS)

def get_all_generator(self):
"""
Get all Bill Payments
:return: Generator of Dicts in Bill Payment Schema
"""
return self._query_get_all_generator('BillPayment', BillPayments.GET_BILLS)

def post(self, data: Dict):
"""
Post Bill Payment to Quickbooks Online
Expand Down
7 changes: 7 additions & 0 deletions qbosdk/apis/bills.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ def get(self):
"""
return self._query_get_all('Bill', Bills.GET_BILLS)

def get_all_generator(self):
"""
Get all Bills
:return: Generator with Dicts in Bill Schema
"""
return self._query_get_all_generator('Bill', Bills.GET_BILLS)

def post(self, data: Dict):
"""
Post Bill to Quickbooks Online
Expand Down
8 changes: 8 additions & 0 deletions qbosdk/apis/classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,11 @@ def get(self):
List with dicts in Classes schema.
"""
return self._query_get_all('Class', Classes.GET_CLASSES)

def get_all_generator(self):
"""Get a list of the existing Classes in the Organization.

Returns:
Generator with dicts in Classes schema.
"""
return self._query_get_all_generator('Class', Classes.GET_CLASSES)
8 changes: 8 additions & 0 deletions qbosdk/apis/customers.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ def get(self):
"""
return self._query_get_all('Customer', Customers.GET_CUSTOMERS)

def get_all_generator(self):
"""Get a list of the existing Customers in the Organization.

Returns:
Generator with dicts in Customers schema.
"""
return self._query_get_all_generator('Customer', Customers.GET_CUSTOMERS)

def count(self):
"""Get count of Customers in the Organization.

Expand Down
8 changes: 8 additions & 0 deletions qbosdk/apis/departments.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,11 @@ def get(self):
List with dicts in Departments schema.
"""
return self._query_get_all('Department', Departments.GET_DEPARTMENTS)

def get_all_generator(self):
"""Get a list of the existing Departments in the Organization.

Returns:
Generator with dicts in Departments schema.
"""
return self._query_get_all_generator('Department', Departments.GET_DEPARTMENTS)
8 changes: 8 additions & 0 deletions qbosdk/apis/employees.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ def get(self):
"""
return self._query_get_all('Employee', Employees.GET_EMPLOYEES)

def get_all_generator(self):
"""Get a list of the existing Employees in the Organization.

Returns:
Generator with dicts in Employees schema.
"""
return self._query_get_all_generator('Employee', Employees.GET_EMPLOYEES)

def post(self, data: Dict):
"""
Post Employee to Quickbooks Online
Expand Down
12 changes: 12 additions & 0 deletions qbosdk/apis/exchange_rates.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,18 @@ def get(self, as_of_date: str = None):
ExchangeRates.GET_EXCHANGE_RATES = ExchangeRates.GET_EXCHANGE_RATES.format(as_of_date, '{0}')
return self._query_get_all('ExchangeRate', ExchangeRates.GET_EXCHANGE_RATES)

def get_all_generator(self, as_of_date: str = None):
"""
Get all the exchange rates
:param as_of_date: date to get rates for (1 day prior if left empty)
:return: List of Dicts for exchange rates
"""
if not as_of_date:
as_of_date = (datetime.now() - timedelta(days=1)).strftime("%Y-%m-%d")

ExchangeRates.GET_EXCHANGE_RATES = ExchangeRates.GET_EXCHANGE_RATES.format(as_of_date, '{0}')
return self._query_get_all_generator('ExchangeRate', ExchangeRates.GET_EXCHANGE_RATES)

def get_by_source(self, source_currency_code: str):
"""
Get all the exchange rates
Expand Down
8 changes: 8 additions & 0 deletions qbosdk/apis/items.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,11 @@ def get(self):
List with dicts in Items schema.
"""
return self._query_get_all('Item', Items.GET_ITEMS)

def get_all_generator(self):
"""Get a list of the existing items in the Organization.

Returns:
Generator with dicts in Items schema.
"""
return self._query_get_all_generator('Item', Items.GET_ITEMS)
7 changes: 7 additions & 0 deletions qbosdk/apis/journal_entries.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ def get(self):
"""
return self._query_get_all('JournalEntry', JournalEntries.GET_JOURNAL_ENTRIES)

def get_all_generator(self):
"""
Get all JournalEntries
:return: Generator with Dicts in JournalEntry Schema
"""
return self._query_get_all_generator('JournalEntry', JournalEntries.GET_JOURNAL_ENTRIES)

def post(self, data: Dict):
"""
Post JournalEntry to Quickbooks Online
Expand Down
7 changes: 7 additions & 0 deletions qbosdk/apis/purchases.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ def get(self):
"""
return self._query_get_all('Purchase', Purchases.GET_PURCHASES)

def get_all_generator(self):
"""
Get all Purchases
:return: Generator with Dicts in Purchase Schema
"""
return self._query_get_all_generator('Purchase', Purchases.GET_PURCHASES)

def post(self, data: Dict):
"""
Post Purchase (check, etc) to Quickbooks Online
Expand Down
8 changes: 8 additions & 0 deletions qbosdk/apis/tax_codes.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,11 @@ def get(self):
Dict in TaxCode schema.
"""
return self._query_get_all('TaxCode', TaxCodes.GET_TAX_CODES)

def get_all_generator(self):
"""Get a list of the existing Tax Code in the Organization.

Returns:
Generator with dicts in TaxCode schema.
"""
return self._query_get_all_generator('TaxCode', TaxCodes.GET_TAX_CODES)
7 changes: 7 additions & 0 deletions qbosdk/apis/tax_rates.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ def get(self):
"""
return self._query_get_all('TaxRate', TaxRates.GET_TAX_RATES)

def get_all_generator(self):
"""
Get all Taxrates
:return: Generator of Dicts in Taxrates Schema
"""
return self._query_get_all_generator('TaxRate', TaxRates.GET_TAX_RATES)

def get_by_id(self, taxrateId: str):
"""
Get Taxrates from Quickbooks Online
Expand Down
8 changes: 8 additions & 0 deletions qbosdk/apis/vendors.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ def get(self):
"""
return self._query_get_all('Vendor', Vendors.GET_VENDORS)

def get_all_generator(self):
"""Get a list of the existing Vendors in the Organization.

Returns:
Generator with dicts in Vendors schema.
"""
return self._query_get_all_generator('Vendor', Vendors.GET_VENDORS)

def post(self, data: Dict):
"""
Post Vendor to Quickbooks Online
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

setuptools.setup(
name='qbosdk',
version='0.16.1',
version='0.17.0',
author='Shwetabh Kumar',
author_email='[email protected]',
description='Python SDK for accessing Quickbooks Online APIs',
Expand Down