def post(self): """Post a list of messages.""" try: data = request.get_json() driver_name = request.headers.get('X-DRIVER-NAME', '*') job_controller = request.headers.get('X-JOB-CONTROLLER', '0') == '1' job_id = LoaderAPIFacade().publish_updates(data, driver_name, job_controller) res = { 'message': 'Updates published successfully', } if job_id: res.update({'jobid': job_id}) return res, 202, { 'Location': '{}/job/{}'.format(request.path, job_id) } except ValidationError as error: app.logger.exception('Error sending updates to rabbitmq') api.abort(400, errors=util.validate(error)) except BadRequest as err: app.logger.exception('Error sending updates to rabbitmq') api.abort(400, errors=err.description) except: app.logger.exception('Error sending updates to rabbitmq') res = {'message': 'Error sending updates to queue'} return api.abort(500, errors=res)
def get(self, job_id): job = models.Job.find_by_uuid(job_id) if not job: res = {'message': 'Job not found'} api.abort(404, errors=res) errors = [] for error in job.errors: error_response = error.response try: error_response = json.loads(error.response) except: pass errors.append({ 'request': json.loads(error.request_body), 'response': error_response, 'status_code': error.status_code }) response = { 'uuid': job_id, 'driver': job.driver, 'completed': job.completed, 'total_update_count': job.updates_count, 'successful_update_count': job.success_count, 'error_update_count': job.error_count, 'date': job.date_time, 'errors': errors } return response, 200
def post(self): """Create token""" try: data = request.get_json() if type(data) is dict: username = data.get('username') password = data.get('password') else: username = None password = None if not username or not password: app.logger.error('Username and Password is required.') api.abort(401, errors='Username and Password is required.') token = facade.create_token(username, password) return token, 200 except exceptions.Unauthorized: app.logger.error('User %s not Unauthorized.', username) api.abort(401, 'User not Unauthorized.') except exceptions.AuthException: app.logger.error('Auth Unavailable.') api.abort(503, 'Auth Unavailable.') except AuthException: app.logger.error('Auth Unavailable.') api.abort(503, 'Auth Unavailable.')
def validate_token(token): auth_inst = Auth() try: auth_inst.set_token(token) auth_inst.validate_token() return auth_inst except exceptions.InvalidToken: app.logger.error('Invalid Token') api.abort(401, errors='Invalid Token') except exceptions.AuthException: err_msg = 'Error to validate token' app.logger.exception(err_msg) api.abort(503)
def validate_token(self): token_data = self.auth.get_token_data_details() if not self.has_permission(token_data): api.abort(403, 'User have not permission to this action') app.logger.error('User have not permission to this action')