Skip to content

Commit 3addda1

Browse files
implement first pass functionality of the APIHelper class
1 parent 87528ee commit 3addda1

File tree

1 file changed

+57
-8
lines changed

1 file changed

+57
-8
lines changed

conSys4Py/core/default_api_helpers.py

Lines changed: 57 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from dataclasses import dataclass
33

44
from conSys4Py import APIResourceTypes, APITerms
5+
from conSys4Py.con_sys_api import ConnectedSystemAPIRequest
56

67

78
def determine_parent_type(res_type: APIResourceTypes):
@@ -77,30 +78,73 @@ class APIHelper(ABC):
7778
password = None
7879
user_auth = False
7980

80-
def create_resource(self, res_type: APIResourceTypes, json_data: any, parent_res_id: str = None):
81+
def create_resource(self, res_type: APIResourceTypes, json_data: any, parent_res_id: str = None,
82+
from_collection: bool = False):
8183
"""
8284
Creates a resource of the given type with the given data, will attempt to create a sub-resource if parent_res_id
8385
is provided.
8486
:param res_type:
8587
:param json_data:
8688
:param parent_res_id:
89+
:param from_collection:
8790
:return:
8891
"""
89-
pass
92+
url = self.resource_url_resolver(res_type, None, parent_res_id, from_collection)
93+
api_request = ConnectedSystemAPIRequest(url=url, request_method='POST', auth=self.get_helper_auth(),
94+
body=json_data)
95+
return api_request.make_request()
9096

9197
def retrieve_resource(self, res_type: APIResourceTypes, res_id: str, parent_res_id: str = None,
9298
from_collection: bool = False,
9399
collection_id: str = None):
94-
pass
100+
"""
101+
Retrieves a resource or list of resources if no res_id is provided, will attempt to retrieve a sub-resource if
102+
parent_res_id is provided.
103+
:param res_type:
104+
:param res_id:
105+
:param parent_res_id:
106+
:param from_collection:
107+
:param collection_id:
108+
:return:
109+
"""
110+
url = self.resource_url_resolver(res_type, res_id, parent_res_id, from_collection)
111+
api_request = ConnectedSystemAPIRequest(url=url, request_method='GET', auth=self.get_helper_auth())
112+
return api_request.make_request()
95113

96-
def update_resource(self, res_type: APIResourceTypes, res_id: str, json_data: any, parent_res_id: str = None):
97-
pass
114+
def update_resource(self, res_type: APIResourceTypes, res_id: str, json_data: any, parent_res_id: str = None,
115+
from_collection: bool = False):
116+
"""
117+
Updates a resource of the given type by its id, if necessary, will attempt to update a sub-resource if
118+
parent_res_id is provided.
119+
:param res_type:
120+
:param res_id:
121+
:param json_data:
122+
:param parent_res_id:
123+
:param from_collection:
124+
:return:
125+
"""
126+
url = self.resource_url_resolver(res_type, None, parent_res_id, from_collection)
127+
api_request = ConnectedSystemAPIRequest(url=url, request_method='PUT', auth=self.get_helper_auth(),
128+
body=json_data)
129+
return api_request.make_request()
98130

99-
def delete_resource(self, res_type: APIResourceTypes, res_id: str, parent_res_id: str = None):
100-
pass
131+
def delete_resource(self, res_type: APIResourceTypes, res_id: str, parent_res_id: str = None,
132+
from_collection: bool = False):
133+
"""
134+
Deletes a resource of the given type by its id, if necessary, will attempt to delete a sub-resource if
135+
parent_res_id is provided.
136+
:param res_type:
137+
:param res_id:
138+
:param parent_res_id:
139+
:param from_collection:
140+
:return:
141+
"""
142+
url = self.resource_url_resolver(res_type, res_id, parent_res_id, from_collection)
143+
api_request = ConnectedSystemAPIRequest(url=url, request_method='DELETE', auth=self.get_helper_auth())
144+
return api_request.make_request()
101145

102146
# Helpers
103-
def resource_url_resolver(self, res_type: APIResourceTypes, res_id: str, parent_res_id: str = None,
147+
def resource_url_resolver(self, res_type: APIResourceTypes, res_id: str = None, parent_res_id: str = None,
104148
from_collection: bool = False):
105149
if res_type is None:
106150
raise ValueError('Resource type must contain a valid APIResourceType')
@@ -129,3 +173,8 @@ def construct_url(self, parent_type, res_id, res_type, parent_res_id):
129173
url = f'{url}/{res_id}'
130174

131175
return url
176+
177+
def get_helper_auth(self):
178+
if self.user_auth:
179+
return self.username, self.password
180+
return None

0 commit comments

Comments
 (0)