"""Model for ows-track.""" import logging import requests import config # noqa import const # noqa from models.api_exceptions import PersistentException def get_track_info(track_id, session=requests): """Get track info. Args: track_id: The id of track to get data for. session: Session object to make requests with. The default is the requests module because it creates a Session object internally and has the same interface. """ url = const.get_service_url( environment=config.ENVIRONMENT, service_name=const.OWS_TRACK_SERVICE_NAME, path=const.OWS_TRACK_GET_TRACK_ENDPOINT.format(track_id)) response = session.get(url, timeout=10) if response.status_code == 404: logging.error('Requested track not found.') raise PersistentException(response.text) elif response.status_code != 200: logging.error('Unable to fetch track information from ows-track.') raise PersistentException(response.text) logging.info('get_track_info success. Data: {}'.format(response.text)) return response.json() def update_track_info(track_id, track_info, session=requests): """Update track info. Args: track_id: The id of track update. track_info: The updated attributes of the track. session: Session object to make requests with. The default is the requests module because it creates a Session object internally and has the same interface. """ url = const.get_service_url( environment=config.ENVIRONMENT, service_name=const.OWS_TRACK_SERVICE_NAME, path=const.OWS_TRACK_UPDATE_TRACK_ENDPOINT.format(track_id)) response = session.put(url, json=track_info, timeout=10) if response.status_code != 200: logging.error('Failed to update ows-track.') raise PersistentException(response.text) logging.info('update_track_info success. Data: {}'.format(response.text)) return response.json() def update_track_contributor( track_id, contributor_type, contributor_id, contributor_info, session=requests): """Update track contributor. Args: track_id: The id of track to update. contributor_type: The type of track contributor to update. contributor_id: The id of the track contributor. contributor_info: The updated attributes of the track contributor. session: Session object to make requests with. The default is the requests module because it creates a Session object internally and has the same interface. """ url = const.get_service_url( environment=config.ENVIRONMENT, service_name=const.OWS_TRACK_SERVICE_NAME, path=const.OWS_TRACK_UPDATE_CONTRIBUTOR_ENDPOINT.format( track_id, contributor_type, contributor_id ) ) response = session.patch(url, json=contributor_info, timeout=10) if response.status_code != 200: logging.error('Failed to update ows-track.') raise PersistentException(response.text) logging.info('update_track_contributor success. Data: {}'.format( response.text)) return response.json()