def fetch(self, path, payload, proxy_uri=None, callback=None, timeout=None, **kwargs): # pragma: no cover """ Perform a FETCH on a certain path. :param path: the path :param payload: the request payload :param proxy_uri: Proxy-Uri option of a request :param callback: the callback function to invoke upon response :param timeout: the timeout of the request :return: the response """ request = self.mk_request(defines.Codes.FETCH, path) request.token = generate_random_token(2) if proxy_uri: request.proxy_uri = proxy_uri request.payload = payload request.content_type = defines.Content_types[ "application/map-keys+json"] for k, v in kwargs.iteritems(): if hasattr(request, k): setattr(request, k, v) return self.send_request(request, callback, timeout)
def observe(self, path, callback, uri_query=None, timeout=None, **kwargs): # pragma: no cover """ Perform a GET with observe on a certain path. :param path: the path :param callback: the callback function to invoke upon notifications :param timeout: the timeout of the request :return: the response to the observe request """ request = self.mk_request(defines.Codes.GET, path) request.token = generate_random_token(2) request.observe = 0 if uri_query is not None: request.uri_query = uri_query for k, v in kwargs.iteritems(): if hasattr(request, k): setattr(request, k, v) return self.send_request(request, callback, timeout)
def put(self, path, payload, callback=None, timeout=None, no_response=False, **kwargs): # pragma: no cover """ Perform a PUT on a certain path. :param path: the path :param payload: the request payload :param callback: the callback function to invoke upon response :param timeout: the timeout of the request :return: the response """ request = self.mk_request(defines.Codes.PUT, path) request.token = generate_random_token(2) request.payload = payload if no_response: request.add_no_response() request.type = defines.Types["NON"] for k, v in kwargs.items(): if hasattr(request, k): setattr(request, k, v) return self.send_request(request, callback, timeout, no_response=no_response)
def put(self, path, payload, proxy_uri=None, callback=None, timeout=None, **kwargs): # pragma: no cover """ Perform a PUT on a certain path. :param path: the path :param payload: the request payload :param proxy_uri: Proxy-Uri option of a request :param callback: the callback function to invoke upon response :param timeout: the timeout of the request :return: the response """ request = self.mk_request(defines.Codes.PUT, path) request.token = generate_random_token(2) request.payload = payload if proxy_uri: request.proxy_uri = proxy_uri for k, v in kwargs.iteritems(): if hasattr(request, k): setattr(request, k, v) return self.send_request(request, callback, timeout)
def send_request(self, request, callback=None, timeout=None): # pragma: no cover """ Send a request to the remote server. :param request: the request to send :param callback: the callback function to invoke upon response :param timeout: the timeout of the request :return: the response (synchronous), or the token (for asynchronous callback) """ with self.requests_lock: # Same requests from the same endpoint must have different tokens # Ensure there is a unique token in case the other side issues a # delayed response after a standalone ACK while request.token in self.requests: request.token = generate_random_token(2) context = _RequestContext(request, callback) self.requests[request.token] = context self.protocol.send_message(request) if callback: # So that requester can cancel asynchronous OBSERVE return request.token # Wait for response context.responded.wait(timeout) del self.requests[request.token] return context.response
def sendPutRequest( self, resource: ResourceNameEnum, payload: str, enableCON=False, timeout: int = IRequestResponseClient.DEFAULT_TIMEOUT) -> bool: """ Send data to CoAP server by requesting a PUT :param resource: The resource name of where to PUT :param payload: Data to send :param enableCON: Whether send a CON CoAP msg :param timeout: Timeout seconds of this request :return: Whether succeed to send request """ if resource: logging.debug("Issuing PUT with path: " + resource.value) request = self.coapClient.mk_request(defines.Codes.PUT, path=resource.value) request.token = generate_random_token(2) request.payload = payload if not enableCON: request.type = defines.Types["NON"] self.coapClient.send_request(request=request, callback=self._onPutResponse, timeout=timeout) return True else: logging.warning("Can't PUT - no path or path list provided.") return False pass
def sendDeleteRequest( self, resource: ResourceNameEnum, enableCON=False, timeout: int = IRequestResponseClient.DEFAULT_TIMEOUT) -> bool: """ Send Delete to CoAP server to delete sth. :param resource: The resource name of what need to be deleted :param enableCON: Whether send a CON CoAP msg :param timeout: Timeout seconds of this request :return: Whether succeed to send request """ if resource: logging.debug("Issuing DELETE with path: " + resource.value) request = self.coapClient.mk_request(defines.Codes.DELETE, path=resource.value) request.token = generate_random_token(2) if not enableCON: request.type = defines.Types["NON"] self.coapClient.send_request(request=request, callback=self._onDeleteResponse, timeout=timeout) return True else: logging.warning("Can't DELETE - no path or path list provided.") return False pass
def post(self, path, payload, callback=None, timeout=None, *options): request = self.mk_request(defines.Codes.POST, path) request.token = generate_random_token(2) request.payload = payload for number, value in options: option = Option() option.number = number option.value = value request.add_option(option) return self.send_request(request, callback, timeout)
def post(self, path, payload, callback=None): # pragma: no cover request = Request() request.destination = self.server request.code = defines.Codes.POST.number request.token = generate_random_token(2) request.uri_path = path request.payload = payload if callback is not None: thread = threading.Thread(target=self._thread_body, args=(request, callback)) thread.start() else: self.protocol.send_message(request) response = self.queue.get(block=True) return response
def post(self, path, payload, callback=None, timeout=None): # pragma: no cover request = Request() request.destination = self.server request.code = defines.Codes.POST.number request.token = generate_random_token(2) request.uri_path = path request.payload = payload if callback is not None: thread = threading.Thread(target=self._thread_body, args=(request, callback)) thread.start() else: self.protocol.send_message(request) response = self.queue.get(block=True, timeout=timeout) return response
def discover(self, callback=None, timeout=None, **kwargs): # pragma: no cover """ Perform a Discover request on the server. :param callback: the callback function to invoke upon response :param timeout: the timeout of the request :return: the response """ request = self.mk_request(defines.Codes.GET, defines.DISCOVERY_URL) request.token = generate_random_token(self.token_length) for k, v in kwargs.iteritems(): if hasattr(request, k): setattr(request, k, v) return self.send_request(request, callback, timeout)
def discover(self, callback=None, timeout=None, **kwargs): # pragma: no cover """ Perform a Discover request on the server. :param callback: the callback function to invoke upon response :param timeout: the timeout of the request :return: the response """ request = self.mk_request(defines.Codes.GET, defines.DISCOVERY_URL) request.token = generate_random_token(2) for k, v in kwargs.iteritems(): if hasattr(request, k): setattr(request, k, v) return self.send_request(request, callback, timeout)
def delete(self, path, callback=None, timeout=None, **kwargs): # pragma: no cover """ Perform a DELETE on a certain path. :param path: the path :param callback: the callback function to invoke upon response :param timeout: the timeout of the request :return: the response """ request = self.mk_request(defines.Codes.DELETE, path) request.token = generate_random_token(self.token_length) for k, v in kwargs.iteritems(): if hasattr(request, k): setattr(request, k, v) return self.send_request(request, callback, timeout)
def observe(self, path, callback, timeout=None, **kwargs): # pragma: no cover """ Perform a GET with observe on a certain path. :param path: the path :param callback: the callback function to invoke upon notifications :param timeout: the timeout of the request :return: the response to the observe request """ request = self.mk_request(defines.Codes.GET, path) request.token = generate_random_token(2) request.observe = 0 for k, v in kwargs.iteritems(): if hasattr(request, k): setattr(request, k, v) return self.send_request(request, callback, timeout)
def _get(self): path ="/sys/dev/status" try: client = self._create_coap_client(self.server, self.port) self._send_hello_sequence(client) request = client.mk_request(defines.Codes.GET, path) request.destination = server=(self.server, self.port) request.type = defines.Types["ACK"] request.token = generate_random_token(4) request.observe = 0 response = client.send_request(request, None, 2) finally: client.stop() if response: return json.loads(response.payload)["state"]["reported"] else: return {}
def get_non(self, path, callback=None, timeout=None, **kwargs): # pragma: no cover """ Perform a GET on a certain path. :param path: the path :param callback: the callback function to invoke upon response :param timeout: the timeout of the request :return: the response """ request = self.mk_request_non(defines.Codes.GET, path) if (request.uri_query is not None and request.uri_query != ""): request.content_type = defines.Content_types["application/link-format"] request.token = generate_random_token(2) for k, v in kwargs.items(): if hasattr(request, k): setattr(request, k, v) return self.send_request(request, callback, timeout)
def post(self, path, payload, callback=None, timeout=None): # pragma: no cover """ Perform a POST on a certain path. :param path: the path :param payload: the request payload :param callback: the callback function to invoke upon response :param timeout: the timeout of the request :return: the response """ request = self.mk_request(defines.Codes.POST, path) request.token = generate_random_token(2) request.payload = payload return self.send_request(request, callback, timeout)
def sendGetRequest( self, resource: ResourceNameEnum, enableCON=False, timeout: int = IRequestResponseClient.DEFAULT_TIMEOUT) -> bool: logging.info("the method sendGetRequest is called") if resource: logging.debug("Issuing GET with path: " + resource.value) request = self.coapClient.mk_request(defines.Codes.GET, path=resource.value) request.token = generate_random_token(2) if not enableCON: request.type = defines.Types["NON"] self.coapClient.send_request(request=request, callback=self._onGetResponse, timeout=timeout) else: logging.warning("Can't test GET - no path or path list provided.")
def delete(self, path, proxy_uri=None, callback=None, timeout=None, **kwargs): # pragma: no cover """ Perform a DELETE on a certain path. :param path: the path :param proxy_uri: Proxy-Uri option of a request :param callback: the callback function to invoke upon response :param timeout: the timeout of the request :return: the response """ request = self.mk_request(defines.Codes.DELETE, path) request.token = generate_random_token(2) if proxy_uri: request.proxy_uri = proxy_uri for k, v in kwargs.iteritems(): if hasattr(request, k): setattr(request, k, v) return self.send_request(request, callback, timeout)
def sendPutRequest( self, resource: ResourceNameEnum, payload=None, enableCON=False, timeout: int = IRequestResponseClient.DEFAULT_TIMEOUT) -> bool: if resource: """logging.debug("Issuing PUT with path: " + resource.value)""" request = self.coapClient.mk_request(defines.Codes.PUT, path=resource.value) request.token = generate_random_token(2) request.payload = payload if not enableCON: request.type = defines.Types["NON"] self.coapClient.send_request(request=request, callback=self._onPutResponse, timeout=timeout) else: """logging.warning("Can't test PUT - no path or path list provided.")"""
def sendDeleteRequest( self, resource: ResourceNameEnum, enableCON=False, timeout: int = IRequestResponseClient.DEFAULT_TIMEOUT) -> bool: logging.info("sendDeleteRequest.") if resource: logging.debug("Issuing DELETE with path: " + resource.value) request = self.coapClient.mk_request(defines.Codes.DELETE, path=resource.value) request.token = generate_random_token(2) # set type if not enableCON: request.type = defines.Types["NON"] # send request self.coapClient.send_request(request=request, callback=self._onDeleteResponse, timeout=timeout) return True else: logging.warning( "Can't test DELETE - no path or path list provided.") return False
def remove_observe(self, path, callback=None,timeout=None, qos=1,no_response=0, **kwargs): # pragma: no cover """ Perform a GET with observe = 1 on a certain path. :param path: the path :param timeout: the timeout of the request :param no_response: the no_response option to include in the request :return: the response to the observe request """ request = self.mk_request(defines.Codes.GET, path) request.token = generate_random_token(2) request.observe = 1 for k, v in kwargs.items(): if hasattr(request, k): setattr(request, k, v) if no_response!=0: request.add_no_response(no_response) if qos == 0: request.type = defines.Types["NON"] #self.pendingRequests[request.token]=request return self.send_request(request,callback,timeout)