Commit ff195c48 authored by BODERE's avatar BODERE
Browse files

fix: fake change

parent ba585d73
Pipeline #9803 passed with stage
in 2 minutes and 19 seconds
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Opensearx webservice
Execute requests on opensearch engines and merge the results.
"""
import asyncio
import os
import uvicorn
from fastapi import Depends, FastAPI
from fastapi.responses import ORJSONResponse
from starlette.requests import Request
from starlette.responses import JSONResponse, Response
from opensearx_ws.config import j2_templates, opensearch_engines, settings
from opensearx_ws.opensearch import merge_opensearch_responses, OpensearchResponse
from opensearx_ws.opensearch.model import (OpensearchQueryParameters,
OpensearchResponseFormat)
app = FastAPI(
root_path=settings.context_path,
title="OpensearX",
description="Execute requests on opensearch engines and merge the results.",
version="0.1.6"
)
@app.get("/engines")
def engines() -> JSONResponse:
"""Display the configuration
Will be removed for production
"""
return JSONResponse({k: v.to_dict() for k, v in opensearch_engines.items()})
@app.get("/granules", response_model=OpensearchResponse)
@app.get("/granules.{response_fmt}", response_model=OpensearchResponse)
async def granules(
request: Request,
response_fmt: OpensearchResponseFormat = OpensearchResponseFormat.atom,
params: OpensearchQueryParameters = Depends()) -> Response:
"""Execute a request on each opensearch engine if the dataset is available.
If the output format is `raw`, then return a list for the raw responses.
Otherwise, the responses are merged into a single one and then is formatted
to the specified format (`atom`, `json`)
"""
# prepare task (i.e. coroutines)
tasks = [asyncio.create_task(engine.request(params)) for engine in opensearch_engines.values()]
# execute taks in parallel and retrieve a list of results
responses = await asyncio.gather(*tasks)
# retrieve engine names as a list (to map with response)
engine_names = list(opensearch_engines.keys())
# if response is `raw` -> return a dict<engine_name, response> as a JSON document
if response_fmt == OpensearchResponseFormat.raw:
return ORJSONResponse({engine_names[idx]: response.dict() for idx, response in enumerate(responses)})
# else, we merge response into a single one
merged_response = merge_opensearch_responses(request.url, responses, params, engine_names)
# if format is `json` -> return the response as a JSON document
if response_fmt == OpensearchResponseFormat.json:
return ORJSONResponse(merged_response.dict())
# if format is `atom` -> return the response as an ATOM document using a jinja2 template
if response_fmt == OpensearchResponseFormat.atom:
return j2_templates.TemplateResponse(
name="granules.atom",
context={"request": request, "response": merged_response},
media_type="application/xml"
)
@app.get("/", include_in_schema=False)
def home(request: Request):
"""Home endpoint
"""
return {
"AVHRR_SST_METOP_A-OSISAF-L2P-v1.0 (atom)":
f"{request.url}granules.atom?datasetId=AVHRR_SST_METOP_A-OSISAF-L2P-v1.0&startPage=0&count=100"
"&timeStart=2000-01-01T00:00:00Z&timeEnd=2021-12-31T23:59:59Z&geoBox=-180.0,-90.0,180.0,90.0",
"AVHRR_SST_METOP_A-OSISAF-L2P-v1.0 (raw)":
f"{request.url}granules.raw?datasetId=AVHRR_SST_METOP_A-OSISAF-L2P-v1.0&startPage=0&count=100"
"&timeStart=2000-01-01T00:00:00Z&timeEnd=2100-12-31T23:59:59Z&geoBox=-180.0,-90.0,180.0,90.0",
"Configuration": f"{request.url}engines",
"Documentation": f"{request.url}docs"
}
if __name__ == "__main__":
uvicorn.run("opensearx_ws.main:app", host="127.0.0.1", port=8000, reload=True)
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Opensearx webservice
Execute requests on opensearch engines and merge the results.
"""
import asyncio
import os
import uvicorn
from fastapi import Depends, FastAPI
from fastapi.responses import ORJSONResponse
from starlette.requests import Request
from starlette.responses import JSONResponse, Response
from opensearx_ws.config import j2_templates, opensearch_engines, settings
from opensearx_ws.opensearch import merge_opensearch_responses, OpensearchResponse
from opensearx_ws.opensearch.model import (OpensearchQueryParameters,
OpensearchResponseFormat)
app = FastAPI(
root_path=settings.context_path,
title="OpensearX",
description="Execute requests on opensearch engines and merge the results.",
version="0.1.6"
)
@app.get("/engines")
def engines() -> JSONResponse:
"""Display the configuration
Will be removed for production
"""
return JSONResponse({k: v.to_dict() for k, v in opensearch_engines.items()})
@app.get("/granules", response_model=OpensearchResponse)
@app.get("/granules.{response_fmt}", response_model=OpensearchResponse)
async def granules(
request: Request,
response_fmt: OpensearchResponseFormat = OpensearchResponseFormat.atom,
params: OpensearchQueryParameters = Depends()) -> Response:
"""Execute a request on each opensearch engine if the dataset is available.
If the output format is `raw`, then return a list for the raw responses.
Otherwise, the responses are merged into a single one and then is formatted
to the specified format (`atom`, `json`)
"""
# prepare task (i.e. coroutines)
tasks = [asyncio.create_task(engine.request(params)) for engine in opensearch_engines.values()]
# execute taks in parallel and retrieve a list of results
responses = await asyncio.gather(*tasks)
# retrieve engine names as a list (to map with response)
engine_names = list(opensearch_engines.keys())
# if response is `raw` -> return a dict<engine_name, response> as a JSON document
if response_fmt == OpensearchResponseFormat.raw:
return ORJSONResponse({engine_names[idx]: response.dict() for idx, response in enumerate(responses)})
# else, we merge response into a single one
merged_response = merge_opensearch_responses(request.url, responses, params, engine_names)
# if format is `json` -> return the response as a JSON document
if response_fmt == OpensearchResponseFormat.json:
return ORJSONResponse(merged_response.dict())
# if format is `atom` -> return the response as an ATOM document using a jinja2 template
if response_fmt == OpensearchResponseFormat.atom:
return j2_templates.TemplateResponse(
name="granules.atom",
context={"request": request, "response": merged_response},
media_type="application/xml"
)
@app.get("/", include_in_schema=False)
def home(request: Request):
"""Home endpoint
"""
return {
"AVHRR_SST_METOP_A-OSISAF-L2P-v1.0 (atom)":
f"{request.url}granules.atom?datasetId=AVHRR_SST_METOP_A-OSISAF-L2P-v1.0&startPage=0&count=100"
"&timeStart=2000-01-01T00:00:00Z&timeEnd=2021-12-31T23:59:59Z&geoBox=-180.0,-90.0,180.0,90.0",
"AVHRR_SST_METOP_A-OSISAF-L2P-v1.0 (raw)":
f"{request.url}granules.raw?datasetId=AVHRR_SST_METOP_A-OSISAF-L2P-v1.0&startPage=0&count=100"
"&timeStart=2000-01-01T00:00:00Z&timeEnd=2100-12-31T23:59:59Z&geoBox=-180.0,-90.0,180.0,90.0",
"Configuration": f"{request.url}engines",
"Documentation": f"{request.url}docs"
}
if __name__ == "__main__":
uvicorn.run("opensearx_ws.main:app", host="127.0.0.1", port=8000, reload=True)
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