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