| Conditions | 10 |
| Total Lines | 90 |
| Code Lines | 56 |
| Lines | 13 |
| Ratio | 14.44 % |
| 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.ticket.TicketCollection.on_get() 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 | from datetime import datetime, timedelta, timezone |
||
| 35 | @staticmethod |
||
| 36 | def on_get(req, resp): |
||
| 37 | """ |
||
| 38 | Handle GET requests to retrieve tickets from workflow system |
||
| 39 | |||
| 40 | Retrieves tickets based on date range and status filters. |
||
| 41 | Communicates with the MyEMS workflow service to fetch ticket data. |
||
| 42 | |||
| 43 | Args: |
||
| 44 | req: Falcon request object with query parameters: |
||
| 45 | - startdatetime: Start date for filtering (required) |
||
| 46 | - enddatetime: End date for filtering (required) |
||
| 47 | - status: Ticket status filter (required) |
||
| 48 | resp: Falcon response object |
||
| 49 | """ |
||
| 50 | access_control(req) |
||
| 51 | start_datetime_local = req.params.get('startdatetime') |
||
| 52 | end_datetime_local = req.params.get('enddatetime') |
||
| 53 | status = req.params.get('status') |
||
| 54 | |||
| 55 | # Calculate timezone offset for datetime conversion |
||
| 56 | timezone_offset = int(config.utc_offset[1:3]) * 60 + int(config.utc_offset[4:6]) |
||
| 57 | if config.utc_offset[0] == '-': |
||
| 58 | timezone_offset = -timezone_offset |
||
| 59 | |||
| 60 | # Validate and parse start datetime |
||
| 61 | if start_datetime_local is None: |
||
| 62 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 63 | description="API.INVALID_START_DATETIME_FORMAT") |
||
| 64 | else: |
||
| 65 | start_datetime_local = str.strip(start_datetime_local) |
||
| 66 | try: |
||
| 67 | start_datetime_utc = datetime.strptime(start_datetime_local, |
||
| 68 | '%Y-%m-%dT%H:%M:%S').replace(tzinfo=timezone.utc) - \ |
||
| 69 | timedelta(minutes=timezone_offset) |
||
| 70 | except ValueError: |
||
| 71 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 72 | description="API.INVALID_START_DATETIME_FORMAT") |
||
| 73 | |||
| 74 | # Validate and parse end datetime |
||
| 75 | if end_datetime_local is None: |
||
| 76 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 77 | description="API.INVALID_END_DATETIME_FORMAT") |
||
| 78 | else: |
||
| 79 | end_datetime_local = str.strip(end_datetime_local) |
||
| 80 | try: |
||
| 81 | end_datetime_utc = datetime.strptime(end_datetime_local, |
||
| 82 | '%Y-%m-%dT%H:%M:%S').replace(tzinfo=timezone.utc) - \ |
||
| 83 | timedelta(minutes=timezone_offset) |
||
| 84 | except ValueError: |
||
| 85 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 86 | description="API.INVALID_END_DATETIME_FORMAT") |
||
| 87 | |||
| 88 | # Validate date range |
||
| 89 | if start_datetime_utc >= end_datetime_utc: |
||
| 90 | raise falcon.HTTPError(status=falcon.HTTP_400, |
||
| 91 | title='API.BAD_REQUEST', |
||
| 92 | description='API.START_DATETIME_MUST_BE_EARLIER_THAN_END_DATETIME') |
||
| 93 | |||
| 94 | # Validate status parameter |
||
| 95 | View Code Duplication | if status is None: |
|
|
|
|||
| 96 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 97 | description="API.INVALID_STATUS") |
||
| 98 | else: |
||
| 99 | status = str.lower(str.strip(status)) |
||
| 100 | if status not in ['new', 'read', 'acknowledged', 'all']: |
||
| 101 | raise falcon.HTTPError(status=falcon.HTTP_400, title='API.BAD_REQUEST', |
||
| 102 | description='API.INVALID_STATUS') |
||
| 103 | else: |
||
| 104 | if status == 'all': |
||
| 105 | status_query = "" |
||
| 106 | else: |
||
| 107 | status_query = "status = '" + status + "' AND " |
||
| 108 | |||
| 109 | # Prepare workflow service authentication |
||
| 110 | workflow_base_url = config.myems_workflow['base_url'] |
||
| 111 | token = config.myems_workflow['token'] |
||
| 112 | app_name = config.myems_workflow['app_name'] |
||
| 113 | user_name = config.myems_workflow['user_name'] |
||
| 114 | |||
| 115 | # Generate authentication signature |
||
| 116 | timestamp = str(time.time())[:10] |
||
| 117 | ori_str = timestamp + token |
||
| 118 | signature = hashlib.md5(ori_str.encode(encoding='utf-8')).hexdigest() |
||
| 119 | headers = dict(signature=signature, timestamp=timestamp, appname=app_name, username=user_name) |
||
| 120 | |||
| 121 | # Request tickets from workflow service |
||
| 122 | get_data = dict(per_page=20, category='all') |
||
| 123 | r = requests.get(workflow_base_url + 'tickets', headers=headers, params=get_data) |
||
| 124 | resp.text = json.dumps(r.text) |
||
| 125 | |||
| 244 |