Skip to content

Commit

Permalink
Fixed data json dump.
Browse files Browse the repository at this point in the history
  • Loading branch information
Knucklessg1 committed May 13, 2024
1 parent 3aa1a6e commit 1fd0dca
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 24 deletions.
49 changes: 27 additions & 22 deletions servicenow_api/servicenow_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# coding: utf-8

import requests
import json
from requests import Response
import urllib3
from base64 import b64encode
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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,
)
Expand Down Expand Up @@ -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,
)
Expand Down Expand Up @@ -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,
)
Expand Down Expand Up @@ -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,
)
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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,
)
Expand Down Expand Up @@ -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,
)
Expand Down Expand Up @@ -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,
)
Expand Down Expand Up @@ -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,
)
Expand Down Expand Up @@ -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()}")
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion servicenow_api/version.py
Original file line number Diff line number Diff line change
@@ -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"
2 changes: 1 addition & 1 deletion test/test_servicenow_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 1fd0dca

Please sign in to comment.