10000 REquest and Response added with some log optimization · cloudintro/python-microservices@a780643 · GitHub
[go: up one dir, main page]

Skip to content

Commit a780643

Browse files
committed
REquest and Response added with some log optimization
1 parent 703f197 commit a780643

16 files changed

+223
-64
lines changed

asset_mgr/.idea/modules.xml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

asset_mgr/app/src/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
# uvicorn app_assetmgr:app_v1 --port 8001 --reload
1+
# uvicorn app_assetmgr:app --port 8001 --reload

asset_mgr/app/src/app_assetmgr.py

Lines changed: 80 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,101 @@
11
from fastapi import FastAPI, Query
22
from logzero import logger as log
3+
from starlette import status
34
from app_services import AssetInfoService
45
from app_models import AssetMgrRequest, AssetMgrResponse
6+
from app_config import *
57

6-
app_v1 = FastAPI()
8+
app = FastAPI()
79

810
asset_service = AssetInfoService()
911

1012

11-
@app_v1.get("/v1/assetmgr/{assetid}")
12-
def get_asset_info(assetid: str = "-1"):
13-
log.info(f"get_asset_info {assetid}")
14-
result = asset_service.get_asset_info(assetid)
15-
return {"asset_info": result}
13+
@app.get(
14+
"/v1/assetmgr/{assetid}",
15+
response_model=AssetMgrResponse,
16+
status_code=status.HTTP_200_OK,
17+
)
18+
def get_asset_info(assetid: str = DEFAULT_ALL):
19+
log.info(f"get_asset_info request {assetid}")
20+
try:
21+
result_data = asset_service.get_asset_info(assetid)
22+
result_status = STATUS_SUCCESS
23+
except Exception as e:
24+
result_data = str(e)
25+
result_status = STATUS_FAILURE
26+
log.info(f"get_asset_info response {assetid}")
27+
response = AssetMgrResponse(status=result_status, data={"assets": result_data})
28+
log.debug(f"get_asset_info response {response}")
29+
return response
1630

1731

18-
@app_v1.post("/v1/assetmgr")
32+
@app.post(
33+
"/v1/assetmgr", response_model=AssetMgrResponse, status_code=status.HTTP_201_CREATED
34+
)
1935
def add_asset_info(asset_info: AssetMgrRequest):
20-
log.info(f"add_asset_info {asset_info}")
21-
result = asset_service.add_asset_info(asset_info)
22-
return {"asset_info": result}
36+
log.info(f"add_asset_info request {asset_info}")
37+
try:
38+
result_data = asset_service.add_asset_info(asset_info)
39+
result_status = STATUS_SUCCESS
40+
except Exception as e:
41+
result_data = str(e)
42+
result_status = STATUS_FAILURE
43+
response = AssetMgrResponse(status=result_status, data={"asset": result_data})
44+
log.debug(f"add_asset_info response {response}")
45+
return response
2346

2447

25-
@app_v1.put("/v1/assetmgr/assignuser")
48+
@app.put(
49+
"/v1/assetmgr/assignuser",
50+
response_model=AssetMgrResponse,
51+
status_code=status.HTTP_201_CREATED,
52+
)
2653
def assign_asset_to_user(asset_category: str = Query(...), userid: str = Query(...)):
27-
log.info(f"assign_asset_to_user {asset_category} {userid}")
28-
result = asset_service.assign_asset_to_user(asset_category, userid)
29-
return AssetMgrResponse(status="OK", data={"asset_id": result})
54+
log.info(f"assign_asset_to_user request {asset_category} {userid}")
55+
try:
56+
result_data = asset_service.assign_asset_to_user(asset_category, userid)
57+
result_status = STATUS_SUCCESS
58+
except Exception as e:
59+
result_data = str(e)
60+
result_status = STATUS_FAILURE
61+
response = AssetMgrResponse(status=result_status, data={"assetid": result_data})
62+
log.debug(f"assign_asset_to_user response {response}")
63+
return response
3064

3165

32-
@app_v1.put("/v1/assetmgr/{assetid}")
66+
@app.put(
67+
"/v1/assetmgr/{assetid}",
68+
response_model=AssetMgrResponse,
69+
status_code=status.HTTP_201_CREATED,
70+
)
3371
def update_asset_info(assetid: str, asset_info: AssetMgrRequest):
34-
log.info(f"update_asset_info {asset_info}")
35-
result = asset_service.update_asset_info(assetid, asset_info)
36-
return {"asset_info": result}
72+
log.info(f"update_asset_info request {asset_info}")
73+
try:
74+
result_data = asset_service.update_asset_info(assetid, asset_info)
75+
result_status = STATUS_SUCCESS
76+
except Exception as e:
77+
result_data = str(e)
78+
result_status = STATUS_FAILURE
79+
response = AssetMgrResponse(status=result_status, data={"asset": result_data})
80+
log.debug(f"update_asset_info response {response}")
81+
return response
3782

