Commit 337a9646 authored by Jonas Waeber's avatar Jonas Waeber
Browse files

Refactor update record set endpoint config values to class values.

parent 81829b01
......@@ -16,6 +16,14 @@ class UpdateRecordSet(Resource):
self.producer = KafkaProducer(bootstrap_servers=current_app.config['kafka-broker-url'],
value_serializer=lambda m: json.dumps(m, ensure_ascii=False)
.encode('utf-8'))
self.base_url = current_app.config['drupal-api-url']
self.json_api_path = '/jsonapi/node/record_set/'
self.institution_path = '/jsonapi/node/institution/'
self.metadata_language_path = '/jsonapi/taxonomy_term/language_of_metadata/'
self.headers = {'X-API-Key': current_app.config['drupal-api-key']}
user = current_app.config['drupal-user']
password = current_app.config['drupal-password']
self.auth = HTTPBasicAuth(user, password)
def get(self, record_set_drupal_uuid):
"""
......@@ -46,52 +54,43 @@ class UpdateRecordSet(Resource):
"""
result_topic_value = ''
base_url = current_app.config['drupal-api-url']
json_api_path = '/jsonapi/node/record_set/'
metadata_language_path = '/jsonapi/taxonomy_term/language_of_metadata/'
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']}
# Retrieve Drupal Entities for each language.
# Returns default entity if there is a language missing.
de_drupal_url = f'{base_url}/de{json_api_path}{record_set_drupal_uuid}'
fr_drupal_url = f'{base_url}/fr{json_api_path}{record_set_drupal_uuid}'
it_drupal_url = f'{base_url}/it{json_api_path}{record_set_drupal_uuid}'
de_drupal_url = f'{self.base_url}/de{self.json_api_path}{record_set_drupal_uuid}'
fr_drupal_url = f'{self.base_url}/fr{self.json_api_path}{record_set_drupal_uuid}'
it_drupal_url = f'{self.base_url}/it{self.json_api_path}{record_set_drupal_uuid}'
try:
drupal_record_set_de = \
requests.get(de_drupal_url, headers=headers, auth=auth).json()['data']
requests.get(de_drupal_url, headers=self.headers, auth=self.auth).json()['data']
drupal_record_set_fr = \
requests.get(fr_drupal_url, headers=headers, auth=auth).json()['data']
requests.get(fr_drupal_url, headers=self.headers, auth=self.auth).json()['data']
drupal_record_set_it = \
requests.get(it_drupal_url, headers=headers, auth=auth).json()['data']
requests.get(it_drupal_url, headers=self.headers, auth=self.auth).json()['data']
institutions = drupal_record_set_de['relationships']['field_institution']['data']
institution_ids = UpdateRecordSet.get_institution_id_list(institutions, headers, auth)
institution_ids = UpdateRecordSet.get_institution_id_list(institutions)
institutions = \
drupal_record_set_de['relationships']['field_resp_institution_access']['data']
access_institution_ids = UpdateRecordSet.get_institution_id_list(
institutions, headers, auth)
access_institution_ids = UpdateRecordSet.get_institution_id_list(institutions)
institutions = \
drupal_record_set_de['relationships']['field_resp_institution_master']['data']
master_institutions_ids = UpdateRecordSet.get_institution_id_list(
institutions, headers, auth)
master_institutions_ids = UpdateRecordSet.get_institution_id_list(institutions)
institutions = \
drupal_record_set_de['relationships']['field_resp_institution_original']['data']
original_institution_ids = UpdateRecordSet.get_institution_id_list(
institutions, headers, auth)
original_institution_ids = UpdateRecordSet.get_institution_id_list(institutions)
metadata_language_codes = []
metadata_languages = \
drupal_record_set_de['relationships']['field_metadata_languages']['data']
for metadataLanguage in metadata_languages:
drupal_record_set_language_code = \
requests.get(f'{base_url}{metadata_language_path}{metadataLanguage["id"]}',
headers=headers, auth=auth)
requests.get(
f'{self.base_url}{self.metadata_language_path}{metadataLanguage["id"]}',
headers=self.headers, auth=self.auth)
metadata_language_codes.append(
drupal_record_set_language_code.json()['data']['attributes']['name']
)
......@@ -262,13 +261,11 @@ class UpdateRecordSet(Resource):
'result_topic_value': result_topic_value
}, 200
@staticmethod
def get_institution_id_list(institution_data, headers, auth):
def get_institution_id_list(self, institution_data):
institution_ids = []
for institution in institution_data:
drupal_institution = \
requests.get(current_app.config['drupal-api-url'] + '/jsonapi/node/' +
'institution/' + institution['id'], headers=headers, auth=auth)
url = f"{self.base_url}{self.institution_path}{institution['id']}"
drupal_institution = requests.get(url, headers=self.headers, auth=self.auth)
institution_ids.append(
drupal_institution.json()['data']['attributes']['field_memobase_id']
)
......
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