Ejemplo n.º 1
0
    def __send_request(self, method, url, headers=None, payload=None, verify=None, query=None):
        """
        Send a request to a specific url in a specifig type of http request
        """

        parameters = {"method": method, "url": url}

        if headers is not None:
            parameters.update({"headers": headers})

        if payload is not None:
            if self.check_json:
                parameters.update({"data": json.dumps(payload)})
            else:
                parameters.update({"data": payload})

        if query is not None:
            parameters.update({"params": query})

        if verify is not None:
            parameters.update({"verify": verify})
        # Send the requests
        try:
            response = requests.request(**parameters)
        except RequestException, e:
            PqaTools.log_requestAndResponse(url=url, headers=headers, data=payload, comp="STH", method=method)
            assert False, "ERROR: [NETWORK ERROR] {}".format(e)
Ejemplo n.º 2
0
    def sendRegister(self, apikey, device, asset, model, phenomena):
        url = self.getUrl('RegLight', apikey, device)
        print 'url: ' + url
        uom_id = 1
        result = "<rs>"
        result += "<id href=\"1:1\">" + device + "</id>"
        result += "<id href=\"1:8\">" + asset + "</id>"
        result += "<param name=\"ModelName\">"
        result += "<text>" + model + "</text>"
        result += "</param>"
        print phenomena
        for phenom in phenomena:
            result += "<what href=\"" + str(phenom.get("href", "")) + "\" id=\"" + str(phenom.get("alias", "")) + "\"/>"
        for phenom in phenomena:
            result += "<data name=\"" + str(phenom.get("phenom", "")) + "\" id=\"" + str(
                phenom.get("alias", "")) + "\">"
            if phenom.get("uom"):
                result += "<quan uom=\"" + str(phenom.get("uom", "")) + "\">" + str(uom_id) + "</quan>"
            else:
                result += "<text>" + str(uom_id) + "</text>"
            uom_id += 1
            result += "</data>"
        result += "</rs>"
        print result
        res = requests.post(url, data=result)

        #log request
        PqaTools.log_requestAndResponse(url=url, headers={}, data=result, comp='IOTA', response=res, method='post')

        return res
Ejemplo n.º 3
0
    def __send_request(self, method, url, headers=None, payload=None, verify=None, query=None):
        """
        Send a request to a specific url in a specifig type of http request
        """

        parameters = {
            'method': method,
            'url': url,
        }

        if headers is not None:
            parameters.update({'headers': headers})

        if payload is not None:
            if self.check_json:
                parameters.update({'data': json.dumps(payload, ensure_ascii=False).encode('utf-8')})
            else:
                parameters.update({'data': payload})

        if query is not None:
            parameters.update({'params': query})

        if verify is not None:
            parameters.update({'verify': verify})

        # Send the requests
        try:
            response = requests.request(**parameters)
        except RequestException, e:
            PqaTools.log_requestAndResponse(url=url, headers=headers, params=query, data=payload, comp='CB',
                                            method=method)
            assert False, 'ERROR: [NETWORK ERROR] {}'.format(e)
Ejemplo n.º 4
0
    def __send_request(self, method, url, headers=None, payload=None, verify=None, query=None):
        """
        Send a request to a specific url in a specifig type of http request
        """

        parameters = {
            'method': method,
            'url': url,
        }

        if headers is not None:
            parameters.update({'headers': headers})

        if payload is not None:
            if self.check_json:
                parameters.update({'data': json.dumps(payload)})
            else:
                parameters.update({'data': payload})

        if query is not None:
            parameters.update({'params': query})

        if verify is not None:
            parameters.update({'verify': verify})
        # Send the requests
        try:
            response = requests.request(**parameters)
        except RequestException, e:
            PqaTools.log_requestAndResponse(url=url, headers=headers, data=payload, comp='STH', method=method)
            assert False, 'ERROR: [NETWORK ERROR] {}'.format(e)
