def createBehavior(self, behavior): self.__getToken() if not isinstance(behavior, QiStreamBehavior): return conn = http.HTTPSConnection(self.url) payload = behavior.toString() conn.request("POST", self.__behaviorBase, payload, self.__qi_headers()) response = conn.getresponse() if response.status == 302: # Found url = urlparse(response.getheader("Location")) conn = http.HTTPSConnection(self.url) conn.request("GET", url.path, headers=self.__qi_headers()) response = conn.getresponse() if response.status == 200 or response.status == 201: string = response.read().decode() type = QiStreamBehavior.fromString(string) conn.close() return type else: conn.close() raise QiError( "Failed to create behavior. {status}:{reason}".format( status=response.status, reason=response.reason))
def createBehavior(self, tenant_id, namespace_id, behavior): self.__getToken() if not isinstance(behavior, QiStreamBehavior): return conn = http.HTTPSConnection(self.url) payload = behavior.toString() conn.request( "POST", self.__behaviorBase.format(tenant_id=tenant_id, namespace_id=namespace_id), payload, self.__qi_headers(), ) response = conn.getresponse() if response.status == 302: # Found url = urlparse(response.getheader("Location")) conn = http.HTTPSConnection(self.url) conn.request("GET", url.path, headers=self.__qi_headers()) response = conn.getresponse() if response.status == 200 or response.status == 201: string = response.read().decode() type = QiStreamBehavior.fromString(string) conn.close() return type else: conn.close() raise QiError( "Failed to create behavior. {status}:{reason}".format(status=response.status, reason=response.reason) )
def getBehaviorReferenceCount(self, namespace_id, behavior_id): """Retrieves the behavior reference count""" if namespace_id is None: raise TypeError if behavior_id is None: raise TypeError response = requests.get( self.__url + self.__behaviorsPath.format(tenant_id=self.__tenant, namespace_id=namespace_id, behavior_id=behavior_id) + "/ReferenceCount", headers=self.__qiHeaders()) if response.status_code < 200 or response.status_code >= 300: response.close() raise QiError("Failed to get QiBehavior reference count, {behavior_id}. {status}:{reason}". format(behavior_id=behavior_id, status=response.status_code, reason=response.text)) behavior = QiStreamBehavior.fromJson(json.loads(response.content)) response.close() return behavior
def getOrCreateBehavior(self, namespace_id, behavior): """Tells Qi Service to create a behavior based on a local QiBehavior object""" if namespace_id is None: raise TypeError if behavior is None or not isinstance(behavior, QiStreamBehavior): raise TypeError response = requests.post( self.__url + self.__behaviorsPath.format(tenant_id=self.__tenant, namespace_id=namespace_id, behavior_id=behavior.Id), data=behavior.toJson(), headers=self.__qiHeaders()) if response.status_code < 200 or response.status_code >= 300: response.close() raise QiError("Failed to create QiBehavior, {behavior_id}. {status}:{reason}". format(behavior_id=behavior.Id, status=response.status_code, reason=response.text)) behavior = QiStreamBehavior.fromJson(json.loads(response.content)) response.close() return behavior
def getBehaviors(self, namespace_id, skip=0, count=100): """Retrieves a list of behaviors associated with the specified 'namespace_id' under the current tenant""" if namespace_id is None: raise TypeError response = requests.get( self.__url + self.__behaviorsPath.format(tenant_id=self.__tenant, namespace_id=namespace_id, skip=skip, count=count), headers=self.__qiHeaders()) if response.status_code < 200 or response.status_code >= 300: response.close() raise QiError("Failed to get all QiBehaviors. {status}:{reason}". format(status=response.status_code, reason=response.text)) content = json.loads(response.content) results = [] for item in content: results.append(QiStreamBehavior.fromJson(item)) response.close() return results
def getBehavior(self, namespace_id, behavior_id): """Retrieves the behavior specified by 'behavior_id' from Qi Service""" if namespace_id is None: raise TypeError if behavior_id is None: raise TypeError response = requests.get( self.__url + self.__getBehaviorPath.format(tenant_id=self.__tenant, namespace_id=namespace_id, behavior_id=behavior_id), headers=self.__qiHeaders()) if response.status_code == 200: print("Succeeded in getting behavior") else: response.close() raise QiError("Failed to get QiBehavior, {behavior_id}. {status}:{reason}". format(behavior_id=behavior_id, status=response.status_code, reason=response.text)) string = response.content behavior = QiStreamBehavior.fromJson(json.loads(string)) response.close() return behavior