12
12
# See the License for the specific language governing permissions and
13
13
# limitations under the License.
14
14
15
+ """ Rule endpoints """
16
+
15
17
from fastapi import APIRouter , HTTPException
16
18
from models .rule import Rule
17
19
from schemas .rule import RuleSchema
18
20
import datetime
19
21
20
- # disabling for linting to pass
21
- # pylint: disable = broad-except
22
-
23
22
router = APIRouter (prefix = "/rule" , tags = ["rule" ])
24
23
25
24
SUCCESS_RESPONSE = {"status" : "Success" }
26
25
27
26
28
- @router .get ("/{id }" , response_model = RuleSchema )
29
- async def get (id : str ):
27
+ @router .get ("/{rule_id }" , response_model = RuleSchema )
28
+ async def get (rule_id : str ):
30
29
"""Get a Rule
31
30
32
31
Args:
33
- id (str): unique id of the rule
32
+ rule_id (str): unique id of the rule
34
33
35
34
Raises:
36
35
HTTPException: 404 Not Found if rule doesn't exist for the given rule id
@@ -39,10 +38,10 @@ async def get(id: str):
39
38
Returns:
40
39
[rule]: rule object for the provided rule id
41
40
"""
42
- rule = Rule .find_by_doc_id (id )
41
+ rule = Rule .find_by_doc_id (rule_id )
43
42
44
43
if rule is None :
45
- raise HTTPException (status_code = 404 , detail = f"Rule { id } not found." )
44
+ raise HTTPException (status_code = 404 , detail = f"Rule { rule_id } not found." )
46
45
return rule
47
46
48
47
@@ -59,12 +58,12 @@ async def post(data: RuleSchema):
59
58
Returns:
60
59
[JSON]: rule ID of the rule if the rule is successfully created
61
60
"""
62
- id = data .id
63
- existing_rule = Rule .find_by_doc_id (id )
61
+ rule_id = data .id
62
+ existing_rule = Rule .find_by_doc_id (rule_id )
64
63
65
64
if existing_rule :
66
65
raise HTTPException (status_code = 409 ,
67
- detail = f"Rule { id } already exists." )
66
+ detail = f"Rule { rule_id } already exists." )
68
67
69
68
new_rule = Rule ()
70
69
new_rule = new_rule .from_dict ({** data .dict ()})
@@ -88,26 +87,26 @@ async def put(data: RuleSchema):
88
87
Returns:
89
88
[JSON]: {'status': 'Succeed'} if the rule is updated
90
89
"""
91
- id = data .id
92
- rule = Rule .find_by_doc_id (id )
90
+ rule_id = data .id
91
+ rule = Rule .find_by_doc_id (rule_id )
93
92
94
93
if rule :
95
94
rule = rule .from_dict ({** data .dict ()})
96
95
rule .modified_at = datetime .datetime .utcnow ()
97
96
rule .save ()
98
97
99
98
else :
100
- raise HTTPException (status_code = 404 , detail = f"Rule { id } not found." )
99
+ raise HTTPException (status_code = 404 , detail = f"Rule { rule_id } not found." )
101
100
102
101
return SUCCESS_RESPONSE
103
102
104
103
105
- @router .delete ("/{id }" )
106
- async def delete (id : str ):
104
+ @router .delete ("/{rule_id }" )
105
+ async def delete (rule_id : str ):
107
106
"""Delete a Rule
108
107
109
108
Args:
110
- id (str): unique id of the rule
109
+ rule_id (str): unique id of the rule
111
110
112
111
Raises:
113
112
HTTPException: 500 Internal Server Error if something fails
@@ -116,9 +115,9 @@ async def delete(id: str):
116
115
[JSON]: {'status': 'Succeed'} if the rule is deleted
117
116
"""
118
117
119
- rule = Rule .find_by_doc_id (id )
118
+ rule = Rule .find_by_doc_id (rule_id )
120
119
if rule is None :
121
- raise HTTPException (status_code = 404 , detail = f"Rule { id } not found." )
120
+ raise HTTPException (status_code = 404 , detail = f"Rule { rule_id } not found." )
122
121
123
122
Rule .collection .delete (rule .key )
124
123
0 commit comments