From 1fd0dca24cee190fb2e14175e46f22d3e5d21bb4 Mon Sep 17 00:00:00 2001 From: knucklessg1 Date: Mon, 13 May 2024 13:52:39 -0500 Subject: [PATCH] Fixed data json dump. --- servicenow_api/servicenow_api.py | 49 ++++++++++++++++++-------------- servicenow_api/version.py | 2 +- test/test_servicenow_models.py | 2 +- 3 files changed, 29 insertions(+), 24 deletions(-) diff --git a/servicenow_api/servicenow_api.py b/servicenow_api/servicenow_api.py index 6319db2..788a611 100644 --- a/servicenow_api/servicenow_api.py +++ b/servicenow_api/servicenow_api.py @@ -2,6 +2,7 @@ # coding: utf-8 import requests +import json from requests import Response import urllib3 from base64 import b64encode @@ -89,14 +90,16 @@ def __init__( "password": password, } try: - response = requests.post(url=self.auth_url, data=self.auth_data, headers=self.auth_headers) + response = requests.post( + url=self.auth_url, data=self.auth_data, headers=self.auth_headers + ) response = response.json() self.token = response["access_token"] except Exception as e: print(f"Error Authenticating with OAuth: \n\n{e}") raise e self.headers = { - "Authorization": f'Bearer {self.token}', + "Authorization": f"Bearer {self.token}", "Content-Type": "application/json", } elif username and password: @@ -135,15 +138,17 @@ def refresh_auth_token(self) -> Response: """ refresh_data = { - 'grant_type': "refresh_token", - 'client_id': self.auth_data['client_id'], - 'client_secret': self.auth_data['client_secret'], - 'refresh_token': self.token + "grant_type": "refresh_token", + "client_id": self.auth_data["client_id"], + "client_secret": self.auth_data["client_secret"], + "refresh_token": self.token, } try: - response = requests.post(url=self.auth_url, data=refresh_data, headers=self.auth_headers) + response = requests.post( + url=self.auth_url, data=refresh_data, headers=self.auth_headers + ) response_json = response.json() - self.token = response_json['access_token'] + self.token = response_json["access_token"] except ValidationError as e: raise ParameterError(f"Invalid parameters: {e.errors()}") return response @@ -315,7 +320,7 @@ def batch_install(self, **kwargs) -> Response: url=f"{self.url}/sn_cicd/app/batch/install", headers=self.headers, verify=self.verify, - data=cicd.data, + data=json.dumps(cicd.data, indent=2), proxies=self.proxies, ) except ValidationError as e: @@ -550,7 +555,7 @@ def suite_scan(self, **kwargs) -> Response: response = self._session.post( url=f"{self.url}/sn_cicd/instance_scan/suite_scan/{cicd.suite_sys_id}/{cicd.scan_type}", headers=self.headers, - data=cicd.data, + data=json.dumps(cicd.data, indent=2), verify=self.verify, proxies=self.proxies, ) @@ -1362,7 +1367,7 @@ def create_change_request(self, **kwargs) -> Response: url=f"{self.url}/sn_chg_rest" f"/change{change_type}{standard_change_template_id}", headers=self.headers, - data=change_request.data, + data=json.dumps(change_request.data, indent=2), verify=self.verify, proxies=self.proxies, ) @@ -1397,7 +1402,7 @@ def create_change_request_task(self, **kwargs) -> Response: response = self._session.post( url=f"{self.url}/sn_chg_rest/change/task", headers=self.headers, - data=change_request.data, + data=json.dumps(change_request.data, indent=2), verify=self.verify, proxies=self.proxies, ) @@ -1439,7 +1444,7 @@ def create_change_request_ci_association(self, **kwargs) -> Response: url=f"{self.url}/sn_chg_rest" f"/change/{change_request.change_request_sys_id}/ci", headers=self.headers, - data=change_request.data, + data=json.dumps(change_request.data, indent=2), verify=self.verify, proxies=self.proxies, ) @@ -1564,7 +1569,7 @@ def approve_change_request(self, **kwargs) -> Response: f"/change/{change_request.change_request_sys_id}/approvals", headers=self.headers, verify=self.verify, - data=change_request.data, + data=json.dumps(change_request.data, indent=2), proxies=self.proxies, ) except ValidationError as e: @@ -1608,7 +1613,7 @@ def update_change_request(self, **kwargs) -> Response: url=f"{self.url}/sn_chg_rest" f"/change{change_type}/{change_request.change_request_sys_id}", headers=self.headers, - data=change_request.data, + data=json.dumps(change_request.data, indent=2), verify=self.verify, proxies=self.proxies, ) @@ -1679,7 +1684,7 @@ def update_change_request_task(self, **kwargs) -> Response: f"/sn_chg_rest/change/{change_request.change_request_sys_id}" f"/task/{change_request.change_request_task_sys_id}", headers=self.headers, - data=change_request.data, + data=json.dumps(change_request.data, indent=2), verify=self.verify, proxies=self.proxies, ) @@ -1880,7 +1885,7 @@ def insert_import_set(self, **kwargs) -> Response: response = self._session.post( url=f"{self.url}/now/import/{import_set.table}", headers=self.headers, - data=import_set.data, + data=json.dumps(import_set.data, indent=2), verify=self.verify, proxies=self.proxies, ) @@ -1911,7 +1916,7 @@ def insert_multiple_import_sets(self, **kwargs) -> Response: response = self._session.post( url=f"{self.url}/now/import/{import_set.table}/insertMultiple", headers=self.headers, - data=import_set.data, + data=json.dumps(import_set.data, indent=2), verify=self.verify, proxies=self.proxies, ) @@ -1971,7 +1976,7 @@ def create_incident(self, **kwargs) -> Response: url=f"{self.url}/now/table/incident", headers=self.headers, verify=self.verify, - data=incident.data, + data=json.dumps(incident.data, indent=2), ) except ValidationError as e: raise ParameterError(f"Invalid parameters: {e.errors()}") @@ -2335,7 +2340,7 @@ def patch_table_record(self, **kwargs) -> Response: try: response = self._session.patch( url=f"{self.url}/now/table/{table.table}/{table.table_record_sys_id}", - data=table.data, + data=json.dumps(table.data, indent=2), headers=self.headers, verify=self.verify, proxies=self.proxies, @@ -2372,7 +2377,7 @@ def update_table_record(self, **kwargs) -> Response: try: response = self._session.put( url=f"{self.url}/now/table/{table}/{table.table_record_sys_id}", - data=table.data, + data=json.dumps(table.data, indent=2), headers=self.headers, verify=self.verify, proxies=self.proxies, @@ -2403,7 +2408,7 @@ def add_table_record(self, **kwargs) -> Response: try: response = self._session.post( url=f"{self.url}/now/table/{table}", - data=table.data, + data=json.dumps(table.data, indent=2), headers=self.headers, verify=self.verify, proxies=self.proxies, diff --git a/servicenow_api/version.py b/servicenow_api/version.py index 2051988..bb29438 100644 --- a/servicenow_api/version.py +++ b/servicenow_api/version.py @@ -1,6 +1,6 @@ #!/usr/bin/env python # coding: utf-8 -__version__ = '0.20.21' +__version__ = "0.20.21" __author__ = "Audel Rouhi" __credits__ = "Audel Rouhi" diff --git a/test/test_servicenow_models.py b/test/test_servicenow_models.py index 4834eb6..5a54c8e 100644 --- a/test/test_servicenow_models.py +++ b/test/test_servicenow_models.py @@ -101,7 +101,7 @@ def test_servicenow_import_set(): @pytest.mark.skipif( sys.platform in ["darwin"] or skip, reason=reason, - ) +) def test_servicenow_cmdb(): cmdb_id = "asdofaisudfa098098as0df9a8s" cmdb = CMDBModel(cmdb_id=cmdb_id)