rcer-iot-client-pkg 0.6.0__tar.gz → 0.6.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (47) hide show
  1. {rcer_iot_client_pkg-0.6.0 → rcer_iot_client_pkg-0.6.1}/PKG-INFO +4 -6
  2. {rcer_iot_client_pkg-0.6.0 → rcer_iot_client_pkg-0.6.1}/README.md +3 -5
  3. {rcer_iot_client_pkg-0.6.0 → rcer_iot_client_pkg-0.6.1}/pyproject.toml +1 -1
  4. {rcer_iot_client_pkg-0.6.0 → rcer_iot_client_pkg-0.6.1}/src/rcer_iot_client_pkg/__init__.py +2 -2
  5. rcer_iot_client_pkg-0.6.1/src/rcer_iot_client_pkg/general_types/api/__init__.py +3 -0
  6. {rcer_iot_client_pkg-0.6.0 → rcer_iot_client_pkg-0.6.1}/src/rcer_iot_client_pkg/general_types/api/update_thies_data_types.py +2 -1
  7. {rcer_iot_client_pkg-0.6.0 → rcer_iot_client_pkg-0.6.1}/src/rcer_iot_client_pkg/general_types/error_types/api/update_thies_data_error_types.py +28 -1
  8. {rcer_iot_client_pkg-0.6.0 → rcer_iot_client_pkg-0.6.1}/src/rcer_iot_client_pkg/libs/sharepoint_client/clients/sharepoint_rest_api.py +17 -16
  9. {rcer_iot_client_pkg-0.6.0 → rcer_iot_client_pkg-0.6.1}/src/rcer_iot_client_pkg/libs/sharepoint_client/sharepoint_client.py +2 -2
  10. {rcer_iot_client_pkg-0.6.0 → rcer_iot_client_pkg-0.6.1}/src/rcer_iot_client_pkg/libs/sharepoint_client/types/sharepoint_client_types.py +1 -1
  11. {rcer_iot_client_pkg-0.6.0 → rcer_iot_client_pkg-0.6.1}/src/rcer_iot_client_pkg/services/epii/api.py +5 -3
  12. {rcer_iot_client_pkg-0.6.0 → rcer_iot_client_pkg-0.6.1}/src/rcer_iot_client_pkg/services/epii/constants/update_thies_data_constants.py +1 -1
  13. {rcer_iot_client_pkg-0.6.0 → rcer_iot_client_pkg-0.6.1}/src/rcer_iot_client_pkg/services/epii/controllers/types/update_thies_data_types.py +4 -2
  14. {rcer_iot_client_pkg-0.6.0 → rcer_iot_client_pkg-0.6.1}/src/rcer_iot_client_pkg/services/epii/controllers/update_thies_data.py +12 -4
  15. {rcer_iot_client_pkg-0.6.0 → rcer_iot_client_pkg-0.6.1}/src/rcer_iot_client_pkg/services/epii/use_cases/update_thies_data.py +49 -5
  16. rcer_iot_client_pkg-0.6.1/src/rcer_iot_client_pkg/services/epii/utils/update_thies_data_utils.py +21 -0
  17. rcer_iot_client_pkg-0.6.0/src/rcer_iot_client_pkg/general_types/api/__init__.py +0 -3
  18. rcer_iot_client_pkg-0.6.0/src/rcer_iot_client_pkg/libs/async_http_client/__init__.py +0 -10
  19. rcer_iot_client_pkg-0.6.0/src/rcer_iot_client_pkg/libs/async_http_client/async_http_client.py +0 -34
  20. rcer_iot_client_pkg-0.6.0/src/rcer_iot_client_pkg/libs/async_http_client/async_http_client_contract.py +0 -29
  21. rcer_iot_client_pkg-0.6.0/src/rcer_iot_client_pkg/libs/async_http_client/clients/aiohttp_client.py +0 -50
  22. rcer_iot_client_pkg-0.6.0/src/rcer_iot_client_pkg/libs/async_http_client/types/__init__.py +0 -3
  23. rcer_iot_client_pkg-0.6.0/src/rcer_iot_client_pkg/libs/async_http_client/types/async_http_client_types.py +0 -17
  24. rcer_iot_client_pkg-0.6.0/src/rcer_iot_client_pkg/services/epii/__init__.py +0 -0
  25. rcer_iot_client_pkg-0.6.0/src/rcer_iot_client_pkg/services/epii/utils/update_thies_data_utils.py +0 -18
  26. {rcer_iot_client_pkg-0.6.0 → rcer_iot_client_pkg-0.6.1}/LICENSE +0 -0
  27. {rcer_iot_client_pkg-0.6.0 → rcer_iot_client_pkg-0.6.1}/src/rcer_iot_client_pkg/general_types/__init__.py +0 -0
  28. {rcer_iot_client_pkg-0.6.0 → rcer_iot_client_pkg-0.6.1}/src/rcer_iot_client_pkg/general_types/error_types/__init__.py +0 -0
  29. {rcer_iot_client_pkg-0.6.0 → rcer_iot_client_pkg-0.6.1}/src/rcer_iot_client_pkg/general_types/error_types/api/__init__.py +0 -0
  30. {rcer_iot_client_pkg-0.6.0 → rcer_iot_client_pkg-0.6.1}/src/rcer_iot_client_pkg/general_types/error_types/common/__init__.py +0 -0
  31. {rcer_iot_client_pkg-0.6.0 → rcer_iot_client_pkg-0.6.1}/src/rcer_iot_client_pkg/general_types/error_types/common/common_types.py +0 -0
  32. {rcer_iot_client_pkg-0.6.0 → rcer_iot_client_pkg-0.6.1}/src/rcer_iot_client_pkg/libs/ftp_client/__init__.py +0 -0
  33. {rcer_iot_client_pkg-0.6.0/src/rcer_iot_client_pkg/libs/async_http_client → rcer_iot_client_pkg-0.6.1/src/rcer_iot_client_pkg/libs/ftp_client}/clients/__init__.py +0 -0
  34. {rcer_iot_client_pkg-0.6.0 → rcer_iot_client_pkg-0.6.1}/src/rcer_iot_client_pkg/libs/ftp_client/clients/aioftp_client.py +0 -0
  35. {rcer_iot_client_pkg-0.6.0 → rcer_iot_client_pkg-0.6.1}/src/rcer_iot_client_pkg/libs/ftp_client/ftp_client.py +0 -0
  36. {rcer_iot_client_pkg-0.6.0 → rcer_iot_client_pkg-0.6.1}/src/rcer_iot_client_pkg/libs/ftp_client/ftp_client_contract.py +0 -0
  37. {rcer_iot_client_pkg-0.6.0 → rcer_iot_client_pkg-0.6.1}/src/rcer_iot_client_pkg/libs/ftp_client/types/__init__.py +0 -0
  38. {rcer_iot_client_pkg-0.6.0 → rcer_iot_client_pkg-0.6.1}/src/rcer_iot_client_pkg/libs/ftp_client/types/ftp_client_types.py +0 -0
  39. {rcer_iot_client_pkg-0.6.0 → rcer_iot_client_pkg-0.6.1}/src/rcer_iot_client_pkg/libs/sharepoint_client/__init__.py +0 -0
  40. {rcer_iot_client_pkg-0.6.0 → rcer_iot_client_pkg-0.6.1}/src/rcer_iot_client_pkg/libs/sharepoint_client/sharepoint_client_contract.py +0 -0
  41. {rcer_iot_client_pkg-0.6.0 → rcer_iot_client_pkg-0.6.1}/src/rcer_iot_client_pkg/libs/zero_dependency/utils/datetime_utils.py +0 -0
  42. {rcer_iot_client_pkg-0.6.0/src/rcer_iot_client_pkg/libs/ftp_client/clients → rcer_iot_client_pkg-0.6.1/src/rcer_iot_client_pkg/services/epii}/__init__.py +0 -0
  43. {rcer_iot_client_pkg-0.6.0 → rcer_iot_client_pkg-0.6.1}/src/rcer_iot_client_pkg/services/epii/controllers/__init__.py +0 -0
  44. {rcer_iot_client_pkg-0.6.0 → rcer_iot_client_pkg-0.6.1}/src/rcer_iot_client_pkg/services/epii/controllers/types/__init__.py +0 -0
  45. {rcer_iot_client_pkg-0.6.0 → rcer_iot_client_pkg-0.6.1}/src/rcer_iot_client_pkg/services/epii/use_cases/types/__init__.py +0 -0
  46. {rcer_iot_client_pkg-0.6.0 → rcer_iot_client_pkg-0.6.1}/src/rcer_iot_client_pkg/services/epii/use_cases/types/update_thies_data_types.py +0 -0
  47. {rcer_iot_client_pkg-0.6.0 → rcer_iot_client_pkg-0.6.1}/src/rcer_iot_client_pkg/services/epii/utils/__init__.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: rcer_iot_client_pkg
