def execute_collection_exercise(collection_exercise_id): logger.debug('Executing collection exercise', collection_exercise_id=collection_exercise_id) url = f'{app.config["RM_COLLECTION_EXERCISE_SERVICE"]}collectionexerciseexecution/{collection_exercise_id}' response = request_handler('POST', url, auth=app.config['BASIC_AUTH']) if response.status_code == 404: logger.error('Error retrieving collection exercise', collection_exercise_id=collection_exercise_id) raise ApiError(url, response.status_code) if response.status_code not in (200, 201, 202): logger.error('Error executing collection exercise', collection_exercise_id=collection_exercise_id) raise ApiError(url, response.status_code) logger.debug('Successfully executed collection exercise', collection_exercise_id=collection_exercise_id)
def update_collection_exercise_period(collection_exercise_id, period): logger.debug('Updating collection exercise period', collection_exercise_id=collection_exercise_id, period=period) header = {'Content-Type': "text/plain"} url = f'{app.config["RM_COLLECTION_EXERCISE_SERVICE"]}collectionexercises/{collection_exercise_id}/exerciseRef' response = request_handler('PUT', url, headers=header, data=period, auth=app.config['BASIC_AUTH']) if response.status_code == 404: logger.error('Error retrieving collection exercise', collection_exercise_id=collection_exercise_id) raise ApiError(url, response.status_code) if response.status_code not in (200, 201, 202): logger.error('Error updating collection exercise period', collection_exercise_id=collection_exercise_id) raise ApiError(url, response.status_code) logger.debug('Successfully updated collection exercise period', collection_exercise_id=collection_exercise_id)
def upload_sample(collection_exercise_id, sample_file, survey_type='B'): logger.debug('Uploading sample file', collection_exercise_id=collection_exercise_id, survey_type=survey_type) url = f'{app.config["RM_SAMPLE_SERVICE"]}samples/{survey_type}/fileupload' response = request_handler(url=url, method='POST', auth=app.config['BASIC_AUTH'], files={'file': sample_file}) # Sample service *should* return something other than 201 when upload / ingest fails if response.status_code != 201: logger.error('Error uploading sample file', collection_exercise_id=collection_exercise_id, status=response.status_code, survey_type=survey_type) raise ApiError(url, response.status_code) response_json = response.json() logger.debug('Successfully uploaded sample file', collection_exercise_id=collection_exercise_id, response_json=response_json, survey_type=survey_type) return response_json
def get_collection_instruments_by_classifier(survey_id=None, collection_exercise_id=None, ci_type=None): logger.debug('Retrieving collection instruments', survey_id=survey_id, collection_exercise_id=collection_exercise_id, ci_type=ci_type) url = f'{app.config["RAS_COLLECTION_INSTRUMENT_SERVICE"]}' \ f'collection-instrument-api/1.0.2/collectioninstrument' classifiers = _build_classifiers(collection_exercise_id, survey_id, ci_type) response = request_handler( url=url, method='GET', auth=app.config['BASIC_AUTH'], params={'searchString': json.dumps(classifiers)}) if response.status_code != 200: logger.error('Error retrieving collection instruments') raise ApiError(url, response.status_code) logger.debug('Successfully retrieved collection instruments', survey_id=survey_id, collection_exercise_id=collection_exercise_id, ci_type=ci_type) return json.loads(response.text)
def resend_verification_email(party_id): logger.debug('Resending verification email', party_id=party_id) url = app.config['RAS_PARTY_RESEND_VERIFICATION_EMAIL'].format(party_id) response = request_handler('GET', url, auth=app.config['BASIC_AUTH']) if response.status_code != 200: logger.error('Failed to resend verification email', party_id=party_id) raise ApiError(url=url, status_code=response.status_code) logger.debug('Successfully resent verification email')
def get_survey_by_shortname(short_name): logger.debug('Retrieving survey', short_name=short_name) url = f'{app.config["RM_SURVEY_SERVICE"]}surveys/shortname/{short_name}' response = request_handler('GET', url, auth=app.config['BASIC_AUTH']) if response.status_code != 200: logger.error('Error retrieving survey', short_name=short_name) raise ApiError(url, response.status_code) logger.debug('Successfully retrieved survey', short_name=short_name) return response.json()
def get_party_by_respondent_id(party_id): logger.debug('Retrieving respondent party', party_id=party_id) url = f'{app.config["RAS_PARTY_SERVICE"]}party-api/v1/respondents/id/{party_id}' response = request_handler('GET', url, auth=app.config['BASIC_AUTH']) if response.status_code != 200: logger.error('Error retrieving respondent party', party_id=party_id) raise ApiError(url, response.status_code) logger.debug('Successfully retrieved respondent party', party_id=party_id) return response.json()
def get_collection_exercise_events(collection_exercise_id): logger.debug('Retrieving collection exercise events', collection_exercise_id=collection_exercise_id) url = f'{app.config["RM_COLLECTION_EXERCISE_SERVICE"]}collectionexercises/{collection_exercise_id}/events' response = request_handler('GET', url, auth=app.config['BASIC_AUTH']) if response.status_code != 200: logger.error('Error retrieving collection exercise events', collection_exercise_id=collection_exercise_id) raise ApiError(url, response.status_code) logger.debug('Successfully retrieved collection exercise events', collection_exercise_id=collection_exercise_id) return response.json()
def get_party_by_ru_ref(ru_ref): logger.debug('Retrieving reporting unit', ru_ref=ru_ref) url = f'{app.config["RAS_PARTY_SERVICE"]}party-api/v1/parties/type/B/ref/{ru_ref}' response = request_handler('GET', url, auth=app.config['BASIC_AUTH']) if response.status_code != 200: logger.error('Error retrieving reporting unit', ru_ref=ru_ref) raise ApiError(url, response.status_code) logger.debug('Successfully retrieved reporting unit', ru_ref=ru_ref) return response.json()
def create_event(collection_exercise_id, tag, event_dto): logger.debug('Creating event', collection_exercise_id=collection_exercise_id, tag=tag) url = f'{app.config["RM_COLLECTION_EXERCISE_SERVICE"]}collectionexercises/{collection_exercise_id}/events' response = request_handler('POST', url, auth=app.config['BASIC_AUTH'], json=event_dto) if not response.ok: logger.error('Error creating event', collection_exercise_id=collection_exercise_id, tag=tag) raise ApiError(url, response.status_code) logger.debug('Successfully created event', collection_exercise_id=collection_exercise_id, tag=tag)
def link_sample_summary_to_collection_exercise(collection_exercise_id, sample_summary_id): logger.debug('Linking sample summary to collection exercise', collection_exercise_id=collection_exercise_id, sample_summary_id=sample_summary_id) url = f'{app.config["RM_COLLECTION_EXERCISE_SERVICE"]}collectionexercises/link/{collection_exercise_id}' # Currently we only need to link a single sample to a single collection exercise payload = {'sampleSummaryIds': [str(sample_summary_id)]} response = request_handler('PUT', url, auth=app.config['BASIC_AUTH'], json=payload) if response.status_code == 404: logger.error('Error retrieving collection exercise', collection_exercise_id=collection_exercise_id) raise ApiError(url, response.status_code) if response.status_code != 200: logger.error('Error linking sample to collection exercise', collection_exercise_id=collection_exercise_id, sample_summary_id=sample_summary_id) raise ApiError(url, response.status_code) logger.debug('Successfully linked sample summary with collection exercise', collection_exercise_id=collection_exercise_id, sample_summary_id=sample_summary_id) return response.json()
def update_survey_details(survey_ref, updated_survey_details): logger.debug('Updating survey details', survey_ref=survey_ref) url = f'{app.config["RM_SURVEY_SERVICE"]}surveys/ref/{survey_ref}' payload = { "ShortName": updated_survey_details['short_name'], "LongName": updated_survey_details['long_name'] } response = request_handler('PUT', url, auth=app.config['BASIC_AUTH'], json=payload) if response.status_code == 404: logger.warning('Error retrieving survey details', survey_ref=survey_ref) raise ApiError(url, response.status_code) if not response.ok: logger.error('Error updating survey details', survey_ref=survey_ref) raise ApiError(url, response.status_code) logger.debug('Successfully updated survey details', survey_ref=survey_ref)
def put_respondent_enrolment_status(enrolment): logger.debug('Changing enrolment status', enrolment=enrolment) url = f'{app.config["RAS_PARTY_SERVICE"]}party-api/v1/respondents/change_enrolment_status' response = request_handler('PUT', url, auth=app.config['BASIC_AUTH'], json=enrolment) if response.status_code != 200: logger.error('Failed to change enrolment status', enrolment=enrolment) raise ApiError(url=url, status_code=response.status_code) logger.debug('Successfully changed enrolment status')
def sign_in(username, password): logger.debug('Retrieving OAuth2 token for sign-in') url = f'{app.config["UAA_SERVICE_URL"]}{"/oauth/token"}' data = { 'grant_type': 'password', 'client_id': app.config['UAA_CLIENT_ID'], 'client_secret': app.config['UAA_CLIENT_SECRET'], 'username': username, 'password': password, 'response_type': 'token', 'token_format': 'jwt', } headers = { 'Accept': 'application/json', 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8', } response = request_handler('POST', url, data=data, headers=headers) try: response.raise_for_status() except HTTPError: logger.exception(f'Failed to retrieve OAuth2 token {response.text}') raise ApiError(url, response.status_code) try: logger.debug('Successfully retrieved UAA token') token = response.json() access_token = token.get('access_token') return access_token except KeyError: logger.exception("No access_token claim in jwt") raise ApiError(url, status_code=401) except (JSONDecodeError, ValueError) as e: logger.exception("Error decoding JSON response") abort(500, error=str(e))
def get_collection_exercises_by_survey(survey_id): logger.debug('Retrieving collection exercises', survey_id=survey_id) url = f'{app.config["RM_COLLECTION_EXERCISE_SERVICE"]}collectionexercises/survey/{survey_id}' response = request_handler('GET', url, auth=app.config['BASIC_AUTH']) if response.status_code == 204: logger.debug('No collection exercises', survey_id=survey_id) return [] if response.status_code != 200: logger.error('Error retrieving collection exercises', survey_id=survey_id) raise ApiError(url, response.status_code) logger.debug('Successfully retrieved collection exercises', survey_id=survey_id) return response.json()
def get_survey_list(): logger.debug('Retrieving survey list') url = f'{app.config["RM_SURVEY_SERVICE"]}surveys' response = request_handler('GET', url, auth=app.config['BASIC_AUTH']) if response.status_code == 204: logger.debug('No surveys found in survey service') return [] if response.status_code != 200: logger.error('Error retrieving the survey list') raise ApiError(url, response.status_code) logger.debug('Successfully retrieved the survey list') return response.json()
def update_event(collection_exercise_id, tag, timestamp): logger.debug('Updating event', collection_exercise_id=collection_exercise_id, tag=tag) url = f'{app.config["RM_COLLECTION_EXERCISE_SERVICE"]}collectionexercises/{collection_exercise_id}/events/{tag}' response = request_handler('PUT', url, auth=app.config['BASIC_AUTH'], headers={'content-type': 'text/plain'}, data=timestamp) if not response.ok: logger.error('Error updating event', collection_exercise_id=collection_exercise_id, tag=tag) raise ApiError(url, response.status_code) logger.debug('Successfully updated event', collection_exercise_id=collection_exercise_id, tag=tag)
def get_survey_ci_classifier(survey_id): logger.debug('Retrieving classifier type selectors', survey_id=survey_id) url = f'{app.config["RM_SURVEY_SERVICE"]}surveys/{survey_id}/classifiertypeselectors' response = request_handler('GET', url, auth=app.config['BASIC_AUTH']) if response.status_code != 200: logger.error('Error classifier type selectors', survey_id=survey_id) raise ApiError(url, response.status_code) logger.debug('Successfully retrieved classifier type selectors', survey_id=survey_id) classifier_type_selectors = response.json() ci_selector = None for selector in classifier_type_selectors: if selector['name'] == "COLLECTION_INSTRUMENT": ci_selector = selector break logger.debug('Retrieving classifiers for CI selector type', survey_id=survey_id, ci_selector=ci_selector['id']) url = f'{app.config["RM_SURVEY_SERVICE"]}surveys/{survey_id}/classifiertypeselectors/{ci_selector["id"]}' response = request_handler('GET', url, auth=app.config['BASIC_AUTH']) if response.status_code != 200: logger.error('Error retrieving classifiers for CI selector type', survey_id=survey_id, ci_selector=ci_selector['id']) raise ApiError(url, response.status_code) logger.debug('Successfully retrieved classifiers for CI selector type', survey_id=survey_id, ci_selector=ci_selector['id']) return response.json()
def get_businesses_by_search(query): logger.debug('Retrieving businesses by search query', query=query) url = f'{app.config["RAS_PARTY_SERVICE"]}party-api/v1/businesses/search' response = request_handler('GET', url, auth=app.config['BASIC_AUTH'], params={"query": query}) if response.status_code != 200: logger.error('Error retrieving businesses by search query', query=query) raise ApiError(url, response.status_code) logger.debug('Successfully retrieved businesses by search query') return json.loads(response.text)
def get_case_groups_by_business_party_id(business_party_id): logger.debug('Retrieving case groups', business_party_id=business_party_id) url = f'{app.config["RM_CASE_SERVICE"]}casegroups/partyid/{business_party_id}' response = request_handler('GET', url, auth=app.config['BASIC_AUTH']) if response.status_code == 204: logger.debug('No caseGroups found for business', business_party_id=business_party_id) return [] if response.status_code != 200: logger.error('Error retrieving cases', business_party_id=business_party_id) raise ApiError(url, response.status_code) logger.debug('Successfully retrieved caseGroups', business_party_id=business_party_id) return response.json()
def generate_new_enrolment_code(collection_exercise_id, ru_ref): logger.debug('Generating new enrolment code', collection_exercise_id=collection_exercise_id, ru_ref=ru_ref) url = f'{app.config["RM_CASE_SERVICE"]}cases/iac/{collection_exercise_id}/{ru_ref}' response = request_handler('POST', url, auth=app.config['BASIC_AUTH']) if response.status_code != 200: logger.error('Failed to generate new enrolment code', collection_exercise_id=collection_exercise_id, ru_ref=ru_ref) raise ApiError(url, response.status_code) logger.debug('Successfully generated new enrolment code', collection_exercise_id=collection_exercise_id, ru_ref=ru_ref) return response.json()
def get_respondent_by_email(email): logger.debug('Getting respondent by email') url = f'{app.config["RAS_PARTY_SERVICE"]}party-api/v1/respondents/email' response = request_handler('GET', url, json=email, auth=app.config['BASIC_AUTH']) if response.status_code == 404: logger.debug("No respondent found", status_code=response.status_code) return {"Response": "No respondent found"} elif response.status_code != 200: logger.error('Error retrieving respondent') raise ApiError(url, response.status_code) logger.debug("Successfully retrieved respondent") return response.json()
def get_party_by_business_id(party_id, collection_exercise_id=None): logger.debug('Retrieving business party', party_id=party_id) url = f'{app.config["RAS_PARTY_SERVICE"]}party-api/v1/businesses/id/{party_id}' params = { "collection_exercise_id": collection_exercise_id, "verbose": True } response = request_handler('GET', url, params=params, auth=app.config['BASIC_AUTH']) if response.status_code != 200: logger.error('Error retrieving business party', party_id=party_id) raise ApiError(url, response.status_code) logger.debug('Successfully retrieved business party', party_id=party_id) return response.json()
def get_sample_summary(sample_summary_id): logger.debug('Retrieving sample summary', sample_summary_id=sample_summary_id) url = f'{app.config["RM_SAMPLE_SERVICE"]}samples/samplesummary/{sample_summary_id}' response = request_handler(url=url, method='GET', auth=app.config['BASIC_AUTH']) if response.status_code != 200: logger.error('Error retrieving sample summary', sample_summary_id=sample_summary_id, status_code=response.status_code) raise ApiError(url, response.status_code) logger.debug('Successfully retrieved sample summary', sample_summary_id=sample_summary_id) return response.json()
def get_iac(iac): logger.debug('Retrieving iac') if not iac: logger.warning('No iac provided') return None url = f'{app.config["RM_IAC_SERVICE"]}iacs/{iac}' response = request_handler('GET', url, auth=app.config['BASIC_AUTH']) if response.status_code == 404: logger.warning('IAC code not found') return if response.status_code != 200: logger.error('Error retrieving iac') raise ApiError(url, response.status_code) logger.debug('Successfully retrieved iac') return response.json()
def upload_collection_instrument(collection_exercise_id, file, params=None): logger.debug('Uploading collection instrument', collection_exercise_id=collection_exercise_id) url = f'{app.config["RAS_COLLECTION_INSTRUMENT_SERVICE"]}' \ f'collection-instrument-api/1.0.2/upload/{collection_exercise_id}' files = {"file": (file.filename, file.stream, file.mimetype)} response = request_handler(url=url, method='POST', auth=app.config['BASIC_AUTH'], files=files, params=params) if response.status_code != 200: logger.error('Error uploading collection instrument') raise ApiError(url, response.status_code) logger.debug('Successfully uploaded collection instrument', collection_exercise_id=collection_exercise_id)
def request_handler(method, url, params=None, auth=None, headers=None, json=None, data=None, files=None): try: if method == 'POST': response = requests.post(url, params=params, auth=auth, headers=headers, json=json, data=data, files=files) elif method == 'PUT': response = requests.put(url, params=params, auth=auth, headers=headers, json=json, data=data, files=files) else: response = requests.get(url, params=params, auth=auth, headers=headers, json=json) except RequestException as e: logger.exception('Failed to connect to external service', method=method, url=url, exception=str(e)) raise ApiError(url) logger.debug('Request response', method=method, url=url, status=response.status_code) return response
def get_linked_sample_summary_id(collection_exercise_id): logger.debug('Retrieving sample linked to collection exercise', collection_exercise_id=collection_exercise_id) url = f'{app.config["RM_COLLECTION_EXERCISE_SERVICE"]}collectionexercises/link/{collection_exercise_id}' response = request_handler('GET', url, auth=app.config['BASIC_AUTH']) if response.status_code == 204: logger.info('No samples linked to collection exercise', collection_exercise_id=collection_exercise_id) return elif response.status_code != 200: logger.error('Error retrieving sample summaries linked to collection exercise', collection_exercise_id=collection_exercise_id) raise ApiError(url, response.status_code) # currently, we only want a single sample summary sample_summary_id = response.json()[0] logger.debug('Successfully retrieved linked sample summary', collection_exercise_id=collection_exercise_id, sample_summary_id=sample_summary_id) return sample_summary_id
def unlink_collection_instrument_and_exercise(collection_instrument_id, collection_exercise_id): logger.debug('Unlinking collection instrument and exercise', collection_instrument_id=collection_instrument_id, collection_exercise_id=collection_exercise_id) url = f'{app.config["RAS_COLLECTION_INSTRUMENT_SERVICE"]}' \ f'collection-instrument-api/1.0.2/unlink-exercise/{collection_instrument_id}/{collection_exercise_id}' response = request_handler(url=url, method='PUT', auth=app.config['BASIC_AUTH']) if response.status_code != 200: logger.error('Failed to unlink collection instrument and exercise', collection_instrument_id=collection_instrument_id, collection_exercise_id=collection_exercise_id) raise ApiError(url, response.status_code) logger.debug('Successfully unlinked collection instrument and exercise', collection_instrument_id=collection_instrument_id, collection_exercise_id=collection_exercise_id)
def update_case_group_status(collection_exercise_id, ru_ref, case_group_event): logger.debug('Updating status', collection_exercise_id=collection_exercise_id, ru_ref=ru_ref, case_group_event=case_group_event) url = f'{app.config["RM_CASE_SERVICE"]}casegroups/transitions/{collection_exercise_id}/{ru_ref}' response = request_handler('PUT', url, auth=app.config['BASIC_AUTH'], json={'event': case_group_event}) if response.status_code != 200: logger.error('Error updating status', collection_exercise_id=collection_exercise_id, ru_ref=ru_ref, case_group_event=case_group_event) raise ApiError(url, response.status_code) logger.debug('Successfully updated status', collection_exercise_id=collection_exercise_id, ru_ref=ru_ref, case_group_event=case_group_event)