Exemplo n.º 1
0
    async def query(self, query_payload):
        """

        :param query_payload:
        :return:
        :Example:
            curl -X PUT http://0.0.0.0:8080/storage/reading/query -d @payload.json

            @payload.json content:
            {
              "where" : {
                "column" : "asset_code",
                "condition" : "=",
                "value" : "MyAsset"
                }
            }
        """

        if not query_payload:
            raise ValueError("Query payload is missing")

        if not Utils.is_json(query_payload):
            raise TypeError("Query payload must be a valid JSON")

        url = 'http://' + self._base_url + '/storage/reading/query'
        async with aiohttp.ClientSession() as session:
            async with session.put(url, data=query_payload) as resp:
                status_code = resp.status
                jdoc = await resp.json()
                if status_code not in range(200, 209):
                    _LOGGER.error("PUT url %s with query payload: %s, Error code: %d, reason: %s, details: %s",
                                  '/storage/reading/query', query_payload, resp.status, resp.reason, jdoc)
                    raise StorageServerError(code=resp.status, reason=resp.reason, error=jdoc)

        return jdoc
Exemplo n.º 2
0
    def append(cls, readings):
        """
        :param readings:
        :return:

        :Example:
            curl -X POST http://0.0.0.0:8080/storage/reading -d @payload.json

            {
              "readings" : [
                {
                  "asset_code": "MyAsset",
                  "read_key" : "5b3be500-ff95-41ae-b5a4-cc99d08bef40",
                  "reading" : { "rate" : 18.4 },
                  "user_ts" : "2017-09-21 15:00:09.025655"
                },
                {
                "asset_code": "MyAsset",
                "read_key" : "5b3be500-ff95-41ae-b5a4-cc99d18bef40",
                "reading" : { "rate" : 45.1 },
                "user_ts" : "2017-09-21 15:03:09.025655"
                }
              ]
            }

        """

        conn = http.client.HTTPConnection(cls._base_url)
        # TODO: need to set http / https based on service protocol

        if not readings:
            raise ValueError("ReadingsStorageClient payload is missing")

        if not Utils.is_json(readings):
            raise TypeError(
                "ReadingsStorageClient payload must be a valid JSON")

        conn.request('POST', url='/storage/reading', body=readings)
        r = conn.getresponse()

        # TODO: FOGL-615
        # log error with message if status is 4xx or 5xx
        if r.status in range(400, 500):
            _LOGGER.error("Post readings: Client error code: %d", r.status)
        if r.status in range(500, 600):
            _LOGGER.error("Post readings: Server error code: %d", r.status)

        res = r.read().decode()
        conn.close()
        return json.loads(res, strict=False)
Exemplo n.º 3
0
    def append(cls, readings):
        """
        :param readings:
        :return:

        :Example:
            curl -X POST http://0.0.0.0:8080/storage/reading -d @payload.json

            {
              "readings" : [
                {
                  "asset_code": "MyAsset",
                  "read_key" : "5b3be500-ff95-41ae-b5a4-cc99d08bef40",
                  "reading" : { "rate" : 18.4 },
                  "user_ts" : "2017-09-21 15:00:09.025655"
                },
                {
                "asset_code": "MyAsset",
                "read_key" : "5b3be500-ff95-41ae-b5a4-cc99d18bef40",
                "reading" : { "rate" : 45.1 },
                "user_ts" : "2017-09-21 15:03:09.025655"
                }
              ]
            }

        """

        conn = http.client.HTTPConnection(cls._base_url)
        # TODO: need to set http / https based on service protocol

        if not readings:
            raise ValueError("Readings payload is missing")

        if not Utils.is_json(readings):
            raise TypeError("Readings payload must be a valid JSON")

        conn.request('POST', url='/storage/reading', body=readings)
        r = conn.getresponse()
        res = r.read().decode()
        conn.close()
        jdoc = json.loads(res, strict=False)

        if r.status in range(400, 600):
            _LOGGER.error("POST url %s with payload: %s, Error code: %d, reason: %s, details: %s",
                          '/storage/reading', readings, r.status, r.reason, jdoc)
            raise StorageServerError(code=r.status, reason=r.reason, error=jdoc)

        return jdoc
