Skip to content

Commit

Permalink
refactor: reduce redundancy in error handling (#66)
Browse files Browse the repository at this point in the history
  • Loading branch information
holtgrewe authored Nov 1, 2023
1 parent 2c9e402 commit 399fc09
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 194 deletions.
226 changes: 34 additions & 192 deletions varfish_cli/api/case.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,6 @@ def _paginated_request(endpoint, result_data=None, **kwargs):

result = requests.get(endpoint, **kwargs)
raise_for_status(result)

result_json = result.json()
if "results" in result_json and "next" in result_json:
result_data += result_json["results"]
Expand Down Expand Up @@ -196,17 +195,8 @@ def case_import_info_list(
params = None
logger.debug("Sending GET request to end point %s, params: %s", endpoint, params)
result = requests.get(endpoint, headers=headers, params=params, verify=verify_ssl)
if not result.ok:
try:
msg = "REST API returned status code %d: %s" % (
result.status_code,
" ".join(map(str, result.json().values())),
)
except (JSONDecodeError, SimpleJSONDecodeError):
msg = "REST API returned status code %d: %s" % (result.status_code, result.content)
raise RestApiCallException(msg)
else:
return pydantic.TypeAdapter(typing.List[CaseImportInfo]).validate_python(result.json())
raise_for_status(result)
return pydantic.TypeAdapter(typing.List[CaseImportInfo]).validate_python(result.json())


def case_import_info_retrieve(
Expand All @@ -226,17 +216,8 @@ def case_import_info_retrieve(
headers = {"Authorization": "Token %s" % api_token}
logger.debug("Sending GET request to end point %s", endpoint)
result = requests.get(endpoint, headers=headers, verify=verify_ssl)
if not result.ok:
try:
msg = "REST API returned status code %d: %s" % (
result.status_code,
" ".join([" ".join(v) for v in result.json().values()]),
)
except (JSONDecodeError, SimpleJSONDecodeError):
msg = "REST API returned status code %d: %s" % (result.status_code, result.content)
raise RestApiCallException(msg)
else:
return pydantic.TypeAdapter(CaseImportInfo).validate_python(result.json())
raise_for_status(result)
return pydantic.TypeAdapter(CaseImportInfo).validate_python(result.json())


def case_import_info_create(
Expand All @@ -258,17 +239,8 @@ def case_import_info_create(
result = requests.post(
endpoint, headers=headers, json=data.model_dump(mode="json"), verify=verify_ssl
)
if not result.ok:
try:
msg = "REST API returned status code %d: %s" % (
result.status_code,
" ".join([" ".join(v) for v in result.json().values()]),
)
except (JSONDecodeError, SimpleJSONDecodeError):
msg = "REST API returned status code %d: %s" % (result.status_code, result.content)
raise RestApiCallException(msg)
else:
return pydantic.TypeAdapter(CaseImportInfo).validate_python(result.json())
raise_for_status(result)
return pydantic.TypeAdapter(CaseImportInfo).validate_python(result.json())


def case_import_info_update(
Expand All @@ -293,17 +265,8 @@ def case_import_info_update(
result = requests.put(
endpoint, headers=headers, json=data.model_dump(mode="json"), verify=verify_ssl
)
if not result.ok:
try:
msg = "REST API returned status code %d: %s" % (
result.status_code,
" ".join([" ".join(v) for v in result.json().values()]),
)
except (JSONDecodeError, SimpleJSONDecodeError):
msg = "REST API returned status code %d: %s" % (result.status_code, result.content)
raise RestApiCallException(msg)
else:
return pydantic.TypeAdapter(CaseImportInfo).validate_python(result.json())
raise_for_status(result)
return pydantic.TypeAdapter(CaseImportInfo).validate_python(result.json())


def variant_set_import_info_list(
Expand All @@ -321,19 +284,8 @@ def variant_set_import_info_list(
logger.debug("Sending GET request to end point %s", endpoint)
headers = {"Authorization": "Token %s" % api_token}
result = requests.get(endpoint, headers=headers, verify=verify_ssl)
if not result.ok:
try:
msg = "REST API returned status code %d: %s" % (
result.status_code,
" ".join([" ".join(v) for v in result.json().values()]),
)
except (JSONDecodeError, SimpleJSONDecodeError):
msg = "REST API returned status code %d: %s" % (result.status_code, result.content)
raise RestApiCallException(msg)
else:
return pydantic.TypeAdapter(typing.List[VariantSetImportInfo]).validate_python(
result.json()
)
raise_for_status(result)
return pydantic.TypeAdapter(typing.List[VariantSetImportInfo]).validate_python(result.json())


def variant_set_import_info_create(
Expand All @@ -354,17 +306,8 @@ def variant_set_import_info_create(
result = requests.post(
endpoint, headers=headers, json=data.model_dump(mode="json"), verify=verify_ssl
)
if not result.ok:
try:
msg = "REST API returned status code %d: %s" % (
result.status_code,
" ".join([" ".join(v) for v in result.json().values()]),
)
except (JSONDecodeError, SimpleJSONDecodeError):
msg = "REST API returned status code %d: %s" % (result.status_code, result.content)
raise RestApiCallException(msg)
else:
return pydantic.TypeAdapter(VariantSetImportInfo).validate_python(result.json())
raise_for_status(result)
return pydantic.TypeAdapter(VariantSetImportInfo).validate_python(result.json())


def variant_set_import_info_update(
Expand All @@ -390,17 +333,8 @@ def variant_set_import_info_update(
result = requests.put(
endpoint, headers=headers, json=data.model_dump(mode="json"), verify=verify_ssl
)
if not result.ok:
try:
msg = "REST API returned status code %d: %s" % (
result.status_code,
" ".join([" ".join(v) for v in result.json().values()]),
)
except (JSONDecodeError, SimpleJSONDecodeError):
msg = "REST API returned status code %d: %s" % (result.status_code, result.content)
raise RestApiCallException(msg)
else:
return pydantic.TypeAdapter(VariantSetImportInfo).validate_python(result.json())
raise_for_status(result)
return pydantic.TypeAdapter(VariantSetImportInfo).validate_python(result.json())


def bam_qc_file_list(
Expand All @@ -417,17 +351,8 @@ def bam_qc_file_list(
logger.debug("Sending GET request to end point %s", endpoint)
headers = {"Authorization": "Token %s" % api_token}
result = requests.get(endpoint, headers=headers, verify=verify_ssl)
if not result.ok:
try:
msg = "REST API returned status code %d: %s" % (
result.status_code,
" ".join([" ".join(v) for v in result.json().values()]),
)
except (JSONDecodeError, SimpleJSONDecodeError):
msg = "REST API returned status code %d: %s" % (result.status_code, result.content)
raise RestApiCallException(msg)
else:
return pydantic.TypeAdapter(typing.List[BamQcFile]).validate_python(result.json())
raise_for_status(result)
return pydantic.TypeAdapter(typing.List[BamQcFile]).validate_python(result.json())


def bam_qc_file_upload(
Expand All @@ -448,17 +373,8 @@ def bam_qc_file_upload(
result = requests.post(
endpoint, headers=headers, data=data.model_dump(mode="json"), files=files, verify=verify_ssl
)
if not result.ok:
try:
msg = "REST API returned status code %d: %s" % (
result.status_code,
" ".join([" ".join(v) for v in result.json().values()]),
)
except (JSONDecodeError, SimpleJSONDecodeError):
msg = "REST API returned status code %d: %s" % (result.status_code, result.content)
raise RestApiCallException(msg)
else:
return pydantic.TypeAdapter(BamQcFile).validate_python(result.json())
raise_for_status(result)
return pydantic.TypeAdapter(BamQcFile).validate_python(result.json())


def bam_qc_file_destroy(
Expand Down Expand Up @@ -503,19 +419,8 @@ def case_gene_annotation_file_list(
logger.debug("Sending GET request to end point %s", endpoint)
headers = {"Authorization": "Token %s" % api_token}
result = requests.get(endpoint, headers=headers, verify=verify_ssl)
if not result.ok:
try:
msg = "REST API returned status code %d: %s" % (
result.status_code,
" ".join([" ".join(v) for v in result.json().values()]),
)
except (JSONDecodeError, SimpleJSONDecodeError):
msg = "REST API returned status code %d: %s" % (result.status_code, result.content)
raise RestApiCallException(msg)
else:
return pydantic.TypeAdapter(typing.List[CaseGeneAnnotationFile]).validate_python(
result.json()
)
raise_for_status(result)
return pydantic.TypeAdapter(typing.List[CaseGeneAnnotationFile]).validate_python(result.json())


def case_gene_annotation_file_upload(
Expand All @@ -538,17 +443,8 @@ def case_gene_annotation_file_upload(
result = requests.post(
endpoint, headers=headers, data=data.model_dump(mode="json"), files=files, verify=verify_ssl
)
if not result.ok:
try:
msg = "REST API returned status code %d: %s" % (
result.status_code,
" ".join([" ".join(v) for v in result.json().values()]),
)
except (JSONDecodeError, SimpleJSONDecodeError):
msg = "REST API returned status code %d: %s" % (result.status_code, result.content)
raise RestApiCallException(msg)
else:
return pydantic.TypeAdapter(CaseGeneAnnotationFile).validate_python(result.json())
raise_for_status(result)
return pydantic.TypeAdapter(CaseGeneAnnotationFile).validate_python(result.json())


def case_gene_annotation_file_destroy(
Expand Down Expand Up @@ -596,17 +492,8 @@ def genotype_file_list(
logger.debug("Sending GET request to end point %s", endpoint)
headers = {"Authorization": "Token %s" % api_token}
result = requests.get(endpoint, headers=headers, verify=verify_ssl)
if not result.ok:
try:
msg = "REST API returned status code %d: %s" % (
result.status_code,
" ".join([" ".join(v) for v in result.json().values()]),
)
except (JSONDecodeError, SimpleJSONDecodeError):
msg = "REST API returned status code %d: %s" % (result.status_code, result.content)
raise RestApiCallException(msg)
else:
return pydantic.TypeAdapter(typing.List[GenotypeFile]).validate_python(result.json())
raise_for_status(result)
return pydantic.TypeAdapter(typing.List[GenotypeFile]).validate_python(result.json())


def genotype_file_upload(
Expand All @@ -629,17 +516,8 @@ def genotype_file_upload(
result = requests.post(
endpoint, headers=headers, data=data.model_dump(mode="json"), files=files, verify=verify_ssl
)
if not result.ok:
try:
msg = "REST API returned status code %d: %s" % (
result.status_code,
" ".join([" ".join(v) for v in result.json().values()]),
)
except (JSONDecodeError, SimpleJSONDecodeError):
msg = "REST API returned status code %d: %s" % (result.status_code, result.content)
raise RestApiCallException(msg)
else:
return pydantic.TypeAdapter(GenotypeFile).validate_python(result.json())
raise_for_status(result)
return pydantic.TypeAdapter(GenotypeFile).validate_python(result.json())


def genotype_file_destroy(
Expand Down Expand Up @@ -687,17 +565,8 @@ def effects_file_list(
logger.debug("Sending GET request to end point %s", endpoint)
headers = {"Authorization": "Token %s" % api_token}
result = requests.get(endpoint, headers=headers, verify=verify_ssl)
if not result.ok:
try:
msg = "REST API returned status code %d: %s" % (
result.status_code,
" ".join([" ".join(v) for v in result.json().values()]),
)
except (JSONDecodeError, SimpleJSONDecodeError):
msg = "REST API returned status code %d: %s" % (result.status_code, result.content)
raise RestApiCallException(msg)
else:
return pydantic.TypeAdapter(typing.List[EffectsFile]).validate_python(result.json())
raise_for_status(result)
return pydantic.TypeAdapter(typing.List[EffectsFile]).validate_python(result.json())


def effects_file_upload(
Expand All @@ -720,17 +589,8 @@ def effects_file_upload(
result = requests.post(
endpoint, headers=headers, data=data.model_dump(mode="json"), files=files, verify=verify_ssl
)
if not result.ok:
try:
msg = "REST API returned status code %d: %s" % (
result.status_code,
" ".join([" ".join(v) for v in result.json().values()]),
)
except (JSONDecodeError, SimpleJSONDecodeError):
msg = "REST API returned status code %d: %s" % (result.status_code, result.content)
raise RestApiCallException(msg)
else:
return pydantic.TypeAdapter(EffectsFile).validate_python(result.json())
raise_for_status(result)
return pydantic.TypeAdapter(EffectsFile).validate_python(result.json())


def effects_file_destroy(
Expand Down Expand Up @@ -778,17 +638,8 @@ def db_info_file_list(
logger.debug("Sending GET request to end point %s", endpoint)
headers = {"Authorization": "Token %s" % api_token}
result = requests.get(endpoint, headers=headers, verify=verify_ssl)
if not result.ok:
try:
msg = "REST API returned status code %d: %s" % (
result.status_code,
" ".join([" ".join(v) for v in result.json().values()]),
)
except (JSONDecodeError, SimpleJSONDecodeError):
msg = "REST API returned status code %d: %s" % (result.status_code, result.content)
raise RestApiCallException(msg)
else:
return pydantic.TypeAdapter(typing.List[DatabaseInfoFile]).validate_python(result.json())
raise_for_status(result)
return pydantic.TypeAdapter(typing.List[DatabaseInfoFile]).validate_python(result.json())


def db_info_file_upload(
Expand All @@ -811,17 +662,8 @@ def db_info_file_upload(
result = requests.post(
endpoint, headers=headers, data=data.model_dump(mode="json"), files=files, verify=verify_ssl
)
if not result.ok:
try:
msg = "REST API returned status code %d: %s" % (
result.status_code,
" ".join([" ".join(v) for v in result.json().values()]),
)
except (JSONDecodeError, SimpleJSONDecodeError):
msg = "REST API returned status code %d: %s" % (result.status_code, result.content)
raise RestApiCallException(msg)
else:
return pydantic.TypeAdapter(DatabaseInfoFile).validate_python(result.json())
raise_for_status(result)
return pydantic.TypeAdapter(DatabaseInfoFile).validate_python(result.json())


def db_info_file_destroy(
Expand Down
15 changes: 13 additions & 2 deletions varfish_cli/api/common.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
from json import JSONDecodeError

from simplejson import JSONDecodeError as SimpleJSONDecodeError

from varfish_cli.exceptions import RestApiCallException


def raise_for_status(response):
if not response.ok: # pragma: no cover
raise RestApiCallException(f"Problem with API call: {response.text}.")
if not response.ok:
try:
msg = "REST API returned status code %d: %s" % (
response.status_code,
" ".join([" ".join(v) for v in response.json().values()]),
)
except (JSONDecodeError, SimpleJSONDecodeError):
msg = "REST API returned status code %d: %s" % (response.status_code, response.content)
raise RestApiCallException(msg)

0 comments on commit 399fc09

Please sign in to comment.