| Conditions | 9 |
| Total Lines | 57 |
| Lines | 0 |
| Ratio | 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 | # Licensed to the StackStorm, Inc ('StackStorm') under one or more |
||
| 20 | def GetScanExecutions(config, scan_id): |
||
| 21 | """ |
||
| 22 | The template class for |
||
| 23 | |||
| 24 | Returns: An blank Dict. |
||
| 25 | |||
| 26 | Raises: |
||
| 27 | ValueError: On lack of key in config. |
||
| 28 | """ |
||
| 29 | results = {} |
||
| 30 | |||
| 31 | url = "https://{}/api/scan/v1/scans/{}".format(config['api_host'], scan_id) |
||
| 32 | headers = {"Accept": "application/json"} |
||
| 33 | |||
| 34 | try: |
||
| 35 | r = requests.get(url, |
||
| 36 | headers=headers, |
||
| 37 | auth=(config['api_key'], '')) |
||
| 38 | r.raise_for_status() |
||
| 39 | except: |
||
| 40 | raise ValueError("HTTP error: %s on %s" % (r.status_code, r.url)) |
||
| 41 | |||
| 42 | try: |
||
| 43 | data = r.json() |
||
| 44 | except: |
||
| 45 | raise ValueError("Invalid JSON") |
||
| 46 | else: |
||
| 47 | results = {'latest_complete': None, 'scans': []} |
||
| 48 | for item in data: |
||
| 49 | cd = datetime.fromtimestamp(item['create_date']) |
||
| 50 | create_date = cd.strftime('%Y-%m-%d %H:%M:%S') |
||
| 51 | |||
| 52 | if item['finish_date'] is not None: |
||
| 53 | fd = datetime.fromtimestamp(item['finish_date']) |
||
| 54 | finish_date = fd.strftime('%Y-%m-%d %H:%M:%S') |
||
| 55 | duration = str(fd - cd) |
||
| 56 | else: |
||
| 57 | finish_date = "-" |
||
| 58 | duration = str(datetime.now().replace(microsecond=0) - cd) |
||
| 59 | |||
| 60 | results['scans'].append({"id": item['id'], |
||
| 61 | "active": item['active'], |
||
| 62 | "create_date": create_date, |
||
| 63 | "finish_date": finish_date, |
||
| 64 | "duration": duration}) |
||
| 65 | |||
| 66 | # This list can be very large, limit to the last 10. |
||
| 67 | results['scans'].sort(reverse=True) |
||
| 68 | results['scans'] = results['scans'][0:10] |
||
| 69 | |||
| 70 | # Find the latest ccmpleted scan.. |
||
| 71 | for item in results['scans']: |
||
| 72 | if item['active'] is False: |
||
| 73 | results['latest_complete'] = item['id'] |
||
| 74 | break |
||
| 75 | |||
| 76 | return results |
||
| 77 |