You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

97 lines
3.1 KiB
Python

import requests
from read_api_details import parse_csv_to_dict
from sys import platform
import subprocess
def ping(host):
#Returns True if host (str) responds to a ping request.
# Option for the number of packets as a function of
if platform == "win32":
param1 = '-n'
param2 = '-w'
param3 = '250'
else:
param1 = '-c'
param2 = '-W'
param3 = '0.25'
# Building the command. Ex: "ping -c 1 google.com"
command = ['ping', param1, '1', param2, param3, host]
return subprocess.call(command, stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT) == 0
def call_api(api_url, method, params):
"""
Function to make an API call.
Parameters:
- api_url (str): The URL of the API endpoint.
- method (str): The HTTP method to use ('POST' or 'GET').
- params (dict): Dictionary of parameters.
Returns:
- response: The response from the API call.
"""
print("Calling API",api_url,"with method",method,"and parameters",params)
if method.upper() == 'POST':
response = requests.post(api_url, data=params, verify='./apicert.crt', timeout=8)
elif method.upper() == 'GET':
response = requests.get(api_url, params=params, verify='./apicert.crt', timeout=8)
else:
raise ValueError("Method must be 'POST' or 'GET'")
return response
def call_api_preloaded(callname, method="auto", params={}, baseaddr="https://192.168.49.1/tool/", conntype="eth"):
details = parse_csv_to_dict("apidetails.csv")
if not callname.find(".php") > 0:
callname += ".php"
full_url = baseaddr + callname
auto_include = []
methodcount = {}
if 'PARAM' in details[callname]:
for param, val in details[callname]["PARAM"].items():
if not val in methodcount:
methodcount[val] = 0
methodcount[val] += 1
mx = (None, 0)
if method == "auto" or method == "":
for mt, ct in methodcount.items():
if ct > mx[1]:
mx = (mt, ct)
if mx[0] is None:
# no methods?
return None
else:
method = mx[0]
if 'PARAM' in details[callname]:
for param, val in details[callname]["PARAM"].items():
# print(param, val)
if val == method:
print(param, end="")
if 'KEY' in details[callname]:
for keyname, key in details[callname]["KEY"].items():
if keyname == param:
print("=" + key, end="")
auto_include.append((keyname, key))
if param == "type":
# common parameter checks if in BLE mode
print("=" + conntype, end="")
auto_include.append(("type", conntype))
print("")
for param in auto_include:
params[param[0]] = param[1]
if not ping("192.168.49.1"):
print("Connecting to netool...")
while not ping("192.168.49.1"):
pass
return call_api(full_url, method, params)