| Conditions | 18 | 
| Total Lines | 73 | 
| Code Lines | 59 | 
| 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 core.aliyunsmsapi.AliyunSMSAPICollection.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  | 
            ||
| 43 | @staticmethod  | 
            ||
| 44 | @user_logger  | 
            ||
| 45 | def on_post(req, resp):  | 
            ||
| 46 | """Handles POST requests"""  | 
            ||
| 47 | try:  | 
            ||
| 48 |             raw_json = req.stream.read().decode('utf-8') | 
            ||
| 49 | except Exception as ex:  | 
            ||
| 50 | raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR', description=ex)  | 
            ||
| 51 | |||
| 52 | new_values = json.loads(raw_json)  | 
            ||
| 53 | |||
| 54 | if 'access_key_id' not in new_values['data'].keys() or \  | 
            ||
| 55 | not isinstance(new_values['data']['access_key_id'], str) or \  | 
            ||
| 56 | len(str.strip(new_values['data']['access_key_id'])) == 0:  | 
            ||
| 57 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',  | 
            ||
| 58 | description='API.INVALID_ALIYUN_SMS_API_ACCESS_KEY_ID')  | 
            ||
| 59 | access_key_id = str.strip(new_values['data']['access_key_id'])  | 
            ||
| 60 | |||
| 61 | if 'access_key_secret' not in new_values['data'].keys() or \  | 
            ||
| 62 | not isinstance(new_values['data']['access_key_secret'], str) or \  | 
            ||
| 63 | len(new_values['data']['access_key_secret']) == 0:  | 
            ||
| 64 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',  | 
            ||
| 65 | description='API.INVALID_ALIYUN_SMS_API_ACCESS_KEY_SECRET')  | 
            ||
| 66 | access_key_secret = new_values['data']['access_key_secret']  | 
            ||
| 67 | |||
| 68 | if 'endpoint' not in new_values['data'].keys() or \  | 
            ||
| 69 | not isinstance(new_values['data']['endpoint'], str) or \  | 
            ||
| 70 | len(new_values['data']['endpoint']) == 0:  | 
            ||
| 71 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',  | 
            ||
| 72 | description='API.INVALID_ALIYUN_SMS_API_ENDPOINT')  | 
            ||
| 73 | endpoint = new_values['data']['endpoint']  | 
            ||
| 74 | |||
| 75 | if 'sign_name' not in new_values['data'].keys() or \  | 
            ||
| 76 | not isinstance(new_values['data']['sign_name'], str) or \  | 
            ||
| 77 | len(new_values['data']['sign_name']) == 0:  | 
            ||
| 78 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',  | 
            ||
| 79 | description='API.INVALID_ALIYUN_SMS_API_SIGN_NAME')  | 
            ||
| 80 | sign_name = new_values['data']['sign_name']  | 
            ||
| 81 | |||
| 82 | if 'template_code' not in new_values['data'].keys() or \  | 
            ||
| 83 | not isinstance(new_values['data']['template_code'], str) or \  | 
            ||
| 84 | len(new_values['data']['template_code']) == 0:  | 
            ||
| 85 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',  | 
            ||
| 86 | description='API.INVALID_ALIYUN_SMS_API_TEMPLATE_CODE')  | 
            ||
| 87 | template_code = new_values['data']['template_code']  | 
            ||
| 88 | |||
| 89 | cnx = mysql.connector.connect(**config.myems_fdd_db)  | 
            ||
| 90 | cursor = cnx.cursor()  | 
            ||
| 91 | |||
| 92 |         cursor.execute(" SELECT id " | 
            ||
| 93 | " FROM tbl_aliyun_sms_api "  | 
            ||
| 94 | " WHERE access_key_id = %s ", (access_key_id,))  | 
            ||
| 95 | if cursor.fetchone() is not None:  | 
            ||
| 96 | cursor.close()  | 
            ||
| 97 | cnx.disconnect()  | 
            ||
| 98 | raise falcon.HTTPError(falcon.HTTP_404, title='API.BAD_REQUEST',  | 
            ||
| 99 | description='API.ALIYUN_SMS_API_ACCESS_KEY_ID_IS_ALREADY_IN_USE')  | 
            ||
| 100 | |||
| 101 |         add_value = (" INSERT INTO tbl_aliyun_sms_api " | 
            ||
| 102 | " (access_key_id, access_key_secret, endpoint, sign_name, template_code) "  | 
            ||
| 103 | " VALUES (%s, %s, %s, %s, %s) ")  | 
            ||
| 104 | cursor.execute(add_value, (access_key_id,  | 
            ||
| 105 | access_key_secret,  | 
            ||
| 106 | endpoint,  | 
            ||
| 107 | sign_name,  | 
            ||
| 108 | template_code))  | 
            ||
| 109 | new_id = cursor.lastrowid  | 
            ||
| 110 | cnx.commit()  | 
            ||
| 111 | cursor.close()  | 
            ||
| 112 | cnx.disconnect()  | 
            ||
| 113 | |||
| 114 | resp.status = falcon.HTTP_201  | 
            ||
| 115 | resp.location = '/aliyunsmsapis/' + str(new_id)  | 
            ||
| 116 | |||
| 269 |