Ejemplo n.º 5
0
    def SendCmdResponse(self, measure_type, apikey, idDevice, command, response):
        url = self.getUrl(measure_type, apikey, idDevice, command)
        print 'url: ' + url
        data = str(command) + "|" + str(response)
        res = requests.post(url, data=data)

        #log request
        PqaTools.log_requestAndResponse(url=url, headers={}, data=data, comp='IOTA', response=res, method='post')
        return res
Ejemplo n.º 6
0
    def getCommand(self, measure_type, apikey, idDevice, ip={}):
        url = self.getUrl(measure_type, apikey, idDevice)
        if ip:
            url += "&ip=" + ip
        print 'url: ' + url
        res = requests.get(url)

        #log request
        PqaTools.log_requestAndResponse(url=url, headers={}, data='', comp='IOTA', response=res, method='get')
        return res
Ejemplo n.º 7
0
    def getCommand(self, measure_type, apikey, idDevice, ip={}):
        url = self.getUrl(measure_type, apikey, idDevice)
        if ip:
            url += "&ip=" + ip
        print 'url: ' + url
        res = requests.get(url)

        #log request
        PqaTools.log_requestAndResponse(url=url,
                                        headers={},
                                        data='',
                                        comp='IOTA',
                                        response=res,
                                        method='get')
        return res
Ejemplo n.º 8
0
    def sendMeasure(self, measure_type, apikey, idDevice, measures, field={}):
        url = self.getUrl(measure_type, apikey, idDevice)
        if field == 'getCmd':
            url += '&getCmd=1'
        if "ip" in field:
            url += '&ip='
            url += field["ip"]

        print 'url: ' + url
        data = self.getMeasure(measure_type, measures, field)
        res = requests.post(url,headers = {'content-type': 'text/plain'},data=data)

        #log request
        PqaTools.log_requestAndResponse(url=url, headers={}, data=data, comp='IOTA', response=res, method='post')
        return res
Ejemplo n.º 9
0
    def SendCmdResponse(self, measure_type, apikey, idDevice, command,
                        response):
        url = self.getUrl(measure_type, apikey, idDevice, command)
        print 'url: ' + url
        data = str(command) + "|" + str(response)
        res = requests.post(url, data=data)

        #log request
        PqaTools.log_requestAndResponse(url=url,
                                        headers={},
                                        data=data,
                                        comp='IOTA',
                                        response=res,
                                        method='post')
        return res
Ejemplo n.º 10
0
    def __send_request(self,
                       method,
                       url,
                       headers=None,
                       payload=None,
                       verify=None,
                       query=None):
        """
        Send a request to a specific url in a specifying type of http request
        """

        parameters = {
            'method': method,
            'url': url,
        }

        if headers is not None:
            parameters.update({'headers': headers})

        if payload is not None:
            if self.check_json:
                parameters.update({
                    'data':
                    json.dumps(payload, ensure_ascii=False).encode('utf-8')
                })
            else:
                parameters.update({'data': payload})

        if query is not None:
            parameters.update({'params': query})

        if verify is not None:
            parameters.update({'verify': verify})
        else:
            # If the method does not include the verify parameter, it takes the value from object
            parameters.update({'verify': self.verify})

        # Send the requests
        try:
            response = requests.request(**parameters)
        except RequestException, e:
            PqaTools.log_requestAndResponse(url=url,
                                            headers=headers,
                                            params=query,
                                            data=payload,
                                            comp='CB',
                                            method=method)
            assert False, 'ERROR: [NETWORK ERROR] {}'.format(e)
