bugfix bdCheck: add meaningful error message on api retrive failure

This commit is contained in:
marko
2025-03-08 17:45:07 +01:00
parent ab31c5acca
commit b3e53ae588
2 changed files with 96 additions and 46 deletions

View File

@@ -6,7 +6,7 @@
"configurations": [ "configurations": [
{ {
"name": "post", "name": "post",
"type": "python", "type": "debugpy",
"request": "launch", "request": "launch",
"program": "testApi.py", "program": "testApi.py",
"console": "integratedTerminal", "console": "integratedTerminal",
@@ -18,7 +18,7 @@
}, },
{ {
"name": "getTrainees", "name": "getTrainees",
"type": "python", "type": "debugpy",
"request": "launch", "request": "launch",
"program": "testApi.py", "program": "testApi.py",
"console": "integratedTerminal", "console": "integratedTerminal",
@@ -26,5 +26,17 @@
"args": [ "args": [
"GET", "GET",
"--endpoint", "trainees" ] "--endpoint", "trainees" ]
} ] },
{
"name": "bdCheck",
"type": "debugpy",
"request": "launch",
"program": "bdCheck.py",
"console": "integratedTerminal",
"justMyCode": true,
"args": [
]
},
]
} }

View File

@@ -3,71 +3,109 @@ from datetime import datetime
from MatrixIm import MatrixIm from MatrixIm import MatrixIm
def get_days_since_birthday(birthday: datetime.date, today: datetime.date=datetime.now()) -> int: def get_days_since_birthday(
birthday: datetime.date, today: datetime.date = datetime.now()
) -> int:
# get number of days distance to birthday in this year: # get number of days distance to birthday in this year:
this_year = (datetime(today.year, birthday.month, birthday.day) - today).days this_year = (datetime(today.year, birthday.month, birthday.day) - today).days
# The birthday is already past return it # The birthday is already past return it
if this_year <= 0: if this_year <= 0:
return this_year return this_year
# otherwise we have to take the distance to last years birthday # otherwise we have to take the distance to last years birthday
return (datetime(today.year-1, birthday.month, birthday.day) - today).days return (datetime(today.year - 1, birthday.month, birthday.day) - today).days
def get_days_until_birthday(birthday: datetime.date, today: datetime.date=datetime.now()) -> int:
def get_days_until_birthday(
birthday: datetime.date, today: datetime.date = datetime.now()
) -> int:
# get number of days distance to birthday in this year: # get number of days distance to birthday in this year:
this_year = (datetime(today.year, birthday.month, birthday.day) - today).days this_year = (datetime(today.year, birthday.month, birthday.day) - today).days
# The birthday is already past return it # The birthday is already past return it
if this_year >= 0: if this_year >= 0:
return this_year return this_year
# otherwise we have to take the distance to next years birthday # otherwise we have to take the distance to next years birthday
return (datetime(today.year+1, birthday.month, birthday.day) - today).days return (datetime(today.year + 1, birthday.month, birthday.day) - today).days
with open("testApi.config.yaml", "r") as yaml_file: with open("testApi.config.yaml", "r") as yaml_file:
from yaml import safe_load from yaml import safe_load
config = safe_load(yaml_file) config = safe_load(yaml_file)
trainees = [{ response = testApi.apiCall.call(
"name": {"family": t['name'], "given": t['vorname']}
, "date_of_birth": datetime.strptime(t['gebDatum'], "%Y-%m-%d")
} for t in testApi.apiCall.call(
method="GET", method="GET",
host="cwsvJudo.bplaced.net", host="cwsvJudo.bplaced.net",
url="participo/api/trainees", url="participo/api/trainees",
headers={ headers={"Authorization": f"Basic: {config['apiKey']}"},
"Authorization": f"Basic: {config['apiKey']}"
}
) if t['gebDatum'] not in [None, "0000-00-00"]]
MatrixIm.post(
message="last birthdays: \n" + "\n".join([
f"- {t['name']['given']} {t['name']['family']}: {get_days_since_birthday(t['date_of_birth'])} ({t['date_of_birth'].strftime('%d.%m.')})"
for t in sorted(
[t for t in trainees if get_days_since_birthday(t['date_of_birth'])>-14]
, key=lambda t: get_days_since_birthday(t['date_of_birth'])
)
])
, home_server_url="https://matrix.org"
, room_id="!vFXxgKsLbRLdWCSTIO:matrix.org"
, credentials={
'user': "@ukenth-the-grumpy:matrix.org"
, 'password': "DQpSs1yn9Y26c1zo"
}
) )
MatrixIm.post( try:
message="next birthdays: \n" + "\n".join([ trainees = [
f"- {t['name']['given']} {t['name']['family']}: {get_days_until_birthday(t['date_of_birth'])} ({t['date_of_birth'].strftime('%d.%m.')})" {
for t in sorted( "name": {"family": t["name"], "given": t["vorname"]},
[t for t in trainees if get_days_until_birthday(t['date_of_birth'])<14] "date_of_birth": datetime.strptime(t["gebDatum"], "%Y-%m-%d"),
, key=lambda t: get_days_until_birthday(t['date_of_birth']) }
for t in testApi.apiCall.call(
method="GET",
host="cwsvJudo.bplaced.net",
url="participo/api/trainees",
headers={"Authorization": f"Basic: {config['apiKey']}"},
) )
]) if t["gebDatum"] not in [None, "0000-00-00"]
, home_server_url="https://matrix.org" ]
, room_id="!vFXxgKsLbRLdWCSTIO:matrix.org"
, credentials={ MatrixIm.post(
'user': "@ukenth-the-grumpy:matrix.org" message="last birthdays: \n"
, 'password': "DQpSs1yn9Y26c1zo" + "\n".join(
} [
) f"- {t['name']['given']} {t['name']['family']}: {get_days_since_birthday(t['date_of_birth'])} ({t['date_of_birth'].strftime('%d.%m.')})"
for t in sorted(
[
t
for t in trainees
if get_days_since_birthday(t["date_of_birth"]) > -14
],
key=lambda t: get_days_since_birthday(t["date_of_birth"]),
)
]
),
home_server_url="https://matrix.org",
room_id="!vFXxgKsLbRLdWCSTIO:matrix.org",
credentials={
"user": "@ukenth-the-grumpy:matrix.org",
"password": "DQpSs1yn9Y26c1zo",
},
)
MatrixIm.post(
message="next birthdays: \n"
+ "\n".join(
[
f"- {t['name']['given']} {t['name']['family']}: {get_days_until_birthday(t['date_of_birth'])} ({t['date_of_birth'].strftime('%d.%m.')})"
for t in sorted(
[
t
for t in trainees
if get_days_until_birthday(t["date_of_birth"]) < 14
],
key=lambda t: get_days_until_birthday(t["date_of_birth"]),
)
]
),
home_server_url="https://matrix.org",
room_id="!vFXxgKsLbRLdWCSTIO:matrix.org",
credentials={
"user": "@ukenth-the-grumpy:matrix.org",
"password": "DQpSs1yn9Y26c1zo",
},
)
except Exception as e:
MatrixIm.post(
message=f"bdCheck: Exception {e} ({repr(e)})\nResponse was {response}",
home_server_url="https://matrix.org",
room_id="!vFXxgKsLbRLdWCSTIO:matrix.org",
credentials={
"user": "@ukenth-the-grumpy:matrix.org",
"password": "DQpSs1yn9Y26c1zo",
},
)