def http_status_404(error=None): if request_wants_json(): rest_response = get_response_template(codes.NOT_FOUND, 'fail', None) rest_response['data'] = None rest_response['message'] = error.name return make_response(jsonify(rest_response), codes.NOT_FOUND) return render_template('404.html')
def count(api_version): try: url_coll = get_mongodb_db_collection(app.config['MONGODB_URLS']) rc = url_coll.count() resp = get_response_template(codes.OK, 'success', None) resp['data'] = {'record_count': rc} resp['message'] = 'urls record count', code = codes.OK except: app.logger.debug(inspect.stack()[0][3] + ': ' + get_app_message('db_na')) resp = get_response_template(codes.SERVICE_UNAVAILABLE, 'error', None) resp['message'] = 'malewaredb service unavailable', resp['data'] = None code = codes.SERVICE_UNAVAILABLE return make_response(jsonify(resp), code)
def get_urlinfo(api_version, urlfunc, urlkey, **kwargs): try: urlcheck = urlfunc(urlkey, **kwargs) rest_response = get_response_template(codes.OK, 'success', api_version) rest_response['data'] = make_success_data(api_version, urlcheck) return (rest_response, codes.OK) except Exception as ex: except_type = type(ex).__name__ except_info = {} if app.config['DEBUG']: except_info['type'] = except_type except_info['module'] = type(ex).__module__ # try to raise a well-formatted jsend-like response based upon type of error if except_type in ['ConnectionFailure', 'AutoReconnect']: raise ApiExceptionIssue(status_type='error', status_code=codes.SERVICE_UNAVAILABLE, data={'args':ex.args} if app.config['DEBUG'] else None, message=except_type) elif except_type == 'InvalidId': raise ApiExceptionIssue(status_type='fail', status_code=codes.UNPROCESSABLE_ENTITY, data={type(ex).__name__ : ex.args}, message=except_info if app.config['DEBUG'] else None) else: raise ApiExceptionIssue(status_type='fail', status_code=codes.BAD_REQUEST, data=request.url, message=except_info if app.config['DEBUG'] else app_message['except_unknown_issue'])
def format_exception_response(self): api_version = request.view_args[ 'api_version'] if 'api_version' in request.view_args else None exception_response = get_response_template(self.status_code, self.status_type, api_version) exception_response['message'] = self.message exception_response['data'] = self.data return exception_response
def missing_data_urlinfo_by_id(api_version): # check if API version requested is active if api_version not in app.config['API_VERSION_ACTIVE']: abort(codes.NOT_FOUND) # produce error response rest_response = get_response_template(codes.UNPROCESSABLE_ENTITY, 'fail', api_version) rest_response['data'] = { 'id': 'id required: ' + request.path.rstrip('/') + '/<id>', 'message': get_app_message('id_api_desc') } # return response as json with success status code in header return make_response(jsonify(rest_response), codes.UNPROCESSABLE_ENTITY)
def get_urlinfo_home(api_version): # check if API version requested is active if api_version not in app.config['API_VERSION_ACTIVE']: abort(codes.NOT_FOUND) # iniitialize restful response with common elements rest_response = get_response_template(codes.OK, 'success', api_version) # generate valid urls and instructions for accessing API rest_response['data'] = { '_links': [{ 'rel': 'path', 'href': url_for('find_urlinfo_by_path', api_version=app.config['API_VERSION_CURRENT'], path='', _external=True, _scheme=get_app_scheme()), 'message': get_app_message('path_api_desc') }, { 'rel': 'search', 'href': url_for('search_urlinfo_by_path', api_version=app.config['API_VERSION_CURRENT'], path='', _external=True, _scheme=get_app_scheme()), 'message': get_app_message('search_api_desc') }, { 'rel': 'id', 'href': url_for('find_urlinfo_by_id', api_version=app.config['API_VERSION_CURRENT'], urlid='', _external=True, _scheme=get_app_scheme()), 'message': get_app_message('id_api_desc') }] } # return response as json with success status code in header return make_response(jsonify(rest_response), codes.OK)
def missing_data_urlinfo_by_path(api_version): if api_version not in app.config['API_VERSION_ACTIVE']: abort(codes.NOT_FOUND) # produce error response rest_response = get_response_template(codes.UNPROCESSABLE_ENTITY, 'fail', api_version) rest_response['data'] = { 'path': 'url required: ' + request.path.rstrip('/') + get_app_message('path_api_example'), 'message': get_app_message('path_api_desc') } # return response as json with success status code in header return make_response(jsonify(rest_response), codes.UNPROCESSABLE_ENTITY)