Commit 0af67255 authored by BODERE's avatar BODERE
Browse files

feat: merge_response

parent 3df25b97
......@@ -9,7 +9,7 @@ import uvicorn
from starlette.requests import Request
from opensearx_ws.config import opensearch_engines
from opensearx_ws.opensearch import OpensearchQueryParameters, merge_response
from opensearx_ws.opensearch import OpensearchQueryParameters, merge_response, OpensearchResponseFormat
app = FastAPI()
......@@ -19,13 +19,19 @@ def conf():
return opensearch_engines
@app.get("/granules")
@app.get("/granules_raw")
async def granule(params: OpensearchQueryParameters = Depends()):
# @see : https://docs.python.org/fr/3/library/asyncio-task.html#running-tasks-concurrently
tasks = [asyncio.create_task(engine.request(params)) for engine in opensearch_engines.values()]
responses = await asyncio.gather(*tasks)
#return merge_response(responses, params)
return responses
return await asyncio.gather(*tasks)
@app.get("/granules{format}")
async def granule(
request: Request,
format: OpensearchResponseFormat = OpensearchResponseFormat.atom,
params: OpensearchQueryParameters = Depends()):
tasks = [asyncio.create_task(engine.request(params)) for engine in opensearch_engines.values()]
return merge_response(request.url, await asyncio.gather(*tasks), params)
@app.get("/")
......@@ -35,6 +41,8 @@ def home(request: Request):
"&timeStart=2014-01-01T00:00:00Z&timeEnd=2020-05-18T23:59:59Z&geoBox=134,-64,135,-63",
"Test granule Nasa": "http://127.0.0.1:8000/granules?datasetId=C1693233348-PODAAC&startPage=0&count=1000"
"&timeStart=2014-01-01T00:00:00Z&timeEnd=2020-05-18T23:59:59Z&geoBox=134,-64,135,-63",
"Raw response": "http://127.0.0.1:8000/granules_raw?datasetId=sca_l2a___&startPage=0&count=1000"
"&timeStart=2014-01-01T00:00:00Z&timeEnd=2020-05-18T23:59:59Z&geoBox=134,-64,135,-63",
"Configuration": "http://127.0.0.1:8000/conf"
}
......
......@@ -10,13 +10,15 @@ from typing import List, Dict, Optional, Tuple
import httpx
from httpx import Response
from pydantic import HttpUrl
from pydantic.main import BaseModel
from starlette.datastructures import URL
class OpensearchResponseFormat(str, Enum):
atom = 'atom'
json = 'json'
html = 'html'
atom = '.atom'
json = '.json'
html = '.html'
class OpensearchQueryParameters(BaseModel):
......@@ -26,7 +28,6 @@ class OpensearchQueryParameters(BaseModel):
timeStart: datetime
timeEnd: datetime = datetime.now()
geoBox: str
format: OpensearchResponseFormat = OpensearchResponseFormat.atom
class OpensearchResponseEntryLink(BaseModel):
......@@ -221,5 +222,26 @@ class NasaOpensearchEngine(OpensearchAtomEngine):
)
def merge_response(responses: List[OpensearchResponse], params: OpensearchQueryParameters) -> OpensearchResponse:
return responses[0]
def merge_response(
query_url: URL,
responses: List[OpensearchResponse],
params: OpensearchQueryParameters) -> OpensearchResponse:
computed_total_results = 0
computed_entries: List[OpensearchResponseEntry] = list()
for response in responses:
if response.header is None:
continue
computed_total_results += response.header.total_results
computed_entries.extend(response.entries)
return OpensearchResponse(
header=OpensearchResponseHeader(
id=str(query_url),
title="opensearx results",
total_results=computed_total_results,
start_index=params.startPage,
items_per_page=params.count
),
entries=computed_entries
)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment