Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

217 add functionality to send callback messages #225

Merged
merged 11 commits into from
Jan 10, 2024
46 changes: 46 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,48 @@
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
volodymyrss marked this conversation as resolved.
Show resolved Hide resolved
if not os.path.isfile(callback_file):
return
with open(callback_file, 'r') as file:
self._callback = file.read().strip()

Check warning on line 1354 in oda_api/api.py

View check run for this annotation

Codecov / codecov/patch

oda_api/api.py#L1349-L1354

Added lines #L1349 - L1354 were not covered by tests

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

Check warning on line 1358 in oda_api/api.py

View check run for this annotation

Codecov / codecov/patch

oda_api/api.py#L1358

Added line #L1358 was not covered by tests

def report_progress(self, stage: str=None, progress: int=50, substage: str=None, subprogress: int=None, message:str=None):
"""
Report progress via callback URL
:param stage: current stage description string
:param stage: current stage progress in %
:param stage: current substage description string
:param stage: current substage progress in %
:param message: message to pass
volodymyrss marked this conversation as resolved.
Show resolved Hide resolved
"""
callback_payload = {k: str(v) for k, v in locals().items() if v is not None and k != 'self'}

Check warning on line 1369 in oda_api/api.py

View check run for this annotation

Codecov / codecov/patch

oda_api/api.py#L1369

Added line #L1369 was not covered by tests
okolo marked this conversation as resolved.
Show resolved Hide resolved
volodymyrss marked this conversation as resolved.
Show resolved Hide resolved

okolo marked this conversation as resolved.
Show resolved Hide resolved
if not self.enabled:
logger.info('no callback registered, skipping')
return

Check warning on line 1373 in oda_api/api.py

View check run for this annotation

Codecov / codecov/patch

oda_api/api.py#L1371-L1373

Added lines #L1371 - L1373 were not covered by tests

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

Check warning on line 1375 in oda_api/api.py

View check run for this annotation

Codecov / codecov/patch

oda_api/api.py#L1375

Added line #L1375 was not covered by tests

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

Check warning on line 1380 in oda_api/api.py

View check run for this annotation

Codecov / codecov/patch

oda_api/api.py#L1377-L1380

Added lines #L1377 - L1380 were not covered by tests

elif re.match('^https?://', self._callback):
r = requests.get(self._callback, params=callback_payload)
volodymyrss marked this conversation as resolved.
Show resolved Hide resolved
logger.info('callback %s returns %s : %s', self._callback, r, r.text)

Check warning on line 1384 in oda_api/api.py

View check run for this annotation

Codecov / codecov/patch

oda_api/api.py#L1382-L1384

Added lines #L1382 - L1384 were not covered by tests

else:
raise NotImplementedError

Check warning on line 1387 in oda_api/api.py

View check run for this annotation

Codecov / codecov/patch

oda_api/api.py#L1387

Added line #L1387 was not covered by tests
Loading