Skip to content

Commit

Permalink
feat: Added count api for account and vendor (#19)
Browse files Browse the repository at this point in the history
* feat: Added count api for account and vendor

* comment resolved

* bump up version

* comment resolved
  • Loading branch information
Ashutosh619-sudo authored Oct 13, 2024
1 parent cb811dc commit 317f4c7
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 2 deletions.
11 changes: 11 additions & 0 deletions dynamics/apis/accounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class Accounts(ApiBase):

GET_ACCOUNTS = '/accounts'
POST_ACCOUNT = '/accounts'
COUNT_ACCOUNT = "/accounts/$count"

def get_all(self, **kwargs):
"""
Expand All @@ -15,3 +16,13 @@ def get_all(self, **kwargs):
return self._get_request({
**kwargs
}, Accounts.GET_ACCOUNTS)['value']

def count(self, **kwargs):
"""
Get counts of accounts
:return: Count in Int
"""
return self._get_request_for_count({
**kwargs
}, Accounts.COUNT_ACCOUNT)['value']

61 changes: 60 additions & 1 deletion dynamics/apis/api_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def _get_request(self, params, api_url):
headers=api_headers,
params=api_params
)

if response.status_code == 200 or response.status_code == 201:
result = json.loads(response.text)
return result
Expand All @@ -100,6 +100,65 @@ def _get_request(self, params, api_url):

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

def _get_request_for_count(self, params, api_url):
"""Create a HTTP GET request for count.
Parameters:
params (dict): HTTP GET parameters for the wanted API.
Returns:
A response from the request (dict).
"""

api_headers = {
'Authorization': self.__access_token,
'Accept': 'application/json'
}
api_params = {}

for k in params:
# ignore all unused params
if not params[k] is None:
p = params[k]

# convert boolean to lowercase string
if isinstance(p, bool):
p = str(p).lower()

api_params[k] = p

response = requests.get(
'{0}{1}'.format(self.__server_url, api_url),
headers=api_headers,
params=api_params
)

if response.status_code == 200 or response.status_code == 201:
raw_content = response.content
result = raw_content.decode('utf-8')
return {"value": result}

elif response.status_code == 400:
raise WrongParamsError('Some of the parameters are wrong', response.text)

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

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

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

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

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

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

def _post_request(self, data, api_url):
"""Create a HTTP post request.
Expand Down
10 changes: 10 additions & 0 deletions dynamics/apis/employees.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class Employees(ApiBase):

GET_EMPLOYEES = '/employees'
POST_EMPLOYEES = '/employees'
COUNT_EMPLOYEES = '/employees/$count'

def get_all(self, **kwargs):
"""
Expand All @@ -23,3 +24,12 @@ def post(self, data):
:return:
"""
return self._post_request(data, Employees.POST_EMPLOYEES)

def count(self, **kwargs):
"""
Get counts of employees
:return: Count in Int
"""
return self._get_request_for_count({
**kwargs
}, Employees.COUNT_EMPLOYEES)['value']
10 changes: 10 additions & 0 deletions dynamics/apis/locations.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class Locations(ApiBase):

GET_LOCATIONS = '/locations'
POST_LOCATIONS = '/locations'
COUNT_LOCATIONS = '/locations/$count'

def get_all(self, **kwargs):
"""
Expand All @@ -15,3 +16,12 @@ def get_all(self, **kwargs):
return self._get_request({
**kwargs
}, Locations.GET_LOCATIONS)['value']

def count(self, **kwargs):
"""
Get counts of locations
:return: Count in Int
"""
return self._get_request_for_count({
**kwargs
}, Locations.COUNT_LOCATIONS)['value']
11 changes: 11 additions & 0 deletions dynamics/apis/vendors.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ class Vendors(ApiBase):

GET_VENDORS = '/vendors'
POST_VENDOR = '/vendors'
COUNT_VENDOR = "/vendors/$count"


def get_all(self, **kwargs):
"""
Expand All @@ -23,3 +25,12 @@ def post(self, data):
:return:
"""
return self._post_request(data, Vendors.POST_VENDOR)

def count(self, **kwargs):
"""
Get counts of vendors
:return: Count in Int
"""
return self._get_request_for_count({
**kwargs
}, Vendors.COUNT_VENDOR)['value']
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.4.2',
version='1.5.0',
author='Shwetabh Kumar',
author_email='[email protected]',
description='Python SDK for accessing Dynamics APIs',
Expand Down

0 comments on commit 317f4c7

Please sign in to comment.