3883

39-
@app_v1.delete("/v1/assetmgr/{assetid}")
84+
@app.delete(
85+
"/v1/assetmgr/{assetid}",
86+
response_model=AssetMgrResponse,
87+
status_code=status.HTTP_202_ACCEPTED,
88+
)
4089
def del_asset_info(assetid: str):
41-
log.info(f"del_asset_info {assetid}")
42-
result = asset_service.del_asset_info(assetid)
43-
return {"asset_info": result}
90+
log.info(f"del_asset_info request {assetid}")
91+
try:
92+
result_data = asset_service.del_asset_info(assetid)
93+
result_status = STATUS_SUCCESS
94+
except Exception as e:
95+
result_data = str(e)
96+
result_status = STATUS_FAILURE
97+
response = AssetMgrResponse(
98+
status=result_status, data={"assetid": assetid, "deleted": result_data}
99+
)
100+
log.debug(f"del_asset_info response {response}")
101+
return response

asset_mgr/app/src/app_config.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
db_connect_str = "postgres://postgres:ABC_abc1@localhost:5432/postgres"
2-
default_user_id = "-1"
3-
default_asset_id = "-1"
2+
DEFAULT_ALL = "-1"
3+
STATUS_SUCCESS = "success"
4+
STATUS_FAILURE = "failed"

asset_mgr/app/src/app_repo.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def add_asset_info(asset_info):
3838
.count()
3939
)
4040
if count_result > 0:
41-
log.error(f" user already exists {asset_info.asset_id}")
41+
log.warn(f" user already exists {asset_info.asset_id}")
4242
else:
4343
AssetInfoDao.update_asset_category(asset_info.asset_category, 1)
4444
new_asset = AssetInfo(
@@ -69,7 +69,7 @@ def update_asset_info(asset_id, asset_info):
6969
AssetInfo.asset_id == asset_id
7070
)
7171
if query_result.count() == 0:
72-
log.error(f" user not found", asset_info.asset_id)
72+
log.warn(f" asset not found {asset_info.asset_id}")
7373
else:
7474
old_category = query_result.first().asset_category
7575
query_result.update(
@@ -83,7 +83,11 @@ def update_asset_info(asset_id, asset_info):
8383
AssetInfoDao.update_asset_category(asset_info.asset_category, 1)
8484
AssetInfoDao.update_asset_category(old_category, -1)
8585
session.commit()
86-
result = AssetInfo
86+
result = (
87+
session.query(AssetInfo)
88+
.filter(AssetInfo.asset_id == asset_id)
89+
.all()
90+
)
8791
except Exception as e:
8892
session.rollback
8993
log.exception(f"update_asset_info failed {e}")

asset_mgr/app/src/app_services.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,7 @@ def assign_asset_to_user(asset_category, user_id):
4242
asset_user_dao.assign_asset_to_user(asset_id[0], user_id)
4343
return asset_id[0]
4444
else:
45-
log.error(f"No asset for the given category {asset_category} is available, Please contact asset admin")
46-
return default_asset_id
45+
log.error(
46+
f"No asset for the given category {asset_category} is available, Please contact asset admin"
47+
)
48+
return DEFAULT_ALL

asset_mgr/app/test/test_assetmgr.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import unittest
2+
3+
4+
class MyTestCase(unittest.TestCase):
5+
def test_something(self):
6+
self.assertEqual(True, False)
7+
8+
9+
if __name__ == '__main__':
10+
unittest.main()

asset_mgr/requirements.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
black
2+
fastapi
3+
logzero
4+
pydantic
5+
psycopg2
6+
SQLAlchemy
7+
uvicorn
8+
requests
9+
starlette

user_mgr/app/src/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
# uvicorn app_usermgr:app_v1 --port 8002 --reload
1+
# uvicorn app_usermgr:app --port 8002 --reload

user_mgr/app/src/app_config.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
assign_asset_api = (
33
"http://127.0.0.1:8001/v1/assetmgr/assignuser?asset_category={}&userid={}"
44
)
5-
default_user_id = "-1"
6-
default_asset_id = "-1"
5+
DEFAULT_ALL = "-1"
6+
STATUS_SUCCESS = "success"
7+
STATUS_FAILURE = "failed"

0 commit comments

Comments
 (0)
0