diff --git a/python-lib/tc_etl_lib/README.md b/python-lib/tc_etl_lib/README.md index dc2af9c..ab7e3d6 100644 --- a/python-lib/tc_etl_lib/README.md +++ b/python-lib/tc_etl_lib/README.md @@ -231,11 +231,11 @@ Ejemplo de uso de la clase iotaManager ``` #create an iota manager and use it -iotam: tc.iota.iotaManager = tc.iota.iotaManager(endpoint = 'http://:/iot/json', sensor_id='', api_key='') +iotam: tc.iota.iotaManager = tc.iota.iotaManager(endpoint = 'http://:/iot/json', device_id='', api_key='') iotam.send_http(data={"": "", "": ""}) # Envío de datos en ráfaga al Agente IoT. -iotam: tc.iota.iotaManager = tc.iota.iotaManager(endpoint = 'http://:/iot/json', sensor_id='', api_key='', sleep_send_batch='') +iotam: tc.iota.iotaManager = tc.iota.iotaManager(endpoint = 'http://:/iot/json', device_id='', api_key='', sleep_send_batch='') iotam.send_batch_http(data=[{"": "", "": ""}, {"": "", "": ""}]) ``` @@ -358,8 +358,8 @@ La librería está creada con diferentes clases dependiendo de la funcionalidad - Clase `iotaManager`: En esta clase están las funciones relacionadas con el agente IoT. - `__init__`: constructor de objetos de la clase. - - :param obligatorio `sensor_id`: El ID del sensor. - - :param obligatorio `api_key`: La API key correspondiente al sensor. + - :param obligatorio `device_id`: El ID del device. + - :param obligatorio `api_key`: La API key correspondiente al device. - :param obligatorio `endpoint`: La URL del servicio al que se le quiere enviar los datos. - :param opcional `sleep_send_batch`: Es el tiempo de espera entre cada envío de datos en segundos (default: 0). - `send_http`: Función que envía un archivo en formato JSON al agente IoT por petición HTTP. @@ -505,6 +505,8 @@ TOTAL 403 221 45% ## Changelog +- Fix: rename sensor_id to device_id in iotManager to align with the term used in IOTA library ([#77](https://github.com/telefonicasc/etl-framework/pull/77)) + 0.11.0 (February 2nd, 2024) - Fix: close session upon finalization to avoid warnings ([#76](https://github.com/telefonicasc/etl-framework/pull/76)) diff --git a/python-lib/tc_etl_lib/tc_etl_lib/iota.py b/python-lib/tc_etl_lib/tc_etl_lib/iota.py index 6b86e50..375bb94 100644 --- a/python-lib/tc_etl_lib/tc_etl_lib/iota.py +++ b/python-lib/tc_etl_lib/tc_etl_lib/iota.py @@ -42,19 +42,19 @@ class iotaManager: """IoT Agent Manager. endpoint: define service endpoint iota (example: https://:). - sensor_id: sensor ID. - api_key: API key of the corresponding sensor. + device_id: device ID. + api_key: API key of the corresponding device. sleep_send_batch: time sleep in seconds (default: 0). """ endpoint: str - sensor_id: str + device_id: str api_key: str sleep_send_batch: float - def __init__(self, endpoint: str, sensor_id: str, api_key: str, sleep_send_batch: float = 0): + def __init__(self, endpoint: str, device_id: str, api_key: str, sleep_send_batch: float = 0): self.endpoint = endpoint - self.sensor_id = sensor_id + self.device_id = device_id self.api_key = api_key self.sleep_send_batch = sleep_send_batch @@ -65,7 +65,7 @@ def send_http(self, raise TypeError("The 'data' parameter should be a dictionary with key-value pairs.") params = { - 'i': self.sensor_id, + 'i': self.device_id, 'k': self.api_key } headers = { @@ -103,4 +103,4 @@ def send_batch_http(self, data: Iterable) -> Union[None, bool]: time.sleep(self.sleep_send_batch) except Exception as e: raise SendBatchError(f"send_batch_http error. Index where the error occurred: {i}\nError detail: {str(e)}", original_exception=e, index=i) from e - return True \ No newline at end of file + return True diff --git a/python-lib/tc_etl_lib/tc_etl_lib/test_iota.py b/python-lib/tc_etl_lib/tc_etl_lib/test_iota.py index eeeed28..b8578eb 100644 --- a/python-lib/tc_etl_lib/tc_etl_lib/test_iota.py +++ b/python-lib/tc_etl_lib/tc_etl_lib/test_iota.py @@ -34,7 +34,7 @@ class TestIotaManager(unittest.TestCase): def test_send_http_success(self): """A success message should be displayed when HTTP request is executed successfully.""" - iot = iotaManager(endpoint='http://fakeurl.com', sensor_id='fake_sensor_id', api_key='fake_api_key') + iot = iotaManager(endpoint='http://fakeurl.com', device_id='fake_device_id', api_key='fake_api_key') with patch('requests.post') as mock_post: fake_response = Mock() # Simulates a successful code status. @@ -45,7 +45,7 @@ def test_send_http_success(self): def test_send_http_connection_error(self): """Should raise an exception when there is a server connection error.""" - iot = iotaManager(endpoint='http://fakeurl.com', sensor_id='fake_sensor_id', api_key='fake_api_key') + iot = iotaManager(endpoint='http://fakeurl.com', device_id='fake_device_id', api_key='fake_api_key') with patch('requests.post') as mock_post: mock_post.side_effect = requests.exceptions.ConnectionError() with pytest.raises(requests.exceptions.ConnectionError): @@ -53,7 +53,7 @@ def test_send_http_connection_error(self): def test_send_http_invalid_data_type(self): """Should raise an exception when the data type is incorrect.""" - iot = iotaManager(endpoint='http://fakeurl.com', sensor_id='fake_sensor_id', api_key='fake_api_key') + iot = iotaManager(endpoint='http://fakeurl.com', device_id='fake_device_id', api_key='fake_api_key') with pytest.raises(TypeError) as exc_info: iot.send_http(data=["data"]) exception_message = str(exc_info.value) @@ -62,7 +62,7 @@ def test_send_http_invalid_data_type(self): def test_send_http_set_not_unique(self): """Should raise an exception if the data is an array of dictionaries.""" - iot = iotaManager(endpoint='http://fakeurl.com', sensor_id='fake_sensor_id', api_key='fake_api_key') + iot = iotaManager(endpoint='http://fakeurl.com', device_id='fake_device_id', api_key='fake_api_key') with pytest.raises(TypeError) as exc_info: iot.send_http(data=[ {"key_1": "value_1"}, @@ -73,7 +73,7 @@ def test_send_http_set_not_unique(self): def test_send_http_unauthorized(self): """Should raise an exception when the request is unauthorized.""" - iot = iotaManager(endpoint='http://fakeurl.com', sensor_id='fake_sensor_id', api_key='fake_api_key') + iot = iotaManager(endpoint='http://fakeurl.com', device_id='fake_device_id', api_key='fake_api_key') with patch('requests.post') as mock_post: mock_post.return_value.status_code = 401 with pytest.raises(exceptions.FetchError) as exc_info: @@ -84,7 +84,7 @@ def test_send_http_unauthorized(self): def test_send_http_not_found(self): """Should raise an exception when the request is not found.""" - iot = iotaManager(endpoint='http://fakeurl.com', sensor_id='fake_sensor_id', api_key='fake_api_key') + iot = iotaManager(endpoint='http://fakeurl.com', device_id='fake_device_id', api_key='fake_api_key') with patch('requests.post') as mock_post: mock_post.return_value.status_code = 404 with pytest.raises(exceptions.FetchError) as exc_info: @@ -96,7 +96,7 @@ def test_send_http_not_found(self): def test_send_http_server_error(self): """Should raise an exception if there is a server error.""" - iot = iotaManager(endpoint='http://fakeurl.com', sensor_id='fake_sensor_id', api_key='fake_api_key') + iot = iotaManager(endpoint='http://fakeurl.com', device_id='fake_device_id', api_key='fake_api_key') with patch('requests.post') as mock_post: mock_post.return_value.status_code = 500 with pytest.raises(exceptions.FetchError) as exc_info: @@ -109,13 +109,13 @@ def test_send_http_server_error(self): def test_send_batch_http_dict_success(self): """send_http should be called twice when we send an array with 2 dictionaries.""" with patch('tc_etl_lib.iota.iotaManager.send_http') as mock_send_http: - iot = iotaManager(endpoint='http://fakeurl.com', sensor_id='fake_sensor_id', api_key='fake_api_key', sleep_send_batch=0.25) + iot = iotaManager(endpoint='http://fakeurl.com', device_id='fake_device_id', api_key='fake_api_key', sleep_send_batch=0.25) iot.send_batch_http(data=[{'key1': 'value1'}, {'key2': 'value2'}]) self.assertEqual(mock_send_http.call_count, 2) def test_send_batch_http_dict_value_error(self): """Should raise TypeError and then raise SendBatchError with the index that failed.""" - iot = iotaManager(endpoint='http://fakeurl.com', sensor_id='fake_sensor_id', api_key='fake_api_key', sleep_send_batch=0.25) + iot = iotaManager(endpoint='http://fakeurl.com', device_id='fake_device_id', api_key='fake_api_key', sleep_send_batch=0.25) with patch('requests.post') as mock_post: mock_post.return_value.status_code = 200 with self.assertRaises(SendBatchError) as context: @@ -128,7 +128,7 @@ def test_send_batch_http_dict_value_error(self): def test_send_batch_http_connection_error(self): """Should raise a ConnectionError exception and then raise SedBatchError with the index that failed.""" - iot = iotaManager(endpoint='http://fakeurl.com', sensor_id='fake_sensor_id', api_key='fake_api_key', sleep_send_batch=0.25) + iot = iotaManager(endpoint='http://fakeurl.com', device_id='fake_device_id', api_key='fake_api_key', sleep_send_batch=0.25) with patch('requests.post') as mock_post: mock_success = MagicMock() mock_success.status_code = 200 @@ -150,10 +150,10 @@ def test_send_batch_http_connection_error(self): def test_send_batch_http_data_frame_success(self): """send_http should be called 3 times when we send a DataFrame with 3 rows.""" with patch('tc_etl_lib.iota.iotaManager.send_http') as mock_send_http: - iot = iotaManager(endpoint='http://fakeurl.com', sensor_id='fake_sensor_id', api_key='fake_api_key', sleep_send_batch=0.25) + iot = iotaManager(endpoint='http://fakeurl.com', device_id='fake_device_id', api_key='fake_api_key', sleep_send_batch=0.25) data = pd.DataFrame({ 'column1': [1, 2, 3], 'column2': ['a', 'b', 'c'] }) iot.send_batch_http(data=data) - self.assertEqual(mock_send_http.call_count, 3) \ No newline at end of file + self.assertEqual(mock_send_http.call_count, 3)