Commit f4eb9a6f authored by Jonas Waeber's avatar Jonas Waeber
Browse files

Minor refactor record set

parent 7913df70
......@@ -33,22 +33,22 @@ class UpdateRecordSet(Resource):
type: string
example: SUCCESS/FAILURE
enum: ['SUCCESS', 'FAILURE']
topic_value:
result_topic_value:
type: string/json
example: the value written into the topic
"""
topic_value = ''
# 1. request info from drupal with received uuid
default_request = current_app.config['drupal-api-url'] + '/jsonapi/node/' + \
'record_set/' + record_set_id
base_response = None
result_topic_value = ''
base_url = current_app.config['drupal-api-url']
json_api_path = '/jsonapi/node/record_set/'
user = current_app.config['drupal-user']
password = current_app.config['drupal-password']
auth = HTTPBasicAuth(user, password)
headers = {'X-API-Key': current_app.config['drupal-api-key']}
# 1. request info from drupal with received uuid for default entity
default_request = base_url + json_api_path + record_set_id
base_response = None
try:
base_response = requests.get(default_request, headers=headers, auth=auth)
drupal_record_set = base_response.json()['data']
......@@ -99,7 +99,7 @@ class UpdateRecordSet(Resource):
field_related_record_sets_it = UpdateRecordSet.get_related_record_sets(
drupal_record_set_it)
topic_value = {
result_topic_value = {
'type': drupal_record_set['type'],
'status': drupal_record_set['attributes']['status'],
'title_de': drupal_record_set['attributes']['title'],
......@@ -201,8 +201,8 @@ class UpdateRecordSet(Resource):
current_app.logger.error(msg)
return {
'status': 'FAILURE',
'topic_key': topic_value.get('field_memobase_id'),
'topic_value': topic_value
'topic_key': result_topic_value.get('field_memobase_id'),
'result_topic_value': result_topic_value
}, 500
except Exception as ex:
msg = 'Exception for ' + record_set_id + ': ' + str(ex) + '\n' + \
......@@ -212,8 +212,8 @@ class UpdateRecordSet(Resource):
current_app.logger.error(msg)
return {
'status': 'FAILURE',
'topic_key': topic_value.get('field_memobase_id'),
'topic_value': topic_value,
'topic_key': result_topic_value.get('field_memobase_id'),
'result_topic_value': result_topic_value,
}, 500
# 2. write info into kafka topic
......@@ -221,24 +221,24 @@ class UpdateRecordSet(Resource):
producer = KafkaProducer(bootstrap_servers=current_app.config['kafka-broker-url'],
value_serializer=lambda m: json.dumps(m, ensure_ascii=False)
.encode('utf-8'))
key = bytes(topic_value.get('field_memobase_id'), encoding='utf-8')
producer.send(current_app.config['topic-drupal-export'], topic_value, key=key)
key = bytes(result_topic_value.get('field_memobase_id'), encoding='utf-8')
producer.send(current_app.config['topic-drupal-export'], result_topic_value, key=key)
except Exception as ex:
msg = 'Exception for ' + record_set_id + ': ' + str(ex) + '\n' + \
traceback.format_exc()
current_app.logger.error(msg)
return {
'status': 'FAILURE',
'topic_key': topic_value.get('field_memobase_id'),
'topic_value': topic_value,
'topic_key': result_topic_value.get('field_memobase_id'),
'result_topic_value': result_topic_value,
'exception': msg
}, 500
current_app.logger.debug('success for ' + record_set_id)
return {
'status': 'SUCCESS',
'topic_key': topic_value.get('field_memobase_id'),
'topic_value': topic_value
'topic_key': result_topic_value.get('field_memobase_id'),
'result_topic_value': result_topic_value
}, 200
@staticmethod
......
Supports Markdown
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