Conditions | 8 |
Total Lines | 58 |
Code Lines | 49 |
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:
1 | ''' |
||
29 | @gen.coroutine |
||
30 | def post(self): |
||
31 | if self.should_fail_with_not_authorized(): |
||
32 | self.fail_with_not_authorized() |
||
33 | return |
||
34 | |||
35 | try: |
||
36 | if not self.request.body: |
||
37 | self.error_out(400, "Input body cannot be empty") |
||
38 | self.finish() |
||
39 | return |
||
40 | |||
41 | try: |
||
42 | request_data = json.loads( |
||
43 | self.request.body.decode('utf-8')) |
||
44 | except Exception as ex: |
||
45 | self.error_out( |
||
46 | 400, |
||
47 | "Failed to decode input body", |
||
48 | str(ex)) |
||
49 | self.finish() |
||
50 | return |
||
51 | |||
52 | if 'name' not in request_data: |
||
53 | self.error_out(400, |
||
54 | "name is required to add an endpoint.") |
||
55 | self.finish() |
||
56 | return |
||
57 | |||
58 | name = request_data['name'] |
||
59 | |||
60 | # check if endpoint already exist |
||
61 | if name in self.tabpy_state.get_endpoints(): |
||
62 | self.error_out(400, f'endpoint {name} already exists.') |
||
63 | self.finish() |
||
64 | return |
||
65 | |||
66 | self.logger.log( |
||
67 | logging.DEBUG, |
||
68 | f'Adding endpoint "{name}"') |
||
69 | err_msg = yield self._add_or_update_endpoint('add', name, 1, |
||
70 | request_data) |
||
71 | if err_msg: |
||
72 | self.error_out(400, err_msg) |
||
73 | else: |
||
74 | self.logger.log( |
||
75 | logging.DEBUG, |
||
76 | f'Endpoint {name} successfully added') |
||
77 | self.set_status(201) |
||
78 | self.write(self.tabpy_state.get_endpoints(name)) |
||
79 | self.finish() |
||
80 | return |
||
81 | |||
82 | except Exception as e: |
||
83 | err_msg = format_exception(e, '/add_endpoint') |
||
84 | self.error_out(500, "error adding endpoint", err_msg) |
||
85 | self.finish() |
||
86 | return |
||
87 |