def get_ror_by_query(): """Get ROR results for a specified query.""" args = request.args query = args.get('query') if not query: raise RESTException(description='ROR Query not found.') try: resp, status = _ror(query, by='query') serialized = RORAffiliationsListSchema().dump(resp['items'], many=True) return jsonify(serialized.data), status except ExternalAPIException: raise
def get(self, **kwargs): """Search records. :returns: the search result containing hits and aggregations as returned by invenio-search. """ page = request.values.get('page', 1, type=int) size = request.values.get('size', 10, type=int) if page * size >= self.max_result_window: raise RESTException("Too many results to show!") urlkwargs = dict() search = self.searcher[(page - 1) * size:page * size] search, qs_kwargs = self.search_factory(search) urlkwargs.update(qs_kwargs) current_app.logger.debug(json.dumps(search.to_dict(), indent=4)) # Execute search search_result = search.execute() # Generate links for prev/next urlkwargs.update( size=size, _external=True, ) endpoint = '.{0}'.format(self.view_name) links = dict(self=url_for(endpoint, page=page, **urlkwargs)) if page > 1: links['prev'] = url_for(endpoint, page=page - 1, **urlkwargs) if ES_VERSION >= 7: all_results_count = int(search_result.hits.total.value) else: all_results_count = int(search_result.hits.total) if size * page < all_results_count and \ size * page < self.max_result_window: links['next'] = url_for(endpoint, page=page + 1, **urlkwargs) return self.make_response( search_result=search_result.to_dict(), links=links, )
def get(self, **kwargs): """Search records. :returns: the search result containing hits and aggregations as returned by invenio-search. """ page = request.values.get('page', 1, type=int) size = request.values.get('size', 10, type=int) if page * size >= self.max_result_window: raise RESTException("Too many results to show!") urlkwargs = dict() search = self.searcher[(page - 1) * size:page * size] search, qs_kwargs = self.search_factory(search) urlkwargs.update(qs_kwargs) # Execute search search_result = search.execute() # Execute search # urlkwargs, search_result = self.searcher.search(size=size, page=page) # Generate links for prev/next urlkwargs.update( size=size, _external=True, ) endpoint = '.{0}'.format(self.view_name) links = dict(self=url_for(endpoint, page=page, **urlkwargs)) if page > 1: links['prev'] = url_for(endpoint, page=page - 1, **urlkwargs) if size * page < int(search_result['hits']['total']) and \ size * page < self.max_result_window: links['next'] = url_for(endpoint, page=page + 1, **urlkwargs) return self.make_response( search_result=search_result, links=links, )
@patch('cap.modules.services.views.ror._ror', side_effect=ExternalAPIException()) def test_ror_by_query_returns_exception(mock_ror, app, auth_headers_for_superuser, json_headers): mock_query = {'query': '111'} with app.test_client() as client: resp = client.get('services/ror', data=json.dumps(mock_query), headers=auth_headers_for_superuser + json_headers) assert resp.status_code == 503 assert resp.json['message'] == 'External API replied with an error.' @patch('cap.modules.services.views.ror._ror', side_effect=RESTException(description='ROR Query not found.')) def test_ror_by_query_empty_query(mock_ror, app, auth_headers_for_superuser, json_headers): with app.test_client() as client: resp = client.get('services/ror', data=json.dumps({}), headers=auth_headers_for_superuser + json_headers) assert resp.status_code == 200 assert resp.json['message'] == 'ROR Query not found.' @patch('cap.modules.services.views.ror._ror') def test_ror_by_org(mock_ror, app, auth_headers_for_superuser, json_headers): mock_ror_id = '04b8v1s79' mock_ror.return_value = { 'acronyms': ['TiU'], 'aliases': [], 'country': {'country_code': 'NL', 'country_name': 'Netherlands'},
def test_rest(): raise RESTException(description='error description')