Exemplo n.º 4
0
    async def append(self, readings):
        """
        :param readings:
        :return:

        :Example:
            curl -X POST http://0.0.0.0:8080/storage/reading -d @payload.json

            {
              "readings" : [
                {
                  "asset_code": "MyAsset",
                  "read_key" : "5b3be500-ff95-41ae-b5a4-cc99d08bef40",
                  "reading" : { "rate" : 18.4 },
                  "user_ts" : "2017-09-21 15:00:09.025655"
                },
                {
                "asset_code": "MyAsset",
                "read_key" : "5b3be500-ff95-41ae-b5a4-cc99d18bef40",
                "reading" : { "rate" : 45.1 },
                "user_ts" : "2017-09-21 15:03:09.025655"
                }
              ]
            }

        """

        if not readings:
            raise ValueError("Readings payload is missing")

        if not Utils.is_json(readings):
            raise TypeError("Readings payload must be a valid JSON")

        url = 'http://' + self._base_url + '/storage/reading'
        async with aiohttp.ClientSession() as session:
            async with session.post(url, data=readings) as resp:
                status_code = resp.status
                jdoc = await resp.json()
                if status_code not in range(200, 209):
                    _LOGGER.error(
                        "POST url %s with payload: %s, Error code: %d, reason: %s, details: %s",
                        '/storage/reading', readings, resp.status, resp.reason,
                        jdoc)
                    raise StorageServerError(code=resp.status,
                                             reason=resp.reason,
                                             error=jdoc)

        return jdoc
Exemplo n.º 5
0
    def update_tbl(self, tbl_name, data):
        """ update json payload for specified condition into given table

        :param tbl_name:
        :param data: JSON payload
        :return:

        :Example:
            curl -X PUT http://0.0.0.0:8080/storage/table/statistics_history -d @payload3.json
            @payload3.json content:
            {
                "condition" : {
                    "column" : "key",
                    "condition" : "=",
                    "value" : "SENT_test"
                },
                "values" : {
                    "value" : 44444
                }
            }
        """
        if not tbl_name:
            raise ValueError("Table name is missing")

        if not data:
            raise ValueError("Data to update is missing")

        if not Utils.is_json(data):
            raise TypeError("Provided data to update must be a valid JSON")

        conn = http.client.HTTPConnection(self.base_url)
        # TODO: need to set http / https based on service protocol
        put_url = '/storage/table/{tbl_name}'.format(tbl_name=tbl_name)

        conn.request('PUT', url=put_url, body=data)
        r = conn.getresponse()
        res = r.read().decode()
        conn.close()
        jdoc = json.loads(res, strict=False)

        if r.status in range(400, 600):
            _LOGGER.info("PUT %s, with payload: %s", put_url, data)
            _LOGGER.error("Error code: %d, reason: %s, details: %s", r.status, r.reason, jdoc)
            raise StorageServerError(code=r.status, reason=r.reason, error=jdoc)

        return jdoc
