diff --git a/codecarbon/core/api_client.py b/codecarbon/core/api_client.py index 34067c71c..65d6365fe 100644 --- a/codecarbon/core/api_client.py +++ b/codecarbon/core/api_client.py @@ -88,7 +88,6 @@ def check_auth(self): r = requests.get(url=url, timeout=2, headers=headers) if r.status_code != 200: self._log_error(url, {}, r) - return None return r.json() def get_list_organizations(self): @@ -100,7 +99,6 @@ def get_list_organizations(self): r = requests.get(url=url, timeout=2, headers=headers) if r.status_code != 200: self._log_error(url, {}, r) - return None return r.json() def check_organization_exists(self, organization_name: str): @@ -108,8 +106,6 @@ def check_organization_exists(self, organization_name: str): Check if an organization exists """ organizations = self.get_list_organizations() - if organizations is None: - return False for organization in organizations: if organization["name"] == organization_name: return organization @@ -131,7 +127,6 @@ def create_organization(self, organization: OrganizationCreate): r = requests.post(url=url, json=payload, timeout=2, headers=headers) if r.status_code != 201: self._log_error(url, payload, r) - return None return r.json() def get_organization(self, organization_id): @@ -143,7 +138,6 @@ def get_organization(self, organization_id): r = requests.get(url=url, timeout=2, headers=headers) if r.status_code != 200: self._log_error(url, {}, r) - return None return r.json() def update_organization(self, organization: OrganizationCreate): @@ -156,7 +150,6 @@ def update_organization(self, organization: OrganizationCreate): r = requests.patch(url=url, json=payload, timeout=2, headers=headers) if r.status_code != 200: self._log_error(url, payload, r) - return None return r.json() def list_projects_from_organization(self, organization_id): @@ -168,7 +161,6 @@ def list_projects_from_organization(self, organization_id): r = requests.get(url=url, timeout=2, headers=headers) if r.status_code != 200: self._log_error(url, {}, r) - return None return r.json() def create_project(self, project: ProjectCreate): @@ -181,7 +173,6 @@ def create_project(self, project: ProjectCreate): r = requests.post(url=url, json=payload, timeout=2, headers=headers) if r.status_code != 201: self._log_error(url, payload, r) - return None return r.json() def get_project(self, project_id): @@ -193,7 +184,6 @@ def get_project(self, project_id): r = requests.get(url=url, timeout=2, headers=headers) if r.status_code != 200: self._log_error(url, {}, r) - return None return r.json() def add_emission(self, carbon_emission: dict): @@ -235,7 +225,6 @@ def add_emission(self, carbon_emission: dict): r = requests.post(url=url, json=payload, timeout=2, headers=headers) if r.status_code != 201: self._log_error(url, payload, r) - return False logger.debug(f"ApiClient - Successful upload emission {payload} to {url}") except Exception as e: logger.error(e, exc_info=True) @@ -277,7 +266,6 @@ def _create_run(self, experiment_id: str): r = requests.post(url=url, json=payload, timeout=2, headers=headers) if r.status_code != 201: self._log_error(url, payload, r) - return None self.run_id = r.json()["id"] logger.info( "ApiClient Successfully registered your run on the API.\n\n" @@ -302,7 +290,6 @@ def list_experiments_from_project(self, project_id: str): r = requests.get(url=url, timeout=2, headers=headers) if r.status_code != 200: self._log_error(url, {}, r) - return [] return r.json() def set_experiment(self, experiment_id: str): @@ -322,7 +309,6 @@ def add_experiment(self, experiment: ExperimentCreate): r = requests.post(url=url, json=payload, timeout=2, headers=headers) if r.status_code != 201: self._log_error(url, payload, r) - return None return r.json() def get_experiment(self, experiment_id): @@ -334,7 +320,6 @@ def get_experiment(self, experiment_id): r = requests.get(url=url, timeout=2, headers=headers) if r.status_code != 200: self._log_error(url, {}, r) - return None return r.json() def _log_error(self, url, payload, response): @@ -347,6 +332,7 @@ def _log_error(self, url, payload, response): logger.error( f"ApiClient API return http code {response.status_code} and answer : {response.text}" ) + response.raise_for_status() def close_experiment(self): """ diff --git a/tests/test_api_call.py b/tests/test_api_call.py index ae39f6fd6..a2da47ae1 100644 --- a/tests/test_api_call.py +++ b/tests/test_api_call.py @@ -2,9 +2,11 @@ import unittest from uuid import uuid4 +import requests import requests_mock from codecarbon.core.api_client import ApiClient +from codecarbon.core.schemas import ProjectCreate from codecarbon.output import EmissionsData conf = { @@ -106,3 +108,34 @@ def test_call_api(self): tracking_mode="Machine", ) assert api.add_emission(dataclasses.asdict(carbon_emission)) + + @requests_mock.Mocker() + def test_call_api_error(self, m): + mock_url = "http://test.com" + m.get( + mock_url + "/organizations", + json={"detail": "Internal Server Error"}, + status_code=500, + ) + m.post( + mock_url + "/projects", + json={"detail": "Bad Request"}, + status_code=400, + ) + api = ApiClient( + experiment_id=None, + endpoint_url=mock_url, + api_key="api-key", + conf={}, + create_run_automatically=False, + ) + with self.assertRaises(requests.exceptions.HTTPError): + api.get_list_organizations() + with self.assertRaises(requests.exceptions.HTTPError): + api.create_project( + ProjectCreate( + name="test_project", + description="test_description", + organization_id="test_org_id", + ) + )