Skip to content
This repository was archived by the owner on Nov 4, 2024. It is now read-only.

Add display parameter to getScanResults (API) #479

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion httpobs/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
SOURCE_URL = 'https://github.com/mozilla/http-observatory'
VERSION = '0.9.3'
VERSION = '0.9.4'
1 change: 1 addition & 0 deletions httpobs/docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ Each scan consists of a variety of subtests, including Content Security Policy,
Parameters:

* `scan` scan_id number from the [scan object](#scan)
* `display` format of output, allowed values: `object` (default), `array`

Example:

Expand Down
8 changes: 7 additions & 1 deletion httpobs/website/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ def api_get_scanner_stats():
@sanitized_api_response
def api_get_scan_results():
scan_id = request.args.get('scan')
display = request.args.get('display', 'object').lower()

if not scan_id:
return {'error': 'scan-not-found'}
Expand All @@ -237,9 +238,14 @@ def api_get_scan_results():

# Get all the test results for the given scan id
tests = dict(database.select_test_results(scan_id))
iterator = tests

if display == 'array':
tests = list(tests.values())
iterator = range(0, len(tests))

# For each test, get the test score description and add that in
for test in tests:
for test in iterator:
tests[test]['score_description'] = get_score_description(tests[test]['result'])

return tests
Expand Down
8 changes: 5 additions & 3 deletions httpobs/website/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ def wrapper(*args, **kwargs):
'score_description', 'score_modifier')

# Convert it to a dict (in case it's a DictRow)
output = dict(output)
is_list = type(output) == list
if not is_list:
output = dict(output)

if 'tests_quantity' in output: # autodetect that it's a scan
# Rename 'id' to 'result_id':
Expand All @@ -75,8 +77,8 @@ def wrapper(*args, **kwargs):
# Delete any other things that might have made their way into the results
output = {k: output[k] for k in SCAN_VALID_KEYS if k in output}

elif 'content-security-policy' in output: # autodetect that it's a test result
for test in output:
elif 'content-security-policy' in output or is_list: # autodetect that it's a test result
for test in (range(0, len(output)) if is_list else output):
# Delete unnecessary keys
output[test] = {k: output[test][k] for k in output[test] if k in TEST_RESULT_VALID_KEYS}

Expand Down