Ejemplo n.º 11
0
    def api_put(self, path, headers={}, params={}, data={}):
        res = None
        try:
            res = requests.put(url=path,
                               data=data,
                               headers=headers,
                               params=params)

        except requests.exceptions.Timeout:
            PqaTools.log_requestAndResponse(url=path,
                                            params=params,
                                            headers=headers,
                                            data=data,
                                            comp='IOTA',
                                            response="TIMEOUT",
                                            method='put')

        except requests.exceptions.RequestException as e:
            PqaTools.log_requestAndResponse(url=path,
                                            params=params,
                                            headers=headers,
                                            data=data,
                                            comp='IOTA',
                                            response=e, method='put')
            return "ERROR"

            # log request
        PqaTools.log_requestAndResponse(url=path,
                                        headers=headers,
                                        params=params, data=data,
                                        comp='IOTA',
                                        response=res,
                                        method='put')
        return res
Ejemplo n.º 12
0
    def api_put(self, path, headers={}, params={}, data={}, verify=False):
        res = None
        try:
            res = requests.put(url=path,
                               data=data,
                               headers=headers,
                               params=params,
                               verify=verify)

        except requests.exceptions.Timeout:
            PqaTools.log_requestAndResponse(url=path,
                                            params=params,
                                            headers=headers,
                                            data=data,
                                            comp='IOTA',
                                            response="TIMEOUT",
                                            method='put')

        except requests.exceptions.RequestException as e:
            PqaTools.log_requestAndResponse(url=path,
                                            params=params,
                                            headers=headers,
                                            data=data,
                                            comp='IOTA',
                                            response=e, method='put')
            return "ERROR"

            # log request
        PqaTools.log_requestAndResponse(url=path,
                                        headers=headers,
                                        params=params, data=data,
                                        comp='IOTA',
                                        response=res,
                                        method='put')
        return res
Ejemplo n.º 13
0
    def sendRegister(self, apikey, device, asset, model, phenomena):
        url = self.getUrl('RegLight', apikey, device)
        print 'url: ' + url
        uom_id = 1
        result = "<rs>"
        result += "<id href=\"1:1\">" + device + "</id>"
        result += "<id href=\"1:8\">" + asset + "</id>"
        result += "<param name=\"ModelName\">"
        result += "<text>" + model + "</text>"
        result += "</param>"
        print phenomena
        for phenom in phenomena:
            result += "<what href=\"" + str(
                phenom.get("href", "")) + "\" id=\"" + str(
                    phenom.get("alias", "")) + "\"/>"
        for phenom in phenomena:
            result += "<data name=\"" + str(
                phenom.get("phenom", "")) + "\" id=\"" + str(
                    phenom.get("alias", "")) + "\">"
            if phenom.get("uom"):
                result += "<quan uom=\"" + str(phenom.get(
                    "uom", "")) + "\">" + str(uom_id) + "</quan>"
            else:
                result += "<text>" + str(uom_id) + "</text>"
            uom_id += 1
            result += "</data>"
        result += "</rs>"
        print result
        res = requests.post(url, data=result)

        #log request
        PqaTools.log_requestAndResponse(url=url,
                                        headers={},
                                        data=result,
                                        comp='IOTA',
                                        response=res,
                                        method='post')

        return res
Ejemplo n.º 14
0
    def sendMeasure(self, measure_type, apikey, idDevice, measures, field={}):
        url = self.getUrl(measure_type, apikey, idDevice)
        if field == 'getCmd':
            url += '&getCmd=1'
        if "ip" in field:
            url += '&ip='
            url += field["ip"]

        print 'url: ' + url
        data = self.getMeasure(measure_type, measures, field)
        res = requests.post(url,
                            headers={'content-type': 'text/plain'},
                            data=data)

        #log request
        PqaTools.log_requestAndResponse(url=url,
                                        headers={},
                                        data=data,
                                        comp='IOTA',
                                        response=res,
                                        method='post')
        return res
