|
12 | 12 | LOGGER = logging.getLogger(__name__)
|
13 | 13 |
|
14 | 14 |
|
15 |
| -async def _get_software_versions(vid: int, pid: int) -> Any: |
| 15 | +async def _get_software_versions(session: ClientSession, vid: int, pid: int) -> Any: |
16 | 16 | """Check DCL if there are updates available for a particular node."""
|
17 |
| - async with ClientSession(raise_for_status=False) as http_session: |
18 |
| - # fetch the paa certificates list |
19 |
| - async with http_session.get( |
20 |
| - f"{DCL_PRODUCTION_URL}/dcl/model/versions/{vid}/{pid}" |
21 |
| - ) as response: |
22 |
| - if response.status == HTTPStatus.NOT_FOUND: |
23 |
| - return None |
24 |
| - response.raise_for_status() |
25 |
| - return await response.json() |
| 17 | + # fetch the paa certificates list |
| 18 | + async with session.get(f"/dcl/model/versions/{vid}/{pid}") as response: |
| 19 | + if response.status == HTTPStatus.NOT_FOUND: |
| 20 | + return None |
| 21 | + response.raise_for_status() |
| 22 | + return await response.json() |
26 | 23 |
|
27 | 24 |
|
28 |
| -async def _get_software_version(vid: int, pid: int, software_version: int) -> Any: |
| 25 | +async def _get_software_version( |
| 26 | + session: ClientSession, vid: int, pid: int, software_version: int |
| 27 | +) -> Any: |
29 | 28 | """Check DCL if there are updates available for a particular node."""
|
30 |
| - async with ClientSession(raise_for_status=True) as http_session: |
31 |
| - # fetch the paa certificates list |
32 |
| - async with http_session.get( |
33 |
| - f"{DCL_PRODUCTION_URL}/dcl/model/versions/{vid}/{pid}/{software_version}" |
34 |
| - ) as response: |
35 |
| - return await response.json() |
| 29 | + # fetch the paa certificates list |
| 30 | + async with session.get( |
| 31 | + f"/dcl/model/versions/{vid}/{pid}/{software_version}" |
| 32 | + ) as response: |
| 33 | + response.raise_for_status() |
| 34 | + return await response.json() |
36 | 35 |
|
37 | 36 |
|
38 | 37 | async def _check_update_version(
|
| 38 | + session: ClientSession, |
39 | 39 | vid: int,
|
40 | 40 | pid: int,
|
41 | 41 | current_software_version: int,
|
42 | 42 | requested_software_version: int,
|
43 | 43 | requested_software_version_string: str | None = None,
|
44 | 44 | ) -> None | dict:
|
45 | 45 | version_res: dict = await _get_software_version(
|
46 |
| - vid, pid, requested_software_version |
| 46 | + session, vid, pid, requested_software_version |
47 | 47 | )
|
48 | 48 | if not isinstance(version_res, dict):
|
49 | 49 | raise TypeError("Unexpected DCL response.")
|
@@ -81,39 +81,54 @@ async def check_for_update(
|
81 | 81 | ) -> None | dict:
|
82 | 82 | """Check if there is a software update available on the DCL."""
|
83 | 83 | try:
|
84 |
| - # If a specific version as integer is requested, just fetch it (and hope it exists) |
85 |
| - if isinstance(requested_software_version, int): |
86 |
| - return await _check_update_version( |
87 |
| - vid, pid, current_software_version, requested_software_version |
88 |
| - ) |
89 |
| - |
90 |
| - # Get all versions and check each one of them. |
91 |
| - versions = await _get_software_versions(vid, pid) |
92 |
| - if versions is None: |
93 |
| - LOGGER.info("There is no update information for this device on the DCL.") |
94 |
| - return None |
| 84 | + async with ClientSession( |
| 85 | + base_url=DCL_PRODUCTION_URL, raise_for_status=False |
| 86 | + ) as session: |
| 87 | + # If a specific version as integer is requested, just fetch it (and hope it exists) |
| 88 | + if isinstance(requested_software_version, int): |
| 89 | + return await _check_update_version( |
| 90 | + session, |
| 91 | + vid, |
| 92 | + pid, |
| 93 | + current_software_version, |
| 94 | + requested_software_version, |
| 95 | + ) |
| 96 | + |
| 97 | + # Get all versions and check each one of them. |
| 98 | + versions = await _get_software_versions(session, vid, pid) |
| 99 | + if versions is None: |
| 100 | + LOGGER.info( |
| 101 | + "There is no update information for this device on the DCL." |
| 102 | + ) |
| 103 | + return None |
95 | 104 |
|
96 |
| - all_software_versions: list[int] = versions["modelVersions"]["softwareVersions"] |
97 |
| - newer_software_versions = [ |
98 |
| - version |
99 |
| - for version in all_software_versions |
100 |
| - if version > current_software_version |
101 |
| - ] |
| 105 | + all_software_versions: list[int] = versions["modelVersions"][ |
| 106 | + "softwareVersions" |
| 107 | + ] |
| 108 | + newer_software_versions = [ |
| 109 | + version |
| 110 | + for version in all_software_versions |
| 111 | + if version > current_software_version |
| 112 | + ] |
| 113 | + |
| 114 | + # Check if there is a newer software version available, no downgrade possible |
| 115 | + if not newer_software_versions: |
| 116 | + return None |
102 | 117 |
|
103 |
| - # Check if there is a newer software version available, no downgrade possible |
104 |
| - if not newer_software_versions: |
105 |
| - LOGGER.info("No newer software version available.") |
| 118 | + # Check if latest firmware is applicable, and backtrack from there |
| 119 | + for version in sorted(newer_software_versions, reverse=True): |
| 120 | + if version_candidate := await _check_update_version( |
| 121 | + session, |
| 122 | + vid, |
| 123 | + pid, |
| 124 | + current_software_version, |
| 125 | + version, |
| 126 | + requested_software_version, |
| 127 | + ): |
| 128 | + return version_candidate |
| 129 | + LOGGER.debug("Software version %d not applicable.", version) |
106 | 130 | return None
|
107 | 131 |
|
108 |
| - # Check if latest firmware is applicable, and backtrack from there |
109 |
| - for version in sorted(newer_software_versions, reverse=True): |
110 |
| - if version_candidate := await _check_update_version( |
111 |
| - vid, pid, current_software_version, version, requested_software_version |
112 |
| - ): |
113 |
| - return version_candidate |
114 |
| - LOGGER.debug("Software version %d not applicable.", version) |
115 |
| - return None |
116 |
| - |
117 | 132 | except (ClientError, TimeoutError) as err:
|
118 | 133 | raise UpdateCheckError(
|
119 | 134 | f"Fetching software versions from DCL for device with vendor id {vid} product id {pid} failed."
|
|
0 commit comments