Skip to content

Commit

Permalink
Feat: change api data format
Browse files Browse the repository at this point in the history
Signed-off-by: harlee-x <[email protected]>
  • Loading branch information
harlee-x committed Jul 24, 2024
1 parent f5ba61b commit c637b2c
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def setUp(self):
self.client = TestClient(app=app)
self.catalog_data = {
"mysql": {
"name": "mysql",
"name": "Mysql",
"category": "系统/大数据开发工具",
"description": "mysql",
"i18n": {
Expand Down Expand Up @@ -144,13 +144,15 @@ def test_catalog_category_zh(self):
response = self.client.get("/api/v1/catalogs/category/info")
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json().get("status"), 0)
self.assertEqual(response.json().get("data"), {"系统/大数据开发工具": ["mysql"]})
self.assertEqual(response.json().get("data"), [
{"category": "系统/大数据开发工具", "sub": [{"name": "mysql", "metadataName": "Mysql", "image": ""}]}])

def test_catalog_category1_en(self):
response = self.client.get("/api/v1/catalogs/category/info", headers={"Accept-Language": "en"})
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json().get("status"), 0)
self.assertEqual(response.json().get("data"), {"system.dataManagement": ["mysql"]})
self.assertEqual(response.json().get("data"), [
{"category": "system.dataManagement", "sub": [{"name": "mysql", "metadataName": "Mysql", "image": ""}]}])

def test_catalog_global_empty(self):
response = self.client.get("/api/v1/catalogs/global/info")
Expand Down
3 changes: 3 additions & 0 deletions kdp_catalog_manager/domain/format/format_catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,6 @@ def get_description(self, lang=DEFAULT_LANG):

def get_global(self):
return DictUtils().get_items(self.raw, ["isGlobal"], False)

def get_image(self):
return DictUtils().get_items(self.raw, ["image"], "")
2 changes: 1 addition & 1 deletion kdp_catalog_manager/domain/model/catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class CatalogList(Response):


class CatalogCategory(Response):
data: Dict
data: List


class Catalogglobal(Response):
Expand Down
38 changes: 29 additions & 9 deletions kdp_catalog_manager/domain/service/catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,36 @@ def get_catalog_readme(self):
return FileUtils().read_file(catalog_file)

def get_catalog_category(self):
catalog_category = {}
catalogs_info = self.get_catalogs_info()
for catalog, catalog_info in catalogs_info.items():
category = catalog_info.get('category')
if category not in catalog_category:
catalog_category[category] = [catalog]
catalog_info = {}
catalogs_data = cache_instance.get(CATALOG_KEY)
if not isinstance(catalogs_data, dict):
raise DataTypeError(f"data type is {type(catalogs_data)}")
for catalog, catalog_metadata_info in catalogs_data.items():
catalog_format_obj = FormatCatalog(catalog_metadata_info)
catalog_metadata_name = catalog_format_obj.get_name()
catalog_category_name = catalog_format_obj.get_category(self.lang)
catalog_image = catalog_format_obj.get_image()
if catalog_category_name not in catalog_info:
catalog_info[catalog_category_name] = [{
"name": catalog,
"metadataName": catalog_metadata_name,
"image": catalog_image
}]
continue
if category in catalog_category:
catalog_category[category].append(catalog)
return catalog_category
if catalog_category_name in catalog_info:
catalog_info[catalog_category_name].append({
"name": catalog,
"metadataName": catalog_metadata_name,
"image": catalog_image
})

catalog_categorys = []
for category, catalog_metadata_info in catalog_info.items():
catalog_categorys.append({
"category": category,
"sub": catalog_metadata_info
})
return catalog_categorys

def get_catalog_global(self):
catalog_global_list = []
Expand Down
5 changes: 5 additions & 0 deletions kdp_catalog_manager/domain/service/save_data_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from kdp_catalog_manager.modules.cache.cache import cache_instance
from kdp_catalog_manager.utils.log import log
from kdp_catalog_manager.utils.yamlutils import YAMLUtils
from kdp_catalog_manager.utils.fileutils import FileUtils


class SaveDataToCache(object):
Expand All @@ -31,6 +32,10 @@ def get_catalog_data(self):
continue

catalog_metadata = YAMLUtils().load_all_yaml(catalog_metadata_file)
# get image base64 data to save cache
image_file = os.path.join(CATALOG_DIR, catalog, f"{catalog}.png")
image = FileUtils.get_file_base64(image_file)
catalog_metadata["image"] = image
catalog_data[catalog] = catalog_metadata
rt = self.cache.set(CATALOG_KEY, catalog_data, CACHE_EXPIRE)
if rt:
Expand Down
13 changes: 13 additions & 0 deletions kdp_catalog_manager/domain/service/test/test_catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class TestCatalogController(TestCase):
def setUp(self):
self.catalog_data = {
"mysql": {
"name": "Mysql",
"category": "系统/大数据开发工具",
"description": "mysql",
"i18n": {
Expand Down Expand Up @@ -48,3 +49,15 @@ def test_get_catalog_readme_catalog_is_None(self):
CatalogController(lang="zhs").get_catalog_readme()
except APIRequestedInvalidParamsError:
self.assertEqual(True, True)

def test_get_category(self):
self.catalog_data["mysql"]["image"] = ""
cache_instance.set(CATALOG_KEY, self.catalog_data)
rt = CatalogController().get_catalog_category()
self.assertEqual(rt, [{"category": "系统/大数据开发工具", "sub": [{"name": "mysql", "metadataName": "Mysql", "image": ""}]}])

def test_get_category_en(self):
self.catalog_data["mysql"]["image"] = ""
cache_instance.set(CATALOG_KEY, self.catalog_data)
rt = CatalogController(lang="en").get_catalog_category()
self.assertEqual(rt, [{"category": "system.dataManagement", "sub": [{"name": "mysql", "metadataName": "Mysql", "image": ""}]}])
12 changes: 12 additions & 0 deletions kdp_catalog_manager/utils/fileutils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import base64

from kdp_catalog_manager.exceptions.exception import FileNotExistsError, \
ReadFileError, WriteFileError
Expand Down Expand Up @@ -39,3 +40,14 @@ def write_file(content, target_file):
except Exception as err:
log.error(str(err))
raise WriteFileError(target_file)

@staticmethod
def get_file_base64(target_file):
encoded_string = ""
try:
if os.path.exists(target_file):
with open(target_file, "rb") as f:
encoded_string = base64.b64encode(f.read()).decode()
except Exception as err:
log.error(str(err))
return encoded_string

0 comments on commit c637b2c

Please sign in to comment.