3
- Version: 0.6.0
3
+ Version: 0.6.1
4
4
  Summary: A client library for IoT projects in the RCER initiative
5
5
  License: MIT
6
6
  Author: pedropablozavalat
@@ -19,8 +19,7 @@ Requires-Dist: pydantic (==2.11.3)
19
19
  Requires-Dist: pytest-cov (==6.1.1)
20
20
  Description-Content-Type: text/markdown
21
21
 
22
- # RCER IoT Client Library | `rcer_iot_client_pkg`
23
-
22
+ # RCER IoT Client Package
24
23
 
25
24
  ## Installation
26
25
  You can find the package on [PyPI](https://pypi.org/project/rcer-iot-client-pkg/).
@@ -45,11 +44,11 @@ api_client = EpiiAPI()
45
44
  The library provides a method to synchronize THIES Data Logger files with the RCER SharePoint client. This method updates the folder containing binary files with meteorological data:
46
45
 
47
46
  ```python
48
- from rcer_iot_client_pkg import EpiiAPIConfig
47
+ from rcer_iot_client_pkg import EpiiUpdateThiesConfig
49
48
  import asyncio
50
49
 
51
50
  async def update_thies_data():
52
- config = EpiiAPIConfig(
51
+ config = EpiiUpdateThiesConfig(
53
52
  ftp_port=FTP_PORT,
54
53
  ftp_host=FTP_HOST,
55
54
  ftp_user=FTP_USER,
@@ -68,7 +67,6 @@ asyncio.run(update_thies_data())
68
67
 
69
68
  **Notes:**
70
69
  - Store sensitive data like `FTP_PASSWORD`, `FTP_USER`, and SharePoint credentials securely. Use environment variables or a secrets management tool to avoid hardcoding sensitive information in your codebase.
71
- - Ensure `asyncio` is installed to run concurrent code with `EpiiAPI` methods.
72
70
 
73
71
  ## Development
74
72
 
@@ -1,5 +1,4 @@
1
- # RCER IoT Client Library | `rcer_iot_client_pkg`
2
-
1
+ # RCER IoT Client Package
3
2
 
4
3
  ## Installation
5
4
  You can find the package on [PyPI](https://pypi.org/project/rcer-iot-client-pkg/).
@@ -24,11 +23,11 @@ api_client = EpiiAPI()
24
23
  The library provides a method to synchronize THIES Data Logger files with the RCER SharePoint client. This method updates the folder containing binary files with meteorological data:
25
24
 
26
25
  ```python
27
- from rcer_iot_client_pkg import EpiiAPIConfig
26
+ from rcer_iot_client_pkg import EpiiUpdateThiesConfig
28
27
  import asyncio
29
28
 
30
29
  async def update_thies_data():
31
- config = EpiiAPIConfig(
30
+ config = EpiiUpdateThiesConfig(
32
31
  ftp_port=FTP_PORT,
33
32
  ftp_host=FTP_HOST,
34
33
  ftp_user=FTP_USER,
@@ -47,7 +46,6 @@ asyncio.run(update_thies_data())
47
46
 
48
47
  **Notes:**
49
48
  - Store sensitive data like `FTP_PASSWORD`, `FTP_USER`, and SharePoint credentials securely. Use environment variables or a secrets management tool to avoid hardcoding sensitive information in your codebase.
50
- - Ensure `asyncio` is installed to run concurrent code with `EpiiAPI` methods.
51
49
 
52
50
  ## Development
53
51
 
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "rcer_iot_client_pkg"
3
- version = "0.6.0"
3
+ version = "0.6.1"
4
4
  description = "A client library for IoT projects in the RCER initiative"
5
5
  authors = ["pedropablozavalat"]
6
6
  license = "MIT"
@@ -4,6 +4,6 @@ from importlib.metadata import version
4
4
  __version__ = version("rcer_iot_client_pkg")
5
5
 
6
6
  from .services.epii.api import EpiiAPI
7
- from .general_types.api.update_thies_data_types import EpiiAPIConfig
7
+ from .general_types.api.update_thies_data_types import EpiiUpdateThiesConfig
8
8
 
9
- __all__ = ["EpiiAPI", "EpiiAPIConfig"]
9
+ __all__ = ["EpiiAPI", "EpiiUpdateThiesConfig"]
@@ -0,0 +1,3 @@
1
+ from .update_thies_data_types import EpiiUpdateThiesConfig
2
+
3
+ __all__ = ["EpiiUpdateThiesConfig"]
@@ -2,7 +2,7 @@ from dataclasses import dataclass
2
2
 
3
3
 
4
4
  @dataclass
5
- class EpiiAPIConfig:
5
+ class EpiiUpdateThiesConfig:
6
6
  """
7
7
  Configuration for Epii API.
8
8
 
@@ -16,6 +16,7 @@ class EpiiAPIConfig:
16
16
  sharepoint_tenant_id (str): Tenant ID for SharePoint authentication.
17
17
  sharepoint_tenant_name (str): Tenant name for SharePoint.
18
18
  sharepoint_site_name (str): Site name in SharePoint.
19
+ logger (Logger): Logger object for logging during synchronisation of files from THIES Data Logger
19
20
  """
20
21
 
21
22
  ftp_port: int
@@ -1,3 +1,6 @@
1
+ import json
2
+
3
+
1
4
  class ThiesConnectionError(Exception):
2
5
  """Raised when unable to connect to the THIES FTP Server"""
3
6
 
@@ -26,5 +29,29 @@ class ThiesFetchingError(Exception):
26
29
  class SharePointFetchingError(Exception):
27
30
  """Raised when there is an error fetching file names from the RCER cloud."""
28
31
 
32
+ def __init__(self, *args, reason):
33
+ super().__init__(*args, reason)
34
+ self.reason = reason
35
+
36
+ def __str__(self):
37
+ try:
38
+ _, internal_metadata = self.reason.__str__().split(",", 1)
39
+ internal_metadata_dict = json.loads(internal_metadata)
40
+ return internal_metadata_dict["error_description"]
41
+
42
+ except json.decoder.JSONDecodeError:
43
+ return self.reason.__str__()
44
+
45
+
46
+ class SharePointUploadError(Exception):
47
+ """Raised when there is an error uploading files to the Microsoft SharePoint folder."""
48
+
49
+ def __init__(self, *args, reason):
50
+ super().__init__(*args, reason)
51
+ self.reason = reason
52
+
29
53
  def __str__(self):
30
- return "An error occurred while retrieving file names from the RCER cloud"
54
+ return (
55
+ "An error occurred while uploading files to the Microsoft SharePoint folder. "
56
+ + self.reason.__str__()
57
+ )
@@ -1,4 +1,3 @@
1
- import os
2
1
  from typing import Any
3
2
 
4
3
  from aiohttp import ClientError, ClientSession
@@ -65,17 +64,22 @@ class SharepointRestAPI(SharepointClientContract):
65
64
  }
66
65
 
67
66
  async def __aenter__(self) -> "SharepointRestAPI":
68
- self.credentials = await self._load_credentials()
69
- site_url = f"https://{self.tenant_name}.sharepoint.com"
67
+ try:
68
+ self.credentials = await self._load_credentials()
69
+ site_url = f"https://{self.tenant_name}.sharepoint.com"
70
70
 
71
- self.base_headers = {
72
- "Authorization": f"Bearer {self.credentials['access_token']}",
73
- "Accept": "application/json",
74
- "Content-Type": "application/json",
75
- }
76
- self.base_url = f"{site_url}/sites/{self.site_name}/_api/"
77
- self.session = ClientSession(headers=self.base_headers, base_url=self.base_url)
78
- return self
71
+ self.base_headers = {
72
+ "Authorization": f"Bearer {self.credentials['access_token']}",
73
+ "Accept": "application/json",
74
+ "Content-Type": "application/json",
75
+ }
76
+ self.base_url = f"{site_url}/sites/{self.site_name}/_api/"
77
+ self.session = ClientSession(
78
+ headers=self.base_headers, base_url=self.base_url
79
+ )
80
+ return self
81
+ except ClientError as error:
82
+ raise ConnectionError(error)
79
83
 
80
84
  async def __aexit__(
81
85
  self, _exc_type: type[BaseException], _exc_val: BaseException, _exc_tb: Any
@@ -120,12 +124,9 @@ class SharepointRestAPI(SharepointClientContract):
120
124
  folder_relative_url = (
121
125
  f"GetFolderByServerRelativeUrl('{args.folder_relative_url}')"
122
126
  )
123
- # Read the file
124
- source_file_path = os.path.basename(args.file_path)
125
- with open(source_file_path, "rb") as file:
126
- data = file.read()
127
+ data = args.file_content
127
128
 
128
- endpoint = f"web/{folder_relative_url}/Files/add(url='{source_file_path}',overwrite=false)"
129
+ endpoint = f"web/{folder_relative_url}/Files/add(url='{args.file_name}',overwrite=true)"
129
130
  response = await self.session.post(endpoint, data=data, headers=headers)
130
131
 
131
132
  response.raise_for_status()
@@ -28,7 +28,7 @@ class SharepointClient(SharepointClientContract):
28
28
  return await self.client_obj.list_files(args)
29
29
 
30
30
  async def list_folders(self, args: SpListFoldersArgs) -> list:
31
- return self.client_obj.list_files(args)
31
+ return await self.client_obj.list_files(args)
32
32
 
33
33
  async def upload_file(self, args: SpUploadFileArgs) -> dict:
34
- return self.client_obj.upload_file(args)
34
+ return await self.client_obj.upload_file(args)
@@ -20,6 +20,6 @@ class SpListFoldersArgs:
20
20
 
21
21
  @dataclass
22
22
  class SpUploadFileArgs:
23
- file_path: str
24
23
  folder_relative_url: str
24
+ file_name: str
25
25
  file_content: bytes = bytes()
@@ -2,7 +2,9 @@ from typing import Any, Dict
2
2
 
3
3
  from .controllers.types.update_thies_data_types import UpdateThiesDataControllerInput
4
4
  from .controllers.update_thies_data import UpdateThiesDataController
5
- from rcer_iot_client_pkg.general_types.api.update_thies_data_types import EpiiAPIConfig
5
+ from rcer_iot_client_pkg.general_types.api.update_thies_data_types import (
6
+ EpiiUpdateThiesConfig,
7
+ )
6
8
 
7
9
 
8
10
  class EpiiAPI:
@@ -10,12 +12,12 @@ class EpiiAPI:
10
12
  EpiiAPI is a service class that provides methods to interact with Patagonia Center system.
11
13
  """
12
14
 
13
- async def update_thies_data(self, config: EpiiAPIConfig) -> Dict[str, Any]:
15
+ async def update_thies_data(self, config: EpiiUpdateThiesConfig) -> Dict[str, Any]:
14
16
  """
15
17
  This method establishes a connection to an FTP server using the provided
16
18
  credentials and updates data related to THIES Data Logger.
17
19
  Args:
18
- config (EpiiAPIConfig): configuration class for FTP Server and Microsoft SharePoint credentials.
20
+ config (EpiiUpdateThiesConfig): configuration class for FTP Server and Microsoft SharePoint credentials.
19
21
  Returns:
20
22
  response (dict): A dictionary representation of the API response.
21
23
  """
@@ -1,4 +1,4 @@
1
- SHAREPOINT_BASE_URL = "/sites/uc365_CentrosyEstacionesRegionalesUC/Shared Documents/General/Test_Raspberry/THIES"
1
+ SHAREPOINT_BASE_URL = "/sites/uc365_CentrosyEstacionesRegionalesUC/Shared%20Documents/General/Test_Raspberry/THIES"
2
2
  SHAREPOINT_THIES_FOLDERS = ["AVG", "EXT"]
3
3
 
4
4
  FTP_SERVER_PATH_AVG_FILES = "ftp/thies/BINFILES/ARCH_AV1"
@@ -1,11 +1,13 @@
1
1
  from dataclasses import dataclass, field
2
2
  from typing import Dict
3
- from rcer_iot_client_pkg.general_types.api.update_thies_data_types import EpiiAPIConfig
3
+ from rcer_iot_client_pkg.general_types.api.update_thies_data_types import (
4
+ EpiiUpdateThiesConfig,
5
+ )
4
6
 
5
7
 
6
8
  @dataclass
7
9
  class UpdateThiesDataControllerInput:
8
- config: EpiiAPIConfig
10
+ config: EpiiUpdateThiesConfig
9
11
 
10
12
 
11
13
  @dataclass
@@ -4,6 +4,7 @@ from rcer_iot_client_pkg.general_types.error_types.api.update_thies_data_error_t
4
4
  SharePointFetchingError,
5
5
  ThiesConnectionError,
6
6
  ThiesFetchingError,
7
+ SharePointUploadError,
7
8
  )
8
9
  from rcer_iot_client_pkg.general_types.error_types.common.common_types import (
9
10
  EmptyDataError,
@@ -54,7 +55,7 @@ class UpdateThiesDataController:
54
55
  )
55
56
  except EmptyDataError:
56
57
  return UpdateThiesDataControllerOutput(
57
- message="No files to upload", status=HTTPStatus.NO_CONTENT
58
+ message="No files to upload", status=HTTPStatus.NO_CONTENT.value
58
59
  )
59
60
 
60
61
  except (AttributeError, NameError, ValueError) as error:
@@ -73,14 +74,21 @@ class UpdateThiesDataController:
73
74
  except SharepointClientError as error:
74
75
  return UpdateThiesDataControllerOutput(
75
76
  message="Sharepoint Client initialization fails.",
76
- status=HTTPStatus.BAD_REQUEST.value,
77
+ status=HTTPStatus.INTERNAL_SERVER_ERROR.value,
77
78
  metadata={"error": error.__str__()},
78
79
  )
79
80
 
80
81
  except SharePointFetchingError as error:
81
82
  return UpdateThiesDataControllerOutput(
82
- message="An error occurred while retrieving file names from the RCER cloud",
83
- status=HTTPStatus.INTERNAL_SERVER_ERROR.value,
83
+ message="An error occurred while retrieving file names from Microsoft SharePoint",
84
+ status=HTTPStatus.BAD_REQUEST.value,
85
+ metadata={"error": error.__str__()},
86
+ )
87
+
88
+ except SharePointUploadError as error:
89
+ return UpdateThiesDataControllerOutput(
90
+ message="An error oucrred while uploading files to RCER Cloud",
91
+ status=HTTPStatus.BAD_REQUEST.value,
84
92
  metadata={"error": error.__str__()},
85
93
  )
86
94
 
@@ -3,6 +3,7 @@ from dotenv import load_dotenv
3
3
  import rcer_iot_client_pkg.services.epii.constants.update_thies_data_constants as c
4
4
  from rcer_iot_client_pkg.general_types.error_types.api.update_thies_data_error_types import (
5
5
  SharePointFetchingError,
6
+ SharePointUploadError,
6
7
  ThiesConnectionError,
7
8
  ThiesFetchingError,
8
9
  )
@@ -21,11 +22,12 @@ from rcer_iot_client_pkg.libs.sharepoint_client import (
21
22
  SharepointClient,
22
23
  SharepointClientInitArgs,
23
24
  SpListFilesArgs,
25
+ SpUploadFileArgs,
24
26
  )
25
27
  from rcer_iot_client_pkg.services.epii.use_cases.types import (
26
- UpdateThiesDataUseCaseInput,
27
28
  FtpClientConfig,
28
29
  SharepointConfig,
30
+ UpdateThiesDataUseCaseInput,
29
31
  )
30
32
  from rcer_iot_client_pkg.services.epii.utils import (
31
33
  parse_execute_response,
@@ -109,13 +111,48 @@ class UpdateThiesDataUseCase:
109
111
  content = await self.thies_ftp_client.read_file(
110
112
  FtpReadFileArgs(file_path)
111
113
  )
112
- content_files[filename] = content
114
+ content_files[file] = content # Save the file with its prefix
113
115
  return content_files
114
116
  except ConnectionRefusedError as error:
115
117
  raise ThiesConnectionError(reason=error)
116
118
  except ConnectionAbortedError as error:
117
119
  raise ThiesFetchingError(reason=error)
118
120
 
121
+ async def upload_thies_files_to_sharepoint(
122
+ self, files: dict
123
+ ) -> dict[str, list[str]]:
124
+ """Upload files to SharePoint and categorize the results."""
125
+ upload_results = {"failed_files": [], "overwritten_files": [], "new_files": []}
126
+
127
+ async with self.sharepoint_client:
128
+ for file, file_content in files.items():
129
+ try:
130
+ folder, file_name = file.split("_", 1)
131
+ args = SpUploadFileArgs(
132
+ folder_relative_url=f"{c.SHAREPOINT_BASE_URL}/{folder}",
133
+ file_content=file_content,
134
+ file_name=file_name,
135
+ )
136
+ response = await self.sharepoint_client.upload_file(args)
137
+
138
+ if response.get("Exists", False):
139
+ upload_results["overwritten_files"].append(file)
140
+ else:
141
+ upload_results["new_files"].append(file)
142
+
143
+ except ConnectionError as error:
144
+ upload_results["failed_files"].append(
145
+ f"{file} (Error: {str(error)})"
146
+ )
147
+
148
+ if upload_results["failed_files"]:
149
+ raise SharePointUploadError(
150
+ reason="Files failed to upload: "
151
+ + ", ".join(upload_results["failed_files"])
152
+ )
153
+
154
+ return upload_results
155
+
119
156
  async def execute(self) -> dict:
120
157
  """Synchronize data from the THIES Center to the cloud."""
121
158
  try:
@@ -126,10 +163,17 @@ class UpdateThiesDataUseCase:
126
163
  cloud_files = await self.fetch_cloud_file_names()
127
164
  except RuntimeError as error:
128
165
  raise SharepointClient(error)
166
+
129
167
  self.uploading = thies_files - cloud_files
130
168
  if not self.uploading:
131
169
  raise EmptyDataError(reason="No files to upload.")
132
170
 
133
- thies_file_contents = await self.fetch_thies_file_content()
134
- data = parse_execute_response(thies_file_contents)
135
- return data
171
+ # Fetch the content of the files to be uploaded from THIES FTP Server
172
+ thies_fetched_files = await self.fetch_thies_file_content()
173
+
174
+ # Upload the fetched files to SharePoint and gather statistics
175
+ upload_statistics = await self.upload_thies_files_to_sharepoint(
176
+ thies_fetched_files
177
+ )
178
+
179
+ return parse_execute_response(thies_fetched_files, upload_statistics)
@@ -0,0 +1,21 @@
1
+ from typing import Any
2
+
3
+ from rcer_iot_client_pkg.libs.zero_dependency.utils.datetime_utils import (
4
+ datetime_to_str,
5
+ today,
6
+ )
7
+
8
+
9
+ def parse_execute_response(
10
+ thies_fetched_files: dict[str, Any], upload_statistics: dict[str, Any]
11
+ ) -> dict[str, dict[str, int | str]]:
12
+ return {
13
+ **upload_statistics,
14
+ "processed_files": {
15
+ filename: {
16
+ "file_size": len(data),
17
+ "processed_date": datetime_to_str(today()),
18
+ }
19
+ for filename, data in thies_fetched_files.items()
20
+ },
21
+ }
@@ -1,3 +0,0 @@
1
- from .update_thies_data_types import EpiiAPIConfig
2
-
3
- __all__ = ["EpiiAPIConfig"]
@@ -1,10 +0,0 @@
1
- """Export defined type classes."""
2
-
3
- from .async_http_client import AsyncHTTPClient
4
- from .types.async_http_client_types import (
5
- AsyncHttpClientInitArgs,
6
- GetArgs,
7
- UploadFileArgs,
8
- )
9
-
10
- __all__ = ["AsyncHTTPClient", "AsyncHttpClientInitArgs", "GetArgs", "UploadFileArgs"]
@@ -1,34 +0,0 @@
1
- from typing import Any
2
-
3
- from .async_http_client_contract import AsyncHTTPClientContract
4
- from .clients.aiohttp_client import AioHttpClient
5
- from .types.async_http_client_types import (
6
- AsyncHttpClientInitArgs,
7
- GetArgs,
8
- UploadFileArgs,
9
- )
10
-
11
-
12
- class AsyncHTTPClient(AsyncHTTPClientContract):
13
- CLIENTS = {"aiohttp_client"}
14
-
15
- def __init__(self, args: AsyncHttpClientInitArgs) -> None:
16
- if args.client_name not in AsyncHTTPClient.CLIENTS:
17
- msg = f"Unsupported client '{args.client_name}'"
18
- raise KeyError(msg)
19
- self.client_name = args.client_name
20
-
21
- if args.client_name == "aiohttp_client":
22
- self.client_obj = AioHttpClient(args)
23
-
24
- async def __aenter__(self):
25
- return await self.client_obj.__aenter__()
26
-
27
- async def __aexit__(self, exc_type, exc_val, exc_tb):
28
- await self.client_obj.__aexit__(exc_type, exc_val, exc_tb)
29
-
30
- async def get(self, args: GetArgs) -> dict[str, Any]:
31
- return await self.client_obj.get(args)
32
-
33
- async def upload_file(self, args: UploadFileArgs) -> dict[str, Any]:
34
- return await self.client_obj.upload_file(args)
@@ -1,29 +0,0 @@
1
- from abc import ABC, abstractmethod
2
- from typing import Any
3
-
4
- from .types.async_http_client_types import GetArgs, UploadFileArgs
5
-
6
-
7
- class AsyncHTTPClientContract(ABC):
8
- """
9
- A contract for asynchronous HTTP client implementations.
10
-
11
- This abstract base class defines the required methods for performing
12
- HTTP GET requests and uploading files asynchronously.
13
-
14
- Methods.
15
- -------
16
- get(args: GetArgs) -> dict[str, Any]
17
- Perform an HTTP GET request with the specified arguments.
18
-
19
- upload_file(args: UploadFileArgs) -> dict[str, Any]
20
- Upload a file using the specified arguments.
21
- """
22
-
23
- @abstractmethod
24
- async def get(self, args: GetArgs) -> dict[str, Any]:
25
- pass
26
-
27
- @abstractmethod
28
- async def upload_file(self, args: UploadFileArgs) -> dict[str, Any]:
29
- pass
@@ -1,50 +0,0 @@
1
- from typing import Any
2
-
3
- from aiohttp import ClientError, ClientSession
4
-
5
- from rcer_iot_client_pkg.libs.async_http_client.async_http_client_contract import (
6
- AsyncHTTPClientContract,
7
- )
8
- from rcer_iot_client_pkg.libs.async_http_client.types.async_http_client_types import (
9
- AsyncHttpClientInitArgs,
10
- GetArgs,
11
- UploadFileArgs,
12
- )
13
-
14
-
15
- class AioHttpClient(AsyncHTTPClientContract):
16
- def __init__(self, args: AsyncHttpClientInitArgs) -> None:
17
- self.access_token = args.access_token
18
- self.base_url = args.base_url
19
- self.headers = self._build_headers()
20
- self.session: ClientSession | None = None
21
-
22
- def _build_headers(self) -> dict:
23
- return {"Authorization": f"Bearer {self.access_token}"}
24
-
25
- async def __aenter__(self) -> "AioHttpClient":
26
- self.session = ClientSession(headers=self.headers, base_url=self.base_url)
27
- return self
28
-
29
- async def __aexit__(
30
- self, _exc_type: type[BaseException], _exc_val: BaseException, _exc_tb: Any
31
- ) -> None:
32
- await self.session.close()
33
-
34
- async def get(self, args: GetArgs) -> dict[str, Any]:
35
- try:
36
- endpoint, params = args.endpoint.lstrip("/"), args.params
37
- response = await self.session.get(endpoint, params=params)
38
- response.raise_for_status()
39
- return await response.json()
40
- except ClientError as error:
41
- raise ConnectionError(error) from error
42
-
43
- async def upload_file(self, args: UploadFileArgs) -> dict[str, Any]:
44
- try:
45
- endpoint, file_bytes = args.endpoint.lstrip("/"), args.file_bytes
46
- response = await self.session.put(endpoint, data=file_bytes)
47
- response.raise_for_status()
48
- return await response.json()
49
- except ClientError as error:
50
- raise ConnectionError(error) from error
@@ -1,3 +0,0 @@
1
- from .async_http_client_types import AsyncHttpClientInitArgs, GetArgs, UploadFileArgs
2
-
3
- __all__ = ["AsyncHttpClientInitArgs", "GetArgs", "UploadFileArgs"]
@@ -1,17 +0,0 @@
1
- from pydantic import BaseModel, Field
2
-
3
-
4
- class AsyncHttpClientInitArgs(BaseModel):
5
- access_token: str
6
- base_url: str
7
- client_name: str = "aiohttp_client"
8
-
9
-
10
- class GetArgs(BaseModel):
11
- endpoint: str
12
- params: dict | None = Field(default=None)
13
-
14
-
15
- class UploadFileArgs(BaseModel):
16
- endpoint: str
17
- file_bytes: bytes
@@ -1,18 +0,0 @@
1
- from typing import Any
2
-
3
- from rcer_iot_client_pkg.libs.zero_dependency.utils.datetime_utils import (
4
- datetime_to_str,
5
- today,
6
- )
7
-
8
-
9
- def parse_execute_response(
10
- file_contents: dict[str, Any],
11
- ) -> dict[str, dict[str, int | str]]:
12
- return {
13
- filename: {
14
- "size": len(data),
15
- "date": datetime_to_str(today()),
16
- }
17
- for filename, data in file_contents.items()
18
- }