Exemplo n.º 6
0
    async def update_tbl(self, tbl_name, data):
        """ update json payload for specified condition into given table

        :param tbl_name:
        :param data: JSON payload
        :return:

        :Example:
            curl -X PUT http://0.0.0.0:8080/storage/table/statistics_history -d @payload3.json
            @payload3.json content:
            {
                "condition" : {
                    "column" : "key",
                    "condition" : "=",
                    "value" : "SENT_test"
                },
                "values" : {
                    "value" : 44444
                }
            }
        """
        if not tbl_name:
            raise ValueError("Table name is missing")

        if not data:
            raise ValueError("Data to update is missing")

        if not Utils.is_json(data):
            raise TypeError("Provided data to update must be a valid JSON")

        put_url = '/storage/table/{tbl_name}'.format(tbl_name=tbl_name)

        url = 'http://' + self.base_url + put_url
        async with aiohttp.ClientSession() as session:
            async with session.put(url, data=data) as resp:
                status_code = resp.status
                jdoc = await resp.json()
                if status_code not in range(200, 209):
                    _LOGGER.info("PUT %s, with payload: %s", put_url, data)
                    _LOGGER.error("Error code: %d, reason: %s, details: %s",
                                  resp.status, resp.reason, jdoc)
                    raise StorageServerError(code=resp.status,
                                             reason=resp.reason,
                                             error=jdoc)

        return jdoc
Exemplo n.º 7
0
    async def query_tbl_with_payload(self, tbl_name, query_payload):
        """ Complex SELECT query for the specified table with a payload

        :param tbl_name:
        :param query_payload: payload in valid JSON format
        :return:

        :Example:
            curl -X PUT http://0.0.0.0:8080/storage/table/statistics_history/query -d @payload.json
            @payload.json content:
            "where" : {
                    "column" : "key",
                    "condition" : "=",
                    "value" : "SENT_test"
            }
        """
        if not tbl_name:
            raise ValueError("Table name is missing")

        if not query_payload:
            raise ValueError("Query payload is missing")

        if not Utils.is_json(query_payload):
            raise TypeError("Query payload must be a valid JSON")

        put_url = '/storage/table/{tbl_name}/query'.format(tbl_name=tbl_name)

        url = 'http://' + self.base_url + put_url
        async with aiohttp.ClientSession() as session:
            async with session.put(url, data=query_payload) as resp:
                status_code = resp.status
                jdoc = await resp.json()
                if status_code not in range(200, 209):
                    _LOGGER.info("PUT %s, with query payload: %s", put_url,
                                 query_payload)
                    _LOGGER.error("Error code: %d, reason: %s, details: %s",
                                  resp.status, resp.reason, jdoc)
                    raise StorageServerError(code=resp.status,
                                             reason=resp.reason,
                                             error=jdoc)

        return jdoc
Exemplo n.º 8
0
    def query_tbl_with_payload(self, tbl_name, query_payload):
        """ Complex SELECT query for the specified table with a payload

        :param tbl_name:
        :param query_payload: payload in valid JSON format
        :return:

        :Example:
            curl -X PUT http://0.0.0.0:8080/storage/table/statistics_history/query -d @payload.json
            @payload.json content:
            "where" : {
                    "column" : "key",
                    "condition" : "=",
                    "value" : "SENT_test"
            }
        """
        if not tbl_name:
            raise ValueError("Table name is missing")

        if not query_payload:
            raise ValueError("Query payload is missing")

        if not Utils.is_json(query_payload):
            raise TypeError("Query payload must be a valid JSON")

        conn = http.client.HTTPConnection(self.base_url)
        # TODO: need to set http / https based on service protocol
        put_url = '/storage/table/{tbl_name}/query'.format(tbl_name=tbl_name)

        conn.request('PUT', url=put_url, body=query_payload)
        r = conn.getresponse()
        res = r.read().decode()
        conn.close()
        jdoc = json.loads(res, strict=False)

        if r.status in range(400, 600):
            _LOGGER.info("PUT %s, with query payload: %s", put_url, query_payload)
            _LOGGER.error("Error code: %d, reason: %s, details: %s", r.status, r.reason, jdoc)
            raise StorageServerError(code=r.status, reason=r.reason, error=jdoc)

        return jdoc
