def post_request(self): """Make stack analyses POST request.""" logger.info( 'SA Post request with ecosystem: %s manifest: %s path: %s ' 'show_transitive: %s', self.params.ecosystem, self.params.manifest.filename, self.params.file_path, self.params.show_transitive) # Build manifest file info. self._manifest_file_info = { 'filename': self.params.manifest.filename, 'filepath': self.params.file_path, 'content': self.params.manifest.read().decode('utf-8') } logger.debug('manifest_file_info: %s', self._manifest_file_info) # Generate unique request id using UUID, also record timestamp in readable form self._new_request_id = str(uuid.uuid4().hex) date_str = str(datetime.datetime.now()) # Make backbone request deps = self._make_backbone_request() # Finally save results in RDS and upon success return request id. rdbAnalyses = RdbAnalyses(self._new_request_id, date_str, self._manifest_file_info, deps) rdbAnalyses.save_post_request() data = { 'status': 'success', 'submitted_at': date_str, 'id': self._new_request_id } logger.info('%s response: %s', self._new_request_id, data) return data
def stack_analyses_with_request_id(external_request_id): """Handle stack analyses report fetch api.""" start = time.time() logger.debug("[GET] /stack-analyses/%s", external_request_id) # 1. Build response builder with id and RDB object. sa_response_builder = StackAnalysesResponseBuilder( external_request_id, RdbAnalyses(external_request_id)) # 2. If there was no exception raise, means request is ready to be served. try: data = sa_response_builder.get_response() logger.info('%s took %f seconds for [GET] stack-analyses', external_request_id, time.time() - start) return jsonify(data) except SARBRequestInvalidException as e: raise HTTPError(400, e.args[0]) from e except RDBInvalidRequestException as e: raise HTTPError(404, e.args[0]) from e except RDBServerException as e: raise HTTPError(500, e.args[0]) from e except SARBRequestInprogressException as e: # Avoid HTTPError to ignore sentry reporting for Inprogress request. return jsonify({'error': e.args[0]}), 202 except SARBRequestTimeoutException as e: raise HTTPError(408, e.args[0]) from e
def stack_report_url(stack_id: str): """URL redirect for Stack Report UI.""" stack_id: str = html.escape(stack_id) try: stack_request = RdbAnalyses(stack_id).get_request_data() crda_key = stack_request.user_id except (RDBServerException, RDBInvalidRequestException): logger.exception("Invalid Stack ID %s", stack_id) return jsonify({"error": f"Invalid Stack ID {stack_id}"}), 400 if not crda_key: return jsonify({ "error": "User corresponding to given Stack Id doesn't exists. " "Please authenticate yourself and try again." }), 400 path = f"{STACK_REPORT_UI_HOSTNAME}/#/analyze/{stack_id}" query_params = "?interframe=true&api_data=" + json.dumps( { "access_token": "undefined", "route_config": { "api_url": THREESCALE_API_URL, "ver": "v3", "uuid": str(crda_key) }, "user_key": THREESCALE_USER_KEY }) final_path = path + query_params logger.info("Redirected to URL: %s ", final_path) return redirect(final_path, code=302)
def test_save_post_request_success(self, _commit, _execute): """Test success save request.""" rdbAnalyses = RdbAnalyses('dummy_request_id', '', {}, {}) self.assertEqual(rdbAnalyses.save_post_request(), None)
def test_save_post_request_error(self, _execute): """Test error save request that raises exception.""" rdbAnalyses = RdbAnalyses('dummy_request_id', '', {}, {}) with pytest.raises(Exception) as exception: rdbAnalyses.save_post_request() self.assertIs(exception.type, RDBSaveException)
def test_get_recommendation_data(self, _fetch_sa_request): """Test get SA recommendation data.""" rdbAnalyses = RdbAnalyses('dummy_request_id') self.assertEqual(rdbAnalyses.get_recommendation_data(), {})
def test_get_stack_result(self, _fetch_sa_request): """Test get SA stack result.""" rdbAnalyses = RdbAnalyses('dummy_request_id') self.assertEqual(rdbAnalyses.get_stack_result(), {})
def test_get_request_data_error(self, _fetch_sa_request): """Test get SA request data with return as 404 error.""" rdbAnalyses = RdbAnalyses('dummy_request_id') with pytest.raises(Exception) as exception: rdbAnalyses.get_request_data() self.assertIs(exception.type, RDBInvalidRequestException)
def test_get_request_data_success(self, _fetch_sa_request): """Test get SA request data.""" rdbAnalyses = RdbAnalyses('dummy_request_id') self.assertEqual(rdbAnalyses.get_request_data(), {})