Commit a9e0eb8f authored by Jochem Bijlard's avatar Jochem Bijlard
Browse files

Add /state/register to create a named session, which can be used to load

data to from other sessions.
parent 75b10cfc
Pipeline #5258 failed with stages
in 27 seconds
......@@ -11,6 +11,7 @@ from fractalis.data.schema import create_data_schema
from fractalis.validator import validate_json, validate_schema
from fractalis import celery, redis
from fractalis.sync import remove_data
from fractalis.session import RedisSession
data_blueprint = Blueprint('data_blueprint', __name__)
......@@ -31,12 +32,34 @@ def create_data_task() -> Tuple[Response, int]:
etl_handler = ETLHandler.factory(handler=payload['handler'],
server=payload['server'],
auth=payload['auth'])
task_ids = etl_handler.handle(descriptors=payload['descriptors'],
data_tasks=session['data_tasks'],
use_existing=False,
wait=wait)
session['data_tasks'] += task_ids
session['data_tasks'] = list(set(session['data_tasks']))
alias = payload.get('destination')
try:
if alias is None:
task_ids = etl_handler.handle(descriptors=payload['descriptors'],
data_tasks=session['data_tasks'],
use_existing=False,
wait=wait)
session['data_tasks'] += task_ids
session['data_tasks'] = list(set(session['data_tasks']))
else:
logger.info("Submitting task to alias: {}".format(alias))
session_id = redis.get('alias:{}'.format(alias))
session_key = 'session:{}'.format(session_id)
destination = RedisSession(session_id, json.loads(redis.get(session_key)))
task_ids = etl_handler.handle(descriptors=payload['descriptors'],
data_tasks=destination['data_tasks'],
use_existing=False,
wait=wait)
destination['data_tasks'] = list(set(destination['data_tasks'] + task_ids))
redis.set(session_key, json.dumps(destination))
except Exception as e:
logger.error(e)
raise e
logger.debug("Tasks successfully submitted. Sending response.")
return jsonify(''), 201
......
......@@ -137,3 +137,15 @@ def get_state_data(state_id: UUID) -> Tuple[Response, int]:
repl=session['state_access'][state_id][i],
string=state)
return jsonify({'state': json.loads(state)}), 200
@state_blueprint.route('/register', methods=['POST'])
def register():
logger.debug("Received POST request on /state/<uuid:token>.")
payload = request.get_json(force=True)
alias = payload.get('alias')
redis.set('alias:{}'.format(alias), session.sid)
logger.debug("Registered alias: {}".format(alias))
return jsonify(''), 200
......@@ -315,3 +315,41 @@ class TestState:
body = flask.json.loads(rv.get_data())
assert 403 == rv.status_code, body
assert 'no access' in body.get('error')
def test_etl_through_alias(self, test_client):
def find_session_cookie(headers):
for head in headers:
if head[0] == 'Set-Cookie':
break
else:
raise ValueError('Set-Cookie not found in header.')
return head[1].split(';')[0].split('=')[1]
uuid = str(uuid4())
rv = test_client.post('/state/register', data=json.dumps({'alias': uuid}))
session_id_1 = find_session_cookie(rv.headers)
assert redis.get('alias:{}'.format(uuid)) == session_id_1
test_client.cookie_jar = None
rv = test_client.post('/state/register', data=json.dumps({'alias': uuid}))
session_id_2 = find_session_cookie(rv.headers)
assert session_id_2 != session_id_1
rv = test_client.post('/data', data=json.dumps({
'destination': uuid,
'descriptors': {
'constraint': '',
'data_type': 'categorical',
'label': 'MOCK_LABEL'
},
'auth': {
'token': ''
},
"handler": "transmart",
"server": ''
}))
print(rv)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment