-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
cefili
authored and
cefili
committed
Nov 14, 2023
1 parent
3611471
commit 843d436
Showing
7 changed files
with
404 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
# | ||
# Copyright 2022 Telefónica Soluciones de Informática y Comunicaciones de España, S.A.U. | ||
# | ||
# This file is part of tc_etl_lib | ||
# | ||
# tc_etl_lib is free software: you can redistribute it and/or | ||
# modify it under the terms of the GNU Affero General Public License as | ||
# published by the Free Software Foundation, either version 3 of the | ||
# License, or (at your option) any later version. | ||
# | ||
# tc_etl_lib is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero | ||
# General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU Affero General Public License | ||
# along with IoT orchestrator. If not, see http://www.gnu.org/licenses/. | ||
|
||
""" | ||
Exceptions handling for Python. | ||
""" | ||
|
||
import requests | ||
from typing import Any, Optional | ||
|
||
class FetchError(Exception): | ||
""" | ||
FetchError encapsulates all parameters of an HTTP request and the erroneous response. | ||
""" | ||
|
||
response: requests.Response | ||
method: str | ||
url: str | ||
params: Optional[Any] = None | ||
headers: Optional[Any] = None | ||
body: Optional[Any] = None | ||
|
||
def __init__(self, response: requests.Response, method: str, url: str, params: Optional[Any] = None, headers: Optional[Any] = None, body: Optional[Any] = None): | ||
"""Constructor for FetchError class.""" | ||
self.response = response | ||
self.method = method | ||
self.url = url | ||
self.params = params | ||
self.headers = headers | ||
self.body = body | ||
|
||
def __str__(self) -> str: | ||
return f"Failed to {self.method} {self.url} (headers: {self.headers}, params: {self.params}, body: {self.body}): [{self.response.status_code}] {self.response.text}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
# | ||
# Copyright 2022 Telefónica Soluciones de Informática y Comunicaciones de España, S.A.U. | ||
# | ||
# This file is part of tc_etl_lib | ||
# | ||
# tc_etl_lib is free software: you can redistribute it and/or | ||
# modify it under the terms of the GNU Affero General Public License as | ||
# published by the Free Software Foundation, either version 3 of the | ||
# License, or (at your option) any later version. | ||
# | ||
# tc_etl_lib is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero | ||
# General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU Affero General Public License | ||
# along with IoT orchestrator. If not, see http://www.gnu.org/licenses/. | ||
|
||
''' | ||
IoT routines for Python: | ||
- iot.send_json | ||
- iot.send_batch | ||
''' | ||
|
||
from . import exceptions | ||
import requests | ||
import tc_etl_lib as tc | ||
import time | ||
from typing import Any, Iterable | ||
|
||
|
||
class IoT: | ||
"""IoT is a class that allows us to communicate with the IoT Agent.""" | ||
|
||
def __init__(self): | ||
pass | ||
|
||
def send_json(self, | ||
sensor_name: str, | ||
api_key: str, | ||
req_url: str, | ||
data: Any) -> None: | ||
params = { | ||
'i': sensor_name, | ||
'k': api_key | ||
} | ||
headers = { | ||
"Content-Type": "application/json" | ||
} | ||
|
||
try: | ||
# Verify if data is a single dictionary. | ||
if isinstance(data, dict): | ||
resp = requests.post(url=req_url, json=data, | ||
params=params, headers=headers) | ||
if resp.status_code == 200: | ||
return True | ||
else: | ||
raise exceptions.FetchError( | ||
response=resp, | ||
method="POST", | ||
url=req_url, | ||
params=params, | ||
headers=headers) | ||
else: | ||
raise ValueError( | ||
"The parameter 'data' should be a single dictionary {}.") | ||
except requests.exceptions.ConnectionError as e: | ||
raise e | ||
|
||
def send_batch(self, | ||
sensor_name: str, | ||
api_key: str, | ||
req_url: str, | ||
time_sleep: float, | ||
data: Iterable[Any]) -> None: | ||
params = { | ||
'i': sensor_name, | ||
'k': api_key | ||
} | ||
headers = { | ||
"Content-Type": "application/json" | ||
} | ||
|
||
try: | ||
for i in range(0, len(data)): | ||
resp = requests.post( | ||
url=req_url, json=data[i], params=params, headers=headers) | ||
|
||
if resp.status_code == 200: | ||
time.sleep(time_sleep) | ||
return True | ||
else: | ||
raise exceptions.FetchError(response=resp, | ||
method="POST", | ||
url=req_url, | ||
params=params, | ||
headers=headers) | ||
except requests.exceptions.ConnectionError as e: | ||
raise e |
Oops, something went wrong.