def fetch_all(cls): url = get_bridge_base_url() + cls.url with closing(urlopen(url, timeout=5)) as response: res = json.loads(response.read()) return [ cls.from_dict(k, v) for k, v in res.iteritems() ]
def save(self, path='/'): """Save state to hue bridge. Set path when the PUT url is weird and not on the root.""" url = get_bridge_base_url() + self.url + str(self.id) + path opener = build_opener(HTTPHandler) # only sync dirty attributes payload = {} for key, value in self.local_changes.iteritems(): if self.local_changes[key] == self[key]: continue self[key] = value payload[key] = value super(BaseHueObject, self).__setattr__('local_changes', {}) request = Request(url, data=json.dumps(payload)) request.add_header('Content-Type', 'application/json') request.get_method = lambda: 'PUT' with closing(opener.open(request, timeout=5)) as response: return json.loads(response.read())
def fetch(cls, id_): url = get_bridge_base_url() + cls.url + str(id_) with closing(urlopen(url, timeout=5)) as response: res = json.loads(response.read()) print res return cls.from_dict(id_, res)