Exemplo n.º 9
0
    async def insert_into_tbl(self, tbl_name, data):
        """ insert json payload into given table

        :param tbl_name:
        :param data: JSON payload
        :return:

        :Example:
            curl -X POST http://0.0.0.0:8080/storage/table/statistics_history -d @payload2.json
            @payload2.json content:

            {
                "key" : "SENT_test",
                "history_ts" : "now()",
                "value" : 1
            }
        """
        if not tbl_name:
            raise ValueError("Table name is missing")

        if not data:
            raise ValueError("Data to insert is missing")

        if not Utils.is_json(data):
            raise TypeError("Provided data to insert must be a valid JSON")

        post_url = '/storage/table/{tbl_name}'.format(tbl_name=tbl_name)
        url = 'http://' + self.base_url + post_url
        async with aiohttp.ClientSession() as session:
            async with session.post(url, data=data) as resp:
                status_code = resp.status
                jdoc = await resp.json()
                if status_code not in range(200, 209):
                    _LOGGER.info("POST %s, with payload: %s", post_url, data)
                    _LOGGER.error("Error code: %d, reason: %s, details: %s",
                                  resp.status, resp.reason, jdoc)
                    raise StorageServerError(code=resp.status,
                                             reason=resp.reason,
                                             error=jdoc)

        return jdoc
Exemplo n.º 10
0
    def insert_into_tbl(self, tbl_name, data):
        """ insert json payload into given table

        :param tbl_name:
        :param data: JSON payload
        :return:

        :Example:
            curl -X POST http://0.0.0.0:8080/storage/table/statistics_history -d @payload2.json
            @payload2.json content:
            
            {
                "key" : "SENT_test",
                "history_ts" : "now()",
                "value" : 1
            }
        """
        conn = http.client.HTTPConnection(self.base_url)
        # TODO: need to set http / https based on service protocol

        post_url = '/storage/table/{tbl_name}'.format(tbl_name=tbl_name)
        if not data:
            raise ValueError("Data to insert is missing")

        if not Utils.is_json(data):
            raise TypeError("Provided data to insert must be a valid JSON")

        conn.request('POST', url=post_url, body=data)
        r = conn.getresponse()

        # TODO: FOGL-615
        # log error with message if status is 4xx or 5xx
        if r.status in range(400, 500):
            _LOGGER.error("Post %s: Client error code: %d", post_url, r.status)
        if r.status in range(500, 600):
            _LOGGER.error("Post %s: Server error code: %d", post_url, r.status)

        res = r.read().decode()
        conn.close()
        return json.loads(res, strict=False)
Exemplo n.º 11
0
    async def delete_from_tbl(self, tbl_name, condition=None):
        """ Delete for specified condition from given table

        :param tbl_name:
        :param condition: JSON payload
        :return:

        :Example:
            curl -X DELETE http://0.0.0.0:8080/storage/table/statistics_history -d @payload_del.json
            @payload_del.json content:
            "condition" : {
                    "column" : "key",
                    "condition" : "=",
                    "value" : "SENT_test"
            }
        """

        if not tbl_name:
            raise ValueError("Table name is missing")

        del_url = '/storage/table/{tbl_name}'.format(tbl_name=tbl_name)

        if condition and (not Utils.is_json(condition)):
            raise TypeError("condition payload must be a valid JSON")

        url = 'http://' + self.base_url + del_url
        async with aiohttp.ClientSession() as session:
            async with session.delete(url, data=condition) as resp:
                status_code = resp.status
                jdoc = await resp.json()
                if status_code not in range(200, 209):
                    _LOGGER.info("DELETE %s, with payload: %s", del_url,
                                 condition if condition else '')
                    _LOGGER.error("Error code: %d, reason: %s, details: %s",
                                  resp.status, resp.reason, jdoc)
                    raise StorageServerError(code=resp.status,
                                             reason=resp.reason,
                                             error=jdoc)

        return jdoc
