Merge pull request #307 from rad4day/master

[module/spaceapi] fix freeze on unreachable API
This commit is contained in:
tobi-wan-kenobi 2018-10-26 09:03:52 +02:00 committed by GitHub
commit 4a63bfc45b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -10,6 +10,8 @@ Parameters:
* spaceapi.name: String overwriting the space name * spaceapi.name: String overwriting the space name
* spaceapi.prefix: Prefix for the space string * spaceapi.prefix: Prefix for the space string
* spaceapi.interval: time between updates in minutes * spaceapi.interval: time between updates in minutes
* spaceapi.timeout: Maximum time in seconds to wait for a response from API
endpoint
""" """
import bumblebee.input import bumblebee.input
@ -36,6 +38,10 @@ class Module(bumblebee.engine.Module):
# so you're able to distinguish # so you're able to distinguish
self._name = self.parameter("name", default="") self._name = self.parameter("name", default="")
# The timeout prevents the statusbar from blocking when the destination
# can't be reached.
self._timeout = self.parameter("timeout", default=2)
# Only execute every 5 minutes by default # Only execute every 5 minutes by default
self.interval(self.parameter("interval", default=5)) self.interval(self.parameter("interval", default=5))
@ -61,7 +67,7 @@ class Module(bumblebee.engine.Module):
def update(self, widgets): def update(self, widgets):
try: try:
with requests.get(self._url) as u: with requests.get(self._url, timeout=self.timeout) as u:
json = u.json() json = u.json()
self._open = json["state"]["open"] self._open = json["state"]["open"]
self._name = self.parameter("name", default=json["space"]) self._name = self.parameter("name", default=json["space"])