fix: use a copy of the client headers when setting content-type on specific method calls

This commit is contained in:
Stevan Freeborn
2024-12-18 17:58:11 -06:00
parent 627d21154f
commit ce787860f4
+35 -23
View File
@@ -15,6 +15,7 @@ class OnspringClient:
baseUrl (`str`): The url that should be used as the base for all requests made by the client.
headers (`dict`): Contains key value pairs of the headers necessary for all requests made by the client including the necessary api key.
"""
def __init__(self, url: str, key: str):
self.baseUrl = url
self.headers = {
@@ -183,7 +184,8 @@ class OnspringClient:
endpoint = GetAppsByIdsEndpoint(self.baseUrl)
self.headers['Content-Type'] = 'application/json'
headers = self.headers.copy()
headers['Content-Type'] = 'application/json'
# make sure appIds can be serialized to json string
if not isinstance(appIds, (list, tuple)):
@@ -196,7 +198,7 @@ class OnspringClient:
response = requests.request(
'POST',
endpoint,
headers=self.headers,
headers=headers,
data=appIds)
if response.status_code == 401:
@@ -344,7 +346,8 @@ class OnspringClient:
endpoint = GetFieldsByIdsEndpoint(self.baseUrl)
self.headers['Content-Type'] = 'application/json'
headers = self.headers.copy()
headers['Content-Type'] = 'application/json'
# make sure fieldIds can be serialized to json string
if not isinstance(fieldIds, (list, tuple)):
@@ -357,7 +360,7 @@ class OnspringClient:
response = requests.request(
'POST',
endpoint,
headers=self.headers,
headers=headers,
data=fieldIds)
if response.status_code == 401:
@@ -752,7 +755,6 @@ class OnspringClient:
data,
raw=response)
return ApiResponse(
response.status_code,
raw=response)
@@ -773,7 +775,8 @@ class OnspringClient:
files = [
(
'File',
(saveFileRequest.fileName, open(saveFileRequest.filePath,'rb'),saveFileRequest.contentType)
(saveFileRequest.fileName, open(
saveFileRequest.filePath, 'rb'), saveFileRequest.contentType)
)
]
@@ -805,7 +808,7 @@ class OnspringClient:
message='Unauthorized request',
raw=response)
if response.status_code in [403,404,500]:
if response.status_code in [403, 404, 500]:
jsonResponse = dict(response.json())
@@ -842,9 +845,11 @@ class OnspringClient:
An ApiResponse (`Models.ApiResponse`) containing the results of the request.
"""
endpoint = AddOrUpdateListItemEndpoint(self.baseUrl, listItemRequest.listId)
endpoint = AddOrUpdateListItemEndpoint(
self.baseUrl, listItemRequest.listId)
self.headers['Content-Type'] = 'application/json'
headers = self.headers.copy()
headers['Content-Type'] = 'application/json'
del listItemRequest.__dict__['listId']
@@ -853,7 +858,7 @@ class OnspringClient:
response = requests.request(
'PUT',
endpoint,
headers=self.headers,
headers=headers,
data=requestData)
if response.status_code == 401:
@@ -863,7 +868,7 @@ class OnspringClient:
message='Unauthorized request',
raw=response)
if response.status_code in [403,404]:
if response.status_code in [403, 404]:
jsonResponse = dict(response.json())
@@ -966,7 +971,8 @@ class OnspringClient:
An ApiResponse (`Models.ApiResponse`) containing the results of the request.
"""
endpoint = GetRecordsByAppIdEndpoint(self.baseUrl, getRecordsByAppRequest.appId)
endpoint = GetRecordsByAppIdEndpoint(
self.baseUrl, getRecordsByAppRequest.appId)
params = getRecordsByAppRequest.__dict__
del params['appId']
@@ -1060,7 +1066,8 @@ class OnspringClient:
An ApiResponse (`Models.ApiResponse`) containing the results of the request.
"""
endpoint = GetRecordByIdEndpoint(self.baseUrl, getRecordByIdRequest.appId, getRecordByIdRequest.recordId)
endpoint = GetRecordByIdEndpoint(
self.baseUrl, getRecordByIdRequest.appId, getRecordByIdRequest.recordId)
params = getRecordByIdRequest.__dict__
del params['appId']
@@ -1193,14 +1200,15 @@ class OnspringClient:
endpoint = GetRecordsByIdsEndpoint(self.baseUrl)
self.headers['Content-Type'] = 'application/json'
headers = self.headers.copy()
headers['Content-Type'] = 'application/json'
requestData = json.dumps(getBatchRecordsRequest.__dict__)
response = requests.request(
'POST',
endpoint,
headers=self.headers,
headers=headers,
data=requestData)
if response.status_code == 400:
@@ -1284,7 +1292,8 @@ class OnspringClient:
endpoint = QueryRecordsEndpoint(self.baseUrl)
self.headers['Content-Type'] = 'application/json'
headers = self.headers.copy()
headers['Content-Type'] = 'application/json'
requestData = queryRecordsRequest.__dict__
@@ -1297,7 +1306,7 @@ class OnspringClient:
response = requests.request(
'POST',
endpoint,
headers=self.headers,
headers=headers,
data=requestData,
params=params)
@@ -1385,7 +1394,8 @@ class OnspringClient:
endpoint = AddOrUpdateRecordEndpoint(self.baseUrl)
self.headers['Content-Type'] = 'application/json'
headers = self.headers.copy()
headers['Content-Type'] = 'application/json'
fieldsDict = {}
@@ -1399,7 +1409,7 @@ class OnspringClient:
response = requests.request(
'PUT',
endpoint,
headers=self.headers,
headers=headers,
data=requestData)
if response.status_code == 400:
@@ -1461,14 +1471,15 @@ class OnspringClient:
endpoint = DeleteRecordsByIds(self.baseUrl)
self.headers['Content-Type'] = 'application/json'
headers = self.headers.copy()
headers['Content-Type'] = 'application/json'
requestData = json.dumps(deleteBatchRecordsRequest.__dict__)
response = requests.request(
'POST',
endpoint,
headers=self.headers,
headers=headers,
data=requestData)
if response.status_code == 400:
@@ -1525,7 +1536,8 @@ class OnspringClient:
An ApiResponse (`Models.ApiResponse`) containing the results of the request.
"""
endpoint = GetReportByIdEndpoint(self.baseUrl, getReportByIdRequest.reportId)
endpoint = GetReportByIdEndpoint(
self.baseUrl, getReportByIdRequest.reportId)
params = getReportByIdRequest.__dict__
del params['reportId']
@@ -1595,7 +1607,7 @@ class OnspringClient:
response.status_code,
raw=response)
def GetReportsByAppId(self, appId: int, pagingRequest: PagingRequest=PagingRequest(1,50)) -> ApiResponse:
def GetReportsByAppId(self, appId: int, pagingRequest: PagingRequest = PagingRequest(1, 50)) -> ApiResponse:
"""
Get reports for an app by its id..