|
|
@@ -24,20 +24,31 @@ import sys
|
|
|
import json
|
|
|
import requests
|
|
|
|
|
|
+API_PATH = "/api/v1/data"
|
|
|
+TIMEOUT = 10
|
|
|
+
|
|
|
def main():
|
|
|
- args = sys.argv[1:]
|
|
|
- if len(args) < 1:
|
|
|
- print("Usage: script.py IP or FQDN")
|
|
|
+ if len(sys.argv) < 2:
|
|
|
+ print("Usage: script.py IP_or_FQDN")
|
|
|
sys.exit(1)
|
|
|
|
|
|
- address = args[0]
|
|
|
+ address = sys.argv[1]
|
|
|
+
|
|
|
try:
|
|
|
- response = requests.get('http://{}/api/v1/data'.format(address))
|
|
|
- except requests.exceptions.RequestException as e:
|
|
|
- raise SystemExit(e)
|
|
|
+ response = requests.get(
|
|
|
+ f"http://{address}{API_PATH}",
|
|
|
+ timeout=TIMEOUT,
|
|
|
+ )
|
|
|
+ response.raise_for_status()
|
|
|
+ parsed = response.json()
|
|
|
+
|
|
|
+ except requests.exceptions.RequestException as exc:
|
|
|
+ raise SystemExit(f"API request failed: {exc}")
|
|
|
+
|
|
|
+ except ValueError as exc:
|
|
|
+ raise SystemExit(f"API returned invalid JSON: {exc}")
|
|
|
|
|
|
- parsed = response.json()
|
|
|
- print('<<<local>>>')
|
|
|
+ print("<<<local>>>")
|
|
|
iterate_json(parsed)
|
|
|
|
|
|
# Using For Loop to iterate thru the data offered by the P1-API
|