Exemplo n.º 12
0
    def query(cls, query_payload):
        """

        :param query_payload:
        :return:
        :Example:
            curl -X PUT http://0.0.0.0:8080/storage/reading/query -d @payload.json

            @payload.json content:
            {
              "where" : {
                "column" : "asset_code",
                "condition" : "=",
                "value" : "MyAsset"
                }
            }
        """

        if not query_payload:
            raise ValueError("Query payload is missing")

        if not Utils.is_json(query_payload):
            raise TypeError("Query payload must be a valid JSON")

        conn = http.client.HTTPConnection(cls._base_url)
        # TODO: need to set http / https based on service protocol

        conn.request('PUT', url='/storage/reading/query', body=query_payload)
        r = conn.getresponse()
        res = r.read().decode()
        conn.close()
        jdoc = json.loads(res, strict=False)

        if r.status in range(400, 600):
            _LOGGER.error("PUT url %s with query payload: %s, Error code: %d, reason: %s, details: %s",
                          '/storage/reading/query', query_payload, r.status, r.reason, jdoc)
            raise StorageServerError(code=r.status, reason=r.reason, error=jdoc)

        return jdoc
Exemplo n.º 13
0
    def delete_from_tbl(self, tbl_name, condition=None):
        """ Delete for specified condition from given table

        :param tbl_name:
        :param condition: JSON payload
        :return:

        :Example:
            curl -X DELETE http://0.0.0.0:8080/storage/table/statistics_history -d @payload_del.json
            @payload_del.json content:
            "condition" : {
                    "column" : "key",
                    "condition" : "=",
                    "value" : "SENT_test"
            }
        """

        if not tbl_name:
            raise ValueError("Table name is missing")

        conn = http.client.HTTPConnection(self.base_url)
        # TODO: need to set http / https based on service protocol
        del_url = '/storage/table/{tbl_name}'.format(tbl_name=tbl_name)

        if condition and (not Utils.is_json(condition)):
            raise TypeError("condition payload must be a valid JSON")

        conn.request('DELETE', url=del_url, body=condition)
        r = conn.getresponse()
        res = r.read().decode()
        conn.close()
        jdoc = json.loads(res, strict=False)

        if r.status in range(400, 600):
            _LOGGER.info("DELETE %s, with payload: %s", del_url, condition if condition else '')
            _LOGGER.error("Error code: %d, reason: %s, details: %s", r.status, r.reason, jdoc)
            raise StorageServerError(code=r.status, reason=r.reason, error=jdoc)

        return jdoc
Exemplo n.º 14
0
    def delete_from_tbl(self, tbl_name, condition=None):
        """ Delete for specified condition from given table

        :param tbl_name:
        :param condition: JSON payload
        :return:

        :Example:
            curl -X DELETE http://0.0.0.0:8080/storage/table/statistics_history -d @payload_del.json
            @payload_del.json content:
            "condition" : {
                    "column" : "key",
                    "condition" : "=",
                    "value" : "SENT_test"
            }
        """
        conn = http.client.HTTPConnection(self.base_url)
        # TODO: need to set http / https based on service protocol
        del_url = '/storage/table/{tbl_name}'.format(tbl_name=tbl_name)

        if condition and (not Utils.is_json(condition)):
            raise TypeError("condition payload must be a valid JSON")

        conn.request('DELETE', url=del_url, body=condition)
        r = conn.getresponse()

        # TODO: FOGL-615
        # log error with message if status is 4xx or 5xx
        if r.status in range(400, 500):
            _LOGGER.error("Delete %s: Client error code: %d", del_url,
                          r.status)
        if r.status in range(500, 600):
            _LOGGER.error("Delete %s: Server error code: %d", del_url,
                          r.status)

        res = r.read().decode()
        conn.close()
        return json.loads(res, strict=False)
Exemplo n.º 15
0
 def test_is_json_return_false_with_invalid_json(self, test_input):
     ret_val = Utils.is_json(test_input)
     assert ret_val is False
Exemplo n.º 16
0
 def test_is_json_return_true_with_valid_json(self, test_input):
     ret_val = Utils.is_json(test_input)
     assert ret_val is True