finish06 / pyunifi

https://unifi-sdn.ubnt.com/
MIT License
226 stars 100 forks source link

Allow to define request timeout #14

Open Programie opened 6 years ago

Programie commented 6 years ago

Currently this library doesn't seem to support setting a request timeout. In case the Unifi Controller is not responding, a script using this library is waiting infinitely.

In my case I'm using this library in a small local check for Check_MK to monitor my access points. If the Unifi Controller is not responding, every check will start an additional Python process waiting for the Unifi Controller to respond which will soon end in having hundreds of processes waiting for the Unifi Controller.

Could you please add the possibility to specify the timeout for the API requests?

finish06 commented 6 years ago

Great idea. I think this can be accomplished with the requests module. I will start playing with this...

Thanks!

Programie commented 6 years ago

I don't have much knowledge in Python, but maybe this will help: http://docs.python-requests.org/en/master/user/quickstart/#timeouts

vsnine commented 6 years ago

I'd also like to add that if the module can't reach the UniFi Controller, in the case of the integration with Home Assistant, it causes the entire app to block, and without any noticeable error message. Took a while to find it.

Should be as simple as adding a "timeout=x" entry to each of the calls to requests. I've got a locally edited version of controller.py, i'll see about getting a pull request over to you.

vsnine commented 6 years ago

This is the pull request I came up with: https://github.com/finish06/pyunifi/pull/15