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 collections endpoints #13

Merged
merged 13 commits into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
86 changes: 81 additions & 5 deletions openeo_fastapi/api/app.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@

from typing import Type

import attr
from attrs import Factory, define, field
from fastapi import FastAPI, Response
from fastapi import APIRouter, FastAPI, Response
from starlette.responses import JSONResponse

from openeo_fastapi.client import models

Expand All @@ -10,6 +15,9 @@ class OpenEOApi:

client: field()
app: field(default=Factory(lambda self: FastAPI))
router: APIRouter = attr.ib(default=attr.Factory(APIRouter))
response_class: type[Response] = attr.ib(default=JSONResponse)


def _route_filter(self):
""" """
Expand All @@ -21,7 +29,8 @@ def register_get_capabilities(self):
Returns:
None
"""
self.app.add_api_route(

self.router.add_api_route(
name="capabilities",
path="/",
response_model=models.Capabilities,
Expand All @@ -31,13 +40,43 @@ def register_get_capabilities(self):
endpoint=self.client.get_capabilities,
)


def register_get_collections(self):
"""Register collection Endpoint (GET /collections).
Returns:
None
"""
self.router.add_api_route(
name="collections",
path="/collections",
response_model=None,
response_model_exclude_unset=False,
response_model_exclude_none=True,
methods=["GET"],
endpoint=self.client.get_collections,
)

def register_get_collection(self):
"""Register Endpoint for Individual Collection (GET /collections/{collection_id}).
Returns:
None
"""
self.router.add_api_route(
name="collection",
path="/collections/{collection_id}",
response_model=None,
response_model_exclude_unset=False,
response_model_exclude_none=True,
methods=["GET"],
endpoint=self.client.get_collection,
)

def register_get_conformance(self):
"""Register conformance page (GET /).

Returns:
None
"""
self.app.add_api_route(
self.router.add_api_route(
name="conformance",
path="/",
response_model=models.ConformanceGetResponse,
Expand All @@ -50,10 +89,11 @@ def register_get_conformance(self):
def register_well_known(self):
"""Register well known page (GET /).


Returns:
None
"""
self.app.add_api_route(
self.router.add_api_route(
name=".well-known",
path="/.well-known/openeo",
response_model=models.WellKnownOpeneoGetResponse,
Expand All @@ -62,3 +102,39 @@ def register_well_known(self):
methods=["GET"],
endpoint=self.client.get_well_know,
)

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

GET /
GET /capabilities
GET /collections
GET /collections/{collection_id}
GET /processes
GET /well_known


Injects application logic (OpenEOApi.client) into the API layer.

Returns:
None
"""

self.register_get_capabilities()
self.register_get_collections()
self.register_get_collection()
self.register_well_known()

def __attrs_post_init__(self):
"""Post-init hook.

Responsible for setting up the application upon instantiation of the class.

Returns:
None
"""

# Register core endpoints
self.register_core()
self.app.include_router(router=self.router)

39 changes: 39 additions & 0 deletions openeo_fastapi/client/collections.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import os

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

from openeo_fastapi.client.models import Collection, Collections

router_collections = APIRouter()


async def get_collections():
"""
Basic metadata for all datasets
"""

async with aiohttp.ClientSession() as client:
async with client.get(os.getenv("STAC_API_URL") + "collections") as response:
resp = await response.json()
SerRichard marked this conversation as resolved.
Show resolved Hide resolved
if response.status == 200 and resp.get("collections"):
return Collections(collections=resp["collections"], links=resp["links"])
else:
return resp


async def get_collection(collection_id):
"""
Metadata for specific datasets
"""

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
23 changes: 21 additions & 2 deletions openeo_fastapi/client/core.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
import abc
from collections import namedtuple
from urllib.parse import urlunparse

from attrs import define, field

from openeo_fastapi.client import conformance, models
from openeo_fastapi.client.collections import get_collection, get_collections

from collections import namedtuple
from urllib.parse import urlunparse






@define
class OpenEOCore:
"""Base client for the OpenEO Api."""

# TODO. Improve. Not quite sure about setting these here.

api_dns: str = field()
backend_version: str = field()
billing: str = field()
Expand Down Expand Up @@ -68,9 +75,21 @@ def get_capabilities(self) -> models.Capabilities:
endpoints=self.endpoints,
)


@abc.abstractclassmethod
async def get_collection(self, collection_id) -> models.Collection:
collection = await get_collection(collection_id)
return collection

@abc.abstractclassmethod
async def get_collections(self) -> models.Collections:
collections = await get_collections()
return collections

@abc.abstractmethod
def get_conformance(self) -> models.ConformanceGetResponse:
""" """
return models.ConformanceGetResponse(
conformsTo=conformance.BASIC_CONFORMANCE_CLASSES
)

Loading
Loading