Skip to content

Commit

Permalink
add exceptions for API request
Browse files Browse the repository at this point in the history
  • Loading branch information
Ron Hildebrandt authored and Ron Hildebrandt committed Dec 11, 2024
1 parent 1333d7f commit 2241edf
Showing 1 changed file with 20 additions and 11 deletions.
31 changes: 20 additions & 11 deletions src/pynxtools_raman/rod/rod_get_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,32 @@

import requests


rod_id = 1000679


def save_rod_file_from_ROD_via_API(rod_id: int) -> str:
def save_rod_file_from_ROD_via_API(rod_id: int):
url = "https://solsa.crystallography.net/rod/" + str(rod_id) + ".rod"
response = requests.post(url)

filename = str(rod_id)
print(f"Initialized download of .rod file with ID '{rod_id}' from '{url}'.")

try:
response = requests.post(url)
response.raise_for_status() # Raise HTTP error for bad

print(f"Successfully received .rod file with ID '{rod_id}'")

with open(filename + ".rod", "w", encoding="utf-8") as file:
file.write(response.text)
print("Saved ROD ID %s to file '%s'"(rod_id, filename))
filename = str(rod_id)

with open(filename + ".rod", "w", encoding="utf-8") as file:
file.write(response.text)
print(f"Saved .rod file with ID '{rod_id}' to file '{filename}'")

except requests.exceptions.ConnectionError as con_err:
print(f"ConnectionError occured: {con_err}")
except requests.exceptions.HTTPError as http_err:
print(f"CHTTPError occured: {http_err}")
except requests.exceptions.RequestException as req_exc:
print(f"RequestException occured: {req_exc}")


def trigger_rod_download():
Expand All @@ -30,7 +43,3 @@ def trigger_rod_download():
args = parser.parse_args()

save_rod_file_from_ROD_via_API(args.rod_id)

# Use the parsed argument
print(f"Downloading CIF file: {args.rod_id}")
# Add your logic to download the file here

0 comments on commit 2241edf

Please sign in to comment.