ndejong / pfsense_fauxapi

REST based API interface for pfSense 2.3.x and 2.4.x to facilitate devops
Apache License 2.0
354 stars 61 forks source link

Set timeout #76

Closed dunderrrrrr closed 3 years ago

dunderrrrrr commented 3 years ago

Hello!

Are you able to set a timeout somewhere if the request is failing? Let's say if the pfSense-machine is down.

fapi = PfsenseFauxapi(
    ip,
    faux_apikey,
    faux_apisecret
)
try:
    config = fapi.config_get()
except:
    config = None

The request is running for approx 30-40 seconds before moving on (I'm looping this code).

dunderrrrrr commented 3 years ago

Mmkay I worked around the problem, maybe not the best way but here goes if anyone else is stuck with the same issue.

I made a manual request (fetching system_stats) to the pfSense machine before running my code above.

Since I cannot figure out how to tell the PfsenseFauxApi-function to timeout, I'm telling requests to do it instead r = requests.get(url, headers=headers, verify=False, timeout=3). If it fails, we're moving on in 3 sec.

status = check_device_status(fw.ip, fw.faux_apikey, fw.faux_apisecret)
    if status:
        # device is online, run PfsenseFauxapi()
    else:
        # device is offline, proceed with next one in loop
def check_device_status(ip, faux_apikey, faux_apisecret):
    timestamp = datetime.datetime.now().strftime('%Y%m%dZ%H%M%S')
    url = "https://{}/fauxapi/v1/?action=system_stats".format(ip)
    build_headers = generate_auth(faux_apikey, faux_apisecret, timestamp)
    headers = {
        'fauxapi-auth': build_headers
    }
    try:
        r = requests.get(url, headers=headers, verify=False, timeout=3)
        logging.info('[{}] Device is ONLINE'.format(ip))
        return True
    except Exception as E:
        logging.warning('[{}] Device is OFFLINE'.format(ip))
        return False

We'll end up with one extra query to the pfSense, I believe thats no big deal.

ndejong commented 3 years ago

Hi -

This issue needs to be raised for the Python client over here - https://github.com/ndejong/pfsense_fauxapi_client_python/issues

A timeout for the client is a good suggestion though!

ndejong commented 3 years ago

https://github.com/ndejong/pfsense_fauxapi_client_python/issues/4