Example #1
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
Example #2
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
Example #3
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
Example #4
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
Example #5
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
Example #6
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
Example #7
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
Example #8
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