Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RENAME sensor_id -> device_id in code #84

Merged
merged 7 commits into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions python-lib/tc_etl_lib/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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://<iota_endpoint>:<port>/iot/json', sensor_id='<sensor_id>', api_key='<api_key>')
iotam: tc.iota.iotaManager = tc.iota.iotaManager(endpoint = 'http://<iota_endpoint>:<port>/iot/json', device_id='<device_id>', api_key='<api_key>')
iotam.send_http(data={"<key_1>": "<value_1>", "<key_2>": "<value_2>"})

# Envío de datos en ráfaga al Agente IoT.
iotam: tc.iota.iotaManager = tc.iota.iotaManager(endpoint = 'http://<iota_endpoint>:<port>/iot/json', sensor_id='<sensor_id>', api_key='<api_key>', sleep_send_batch='<time_sleep>')
iotam: tc.iota.iotaManager = tc.iota.iotaManager(endpoint = 'http://<iota_endpoint>:<port>/iot/json', device_id='<device_id>', api_key='<api_key>', sleep_send_batch='<time_sleep>')
iotam.send_batch_http(data=[{"<key_1>": "<value_1>", "<key_2>": "<value_2>"}, {"<key_3>": "<value_3>", "<key_4>": "<value_4>"}])
```

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -505,6 +505,8 @@ TOTAL 403 221 45%

## Changelog

- Fix: rename sensor_id to device_it in iotManager to align with the term used in IOTA library ([#77](https://github.com/telefonicasc/etl-framework/pull/76))
fgalan marked this conversation as resolved.
Show resolved Hide resolved

0.11.0 (February 2nd, 2024)

- Fix: close session upon finalization to avoid warnings ([#76](https://github.com/telefonicasc/etl-framework/pull/76))
Expand Down
14 changes: 7 additions & 7 deletions python-lib/tc_etl_lib/tc_etl_lib/iota.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,19 @@ class iotaManager:
"""IoT Agent Manager.

endpoint: define service endpoint iota (example: https://<service>:<port>).
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

Expand All @@ -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 = {
Expand Down Expand Up @@ -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
return True
24 changes: 12 additions & 12 deletions python-lib/tc_etl_lib/tc_etl_lib/test_iota.py
Original file line number Diff line number Diff line change
Expand Up @@ -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_sensor_id', api_key='fake_api_key')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
iot = iotaManager(endpoint='http://fakeurl.com', device_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.
Expand All @@ -45,15 +45,15 @@ 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_sensor_id', api_key='fake_api_key')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
iot = iotaManager(endpoint='http://fakeurl.com', device_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):
iot.send_http(data={"key": "value"})

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_sensor_id', api_key='fake_api_key')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
iot = iotaManager(endpoint='http://fakeurl.com', device_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)
Expand All @@ -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_sensor_id', api_key='fake_api_key')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
iot = iotaManager(endpoint='http://fakeurl.com', device_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"},
Expand All @@ -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_sensor_id', api_key='fake_api_key')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
iot = iotaManager(endpoint='http://fakeurl.com', device_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:
Expand All @@ -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_sensor_id', api_key='fake_api_key')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
iot = iotaManager(endpoint='http://fakeurl.com', device_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:
Expand All @@ -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_sensor_id', api_key='fake_api_key')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
iot = iotaManager(endpoint='http://fakeurl.com', device_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:
Expand All @@ -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_sensor_id', api_key='fake_api_key', sleep_send_batch=0.25)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
iot = iotaManager(endpoint='http://fakeurl.com', device_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_sensor_id', api_key='fake_api_key', sleep_send_batch=0.25)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
iot = iotaManager(endpoint='http://fakeurl.com', device_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:
Expand All @@ -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_sensor_id', api_key='fake_api_key', sleep_send_batch=0.25)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
iot = iotaManager(endpoint='http://fakeurl.com', device_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
Expand All @@ -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_sensor_id', api_key='fake_api_key', sleep_send_batch=0.25)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
iot = iotaManager(endpoint='http://fakeurl.com', device_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)
self.assertEqual(mock_send_http.call_count, 3)
Loading