Skip to content

Commit

Permalink
update the layout of deriving the endpoints by adding class structure…
Browse files Browse the repository at this point in the history
… to handle what used to be routers
  • Loading branch information
SerRichard committed Feb 22, 2024
1 parent a2f6fa9 commit b709fb4
Show file tree
Hide file tree
Showing 8 changed files with 129 additions and 83 deletions.
56 changes: 29 additions & 27 deletions openeo_fastapi/api/app.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import re

import attr
from attrs import define, field
from fastapi import APIRouter, Response
from starlette.responses import JSONResponse
from starlette.routing import Route

from openeo_fastapi.client import models

HIDDEN_PATHS = ["/openapi.json", "/docs", "/docs/oauth2-redirect", "/redoc"]


@define
class OpenEOApi:
Expand All @@ -15,20 +20,32 @@ class OpenEOApi:
router: APIRouter = attr.ib(default=attr.Factory(APIRouter))
response_class: type[Response] = attr.ib(default=JSONResponse)

def _route_filter(self):
""" """
pass
def register_well_known(self):
"""Register well known page (GET /).
Returns:
None
"""
self.router.add_api_route(
name=".well-known",
path="/.well-known/openeo",
response_model=models.WellKnownOpeneoGetResponse,
response_model_exclude_unset=False,
response_model_exclude_none=True,
methods=["GET"],
endpoint=self.client.get_well_known,
)

def register_get_capabilities(self):
"""Register landing page (GET /).
Returns:
None
"""

