|
2 | 2 | from dataclasses import dataclass |
3 | 3 |
|
4 | 4 | from conSys4Py import APIResourceTypes, APITerms |
| 5 | +from conSys4Py.con_sys_api import ConnectedSystemAPIRequest |
5 | 6 |
|
6 | 7 |
|
7 | 8 | def determine_parent_type(res_type: APIResourceTypes): |
@@ -77,30 +78,73 @@ class APIHelper(ABC): |
77 | 78 | password = None |
78 | 79 | user_auth = False |
79 | 80 |
|
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): |
81 | 83 | """ |
82 | 84 | Creates a resource of the given type with the given data, will attempt to create a sub-resource if parent_res_id |
83 | 85 | is provided. |
84 | 86 | :param res_type: |
85 | 87 | :param json_data: |
86 | 88 | :param parent_res_id: |
| 89 | + :param from_collection: |
87 | 90 | :return: |
88 | 91 | """ |
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() |
90 | 96 |
|
91 | 97 | def retrieve_resource(self, res_type: APIResourceTypes, res_id: str, parent_res_id: str = None, |
92 | 98 | from_collection: bool = False, |
93 | 99 | 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() |
95 | 113 |
|
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() |
98 | 130 |
|
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() |
101 | 145 |
|
102 | 146 | # 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, |
104 | 148 | from_collection: bool = False): |
105 | 149 | if res_type is None: |
106 | 150 | 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): |
129 | 173 | url = f'{url}/{res_id}' |
130 | 174 |
|
131 | 175 | 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