diff --git a/python/kadalu_content_apis/__init__.py b/python/kadalu_content_apis/__init__.py index 437f409..45ce26d 100644 --- a/python/kadalu_content_apis/__init__.py +++ b/python/kadalu_content_apis/__init__.py @@ -94,19 +94,19 @@ def object(self, path): def create_template(self, name, content, template_type, output_type="text", public=False): """ Create Template """ - return Template.create(self, name, content, template_type, output_type, public) + return Template.create(self, "/", name, content, template_type, output_type, public) def upload_template(self, file_path, template_type, name="", output_type="text", public=False): """ Upload Template """ - return Template.upload_create(self, file_path, template_type, name, output_type, public) + return Template.upload_create(self, "/", file_path, template_type, name, output_type, public) def list_templates(self, page=1, page_size=30): """ List all templated """ - return Template.list_templates(self, page, page_size) + return Template.list_templates(self, "/", page, page_size) def template(self, name): """ Return Template instance """ - return Template(self, name) + return Template(self, "/", name) diff --git a/python/kadalu_content_apis/folders.py b/python/kadalu_content_apis/folders.py index 11edd08..52d413e 100644 --- a/python/kadalu_content_apis/folders.py +++ b/python/kadalu_content_apis/folders.py @@ -84,6 +84,21 @@ def delete(self, recursive=False): resp = self.conn.http_delete(f"{self.conn.url}/api/folders/{self.name}?recursive={recursive}") return response_object_or_error(Folder, resp, 204) + def create_template(self, name, content, template_type, output_type="text", public=False) + """ Create template with folder-name """ + return Template.create(self, self.name, name, content, template_type, output_type, public) + + def upload_template(self, file_path, template_type, name="", output_type="text", public=False): + """ Upload Template with-folder-name""" + return Template.upload_create(self, self.name, file_path, template_type, name, output_type, public) + + def list_templates(self, page=1, page_size=30): + """ List all templates with folder-name""" + return Template.list_templates(self, self.name, page, page_size) + + def template(self, name): + """ Return Template instance with folder-name""" + return Template(self, self.name, name) def create_object(self, path, data, object_type, threads=False, template=None): """ Create object with folder-name """ diff --git a/python/kadalu_content_apis/templates.py b/python/kadalu_content_apis/templates.py index 61e188d..35f2828 100644 --- a/python/kadalu_content_apis/templates.py +++ b/python/kadalu_content_apis/templates.py @@ -3,23 +3,32 @@ from kadalu_content_apis.helpers import response_object_or_error, Generic class Template(Generic): - def __init__(self, conn=None, name=None, data={}): + def __init__(self, conn=None, folder_name=None, name=None, data={}): """ Intialise Template """ super().__init__(data) if conn is not None: self.conn = conn + if folder_name is not None: + self.folder_name = folder_name + if name is not None: self.name = name # TODO: Handle Invalid Region Name, when only name is passed. @classmethod - def create(cls, conn, name, content, template_type, output_type, public): + def create(cls, conn, folder_name, name, content, template_type, output_type, public): """ Create template """ + folder_name = folder_name.lstrip("/") + if folder_name == "": + url = f"{conn.url}/api/templates" + else: + url = f"{conn.url}/api/folders/{folder_name}/templates" + resp = conn.http_post( - f"{conn.url}/api/templates", + url, { "name" : name, "content": content, @@ -35,7 +44,13 @@ def create(cls, conn, name, content, template_type, output_type, public): return outdata @classmethod - def upload_create(cls, conn, file_path, template_type, name, output_type, public): + def upload_create(cls, conn, folder_name, file_path, template_type, name, output_type, public): + + folder_name = folder_name.lstrip("/") + if folder_name == "": + url = f"{conn.url}/api/templates" + else: + url = f"{conn.url}/api/folders/{folder_name}/templates" file_content = "" with open(file_path, 'rb') as file: @@ -56,13 +71,13 @@ def upload_create(cls, conn, file_path, template_type, name, output_type, public "content": (file_path, file_content) } - resp = conn.http_post_upload(f"{conn.url}/api/templates", data, files) + resp = conn.http_post_upload(url, data, files) outdata = response_object_or_error(Template, resp, 201) outdata.conn = conn return outdata - def upload(self, file_path, template_type=None, name=None, output_type=None, public=None): + def upload(self, folder_name, file_path, template_type=None, name=None, output_type=None, public=None): file_content = "" with open(file_path, 'rb') as file: @@ -97,12 +112,16 @@ def upload(self, file_path, template_type=None, name=None, output_type=None, pub return outdata @classmethod - def list_templates(cls, conn, page, page_size): + def list_templates(cls, conn, folder_name, page, page_size): """ List all templates """ - resp = conn.http_get( - f"{conn.url}/api/templates?page={page}&page_size={page_size}" - ) + folder_name = folder_name.lstrip("/") + if folder_name == "": + url = f"{conn.url}/api/templates" + else: + url = f"{conn.url}/api/folders/{folder_name}/templates" + + resp = conn.http_get(url) templates = response_object_or_error(Template, resp, 200) def update_data(tmpl): @@ -115,9 +134,13 @@ def update_data(tmpl): def get(self): """ Return a Template """ - resp = self.conn.http_get( - f"{self.conn.url}/api/templates/{self.name}" - ) + folder_name = self.folder_name.lstrip("/") + if folder_name == "": + url = f"{self.conn.url}/api/templates/{self.name}" + else: + url = f"{self.conn.url}/api/folders/{self.folder_name}/templates/{self.name}" + + resp = self.conn.http_get(url) outdata = response_object_or_error(Template, resp, 200) outdata.conn = self.conn @@ -127,8 +150,14 @@ def get(self): def update(self, name=None, content=None, template_type=None, output_type=None, public=None): """ Update Template """ + folder_name = self.folder_name.lstrip("/") + if folder_name == "": + url = f"{self.conn.url}/api/templates/{self.name}" + else: + url = f"{self.conn.url}/api/folders/{self.folder_name}/templates/{self.name}" + resp = self.conn.http_put( - f"{self.conn.url}/api/templates/{self.name}", + url, { "name" : name, "content": content, @@ -150,5 +179,12 @@ def update(self, name=None, content=None, template_type=None, output_type=None, def delete(self): """ Delete Template """ - resp = self.conn.http_delete(f"{self.conn.url}/api/templates/{self.name}") + + folder_name = self.folder_name.lstrip("/") + if self.folder_name == "": + url = f"{self.conn.url}/api/templates/{self.name}" + else: + url = f"{self.conn.url}/api/folders/{folder_name}/templates/{self.name}" + + resp = self.conn.http_delete(url) return response_object_or_error(Template, resp, 204)