@@ -20,16 +20,20 @@ def check_for_scan_jobs(config_data):
20
20
"""Check for new scans through the API."""
21
21
22
22
# Build URL to pull new scan jobs. Server determines jobs based off agent (user) making request.
23
- url = "{}:{}/api/scheduled_scans?format=json" .format (
24
- config_data ["master_address" ], config_data ["master_port" ]
25
- )
23
+ master_address = config_data ["master_address" ]
24
+ master_port = config_data ["master_port" ]
25
+ api_token = config_data ["api_token" ]
26
+
27
+ url = f"{ master_address } :{ master_port } /api/scheduled_scans"
26
28
logger .ROOT_LOGGER .info ("check_for_scans URL: {}" .format (url ))
27
29
28
30
# Update User-Agent and add API token.
31
+ # fmt:off
29
32
headers = {
30
33
"user-agent" : config_data ["scan_agent" ],
31
- "Authorization" : "Token {}" . format ( config_data [ "api_token" ]) ,
34
+ "Authorization" : f "Token { api_token } " ,
32
35
}
36
+ # fmt:on
33
37
34
38
try :
35
39
# Make the HTTP GET request.
@@ -40,53 +44,47 @@ def check_for_scan_jobs(config_data):
40
44
return response .json ()
41
45
42
46
else :
43
- logger .ROOT_LOGGER .error (
44
- "Could not access {}. HTTP status code: {}" .format (
45
- url , response .status_code
46
- )
47
- )
47
+ logger .ROOT_LOGGER .error (f"Could not access { url } . HTTP status code: { response .status_code } " )
48
48
return None
49
49
50
50
except Exception as e :
51
- logger .ROOT_LOGGER .error (
52
- "api.check_for_scans function exception: {}" .format (e )
53
- )
51
+ logger .ROOT_LOGGER .error (f"api.check_for_scan_jobs function exception: { e } " )
54
52
55
53
56
54
def update_scan_information (config_data , scan_job , update_info ):
57
55
"""Update scan information using a PATCH API request."""
58
56
57
+ master_address = config_data ["master_address" ]
58
+ master_port = config_data ["master_port" ]
59
+ api_token = config_data ["api_token" ]
60
+ scan_agent = config_data ["scan_agent" ]
61
+ scan_job_id = scan_job ["id" ]
62
+
59
63
# Build URL to update scan job.
60
- url = "{}:{}/api/scheduled_scans/{}" .format (
61
- config_data ["master_address" ], config_data ["master_port" ], scan_job ["id" ]
62
- )
63
- logger .ROOT_LOGGER .info ("update_scan_information URL: {}" .format (url ))
64
+ url = f"{ master_address } :{ master_port } /api/scheduled_scans/{ scan_job_id } "
65
+ logger .ROOT_LOGGER .info (f"update_scan_information URL: { url } " )
64
66
65
- # Update the User-Agent, add API token, and add Content-Type.
67
+ # Update the User-Agent, API token, and Content-Type.
68
+ # fmt:off
66
69
headers = {
67
- "user-agent" : config_data [ " scan_agent" ] ,
68
- "Authorization" : "Token {}" . format ( config_data [ "api_token" ]) ,
70
+ "user-agent" : scan_agent ,
71
+ "Authorization" : f "Token { api_token } " ,
69
72
"Content-Type" : "application/json" ,
70
73
}
74
+ # fmt:on
71
75
72
76
# Make the HTTP PATCH request.
73
- response = requests .patch (
74
- url , headers = headers , verify = False , timeout = 15 , json = update_info
75
- )
77
+ response = requests .patch (url , headers = headers , verify = False , timeout = 15 , json = update_info )
76
78
77
79
if response .status_code == 200 :
78
80
logger .ROOT_LOGGER .info (
79
- "Successfully updated scan information for scan ID {} with data {}" .format (
80
- scan_job ["id" ], update_info
81
- )
81
+ f"Successfully updated scan information for scan ID { scan_job_id } with data { update_info } "
82
82
)
83
83
return None
84
84
85
85
else :
86
86
logger .ROOT_LOGGER .error (
87
- "Could not access {} or failed to update scan ID {}. HTTP status code: {}" .format (
88
- url , scan_job ["id" ], response .status_code
89
- )
87
+ f"Could not access { url } or failed to update scan ID { scan_job_id } . HTTP status code: { response .status_code } "
90
88
)
91
- logger .ROOT_LOGGER .error ("Response content: {}" .format (response . content ))
89
+ logger .ROOT_LOGGER .error (f "Response content: { response . content } " .format ())
92
90
return None
0 commit comments