Skip to content

Commit

Permalink
ProgressReporter class added
Browse files Browse the repository at this point in the history
  • Loading branch information
okolo committed Nov 6, 2023
1 parent 3688a73 commit 6af4ebe
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions oda_api/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import requests
import ast
import json
import re

try:
# compatibility in some remaining environments
Expand Down Expand Up @@ -1339,3 +1340,46 @@ def from_response_json(cls, res_json, instrument, product):
p.meta_data = p.meta

return d

class ProgressReporter(object):
"""
The class allows to report task progress to end user
"""
def __init__(self):
self._callback = None
callback_file = ".oda_api_callback" # perhaps it would be better to define this constant in a common lib
if not os.path.isfile(callback_file):
return
with open(callback_file, 'r') as file:
self._callback = file.read().strip()

@property
def enabled(self):
return self._callback is not None

def report_progress(self, message: str):
"""
Report progress via callback URL
:param message: message to pass
"""
if not self.enabled:
logger.info('no callback registered, skipping')
return

logger.info('will perform callback: %s', self._callback)

callback_payload = dict(
message=message
)

if re.match('^file://', self._callback):
with open(self._callback.replace('file://', ''), "w") as f:
json.dump(callback_payload, f)
logger.info('stored callback in a file %s', self._callback)

elif re.match('^https?://', self._callback):
r = requests.get(self._callback, params=callback_payload)
logger.info('callback %s returns %s : %s', self._callback, r, r.text)

else:
raise NotImplementedError

0 comments on commit 6af4ebe

Please sign in to comment.