Skip to content

Commit

Permalink
🐛 persistent error notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
juftin committed May 28, 2024
1 parent 6164d0f commit b29a11b
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
2 changes: 1 addition & 1 deletion camply/config/notification_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class PushoverConfig:
"""

PUSHOVER_API_ENDPOINT: str = "https://api.pushover.net/1/messages.json"
PUSHOVER_DEFAULT_API_TOKEN: bytes = b"YWpjN3M1a2hhYTRlOG1zYWhncnFnaHduZGdtbmI3"
PUSHOVER_DEFAULT_API_TOKEN: bytes = b"YXBqOWlzNjRrdm5zZWt3YmEyeDZxaDV0cWhxbXI5"
API_HEADERS: dict = {"Content-Type": "application/json"}

Check failure on line 25 in camply/config/notification_config.py

View workflow job for this annotation

GitHub Actions / lint

Missing type parameters for generic type "dict" [type-arg]

PUSH_TOKEN: str = getenv("PUSHOVER_PUSH_TOKEN", None)

Check failure on line 27 in camply/config/notification_config.py

View workflow job for this annotation

GitHub Actions / lint

Argument 2 to "getenv" has incompatible type "None"; expected "str" [arg-type]
Expand Down
17 changes: 9 additions & 8 deletions camply/notifications/pushover.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ def __init__(self, level: Optional[int] = logging.INFO):
)
logger.error(warning_message)
raise EnvironmentError(warning_message)
self.pushover_token = PushoverConfig.PUSH_TOKEN
if self.pushover_token in [None, ""]:
self.pushover_token = base64.b64decode(
PushoverConfig.PUSHOVER_DEFAULT_API_TOKEN
).decode("utf-8")

def send_message(self, message: str, **kwargs) -> requests.Response:
"""
Expand All @@ -47,17 +52,13 @@ def send_message(self, message: str, **kwargs) -> requests.Response:
-------
requests.Response
"""
token = (
PushoverConfig.PUSH_TOKEN
if PushoverConfig.PUSH_TOKEN not in [None, ""]
else base64.b64decode(PushoverConfig.PUSHOVER_DEFAULT_API_TOKEN).decode(
"utf-8"
)
)
response = self.session.post(
url=PushoverConfig.PUSHOVER_API_ENDPOINT,
params=dict(
token=token, user=PushoverConfig.PUSH_USER, message=message, **kwargs
token=self.pushover_token,
user=PushoverConfig.PUSH_USER,
message=message,
**kwargs,
),
)
try:
Expand Down
5 changes: 4 additions & 1 deletion camply/search/base_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ def __init__(
AvailableCampsite
] = self.load_campsites_from_file()
self.loaded_campsites: Set[AvailableCampsite] = self.campsites_found.copy()
self.search_attempts: int = 0

@property
def search_days(self) -> List[datetime]:
Expand Down Expand Up @@ -274,6 +275,7 @@ def _search_matching_campsites_available(
)
logger.info(campsite_availability_message)
raise CampsiteNotFoundError(campsite_availability_message)
self.search_attempts += 1
return matching_campgrounds

@classmethod
Expand Down Expand Up @@ -581,7 +583,8 @@ def get_matching_campsites(
search_once=search_once,
)
except Exception as e:
self.notifier.last_gasp(error=e)
if self.search_attempts >= 1:
self.notifier.last_gasp(error=e)
raise e
else:
starting_count = len(self.campsites_found)
Expand Down

0 comments on commit b29a11b

Please sign in to comment.