Skip to content

Commit

Permalink
log response content in case of HTTPError
Browse files Browse the repository at this point in the history
  • Loading branch information
jo-pol committed Dec 21, 2023
1 parent ed4e982 commit ddc8a63
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 48 deletions.
7 changes: 7 additions & 0 deletions src/datastation/common/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json as jsonlib
import logging
import os
import shutil
import argparse
Expand All @@ -19,6 +20,12 @@ def add_batch_processor_args(parser, report: bool = True):
dest='report_file')


def raise_for_status(r):
if r.status_code >= 400:
logging.error(f"{r.status_code} {r.reason} {r.json()}")
r.raise_for_status()

Check warning on line 26 in src/datastation/common/utils.py

View check run for this annotation

Codecov / codecov/patch

src/datastation/common/utils.py#L24-L26

Added lines #L24 - L26 were not covered by tests


def positive_int_argument_converter(value):
try:
ivalue = int(value)
Expand Down
6 changes: 3 additions & 3 deletions src/datastation/dataverse/banner_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def list(self, dry_run: bool = False):
print(f"Would have sent the following request: {url}")
return
r = requests.get(url, headers=headers, params={'unblock-key': self.unblock_key})
r.raise_for_status()
raise_for_status(r)()

Check warning on line 21 in src/datastation/dataverse/banner_api.py

View check run for this annotation

Codecov / codecov/patch

src/datastation/dataverse/banner_api.py#L21

Added line #L21 was not covered by tests
return r