Ejemplo n.º 15
0
    def __send_request(self, method, url, headers=None, payload=None, verify=None, query=None):
        """
        Send a request to a specific url in a specific type of http request
        :param method: http method (get | post | put | delete)
        :param url: url of the requests
        :param headers: headers of the request
        :param payload: payload of the request
        :param verify: if the requests has SSL verification on client (True | False)
        :param query: params of the request
        """

        parameters = {
            'method': method,
            'url': url,
        }

        if headers is not None:
            parameters.update({'headers': headers})

        if payload is not None:
            try:
                parameters.update({'data': json.dumps(payload)})
            except ValueError:
                parameters.update({'data': payload})

        if query is not None:
            parameters.update({'params': query})

        if verify is not None:
            parameters.update({'verify': verify})

        # Send the requests
        try:
            response = requests.request(**parameters)
        except RequestException, e:
            PqaTools.log_requestAndResponse(url=url, headers=headers, data=payload, comp='CKAN')
            assert False, 'ERROR: [NETWORK ERROR] {}'.format(e)
Ejemplo n.º 16
0
    def __send_request(self, method, url, headers=None, payload=None, verify=None, query=None):
        """
        Send a request to a specific url in a specific type of http request
        :param method: http method (get | post | put | delete)
        :param url: url of the requests
        :param headers: headers of the request
        :param payload: payload of the request
        :param verify: if the requests has SSL verification on client (True | False)
        :param query: params of the request
        """

        parameters = {
            'method': method,
            'url': url,
        }

        if headers is not None:
            parameters.update({'headers': headers})

        if payload is not None:
            try:
                parameters.update({'data': json.dumps(payload)})
            except ValueError:
                parameters.update({'data': payload})

        if query is not None:
            parameters.update({'params': query})

        if verify is not None:
            parameters.update({'verify': verify})

        # Send the requests
        try:
            response = requests.request(**parameters)
        except RequestException, e:
            PqaTools.log_requestAndResponse(url=url, headers=headers, data=payload, comp='CKAN')
            assert False, 'ERROR: [NETWORK ERROR] {}'.format(e)
Ejemplo n.º 17
0
    def api_get(self, path, headers={}, params={}, verify=False):
        res = None

        try:
            # clean content_type in not allowed requests
            if "content-type" in headers:
                headers.pop("content-type", None)
            elif "Content-Type" in headers:  # used in Requests library version 2.11.1 or higher
                headers.pop("Content-Type", None)

            res = requests.get(url=path,
                               headers=headers,
                               params=params,
                               verify=verify)

        except requests.exceptions.Timeout:
            PqaTools.log_requestAndResponse(url=path,
                                            params=params,
                                            headers=headers,
                                            data='',
                                            comp='IOTA',
                                            response="TIMEOUT",
                                            method='get')

        except requests.exceptions.RequestException as e:
            PqaTools.log_requestAndResponse(url=path,
                                            params=params,
                                            headers=headers,
                                            data='',
                                            comp='IOTA',
                                            response=e, method='get')
            return "ERROR"

        # log request
        PqaTools.log_requestAndResponse(url=path,
                                        headers=headers,
                                        params=params,
                                        data='',
                                        comp='IOTA',
                                        response=res,
                                        method='get')
        return res
Ejemplo n.º 18
0
    def api_get(self, path, headers={}, params={}):
        res = None

        try:
            # clean content_type in not allowed requests
            if "content-type" in headers:
                headers.pop("content-type", None)
            elif "Content-Type" in headers:  # used in Requests library version 2.11.1 or higher
                headers.pop("Content-Type", None)

            res = requests.get(url=path,
                               headers=headers,
                               params=params)

        except requests.exceptions.Timeout:
            PqaTools.log_requestAndResponse(url=path,
                                            params=params,
                                            headers=headers,
                                            data='',
                                            comp='IOTA',
                                            response="TIMEOUT",
                                            method='get')

        except requests.exceptions.RequestException as e:
            PqaTools.log_requestAndResponse(url=path,
                                            params=params,
                                            headers=headers,
                                            data='',
                                            comp='IOTA',
                                            response=e, method='get')
            return "ERROR"

        # log request
        PqaTools.log_requestAndResponse(url=path,
                                        headers=headers,
                                        params=params,
                                        data='',
                                        comp='IOTA',
                                        response=res,
                                        method='get')
        return res