From 382fdf9693231c77ce1233c10a4aed59f27356ae Mon Sep 17 00:00:00 2001 From: wahadameh Date: Wed, 8 Jan 2025 16:25:52 +0100 Subject: [PATCH] feat(api): added new links retrieval exception --- src/antares/craft/exceptions/exceptions.py | 6 ++ .../craft/service/api_services/link_api.py | 75 ++++++++++--------- 2 files changed, 45 insertions(+), 36 deletions(-) diff --git a/src/antares/craft/exceptions/exceptions.py b/src/antares/craft/exceptions/exceptions.py index 1d11b599..94a8e0d7 100644 --- a/src/antares/craft/exceptions/exceptions.py +++ b/src/antares/craft/exceptions/exceptions.py @@ -78,6 +78,12 @@ def __init__(self, link_name: str, message: str) -> None: super().__init__(self.message) +class LinksRetrievalError(Exception): + def __init__(self, study_id: str, message: str) -> None: + self.message = f"Could not retrieve links from study {study_id} : {message}" + super().__init__(self.message) + + class ThermalCreationError(Exception): def __init__(self, thermal_name: str, area_id: str, message: str) -> None: self.message = f"Could not create the thermal cluster {thermal_name} inside area {area_id}: " + message diff --git a/src/antares/craft/service/api_services/link_api.py b/src/antares/craft/service/api_services/link_api.py index 31d63ddc..e9636ab4 100644 --- a/src/antares/craft/service/api_services/link_api.py +++ b/src/antares/craft/service/api_services/link_api.py @@ -10,7 +10,7 @@ # # This file is part of the Antares project. -from typing import Optional +from typing import Any, Optional import pandas as pd @@ -22,6 +22,7 @@ LinkDeletionError, LinkDownloadError, LinkPropertiesUpdateError, + LinksRetrievalError, LinkUiUpdateError, LinkUploadError, ) @@ -214,47 +215,49 @@ def create_capacity_indirect(self, series: pd.DataFrame, area_from: str, area_to raise LinkUploadError(area_from, area_to, "indirectcapacity", e.message) from e def read_links(self) -> list[Link]: - url = f"{self._base_url}/studies/{self.study_id}/links" - json_links = self._wrapper.get(url).json() - links = self.read_study_links(json_links) - - links.sort(key=lambda link_obj: link_obj.area_from_id) + try: + url = f"{self._base_url}/studies/{self.study_id}/links" + json_links = self._wrapper.get(url).json() + links = self.read_study_links(json_links) + links.sort(key=lambda link_obj: link_obj.area_from_id) + except APIError as e: + raise LinksRetrievalError(self.study_id, e.message) from e return links + def read_link(self, link: dict[str, Any]) -> Link: + link_area_from_id = link.pop("area1") + link_area_to_id = link.pop("area2") + + link_style = link.pop("linkStyle") + link_width = link.pop("linkWidth") + color_r = link.pop("colorr") + color_g = link.pop("colorg") + color_b = link.pop("colorb") + + link_ui = LinkUi(link_style=link_style, link_width=link_width, colorr=color_r, colorg=color_g, colorb=color_b) + + mapping = { + "hurdlesCost": "hurdles-cost", + "loopFlow": "loop-flow", + "usePhaseShifter": "use-phase-shifter", + "transmissionCapacities": "transmission-capacities", + "displayComments": "display-comments", + "filterSynthesis": "filter-synthesis", + "filterYearByYear": "filter-year-by-year", + "assetType": "asset-type", + } + + link = {mapping.get(k, k): v for k, v in link.items()} + link["filter-synthesis"] = set(link["filter-synthesis"].split(", ")) + link["filter-year-by-year"] = set(link["filter-year-by-year"].split(", ")) + link_properties = LinkProperties(**link) + return Link(link_area_from_id, link_area_to_id, self, link_properties, link_ui) + def read_study_links(self, link_list: list) -> list[Link]: links = [] for link in link_list: - link_area_from_id = link.pop("area1") - link_area_to_id = link.pop("area2") - - link_style = link.pop("linkStyle") - link_width = link.pop("linkWidth") - color_r = link.pop("colorr") - color_g = link.pop("colorg") - color_b = link.pop("colorb") - - link_ui = LinkUi( - link_style=link_style, link_width=link_width, colorr=color_r, colorg=color_g, colorb=color_b - ) - - mapping = { - "hurdlesCost": "hurdles-cost", - "loopFlow": "loop-flow", - "usePhaseShifter": "use-phase-shifter", - "transmissionCapacities": "transmission-capacities", - "displayComments": "display-comments", - "filterSynthesis": "filter-synthesis", - "filterYearByYear": "filter-year-by-year", - "assetType": "asset-type", - } - - link = {mapping.get(k, k): v for k, v in link.items()} - link["filter-synthesis"] = set(link["filter-synthesis"].split(", ")) - link["filter-year-by-year"] = set(link["filter-year-by-year"].split(", ")) - link_properties = LinkProperties(**link) - link_object = Link(link_area_from_id, link_area_to_id, self, link_properties, link_ui) - + link_object = self.read_link(link) links.append(link_object) return links