Conditions | 23 |
Total Lines | 91 |
Code Lines | 72 |
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 emailserver.EmailServerCollection.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 |
||
45 | @staticmethod |
||
46 | def on_post(req, resp): |
||
47 | """Handles POST requests""" |
||
48 | try: |
||
49 | raw_json = req.stream.read().decode('utf-8') |
||
50 | except Exception as ex: |
||
51 | raise falcon.HTTPError(falcon.HTTP_400, title='API.ERROR', description=ex) |
||
52 | |||
53 | new_values = json.loads(raw_json, encoding='utf-8') |
||
54 | |||
55 | if 'host' not in new_values['data'].keys() or \ |
||
56 | not isinstance(new_values['data']['host'], str) or \ |
||
57 | len(str.strip(new_values['data']['host'])) == 0: |
||
58 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
59 | description='API.INVALID_EMAIL_SERVER_HOST') |
||
60 | |||
61 | host = str.strip(new_values['data']['host']) |
||
62 | |||
63 | if 'port' not in new_values['data'].keys() or \ |
||
64 | not isinstance(new_values['data']['port'], int) or \ |
||
65 | new_values['data']['port'] <= 0: |
||
66 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
67 | description='API.INVALID_PORT') |
||
68 | port = float(new_values['data']['port']) |
||
69 | |||
70 | if 'requires_authentication' not in new_values['data'].keys() or \ |
||
71 | not isinstance(new_values['data']['requires_authentication'], bool): |
||
72 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
73 | description='API.INVALID_REQUIRES_AUTHENTICATION') |
||
74 | requires_authentication = new_values['data']['requires_authentication'] |
||
75 | |||
76 | if requires_authentication: |
||
77 | if 'user_name' not in new_values['data'].keys() or \ |
||
78 | not isinstance(new_values['data']['user_name'], str) or \ |
||
79 | len(str.strip(new_values['data']['user_name'])) == 0: |
||
80 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
81 | description='API.INVALID_USER_NAME') |
||
82 | user_name = new_values['data']['user_name'] |
||
83 | else: |
||
84 | user_name = None |
||
85 | |||
86 | if requires_authentication: |
||
87 | if 'password' not in new_values['data'].keys() or \ |
||
88 | not isinstance(new_values['data']['password'], str) or \ |
||
89 | len(str.strip(new_values['data']['password'])) == 0: |
||
90 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
91 | description='API.INVALID_PASSWORD') |
||
92 | password = base64.b64encode(bytearray(new_values['data']['password'], 'utf-8')) |
||
93 | else: |
||
94 | password = None |
||
95 | |||
96 | if 'from_addr' not in new_values['data'].keys() or \ |
||
97 | not isinstance(new_values['data']['from_addr'], str) or \ |
||
98 | len(str.strip(new_values['data']['from_addr'])) == 0: |
||
99 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
100 | description='API.INVALID_FROM_ADDR') |
||
101 | from_addr = new_values['data']['from_addr'] |
||
102 | |||
103 | match = re.match('^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$', from_addr) |
||
104 | if match is None: |
||
105 | raise falcon.HTTPError(falcon.HTTP_400, title='API.BAD_REQUEST', |
||
106 | description='API.INVALID_FROM_ADDR') |
||
107 | |||
108 | cnx = mysql.connector.connect(**config.myems_fdd_db) |
||
109 | cursor = cnx.cursor() |
||
110 | |||
111 | cursor.execute(" SELECT host " |
||
112 | " FROM tbl_email_servers " |
||
113 | " WHERE host = %s ", (host,)) |
||
114 | if cursor.fetchone() is not None: |
||
115 | cursor.close() |
||
116 | cnx.disconnect() |
||
117 | raise falcon.HTTPError(falcon.HTTP_404, title='API.BAD_REQUEST', |
||
118 | description='API.EMAIL_SERVER_HOST_IS_ALREADY_IN_USE') |
||
119 | |||
120 | add_value = (" INSERT INTO tbl_email_servers " |
||
121 | " (host, port, requires_authentication, user_name, password, from_addr) " |
||
122 | " VALUES (%s, %s, %s, %s, %s, %s) ") |
||
123 | cursor.execute(add_value, (host, |
||
124 | port, |
||
125 | requires_authentication, |
||
126 | user_name, |
||
127 | password, |
||
128 | from_addr)) |
||
129 | new_id = cursor.lastrowid |
||
130 | cnx.commit() |
||
131 | cursor.close() |
||
132 | cnx.disconnect() |
||
133 | |||
134 | resp.status = falcon.HTTP_201 |
||
135 | resp.location = '/emailservers/' + str(new_id) |
||
136 | |||
306 |