def add(self, msg: str, dismissible_by_user: bool = False, lang: str = 'en', dry_run: bool = False):
Expand All @@ -39,7 +39,7 @@ def add(self, msg: str, dismissible_by_user: bool = False, lang: str = 'en', dry
print(json.dumps(banner, indent=4))
return
r = requests.post(url, headers=headers, params={'unblock-key': self.unblock_key}, json=banner)
r.raise_for_status()
raise_for_status(r)()

Check warning on line 42 in src/datastation/dataverse/banner_api.py

View check run for this annotation

Codecov / codecov/patch

src/datastation/dataverse/banner_api.py#L42

Added line #L42 was not covered by tests
return r

def remove(self, banner_id: int, dry_run: bool = False):
Expand All @@ -50,5 +50,5 @@ def remove(self, banner_id: int, dry_run: bool = False):
print(f"Would have sent the following request: {url}")
return
r = requests.delete(url, headers=headers, params={'unblock-key': self.unblock_key})
r.raise_for_status()
raise_for_status(r)()

Check warning on line 53 in src/datastation/dataverse/banner_api.py

View check run for this annotation

Codecov / codecov/patch

src/datastation/dataverse/banner_api.py#L53

Added line #L53 was not covered by tests
return r
37 changes: 19 additions & 18 deletions src/datastation/dataverse/dataset_api.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import json
import logging
import time

import requests

from datastation.common.utils import print_dry_run_message
from datastation.common.utils import print_dry_run_message, raise_for_status


class DatasetApi:
Expand All @@ -28,7 +29,7 @@ def get(self, version=":latest", dry_run=False):
return None

dv_resp = requests.get(url, headers=headers, params=params)
dv_resp.raise_for_status()
raise_for_status(dv_resp)

Check warning on line 32 in src/datastation/dataverse/dataset_api.py

View check run for this annotation

Codecov / codecov/patch

src/datastation/dataverse/dataset_api.py#L32

Added line #L32 was not covered by tests

resp_data = dv_resp.json()['data']
return resp_data
Expand All @@ -44,7 +45,7 @@ def edit_metadata(self, data: dict, dry_run=False, replace: bool = False):
return None
else:
r = requests.put(url, headers=headers, params=params, data=data)
r.raise_for_status()
raise_for_status(r)
return r

Check warning on line 49 in src/datastation/dataverse/dataset_api.py

View check run for this annotation

Codecov / codecov/patch

src/datastation/dataverse/dataset_api.py#L47-L49

Added lines #L47 - L49 were not covered by tests

def get_role_assignments(self, dry_run=False):
Expand All @@ -56,7 +57,7 @@ def get_role_assignments(self, dry_run=False):
return None
else:
r = requests.get(url, headers=headers, params=params)
r.raise_for_status()
raise_for_status(r)

Check warning on line 60 in src/datastation/dataverse/dataset_api.py

View check run for this annotation

Codecov / codecov/patch

src/datastation/dataverse/dataset_api.py#L60

Added line #L60 was not covered by tests
return r.json()['data']

def add_role_assignment(self, assignee, role, dry_run=False):
Expand All @@ -70,7 +71,7 @@ def add_role_assignment(self, assignee, role, dry_run=False):
return None
else:
r = requests.post(url, headers=headers, params=params, json=role_assignment)
r.raise_for_status()
raise_for_status(r)

Check warning on line 74 in src/datastation/dataverse/dataset_api.py

View check run for this annotation

Codecov / codecov/patch

src/datastation/dataverse/dataset_api.py#L74

Added line #L74 was not covered by tests
return r

def remove_role_assignment(self, assignment_id, dry_run=False):
Expand All @@ -82,7 +83,7 @@ def remove_role_assignment(self, assignment_id, dry_run=False):
return None
else:
r = requests.delete(url, headers=headers, params=params)
r.raise_for_status()
raise_for_status(r)

Check warning on line 86 in src/datastation/dataverse/dataset_api.py

View check run for this annotation

Codecov / codecov/patch

src/datastation/dataverse/dataset_api.py#L86

Added line #L86 was not covered by tests
return r

def is_draft(self, dry_run=False):
Expand All @@ -94,7 +95,7 @@ def is_draft(self, dry_run=False):
return None
else:
r = requests.get(url, headers=headers, params=params)
r.raise_for_status()
raise_for_status(r)

Check warning on line 98 in src/datastation/dataverse/dataset_api.py

View check run for this annotation

Codecov / codecov/patch

src/datastation/dataverse/dataset_api.py#L98

Added line #L98 was not covered by tests
return r.json()['data']['latestVersion']['versionState'] == 'DRAFT'

def delete_draft(self, dry_run=False):
Expand All @@ -106,7 +107,7 @@ def delete_draft(self, dry_run=False):
return None
else:
r = requests.delete(url, headers=headers, params=params)
r.raise_for_status()
raise_for_status(r)

Check warning on line 110 in src/datastation/dataverse/dataset_api.py

View check run for this annotation

Codecov / codecov/patch

src/datastation/dataverse/dataset_api.py#L110

Added line #L110 was not covered by tests
return r.json()

def destroy(self, dry_run=False):
Expand All @@ -122,7 +123,7 @@ def destroy(self, dry_run=False):
print_dry_run_message(method='DELETE', url=url, headers=headers, params=params)
return None
r = requests.delete(url, headers=headers, params=params)
r.raise_for_status()
raise_for_status(r)

Check warning on line 126 in src/datastation/dataverse/dataset_api.py

View check run for this annotation

Codecov / codecov/patch

src/datastation/dataverse/dataset_api.py#L126

Added line #L126 was not covered by tests
return r.json()

def get_metadata(self, version=':latest', dry_run=False):
Expand All @@ -139,7 +140,7 @@ def get_metadata(self, version=':latest', dry_run=False):
return None
else:
r = requests.get(url, headers=headers, params=params)
r.raise_for_status()
raise_for_status(r)

Check warning on line 143 in src/datastation/dataverse/dataset_api.py

View check run for this annotation

Codecov / codecov/patch

src/datastation/dataverse/dataset_api.py#L143

Added line #L143 was not covered by tests
return r.json()['data']

def get_metadata_export(self, exporter='dataverse_json', dry_run=False):
Expand All @@ -155,7 +156,7 @@ def get_metadata_export(self, exporter='dataverse_json', dry_run=False):
return None
else:
r = requests.get(url, headers=headers, params=params)
r.raise_for_status()
raise_for_status(r)

Check warning on line 159 in src/datastation/dataverse/dataset_api.py

View check run for this annotation

Codecov / codecov/patch

src/datastation/dataverse/dataset_api.py#L159

Added line #L159 was not covered by tests
return r.text

def get_locks(self, lock_type=None, dry_run=False):
Expand All @@ -169,7 +170,7 @@ def get_locks(self, lock_type=None, dry_run=False):
return None
else:
r = requests.get(url, headers=headers, params=params)
r.raise_for_status()
raise_for_status(r)

Check warning on line 173 in src/datastation/dataverse/dataset_api.py

View check run for this annotation

Codecov / codecov/patch

src/datastation/dataverse/dataset_api.py#L173

Added line #L173 was not covered by tests
return r.json()['data']

def add_lock(self, lock_type, dry_run=False):
Expand All @@ -181,7 +182,7 @@ def add_lock(self, lock_type, dry_run=False):
return None
else:
r = requests.post(url, headers=headers, params=params)
r.raise_for_status()
raise_for_status(r)

Check warning on line 185 in src/datastation/dataverse/dataset_api.py

View check run for this annotation

Codecov / codecov/patch

src/datastation/dataverse/dataset_api.py#L185

Added line #L185 was not covered by tests
return r.json()

def remove_lock(self, lock_type=None, dry_run=False):
Expand All @@ -195,7 +196,7 @@ def remove_lock(self, lock_type=None, dry_run=False):
return None
else:
r = requests.delete(url, headers=headers, params=params)
r.raise_for_status()
raise_for_status(r)

Check warning on line 199 in src/datastation/dataverse/dataset_api.py

View check run for this annotation

Codecov / codecov/patch

src/datastation/dataverse/dataset_api.py#L199

Added line #L199 was not covered by tests
return r.json()

def remove_all_locks(self, dry_run=False):
Expand All @@ -209,7 +210,7 @@ def publish(self, update_type='major', dry_run=False):
print_dry_run_message(method='POST', url=url, headers=headers, params=params)
return None
r = requests.post(url, headers=headers, params=params)
r.raise_for_status()
raise_for_status(r)

Check warning on line 213 in src/datastation/dataverse/dataset_api.py

View check run for this annotation

Codecov / codecov/patch

src/datastation/dataverse/dataset_api.py#L213

Added line #L213 was not covered by tests
return r.json()

def reindex(self, dry_run=False):
Expand All @@ -223,7 +224,7 @@ def reindex(self, dry_run=False):
return None
else:
r = requests.get(url, headers=headers, params=params)
r.raise_for_status()
raise_for_status(r)

Check warning on line 227 in src/datastation/dataverse/dataset_api.py

View check run for this annotation

Codecov / codecov/patch

src/datastation/dataverse/dataset_api.py#L227

Added line #L227 was not covered by tests
return r.json()

def modify_registration_metadata(self, dry_run=False):
Expand All @@ -235,7 +236,7 @@ def modify_registration_metadata(self, dry_run=False):
return None
else:
r = requests.post(url, headers=headers, params=params)
r.raise_for_status()
raise_for_status(r)

Check warning on line 239 in src/datastation/dataverse/dataset_api.py

View check run for this annotation

Codecov / codecov/patch

src/datastation/dataverse/dataset_api.py#L239

Added line #L239 was not covered by tests
return r.json()

def get_files(self, version=':latest', dry_run=False):
Expand All @@ -247,7 +248,7 @@ def get_files(self, version=':latest', dry_run=False):
return None
else:
r = requests.get(url, headers=headers, params=params)
r.raise_for_status()
raise_for_status(r)

Check warning on line 251 in src/datastation/dataverse/dataset_api.py

View check run for this annotation

Codecov / codecov/patch

src/datastation/dataverse/dataset_api.py#L251

Added line #L251 was not covered by tests
return r.json()['data']

def await_unlock(self, lock_type=None, sleep_time=5, max_tries=10):
Expand Down
4 changes: 2 additions & 2 deletions src/datastation/dataverse/dataverse_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def get_contents(self, alias="root", dry_run=False):
return None

dv_resp = requests.get(url, headers=headers)
dv_resp.raise_for_status()
raise_for_status(dv_resp)

Check warning on line 21 in src/datastation/dataverse/dataverse_api.py

View check run for this annotation

Codecov / codecov/patch

src/datastation/dataverse/dataverse_api.py#L21

Added line #L21 was not covered by tests

resp_data = dv_resp.json()["data"]
return resp_data
Expand All @@ -32,5 +32,5 @@ def get_storage_size(self, alias="root", dry_run=False):
return None
else:
r = requests.get(url, headers=headers)
r.raise_for_status()
raise_for_status(r)()

Check warning on line 35 in src/datastation/dataverse/dataverse_api.py

View check run for this annotation

Codecov / codecov/patch

src/datastation/dataverse/dataverse_api.py#L35

Added line #L35 was not covered by tests
return r.json()['data']['message']
2 changes: 1 addition & 1 deletion src/datastation/dataverse/file_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ def reingest(self, dry_run=False):
print_dry_run_message(method='POST', url=url, headers=headers, params=params)
return None
r = requests.post(url, headers=headers, params=params)
r.raise_for_status()
raise_for_status(r)()

Check warning on line 23 in src/datastation/dataverse/file_api.py

View check run for this annotation

Codecov / codecov/patch

src/datastation/dataverse/file_api.py#L23

Added line #L23 was not covered by tests
return r
2 changes: 1 addition & 1 deletion src/datastation/dataverse/metrics_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ def get_tree(self, dry_run: bool = False):
print(f"Would have sent the following request: {url}")
return
r = requests.get(url)
r.raise_for_status()
raise_for_status(r)()

Check warning on line 17 in src/datastation/dataverse/metrics_api.py

View check run for this annotation

Codecov / codecov/patch

src/datastation/dataverse/metrics_api.py#L17

Added line #L17 was not covered by tests
return r.json()['data']
2 changes: 1 addition & 1 deletion src/datastation/dataverse/search_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def search(self, query="*", subtree="root", object_type="dataset", dry_run=False

while True:
dv_resp = requests.get(self.url, headers=headers, params=params)
dv_resp.raise_for_status()
raise_for_status(dv_resp)

Check warning on line 57 in src/datastation/dataverse/search_api.py

View check run for this annotation

Codecov / codecov/patch

src/datastation/dataverse/search_api.py#L57

Added line #L57 was not covered by tests

data = dv_resp.json()["data"]
items = data["items"]
Expand Down
Loading

0 comments on commit ddc8a63

Please sign in to comment.