Exemplo n.º 1
0
    def post(self, path, payLoad, contentFormat, tokenString=None):

        print("Inside post")

        self.logger.debug("Posting data to the endpoint")
        #return dictionary struct of header
        header = pyZestUtil.zestHeader()
        header["code"] = 2
        header["token"] = tokenString
        header["tkl"] = len(tokenString)
        header["payload"] = payLoad
        header["oc"] = 3
        print(len(tokenString))
        print("Token string received -- " + str(header["token"]))

        # set header options as an array of dictionaries
        options = []
        #append Uri-path
        options.append({
            "number": 11,
            "len": len(path),
            "value": path,
        })
        #append Uri-host
        options.append({
            "number": 3,
            "len": len(sc.gethostname()),
            "value": sc.gethostname(),
        })
        #append content format
        options.append({
            "number":
            12,
            "len":
            2,
            "value":
            pyZestUtil.content_format_to_int(contentFormat),
        })

        header["options"] = options
        # header marshal into bytes
        header_into_bytes = pyZestUtil.marshalZestHeader(header)

        try:
            response = self.send_request_and_await_response(header_into_bytes)
            print("response from send request " + str(response))
            try:
                parsed_response = self.handle_response(response,
                                                       self.returnPayload)
                return parsed_response
            except (RuntimeError, TypeError, NameError) as e:
                self.logger.error(
                    "Inside Post: Error runtime or type or name - " +
                    str(e.args))

        except ValueError as e:
            self.logger.error("Inside Post: Message sending error - " +
                              str(e.args))
Exemplo n.º 2
0
    def observe(self,
                path,
                contentFormat,
                tokenString=None,
                observeMode=None,
                timeOut=0):
        self.logger.debug("Observing data from the endpoint")
        header = pyZestUtil.zestHeader()
        header["code"] = 1
        header["token"] = tokenString
        header["tkl"] = len(tokenString)
        header["oc"] = 5
        options = []
        options.append({
            "number": 11,
            "len": len(path),
            "value": path,
        })
        options.append({
            "number": 3,
            "len": len(sc.gethostname()),
            "value": sc.gethostname(),
        })
        #Q: guess this is observe option("data" or "audit")
        options.append({
            "number": 6,
            "len": len(observeMode),  #vs451 added observe Mode len assignment
            "value": observeMode,
        })  #vs451 added observe Mode value assignment
        options.append({
            "number":
            12,
            "len":
            2,
            "value":
            pyZestUtil.content_format_to_int(contentFormat),
        })
        #append Max-Age
        options.append({
            "number": 14,
            "len": 4,
            "value": timeOut,
        })
        header["options"] = options

        header_into_bytes = pyZestUtil.marshalZestHeader(header)
        try:
            response = self.send_request_and_await_response(header_into_bytes)
        except Exception as e:
            self.logger.error("Inside Observe: Message sending error - " +
                              str(e.args))
        try:
            parsed_response = self.handle_response(response, self.resolve)
            return parsed_response
        except Exception as e:
            self.logger.error("Inside Observe: Error in handling response: " +
                              str(e.args[0]))
Exemplo n.º 3
0
    def get(self, path, contentFormat, tokenString=None):
        self.logger.debug("Inside GET: Getting data from the endpoint")
        header = pyZestUtil.zestHeader()
        header["code"] = 1
        header["token"] = tokenString
        header["tkl"] = len(tokenString)
        header["oc"] = 3

        # set header options
        options = []
        options.append({
            "number": 11,
            "len": len(path),
            "value": path,
        })

        options.append({
            "number": 3,
            "len": len(sc.gethostname()),
            "value": sc.gethostname(),
        })

        options.append({
            "number":
            12,
            "len":
            2,
            "value":
            pyZestUtil.content_format_to_int(contentFormat),
        })
        header["options"] = options

        # header marshal into bytes
        header_into_bytes = pyZestUtil.marshalZestHeader(header)

        try:
            response = self.send_request_and_await_response(header_into_bytes)
            print("Respons from GET")
            print(response)
            try:
                parsed_response = self.handle_response(response,
                                                       self.returnPayload)
                print(parsed_response)
                if parsed_response is None:
                    return parsed_response
                else:
                    return parsed_response
            except (RuntimeError, TypeError, NameError) as e:
                self.logger.error(
                    "Inside GET: Error runtime or type or name - " +
                    str(e.args))

        except ValueError as e:
            self.logger.error("Inside GET: Message sending error - " +
                              str(e.args))
Exemplo n.º 4
0
    def delete(self, path, contentFormat, tokenString=None):
        self.logger.debug("Inside DELETE: deleting data from the endpoint")
        header = pyZestUtil.zestHeader()
        header["code"] = 4
        header["token"] = tokenString
        header["tkl"] = len(tokenString)
        header["oc"] = 3

        # set header options
        options = []
        options.append({
            "number": 11,
            "len": len(path),
            "value": path,
        })

        options.append({
            "number": 3,
            "len": len(sc.gethostname()),
            "value": sc.gethostname(),
        })

        options.append({
            "number":
            12,
            "len":
            2,
            "value":
            pyZestUtil.content_format_to_int(contentFormat),
        })
        header["options"] = options

        # header marshal into bytes
        header_into_bytes = pyZestUtil.marshalZestHeader(header)

        try:
            response = self.send_request_and_await_response(header_into_bytes)
            try:
                parsed_response = self.handle_response(response,
                                                       self.returnPayload)
            except Exception as e:
                self.logger.error(
                    "Inside DELETE: Error in handling response -" +
                    str(e.args))
            return parsed_response
        except Exception as e:
            self.logger.error("Inside DELETE: Message sending error  " +
                              str(e.args))