Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add processes endpoint #14

Merged
merged 10 commits into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .devcontainer/docker-compose.yml
geospatial-roman marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ version: "3.8"
services:
workspace:
container_name: openeo-fastapi-devcontainer
env_file:
.env

build:
context: ../
dockerfile: ".devcontainer/Dockerfile"
Expand Down
19 changes: 17 additions & 2 deletions openeo_fastapi/api/app.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from typing import Type

import attr
from attrs import Factory, define, field
from fastapi import APIRouter, FastAPI, Response
Expand Down Expand Up @@ -69,6 +67,22 @@ def register_get_collection(self):
endpoint=self.client.get_collection,
)

def register_get_processes(self):
"""Register Endpoint for Processes (GET /processes).

Returns:
None
"""
self.router.add_api_route(
name="processes",
path="/processes",
response_model=None,
response_model_exclude_unset=False,
response_model_exclude_none=True,
methods=["GET"],
endpoint=self.client.get_processes,
)

def register_core(self):
"""Register core OpenEO endpoints.

Expand All @@ -88,6 +102,7 @@ def register_core(self):
self.register_get_capabilities()
self.register_get_collections()
self.register_get_collection()
self.register_get_processes()

def __attrs_post_init__(self):
"""Post-init hook.
Expand Down
70 changes: 51 additions & 19 deletions openeo_fastapi/client/collections.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import os

import aiohttp
from fastapi import APIRouter
from starlette.responses import JSONResponse

from openeo_fastapi.client.models import Collection, Collections
from openeo_fastapi.client.settings import app_settings

router_collections = APIRouter()

Expand All @@ -13,27 +11,61 @@ async def get_collections():
"""
Basic metadata for all datasets
"""
stac_url = (
app_settings.STAC_API_URL
if app_settings.STAC_API_URL.endswith("/")
else app_settings.STAC_API_URL + "/"
)

try:
async with aiohttp.ClientSession() as client:
async with client.get(stac_url + "collections") as response:
resp = await response.json()
if response.status == 200 and resp.get("collections"):
collections_list = []
for collection_json in resp["collections"]:
# For the collections from STAC, only let them through if they're on the whitelist
# This has to be before the legacy collections are added.
geospatial-roman marked this conversation as resolved.
Show resolved Hide resolved
if (
geospatial-roman marked this conversation as resolved.
Show resolved Hide resolved
len(app_settings.STAC_COLLECTIONS_WHITELIST) < 1
or collection_json["id"]
geospatial-roman marked this conversation as resolved.
Show resolved Hide resolved
in app_settings.STAC_COLLECTIONS_WHITELIST
):
collections_list.append(collection_json)

async with aiohttp.ClientSession() as client:
async with client.get(os.getenv("STAC_API_URL") + "collections") as response:
resp = await response.json()
if response.status == 200 and resp.get("collections"):
return Collections(collections=resp["collections"], links=resp["links"])
else:
return resp
return Collections(
collections=collections_list, links=resp["links"]
)
else:
return {"Error": "No Collections found."}
except Exception as e:
raise Exception("Ran into: ", e)


async def get_collection(collection_id):
"""
Metadata for specific datasets
"""
stac_url = (
app_settings.STAC_API_URL
if app_settings.STAC_API_URL.endswith("/")
else app_settings.STAC_API_URL + "/"
)

try:
async with aiohttp.ClientSession() as client:
async with client.get(
stac_url + f"collections/{collection_id}"
) as response:
resp = await response.json()
if response.status == 200 and resp.get("id"):
if (
len(app_settings.STAC_COLLECTIONS_WHITELIST) < 1
or resp["id"] in app_settings.STAC_COLLECTIONS_WHITELIST
geospatial-roman marked this conversation as resolved.
Show resolved Hide resolved
):
return Collection(**resp)
else:
return {"Error": "Collection not found."}

async with aiohttp.ClientSession() as client:
async with client.get(
os.getenv("STAC_API_URL") + f"collections/{collection_id}"
) as response:
resp = await response.json()
if response.status == 200 and resp.get("id"):
return Collection(**resp)
else:
return resp
except Exception as e:
raise Exception("Ran into: ", e)
6 changes: 6 additions & 0 deletions openeo_fastapi/client/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from openeo_fastapi.client import models
from openeo_fastapi.client.collections import get_collection, get_collections
from openeo_fastapi.client.processes import list_processes


@define
Expand Down Expand Up @@ -45,3 +46,8 @@ async def get_collection(self, collection_id) -> models.Collection:
async def get_collections(self) -> models.Collections:
collections = await get_collections()
return collections

@abc.abstractclassmethod
def get_processes(self) -> dict:
processes = list_processes()
return processes
Loading
Loading