Conditions | 18 |
Total Lines | 75 |
Code Lines | 62 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like rule.RuleCollection.on_post() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | import falcon |
||
40 | @staticmethod |
||
41 | def on_post(req, resp): |
||
42 | """Handles POST requests""" |
||
43 | try: |
||
44 | raw_json = req.stream.read().decode('utf-8') |
||
45 | except Exception as ex: |
||
46 | raise falcon.HTTPError(falcon.HTTP_400, title='API.EXCEPTION', description=ex) |
||
47 | |||
48 | new_values = json.loads(raw_json, encoding='utf-8') |
||
49 | if 'name' not in new_values['data'].keys() or \ |
||
50 | not isinstance(new_values['data']['name'], str) or \ |
||
51 | len(str.strip(new_values['data']['name'])) == 0: |
||
52 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
53 | description='API.INVALID_RULE_NAME') |
||
54 | name = str.strip(new_values['data']['name']) |
||
55 | |||
56 | if 'channel' not in new_values['data'].keys() or \ |
||
57 | not isinstance(new_values['data']['channel'], str) or \ |
||
58 | len(str.strip(new_values['data']['channel'])) == 0 or \ |
||
59 | str.strip(new_values['data']['channel']) not in ('call', 'sms', 'email', 'wechat', 'web'): |
||
60 | raise falcon.HTTPError(falcon.HTTP_400, |
||
61 | title='API.BAD_REQUEST', |
||
62 | description='API.INVALID_CHANNEL') |
||
63 | channel = str.strip(new_values['data']['channel']) |
||
64 | |||
65 | if 'expression' not in new_values['data'].keys() or \ |
||
66 | not isinstance(new_values['data']['expression'], str) or \ |
||
67 | len(str.strip(new_values['data']['expression'])) == 0: |
||
68 | raise falcon.HTTPError(falcon.HTTP_400, |
||
69 | title='API.BAD_REQUEST', |
||
70 | description='API.INVALID_EXPRESSION') |
||
71 | expression = str.strip(new_values['data']['expression']) |
||
72 | |||
73 | if 'message' not in new_values['data'].keys() or \ |
||
74 | not isinstance(new_values['data']['message'], str) or \ |
||
75 | len(str.strip(new_values['data']['message'])) == 0: |
||
76 | raise falcon.HTTPError(falcon.HTTP_400, |
||
77 | title='API.BAD_REQUEST', |
||
78 | description='API.INVALID_MESSAGE') |
||
79 | message = str.strip(new_values['data']['message']) |
||
80 | |||
81 | if 'is_enabled' not in new_values['data'].keys() or \ |
||
82 | not isinstance(new_values['data']['is_enabled'], bool): |
||
83 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
84 | description='API.INVALID_IS_ENABLED') |
||
85 | is_enabled = new_values['data']['is_enabled'] |
||
86 | |||
87 | cnx = mysql.connector.connect(**config.myems_fdd_db) |
||
88 | cursor = cnx.cursor() |
||
89 | |||
90 | cursor.execute(" SELECT name " |
||
91 | " FROM tbl_rules " |
||
92 | " WHERE name = %s ", (name,)) |
||
93 | if cursor.fetchone() is not None: |
||
94 | cursor.close() |
||
95 | cnx.disconnect() |
||
96 | raise falcon.HTTPError(falcon.HTTP_404, title='API.BAD_REQUEST', |
||
97 | description='API.RULE_NAME_IS_ALREADY_IN_USE') |
||
98 | |||
99 | add_row = (" INSERT INTO tbl_rules " |
||
100 | " (name, uuid, channel, expression, message, is_enabled) " |
||
101 | " VALUES (%s, %s, %s, %s, %s, %s) ") |
||
102 | cursor.execute(add_row, (name, |
||
103 | str(uuid.uuid4()), |
||
104 | channel, |
||
105 | expression, |
||
106 | message, |
||
107 | is_enabled)) |
||
108 | new_id = cursor.lastrowid |
||
109 | cnx.commit() |
||
110 | cursor.close() |
||
111 | cnx.disconnect() |
||
112 | |||
113 | resp.status = falcon.HTTP_201 |
||
114 | resp.location = '/rules/' + str(new_id) |
||
115 | |||
266 |