self.router.add_api_route(
name="capabilities",
path="/",
path=f"/{self.client.settings.OPENEO_VERSION}" + "/",
response_model=models.Capabilities,
response_model_exclude_unset=False,
response_model_exclude_none=True,
Expand All @@ -43,7 +60,7 @@ def register_get_collections(self):
"""
self.router.add_api_route(
name="collections",
path="/collections",
path=f"/{self.client.settings.OPENEO_VERSION}/collections",
response_model=None,
response_model_exclude_unset=False,
response_model_exclude_none=True,
Expand All @@ -58,7 +75,8 @@ def register_get_collection(self):
"""
self.router.add_api_route(
name="collection",
path="/collections/{collection_id}",
path=f"/{self.client.settings.OPENEO_VERSION}"
+ "/collections/{collection_id}",
response_model=None,
response_model_exclude_unset=False,
response_model_exclude_none=True,
Expand All @@ -73,31 +91,14 @@ def register_get_conformance(self):
"""
self.router.add_api_route(
name="conformance",
path="/conformance",
path=f"/{self.client.settings.OPENEO_VERSION}/conformance",
response_model=models.ConformanceGetResponse,
response_model_exclude_unset=False,
response_model_exclude_none=True,
methods=["GET"],
endpoint=self.client.get_conformance,
)

def register_well_known(self):
"""Register well known page (GET /).
Returns:
None
"""
self.router.add_api_route(
name=".well-known",
path="/.well-known/openeo",
response_model=models.WellKnownOpeneoGetResponse,
response_model_exclude_unset=False,
response_model_exclude_none=True,
methods=["GET"],
endpoint=self.client.get_well_know,
)

def register_get_processes(self):
"""Register Endpoint for Processes (GET /processes).
Expand All @@ -106,7 +107,7 @@ def register_get_processes(self):
"""
self.router.add_api_route(
name="processes",
path="/processes",
path=f"/{self.client.settings.OPENEO_VERSION}/processes",
response_model=None,
response_model_exclude_unset=False,
response_model_exclude_none=True,
Expand All @@ -130,7 +131,6 @@ def register_core(self):
Returns:
None
"""
self.register_get_capabilities()
self.register_get_conformance()
self.register_get_collections()
self.register_get_collection()
Expand All @@ -148,4 +148,6 @@ def __attrs_post_init__(self):

# Register core endpoints
self.register_core()

self.register_get_capabilities()
self.app.include_router(router=self.router)
21 changes: 19 additions & 2 deletions openeo_fastapi/client/collections.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,31 @@
from typing import List

import aiohttp

from openeo_fastapi.client.models import Collection, Collections
from openeo_fastapi.client.models import Collection, Collections, Endpoint
from openeo_fastapi.client.register import EndpointRegister
from openeo_fastapi.client.settings import AppSettings


class CollectionCore:
class CollectionRegister(EndpointRegister):
def __init__(self, settings) -> None:
super().__init__()
self.endpoints = self._initialize_endpoints()
self.settings: AppSettings = settings
pass

def _initialize_endpoints(self) -> list[Endpoint]:
return [
Endpoint(
path="/collections",
methods=["GET"],
),
Endpoint(
path="/collections/{collection_id}",
methods=["GET"],
),
]

async def get_collections(self):
"""
Returns Basic metadata for all datasets
Expand Down
27 changes: 17 additions & 10 deletions openeo_fastapi/client/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
from attrs import define, field

from openeo_fastapi.client import conformance, models
from openeo_fastapi.client.collections import CollectionCore
from openeo_fastapi.client.processes import ProcessCore
from openeo_fastapi.client.collections import CollectionRegister
from openeo_fastapi.client.processes import ProcessRegister
from openeo_fastapi.client.settings import AppSettings


Expand All @@ -15,18 +15,26 @@ class OpenEOCore:
"""Base client for the OpenEO Api."""

billing: str = field()
endpoints: list = field()
links: list = field()

settings: AppSettings = field()

_id: str = field(default="OpenEOApi")

_collections = CollectionCore(settings)
_processes = ProcessCore()
_collections = CollectionRegister(settings)
_processes = ProcessRegister()

@abc.abstractmethod
def get_well_know(self) -> models.WellKnownOpeneoGetResponse:
def _combine_endpoints(self):
"""For the various registers that hold endpoint functions, concat those endpoints to register in get_capabilities."""
registers = [self._collections, self._processes]

endpoints = []
for register in registers:
if register:
endpoints.extend(register.endpoints)
return endpoints

def get_well_known(self) -> models.WellKnownOpeneoGetResponse:
""" """

prefix = "https" if self.settings.API_TLS else "http"
Expand Down Expand Up @@ -56,7 +64,6 @@ def get_well_know(self) -> models.WellKnownOpeneoGetResponse:
]
)

@abc.abstractmethod
def get_capabilities(self) -> models.Capabilities:
""" """
return models.Capabilities(
Expand All @@ -68,7 +75,7 @@ def get_capabilities(self) -> models.Capabilities:
backend_version=self.settings.OPENEO_VERSION,
billing=self.billing,
links=self.links,
endpoints=self.endpoints,
endpoints=self._combine_endpoints(),
)

@abc.abstractclassmethod
Expand All @@ -86,7 +93,7 @@ def get_processes(self) -> dict:
processes = self._processes.list_processes()
return processes

@abc.abstractmethod
@abc.abstractclassmethod
def get_conformance(self) -> models.ConformanceGetResponse:
""" """
return models.ConformanceGetResponse(
Expand Down
6 changes: 2 additions & 4 deletions openeo_fastapi/client/models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import sys
from enum import Enum
from pathlib import Path
Expand Down Expand Up @@ -29,7 +28,6 @@ class Type2(Enum):
other = "other"



class Type5(Enum):
Catalog = "Catalog"

Expand Down Expand Up @@ -232,7 +230,7 @@ class Capabilities(BaseModel):
],
)


class CollectionId(str):
collection_id: constr(regex=rb"^[\w\-\.~\/]+$") = Field(
...,
Expand Down Expand Up @@ -800,7 +798,7 @@ class Error(BaseModel):
)
links: Optional[LogLinks] = None


class ConformanceGetResponse(BaseModel):
conformsTo: list[AnyUrl]

Expand Down
30 changes: 24 additions & 6 deletions openeo_fastapi/client/processes.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,46 @@
import functools
from typing import Union
from typing import List, Union

import openeo_pg_parser_networkx
import openeo_processes_dask.specs
from openeo_pg_parser_networkx import ProcessRegistry

from openeo_fastapi.client.models import Error, Process, ProcessesGetResponse
from openeo_fastapi.client.models import Endpoint, Error, Process, ProcessesGetResponse
from openeo_fastapi.client.register import EndpointRegister


class ProcessCore:
class ProcessRegister(EndpointRegister):
def __init__(self) -> None:
self.process_registry = ProcessRegistry()
super().__init__()
self.endpoints = self._initialize_endpoints()
self.process_registry = self._create_process_registry()
pass

def _initialize_endpoints(self) -> list[Endpoint]:
return [
Endpoint(
path="/processes",
methods=["GET"],
)
]

def _create_process_registry(self):
"""
Returns the process registry based on the predefinied specifications from the openeo_processes_dask module.
"""
process_registry = ProcessRegistry()

predefined_processes_specs = {
process_id: getattr(openeo_processes_dask.specs, process_id)
for process_id in openeo_processes_dask.specs.__all__
}

for process_id, spec in predefined_processes_specs.items():
self.process_registry[
process_registry[
("predefined", process_id)
] = openeo_pg_parser_networkx.Process(spec)

pass
return process_registry

@functools.cache
def get_available_processes(self):
Expand Down
13 changes: 13 additions & 0 deletions openeo_fastapi/client/register.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import abc
from typing import List

from openeo_fastapi.client.models import Endpoint


class EndpointRegister(abc.ABC):
def __init__(self):
self.endpoints = self._initialize_endpoints()

@abc.abstractmethod
def _initialize_endpoints(self) -> list[Endpoint]:
pass
Loading

0 comments on commit b709fb4

Please sign in to comment.