Skip to content

Commit

Permalink
Fixing Company ID allocation for Batch URLs (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
Shwetabhk authored Jan 25, 2024
1 parent 544d48b commit d8404f3
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 30 deletions.
13 changes: 3 additions & 10 deletions dynamics/apis/api_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand Down Expand Up @@ -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:
Expand All @@ -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,
Expand Down
13 changes: 10 additions & 3 deletions dynamics/apis/invoice_line_items.py
Original file line number Diff line number Diff line change
Expand Up @@ -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": "*"
},
Expand All @@ -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
)
11 changes: 8 additions & 3 deletions dynamics/apis/journal_line_items.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
"""
Expand All @@ -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": "*"
},
Expand All @@ -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
)
13 changes: 0 additions & 13 deletions dynamics/core/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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='[email protected]',
description='Python SDK for accessing Dynamics APIs',
Expand Down

0 comments on commit d8404f3

Please sign in to comment.