Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 1 addition & 15 deletions codecarbon/core/api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -100,16 +99,13 @@ 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):
"""
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
Expand All @@ -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):
Expand All @@ -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):
Expand All @@ -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):
Expand All @@ -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):
Expand All @@ -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):
Expand All @@ -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):
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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"
Expand All @@ -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):
Expand All @@ -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):
Expand All @@ -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):
Expand All @@ -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):
"""
Expand Down
33 changes: 33 additions & 0 deletions tests/test_api_call.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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",
)
)
Loading