Skip to content

Commit

Permalink
[WIP] Basic Api and Client classes (#5)
Browse files Browse the repository at this point in the history
* accept reformatting

* accept reformatting

* edit readme

* accept reformatting

* remove unused imports

* accept reformatting

* accept reformatting

---------

Co-authored-by: sean <sean.hoyal@external.eodc.eu>
  • Loading branch information
SerRichard and sean authored Jan 15, 2024
1 parent 590018f commit f9ec761
Show file tree
Hide file tree
Showing 10 changed files with 485 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Included is a vscode dev container which is intended to be used as the developme

2. Once the development environment is ready, run the following commands.
```
# Working from /openeo-fastapi
# From /openeo-fastapi
poetry lock
Expand Down
64 changes: 64 additions & 0 deletions openeo_fastapi/api/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
from attrs import Factory, define, field
from fastapi import FastAPI, Response

from openeo_fastapi.client import models


@define
class OpenEOApi:
"""Factory for creating FastApi applications conformant to the OpenEO Api specification."""

client: field()
app: field(default=Factory(lambda self: FastAPI))

def _route_filter(self):
""" """
pass

def register_get_capabilities(self):
"""Register landing page (GET /).
Returns:
None
"""
self.app.add_api_route(
name="capabilities",
path="/",
response_model=models.Capabilities,
response_model_exclude_unset=False,
response_model_exclude_none=True,
methods=["GET"],
endpoint=self.client.get_capabilities,
)

def register_get_conformance(self):
"""Register conformance page (GET /).
Returns:
None
"""
self.app.add_api_route(
name="conformance",
path="/",
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.app.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,
)
Empty file.
21 changes: 21 additions & 0 deletions openeo_fastapi/client/conformance.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
"""Conformance Classes."""
from enum import Enum


class STACConformanceClasses(str, Enum):
"""STAC Api conformance classes."""

CORE = "https://api.stacspec.org/v1.0.0/core"
COLLECTIONS = "https://api.stacspec.org/v1.0.0/collections"


class OGCConformanceClasses(str, Enum):
"""OGC compliant conformance classes."""

pass


BASIC_CONFORMANCE_CLASSES = [
STACConformanceClasses.CORE,
STACConformanceClasses.COLLECTIONS,
]
76 changes: 76 additions & 0 deletions openeo_fastapi/client/core.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import abc
from collections import namedtuple
from urllib.parse import urlunparse

from attrs import define, field

from openeo_fastapi.client import conformance, models


@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()
endpoints: list = field()
links: list = field()
api_tls: bool = field(default=True)
_id: str = field(default="OpenEOApi")
title: str = field(default="OpenEO FastApi")
description: str = field(default="Implemented from the OpenEO FastAPi package.")
stac_version: str = field(default="1.0.0")
api_version: str = field(default="1.1.0")

@abc.abstractmethod
def get_well_know(self) -> models.WellKnownOpeneoGetResponse:
""" """

prefix = "https" if self.api_tls else "http"

Components = namedtuple(
typename="Components",
field_names=["scheme", "netloc", "url", "path", "query", "fragment"],
)

# TODO Supporting multiple versions should be possible here. But would change how we get the api version.
url = urlunparse(
Components(
scheme=prefix,
netloc=self.api_dns,
query=None,
path="",
url=f"/openeo/{self.api_version}/",
fragment=None,
)
)

return models.WellKnownOpeneoGetResponse(
versions=[
models.Version(url=url, production=False, api_version=self.api_version)
]
)

@abc.abstractmethod
def get_capabilities(self) -> models.Capabilities:
""" """
return models.Capabilities(
id=self._id,
title=self.title,
stac_version=self.stac_version,
api_version=self.api_version,
description=self.description,
backend_version=self.backend_version,
billing=self.billing,
links=self.links,
endpoints=self.endpoints,
)

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

0 comments on commit f9ec761

Please sign in to comment.