| Conditions | 21 | 
| Total Lines | 88 | 
| Code Lines | 68 | 
| 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.AliyunSMSAPIItem.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 falcon  | 
            ||
| 181 | @staticmethod  | 
            ||
| 182 | @user_logger  | 
            ||
| 183 | def on_put(req, resp, id_):  | 
            ||
| 184 | """Handles PUT requests"""  | 
            ||
| 185 | try:  | 
            ||
| 186 |             raw_json = req.stream.read().decode('utf-8') | 
            ||
| 187 | except Exception as ex:  | 
            ||
| 188 | raise falcon.HTTPError(falcon.HTTP_400, title='API.EXCEPTION', description=ex)  | 
            ||
| 189 | |||
| 190 | if not id_.isdigit() or int(id_) <= 0:  | 
            ||
| 191 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',  | 
            ||
| 192 | description='API.INVALID_ALIYUN_SMS_API_ID')  | 
            ||
| 193 | |||
| 194 | new_values = json.loads(raw_json)  | 
            ||
| 195 | |||
| 196 | if 'access_key_id' not in new_values['data'].keys() or \  | 
            ||
| 197 | not isinstance(new_values['data']['access_key_id'], str) or \  | 
            ||
| 198 | len(str.strip(new_values['data']['access_key_id'])) == 0:  | 
            ||
| 199 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',  | 
            ||
| 200 | description='API.INVALID_ALIYUN_SMS_API_ACCESS_KEY_ID')  | 
            ||
| 201 | access_key_id = str.strip(new_values['data']['access_key_id'])  | 
            ||
| 202 | |||
| 203 | if 'access_key_secret' not in new_values['data'].keys() or \  | 
            ||
| 204 | not isinstance(new_values['data']['access_key_secret'], str) or \  | 
            ||
| 205 | len(new_values['data']['access_key_secret']) == 0:  | 
            ||
| 206 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',  | 
            ||
| 207 | description='API.INVALID_ALIYUN_SMS_API_ACCESS_KEY_SECRET')  | 
            ||
| 208 | access_key_secret = new_values['data']['access_key_secret']  | 
            ||
| 209 | |||
| 210 | if 'endpoint' not in new_values['data'].keys() or \  | 
            ||
| 211 | not isinstance(new_values['data']['endpoint'], str) or \  | 
            ||
| 212 | len(new_values['data']['endpoint']) == 0:  | 
            ||
| 213 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',  | 
            ||
| 214 | description='API.INVALID_ALIYUN_SMS_API_ENDPOINT')  | 
            ||
| 215 | endpoint = new_values['data']['endpoint']  | 
            ||
| 216 | |||
| 217 | if 'sign_name' not in new_values['data'].keys() or \  | 
            ||
| 218 | not isinstance(new_values['data']['sign_name'], str) or \  | 
            ||
| 219 | len(new_values['data']['sign_name']) == 0:  | 
            ||
| 220 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',  | 
            ||
| 221 | description='API.INVALID_ALIYUN_SMS_API_SIGN_NAME')  | 
            ||
| 222 | sign_name = new_values['data']['sign_name']  | 
            ||
| 223 | |||
| 224 | if 'template_code' not in new_values['data'].keys() or \  | 
            ||
| 225 | not isinstance(new_values['data']['template_code'], str) or \  | 
            ||
| 226 | len(new_values['data']['template_code']) == 0:  | 
            ||
| 227 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST',  | 
            ||
| 228 | description='API.INVALID_ALIYUN_SMS_API_TEMPLATE_CODE')  | 
            ||
| 229 | template_code = new_values['data']['template_code']  | 
            ||
| 230 | |||
| 231 | cnx = mysql.connector.connect(**config.myems_fdd_db)  | 
            ||
| 232 | cursor = cnx.cursor()  | 
            ||
| 233 | |||
| 234 |         cursor.execute(" SELECT access_key_id " | 
            ||
| 235 | " FROM tbl_aliyun_sms_api "  | 
            ||
| 236 | " WHERE id = %s ",  | 
            ||
| 237 | (id_,))  | 
            ||
| 238 | if cursor.fetchone() is None:  | 
            ||
| 239 | cursor.close()  | 
            ||
| 240 | cnx.disconnect()  | 
            ||
| 241 | raise falcon.HTTPError(falcon.HTTP_404, title='API.NOT_FOUND',  | 
            ||
| 242 | description='API.ALIYUN_SMS_API_NOT_FOUND')  | 
            ||
| 243 | |||
| 244 |         cursor.execute(" SELECT access_key_id " | 
            ||
| 245 | " FROM tbl_aliyun_sms_api "  | 
            ||
| 246 | " WHERE access_key_id = %s AND id != %s ", (access_key_id, id_))  | 
            ||
| 247 | if cursor.fetchone() is not None:  | 
            ||
| 248 | cursor.close()  | 
            ||
| 249 | cnx.disconnect()  | 
            ||
| 250 | raise falcon.HTTPError(falcon.HTTP_404, title='API.BAD_REQUEST',  | 
            ||
| 251 | description='API.ALIYUN_SMS_API_ACCESS_KEY_ID_IS_ALREADY_IN_USE')  | 
            ||
| 252 | |||
| 253 |         update_row = (" UPDATE tbl_aliyun_sms_api " | 
            ||
| 254 | " SET access_key_id = %s, access_key_secret = %s, endpoint = %s, sign_name = %s, "  | 
            ||
| 255 | " template_code = %s "  | 
            ||
| 256 | " WHERE id = %s ")  | 
            ||
| 257 | cursor.execute(update_row, (access_key_id,  | 
            ||
| 258 | access_key_secret,  | 
            ||
| 259 | endpoint,  | 
            ||
| 260 | sign_name,  | 
            ||
| 261 | template_code,  | 
            ||
| 262 | id_,))  | 
            ||
| 263 | cnx.commit()  | 
            ||
| 264 | |||
| 265 | cursor.close()  | 
            ||
| 266 | cnx.disconnect()  | 
            ||
| 267 | |||
| 268 | resp.status = falcon.HTTP_200  | 
            ||
| 269 |