Skip to content

Commit

Permalink
feat(api): reformat code
Browse files Browse the repository at this point in the history
  • Loading branch information
Salem SAOUDI committed Dec 5, 2024
1 parent 90d5172 commit 73d510a
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 6 deletions.
2 changes: 2 additions & 0 deletions src/antares/exceptions/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,11 +231,13 @@ def __init__(self, study_id: str, message: str) -> None:
self.message = f"Could not delete the study {study_id}: " + message
super().__init__(self.message)


class StudyVariantCreationError(Exception):
def __init__(self, study_name: str, message: str) -> None:
self.message = f"Could not create a variant for {study_name}: " + message
super().__init__(self.message)


class ThermalMatrixDownloadError(Exception):
def __init__(self, area_name: str, cluster_name: str, matrix_name: str, message: str) -> None:
self.message = (
Expand Down
3 changes: 3 additions & 0 deletions src/antares/model/study.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ def _directory_not_exists(local_path: Path) -> None:
service_factory=ServiceFactory(config=local_config, study_name=study_params["caption"]),
)


def read_study_api(api_config: APIconf, study_id: str) -> "Study":
session = api_config.set_up_api_conf()
wrapper = RequestWrapper(session)
Expand All @@ -186,6 +187,7 @@ def read_study_api(api_config: APIconf, study_id: str) -> "Study":

return study


def create_variant_api(api_config: APIconf, study_id: str, variant_name: str) -> "Study":
"""
Creates a variant from a study_id
Expand All @@ -202,6 +204,7 @@ def create_variant_api(api_config: APIconf, study_id: str, variant_name: str) ->

return variant


class Study:
def __init__(
self,
Expand Down
4 changes: 2 additions & 2 deletions src/antares/service/api_services/study_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
BindingConstraintDeletionError,
StudyDeletionError,
StudySettingsUpdateError,
StudyVariantCreationError,
)
from antares.model.binding_constraint import BindingConstraint
from antares.model.settings.adequacy_patch import AdequacyPatchParameters
Expand All @@ -29,7 +30,6 @@
from antares.model.settings.thematic_trimming import ThematicTrimmingParameters
from antares.model.settings.time_series import TimeSeriesParameters
from antares.service.base_services import BaseStudyService
from antares.exceptions.exceptions import StudyVariantCreationError


def _returns_study_settings(
Expand Down Expand Up @@ -108,7 +108,7 @@ def delete(self, children: bool) -> None:

def create_variant(self, variant_name: str) -> str:
url = f"{self._base_url}/studies/{self.study_id}/variants?name={variant_name}"
try :
try:
response = self._wrapper.post(url)
return response.text
except APIError as e:
Expand Down
8 changes: 4 additions & 4 deletions tests/antares/services/api_services/test_study_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,16 @@
BindingConstraintCreationError,
LinkCreationError,
StudyCreationError,
StudySettingsUpdateError, StudyVariantCreationError,
StudySettingsUpdateError,
StudyVariantCreationError,
)
from antares.model.area import Area, AreaProperties, AreaUi
from antares.model.binding_constraint import BindingConstraint, BindingConstraintProperties
from antares.model.hydro import HydroProperties
from antares.model.link import Link, LinkProperties, LinkUi
from antares.model.settings.general import GeneralParameters
from antares.model.settings.study_settings import StudySettings
from antares.model.study import Study, create_study_api, read_study_api, create_variant_api
from antares.model.study import Study, create_study_api, create_variant_api, read_study_api
from antares.service.service_factory import ServiceFactory


Expand Down Expand Up @@ -274,7 +275,6 @@ def test_create_variant_success(self):
assert variant.name == variant_name
assert variant.service.study_id == variant_id


def test_create_variant_api_success(self):
variant_name = "variant_test"
with requests_mock.Mocker() as mocker:
Expand Down Expand Up @@ -317,4 +317,4 @@ def test_create_variant_api_fails(self):
error_message = "Variant creation failed"
mocker.post(url, json={"description": error_message}, status_code=404)
with pytest.raises(StudyVariantCreationError, match=error_message):
create_variant_api(self.api, self.study_id, variant_name)
create_variant_api(self.api, self.study_id, variant_name)

0 comments on commit 73d510a

Please sign in to comment.