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