fix: inclui corpo da resposta na mensagem de erro HTTP do LearnHouseClient
raise_for_status() sozinho so mostra status code, sem o corpo do erro 4xx/5xx da API - dificulta debug. Centralizado num helper _raise_for_status pra nao repetir try/except em cada metodo. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,6 +1,13 @@
|
||||
import requests
|
||||
|
||||
|
||||
def _raise_for_status(resp):
|
||||
try:
|
||||
resp.raise_for_status()
|
||||
except requests.HTTPError as error:
|
||||
raise requests.HTTPError(f"{error}: {resp.text}", response=resp) from error
|
||||
|
||||
|
||||
class LearnHouseClient:
|
||||
def __init__(self, base_url, token, org_id, org_slug, session=None):
|
||||
self.base_url = base_url.rstrip("/")
|
||||
@@ -17,7 +24,7 @@ class LearnHouseClient:
|
||||
self._url(f"/api/v1/courses/org_slug/{self.org_slug}/page/1/limit/100"),
|
||||
params={"include_unpublished": "true"},
|
||||
)
|
||||
resp.raise_for_status()
|
||||
_raise_for_status(resp)
|
||||
for course in resp.json():
|
||||
if course["name"] == name:
|
||||
return course
|
||||
@@ -34,14 +41,14 @@ class LearnHouseClient:
|
||||
"public": str(public).lower(),
|
||||
},
|
||||
)
|
||||
resp.raise_for_status()
|
||||
_raise_for_status(resp)
|
||||
return resp.json()
|
||||
|
||||
def update_course(self, course_uuid, **fields):
|
||||
resp = self.session.put(
|
||||
self._url(f"/api/v1/courses/{course_uuid}"), json=fields
|
||||
)
|
||||
resp.raise_for_status()
|
||||
_raise_for_status(resp)
|
||||
return resp.json()
|
||||
|
||||
def create_chapter(self, name, course_id):
|
||||
@@ -55,7 +62,7 @@ class LearnHouseClient:
|
||||
"lock_type": "public",
|
||||
},
|
||||
)
|
||||
resp.raise_for_status()
|
||||
_raise_for_status(resp)
|
||||
return resp.json()
|
||||
|
||||
def create_markdown_activity(self, name, chapter_id, markdown_url):
|
||||
@@ -71,7 +78,7 @@ class LearnHouseClient:
|
||||
"lock_type": "public",
|
||||
},
|
||||
)
|
||||
resp.raise_for_status()
|
||||
_raise_for_status(resp)
|
||||
return resp.json()
|
||||
|
||||
def publish_activity(self, activity_uuid):
|
||||
@@ -79,10 +86,10 @@ class LearnHouseClient:
|
||||
self._url(f"/api/v1/activities/{activity_uuid}"),
|
||||
json={"published": True},
|
||||
)
|
||||
resp.raise_for_status()
|
||||
_raise_for_status(resp)
|
||||
return resp.json()
|
||||
|
||||
def get_course_chapters(self, course_uuid):
|
||||
resp = self.session.get(self._url(f"/api/v1/courses/{course_uuid}/meta"))
|
||||
resp.raise_for_status()
|
||||
_raise_for_status(resp)
|
||||
return resp.json()["chapters"]
|
||||
|
||||
Reference in New Issue
Block a user