def get_requests(): """ Retrieve paginated details for requests. :param int page: the page number to retrieve. Defaults to 1 :param int per_page: the amount of items on each page. Defaults to 20. Ignored if value exceeds configuration's CACHITO_MAX_PER_PAGE. :rtype: flask.Response """ # Check if the user is filtering requests by state state = flask.request.args.get('state') # Default verbose flag to False verbose = str_to_bool(flask.request.args.get('verbose', False)) max_per_page = flask.current_app.config['CACHITO_MAX_PER_PAGE'] # The call to `paginate` will inspect the current HTTP request for the # pagination parameters `page` and `per_page`. query = Request.query if state: if state not in RequestStateMapping.get_state_names(): states = ':'.join(RequestStateMapping.get_state_names()) raise ValidationError( f'{state} is not a valid request state. Valid states are: {states}' ) state_int = RequestStateMapping.__members__[state].value query = query.join(RequestState, Request.request_state_id == RequestState.id) query = query.filter(RequestState.state == state_int) pagination_query = query.paginate(max_per_page=max_per_page) requests = pagination_query.items response = { 'items': [request.to_json(verbose=verbose) for request in requests], 'meta': pagination_metadata(pagination_query, verbose=verbose), } return flask.jsonify(response)
def get_request_logs(request_id): """ Retrieve the logs for the Cachito request. :param int request_id: the value of the request ID :return: a Flask JSON response :rtype: flask.Response :raise NotFound: if the request is not found :raise Gone: if the logs no longer exist """ request_log_dir = flask.current_app.config["CACHITO_REQUEST_FILE_LOGS_DIR"] if not request_log_dir: raise NotFound() request = Request.query.get_or_404(request_id) log_file_path = os.path.join(request_log_dir, f"{request_id}.log") if not os.path.exists(log_file_path): if request.state.state_name == "stale": raise Gone( f"The logs for the Cachito request {request_id} no longer exist" ) finalized = request.state.state_name in RequestStateMapping.get_final_states( ) if finalized: raise NotFound() # The request may not have been initiated yet. Return empty logs until it's processed. return flask.Response("", mimetype="text/plain") return flask.Response(stream_with_context( generate_stream_response(log_file_path)), mimetype="text/plain")
def test_invalid_state(app, auth_env, client, db): """Test that the proper error is thrown when an invalid state is entered.""" rv = client.get('/api/v1/requests?state=complet') assert rv.status_code == 400 response = rv.json states = ':'.join(RequestStateMapping.get_state_names()) assert response[ 'error'] == f'complet is not a valid request state. Valid states are: {states}'
def get_requests(): """ Retrieve paginated details for requests. :rtype: flask.Response """ # Check if the user is filtering requests by state state = flask.request.args.get("state") # Default verbose flag to False verbose = str_to_bool(flask.request.args.get("verbose", False)) max_per_page = flask.current_app.config["CACHITO_MAX_PER_PAGE"] # The call to `paginate` will inspect the current HTTP request for the # pagination parameters `page` and `per_page`. query = Request.query.order_by(Request.id.desc()) if state: if state not in RequestStateMapping.get_state_names(): states = ", ".join(RequestStateMapping.get_state_names()) raise ValidationError( f"{state} is not a valid request state. Valid states are: {states}" ) state_int = RequestStateMapping.__members__[state].value query = query.join(RequestState, Request.request_state_id == RequestState.id) query = query.filter(RequestState.state == state_int) try: per_page = int(flask.request.args.get("per_page", 10)) except ValueError: per_page = 10 pagination_query = query.paginate(per_page=per_page, max_per_page=max_per_page) requests = pagination_query.items query_params = {} if state: query_params["state"] = state if verbose: query_params["verbose"] = verbose response = { "items": [request.to_json(verbose=verbose) for request in requests], "meta": pagination_metadata(pagination_query, **query_params), } return flask.jsonify(response)
def get_requests(): """ Retrieve paginated details for requests. :rtype: flask.Response """ # Check if the user is filtering requests by state state = flask.request.args.get("state") # Default verbose flag to False verbose = str_to_bool(flask.request.args.get("verbose", False)) max_per_page = flask.current_app.config["CACHITO_MAX_PER_PAGE"] # The call to `paginate` will inspect the current HTTP request for the # pagination parameters `page` and `per_page`. query = Request.query.order_by(Request.id.desc()) if state: if state not in RequestStateMapping.get_state_names(): states = ", ".join(RequestStateMapping.get_state_names()) raise ValidationError( f"{state} is not a valid request state. Valid states are: {states}" ) state_int = RequestStateMapping.__members__[state].value query = query.join(RequestState, Request.request_state_id == RequestState.id) query = query.filter(RequestState.state == state_int) repo = flask.request.args.get("repo") if repo: query = query.filter(Request.repo == repo) ref = flask.request.args.get("ref") if ref: if not is_request_ref_valid(ref): raise ValidationError(f"{ref} is not a valid ref.") query = query.filter(Request.ref == ref) pkg_managers = flask.request.args.getlist("pkg_manager") if pkg_managers: pkg_manager_ids = [] for name in pkg_managers: if not name: # Ignore if pkg_manager= presents in the querystring continue pkg_manager: PackageManager = PackageManager.get_by_name(name) if pkg_manager is None: raise ValidationError( f"Cachito does not have package manager {name}.") pkg_manager_ids.append(pkg_manager.id) if pkg_manager_ids: query = (query.join(PackageManager, Request.pkg_managers).filter( PackageManager.id.in_(pkg_manager_ids)).group_by( Request.id).having( func.count(PackageManager.id) == len(pkg_manager_ids))) try: per_page = int(flask.request.args.get("per_page", 10)) except ValueError: per_page = 10 pagination_query = query.paginate(per_page=per_page, max_per_page=max_per_page) requests = pagination_query.items query_params = {} if state: query_params["state"] = state if verbose: query_params["verbose"] = verbose response = { "items": [request.to_json(verbose=verbose) for request in requests], "meta": pagination_metadata(pagination_query, **query_params), } return flask.jsonify(response)