Esempio n. 1
0
    async def search(self, request):
        try:
            search_params = json_loads(request.args.get('q', '{}'))
        except (TypeError, ValueError, OverflowError) as exception:
            #current_app.logger.exception(str(exception))
            return json(dict(error_code="PARAM_ERROR",
                             error_message='Unable to decode data'),
                        status=520)

        try:
            for preprocess in self.preprocess['GET_MANY']:
                if asyncio.iscoroutinefunction(preprocess):
                    resp = await preprocess(
                        request=request,
                        search_params=search_params,
                        collection_name=self.collection_name)
                else:
                    resp = preprocess(request=request,
                                      search_params=search_params,
                                      collection_name=self.collection_name)

                if (resp is not None) and isinstance(resp, HTTPResponse):
                    return resp
        except Exception as exception:
            return response_exception(exception)

        result = await self._search(request, search_params)

        if result is None:
            return json(dict(error_code="NOT_FOUND",
                             error_message='No result found'),
                        status=520)

        try:
            headers = {}
            for postprocess in self.postprocess['GET_MANY']:
                if asyncio.iscoroutinefunction(postprocess):
                    resp = await postprocess(
                        request=request,
                        result=result,
                        search_params=search_params,
                        collection_name=self.collection_name,
                        headers=headers)
                else:
                    resp = postprocess(request=request,
                                       result=result,
                                       search_params=search_params,
                                       collection_name=self.collection_name,
                                       headers=headers)

                if (resp is not None) and isinstance(resp, HTTPResponse):
                    return resp
        except Exception as exception:
            return response_exception(exception)

        return json(result, headers=headers, status=200)
Esempio n. 2
0
 async def search(self, request):
     try:
         search_params = json_loads(request.args.get('q', '{}'))
     except (TypeError, ValueError, OverflowError) as exception:
         #current_app.logger.exception(str(exception))
         return json(dict(error_code="PARAM_ERROR", error_message='Unable to decode data'), status=520)
     
     try:
         for preprocess in self.preprocess['GET_MANY']:
             if asyncio.iscoroutinefunction(preprocess):
                 resp = await preprocess(request=request,search_params=search_params, collection_name=self.collection_name)
             else:
                 resp = preprocess(request=request,search_params=search_params, collection_name=self.collection_name)
             
             if (resp is not None) and isinstance(resp, HTTPResponse):
                 return resp
     except Exception as exception:
         return response_exception(exception)
     
     result = await self._search(request, search_params)
     
     if result is None:
         return json(dict(error_code="NOT_FOUND", error_message='No result found'), status=520)
     
     try:
         headers = {}
         for postprocess in self.postprocess['GET_MANY']:
             if asyncio.iscoroutinefunction(postprocess):
                 resp = await postprocess(request=request, result=result, search_params=search_params, collection_name=self.collection_name, headers=headers)
             else:
                 resp = postprocess(request=request, result=result, search_params=search_params, collection_name=self.collection_name, headers=headers)
             
             if (resp is not None) and isinstance(resp, HTTPResponse):
                 return resp
     except Exception as exception:
         return response_exception(exception)
     
     return json(result, headers=headers, status=200)