Conditions | 12 |
Total Lines | 66 |
Code Lines | 50 |
Lines | 66 |
Ratio | 100 % |
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 core.iotsimcard.IoTSIMCardItem.on_put() 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 re |
||
193 | View Code Duplication | @staticmethod |
|
|
|||
194 | @user_logger |
||
195 | def on_put(req, resp, id_): |
||
196 | """Handles PUT requests""" |
||
197 | admin_control(req) |
||
198 | try: |
||
199 | raw_json = req.stream.read().decode('utf-8') |
||
200 | except Exception as ex: |
||
201 | print(ex) |
||
202 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
203 | title='API.BAD_REQUEST', |
||
204 | description='API.FAILED_TO_READ_REQUEST_STREAM') |
||
205 | |||
206 | if not id_.isdigit() or int(id_) <= 0: |
||
207 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
208 | description='API.INVALID_IOT_SIM_CARD_ID') |
||
209 | |||
210 | new_values = json.loads(raw_json) |
||
211 | |||
212 | if 'iccid' not in new_values['data'].keys() or \ |
||
213 | not isinstance(new_values['data']['iccid'], str) or \ |
||
214 | len(str.strip(new_values['data']['iccid'])) == 0: |
||
215 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
216 | description='API.INVALID_ICCID') |
||
217 | iccid = str.strip(new_values['data']['iccid']) |
||
218 | |||
219 | if 'description' in new_values['data'].keys() and \ |
||
220 | new_values['data']['description'] is not None and \ |
||
221 | len(str(new_values['data']['description'])) > 0: |
||
222 | description = str.strip(new_values['data']['description']) |
||
223 | else: |
||
224 | description = None |
||
225 | |||
226 | cnx = mysql.connector.connect(**config.myems_system_db) |
||
227 | cursor = cnx.cursor() |
||
228 | |||
229 | cursor.execute(" SELECT iccid " |
||
230 | " FROM tbl_iot_sim_cards " |
||
231 | " WHERE id = %s ", (id_,)) |
||
232 | if cursor.fetchone() is None: |
||
233 | cursor.close() |
||
234 | cnx.close() |
||
235 | raise falcon.HTTPError(status=falcon.HTTP_404, title='API.NOT_FOUND', |
||
236 | description='API.IOT_SIM_CARD_NOT_FOUND') |
||
237 | |||
238 | cursor.execute(" SELECT iccid " |
||
239 | " FROM tbl_iot_sim_cards " |
||
240 | " WHERE iccid = %s AND id != %s ", (iccid, id_)) |
||
241 | if cursor.fetchone() is not None: |
||
242 | cursor.close() |
||
243 | cnx.close() |
||
244 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
245 | description='API.ICCID_IS_ALREADY_IN_USE') |
||
246 | |||
247 | update_row = (" UPDATE tbl_iot_sim_cards " |
||
248 | " SET iccid = %s, description = %s " |
||
249 | " WHERE id = %s ") |
||
250 | cursor.execute(update_row, (iccid, |
||
251 | description, |
||
252 | id_,)) |
||
253 | cnx.commit() |
||
254 | |||
255 | cursor.close() |
||
256 | cnx.close() |
||
257 | |||
258 | resp.status = falcon.HTTP_200 |
||
259 | |||
260 |