Skip to content

Commit

Permalink
SDK changes to make Template at Folder Level (#27)
Browse files Browse the repository at this point in the history
* python: Add get_rendered()
* tests: Add tests for upload of templates, objects
* SDK changes to make template folder level

Signed-off-by: Shree Vatsa N <[email protected]>
  • Loading branch information
vatsa287 authored Jan 3, 2024
1 parent 0695bb4 commit 11c0ff9
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 19 deletions.
8 changes: 4 additions & 4 deletions python/kadalu_content_apis/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
15 changes: 15 additions & 0 deletions python/kadalu_content_apis/folders.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 """
Expand Down
66 changes: 51 additions & 15 deletions python/kadalu_content_apis/templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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:
Expand All @@ -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:
Expand Down Expand Up @@ -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):
Expand All @@ -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

Expand All @@ -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,
Expand All @@ -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)

0 comments on commit 11c0ff9

Please sign in to comment.