From d8404f37c99a786fd70a71b991d451a07f4526e6 Mon Sep 17 00:00:00 2001 From: Shwetabh Kumar Date: Thu, 25 Jan 2024 11:28:38 +0530 Subject: [PATCH] Fixing Company ID allocation for Batch URLs (#16) --- dynamics/apis/api_base.py | 13 +++---------- dynamics/apis/invoice_line_items.py | 13 ++++++++++--- dynamics/apis/journal_line_items.py | 11 ++++++++--- dynamics/core/client.py | 13 ------------- setup.py | 2 +- 5 files changed, 22 insertions(+), 30 deletions(-) diff --git a/dynamics/apis/api_base.py b/dynamics/apis/api_base.py index 5df9cf2..b05e37c 100644 --- a/dynamics/apis/api_base.py +++ b/dynamics/apis/api_base.py @@ -18,7 +18,6 @@ def __init__(self): self.__access_token = None self.__server_url = None self.__batch_url = None - self.company_id = None def change_access_token(self, access_token): """Change the old access token with the new one. @@ -44,14 +43,6 @@ def set_batch_url(self, batch_url): """ self.__batch_url = batch_url - def set_company_id(self, company_id): - """Set the company id dynamically upon creating a connection - - Parameters: - company_id(str): The current company id - """ - self.company_id = company_id - def _get_request(self, params, api_url): """Create a HTTP GET request. @@ -252,7 +243,7 @@ def _delete_request(self, params, api_url): else: raise DynamicsError('Error: {0}'.format(response.status_code), response.text) - def _bulk_post_request(self, data, isolation: str, purchase_invoice_id: str = None): + def _bulk_post_request(self, data, isolation: str, company_id: str = None, purchase_invoice_id: str = None): """Create a HTTP batch request. Parameters: @@ -262,6 +253,8 @@ def _bulk_post_request(self, data, isolation: str, purchase_invoice_id: str = No Returns: A response from the request (dict). """ + if company_id: + self.__batch_url = f'{self.__batch_url}?company={company_id}' api_headers = { 'Authorization': self.__access_token, diff --git a/dynamics/apis/invoice_line_items.py b/dynamics/apis/invoice_line_items.py index 9d52ce2..7c25b39 100644 --- a/dynamics/apis/invoice_line_items.py +++ b/dynamics/apis/invoice_line_items.py @@ -40,24 +40,26 @@ def delete(self, purchase_invoice_lineitem_id: str, **kwargs): """ return self._delete_request({**kwargs}, PurchaseInvoiceLineItems.DELETE_PURCHASE_INVOICE_LINEITEM.format(purchase_invoice_lineitem_id)) - def bulk_post(self, purchase_invoice_id: str, line_items: list, isolation: str = 'snapshot'): + def bulk_post(self, purchase_invoice_id: str, line_items: list, company_id: str, isolation: str = 'snapshot'): """ Create PurchaseInvoice LineItems in bulk. :param purchase_invoice_id: The ID of the purchase invoice. :param line_items: A list of line items to be added to the purchase invoice. + :param company_id: The ID of the company. (batch requests mandatorily need this) :param isolation: The isolation level of the bulk post request. :return: Bulk response containing the results of the bulk post operation. """ # Prepare payload for bulk post bulk_payload = [] + for line_item in line_items: # Prepare payload for each line item line_item_payload = { "method": "POST", "url": PurchaseInvoiceLineItems.BULK_POST_PURCHASE_INVOICE_LINEITEM.format(purchase_invoice_id), "headers": { - "CompanyId": self.company_id, + "CompanyId": company_id, "Content-Type": "application/json", "If-Match": "*" }, @@ -69,4 +71,9 @@ def bulk_post(self, purchase_invoice_id: str, line_items: list, isolation: str = bulk_request_payload = {'requests': bulk_payload} # Make the bulk post request - return self._bulk_post_request(bulk_request_payload, isolation, purchase_invoice_id) + return self._bulk_post_request( + data=bulk_request_payload, + isolation=isolation, + purchase_invoice_id=purchase_invoice_id, + company_id=company_id + ) diff --git a/dynamics/apis/journal_line_items.py b/dynamics/apis/journal_line_items.py index abb9756..ea549ef 100644 --- a/dynamics/apis/journal_line_items.py +++ b/dynamics/apis/journal_line_items.py @@ -37,12 +37,13 @@ def delete(self, jounral_lineitem_id: str, **kwargs): """ return self._delete_request({**kwargs}, JournalLineItem.DELETE_JOURNAL_LINE_ITEMS.format(jounral_lineitem_id)) - def bulk_post(self, journal_id: str, line_items: list, isolation: str = 'snapshot'): + def bulk_post(self, journal_id: str, line_items: list, company_id: str, isolation: str = 'snapshot'): """ Create Journal LineItems in bulk. :param journal_id: The ID of the journal. :param line_items: A list of line items to be added to the journal line items. + :param company_id: The ID of the company. (batch requests mandatorily need this) :param isolation: The isolation level of the bulk post request. :return: Bulk response containing the results of the bulk post operation. """ @@ -54,7 +55,7 @@ def bulk_post(self, journal_id: str, line_items: list, isolation: str = 'snapsho "method": "POST", "url": JournalLineItem.BULK_POST_JOURNAL_LINEITEM.format(journal_id), "headers": { - "CompanyId": self.company_id, + "CompanyId": company_id, "Content-Type": "application/json", "If-Match": "*" }, @@ -66,4 +67,8 @@ def bulk_post(self, journal_id: str, line_items: list, isolation: str = 'snapsho bulk_request_payload = {'requests': bulk_payload} # Make the bulk post request - return self._bulk_post_request(bulk_request_payload, isolation) + return self._bulk_post_request( + data=bulk_request_payload, + isolation=isolation, + company_id=company_id + ) diff --git a/dynamics/core/client.py b/dynamics/core/client.py index 3808628..b49f600 100644 --- a/dynamics/core/client.py +++ b/dynamics/core/client.py @@ -75,19 +75,6 @@ def update_access_token(self, access_token: str): for api in api_instances: api.change_access_token(token) - def set_company_id(self, company_id: str): - """ - Set the Company ID in all API objects. - """ - api_instances = [ - self.purchase_invoice_line_items, - self.journal_line_items - ] - - # Set company ID for all API instances - for api in api_instances: - api.set_company_id(company_id) - def set_batch_url(self): """ Set the Batch URL in all API objects. diff --git a/setup.py b/setup.py index 9ed1475..96a99da 100644 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ setuptools.setup( name='ms-dynamics-business-central-sdk', - version='1.3.4', + version='1.4.0', author='Shwetabh Kumar', author_email='shwetabh.kumar@fyle.in', description='Python SDK for accessing Dynamics APIs',