-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: sean <sean.hoyal@external.eodc.eu>
- Loading branch information
1 parent
bb7caa5
commit 43df0a6
Showing
12 changed files
with
278 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
## To be defined |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
## Editing the registers. | ||
|
||
The endpoint registers are a loose abstraction defining what endpoints a register is responsible for, and also what code is expected to be executed for those endpoints. | ||
|
||
The available registers can be imported as follows. | ||
|
||
from openeo_fastapi.client.jobs import JobsRegister | ||
from openeo_fastapi.client.files import FilesRegister | ||
from openeo_fastapi.client.processes import ProcessesRegister | ||
from openeo_fastapi.client.collections import CollectionsRegister | ||
|
||
You will need to know how you are expected to overwrite the functionality of these registers, as the openeo-fastapi package does not define functionality for all available endpoints. | ||
|
||
## How to overwrite an endpoint. | ||
|
||
In the following example, we import the FileRegister, and overwrite the functionality for the list_files method. This will define a new response for that endpoint. This is the framework for editing the functionality of the api endpoints. | ||
|
||
from openeo_fastapi.client.files import FilesRegister | ||
|
||
class OverwrittenFileRegister(FilesRegister): | ||
def __init__(self, settings, links) -> None: | ||
super().__init__(settings, links) | ||
|
||
def list_files( | ||
self, | ||
limit: Optional[int] = 10 | ||
): | ||
""" """ | ||
return FilesGetResponse( | ||
files=[File(path="/somefile.txt", size=10)], | ||
links=[ | ||
Link( | ||
href="https://eodc.eu/", | ||
rel="about", | ||
type="text/html", | ||
title="Homepage of the service provider", | ||
) | ||
], | ||
) | ||
|
||
In order to use this overwritten file register, we instantiate the register, and parse this to the OpenEOCore object. Now, when the api is next deployed, we will return the FilesGetResponse, and not the default HTTPException. | ||
|
||
extended_register = OverwrittenFileRegister(app_settings, test_links) | ||
|
||
client = OpenEOCore( | ||
... | ||
files=extended_register, | ||
... | ||
) | ||
|
||
api = OpenEOApi(client=client, app=FastAPI()) | ||
|
||
## How to add an endpoint | ||
|
||
The registers can also be extended to include extra functionality. In order to do this, we again need to define a new child class for the register you want to extend. | ||
|
||
We can define a new endpoint for the api as follows. | ||
|
||
from openeo_fastapi.api.types import Endpoint | ||
new_endpoint = Endpoint( | ||
path="/files/{path}", | ||
methods=["HEAD"], | ||
) | ||
|
||
When defining the child register, the *_initialize_endpoints* method needs to be redefined to add the new_endpoint object. The new functionality can be defined under a new function, here *get_file_headers*. | ||
|
||
from openeo_fastapi.client.files import FilesRegister, FILE_ENDPOINTS | ||
|
||
class ExtendedFileRegister(FilesRegister): | ||
def __init__(self, settings, links) -> None: | ||
super().__init__(settings, links) | ||
self.endpoints = self._initialize_endpoints() | ||
|
||
def _initialize_endpoints(self) -> list[Endpoint]: | ||
endpoints = list(FILE_ENDPOINTS) | ||
endpoints.append(new_endpoint) | ||
return endpoints | ||
|
||
def get_file_headers( | ||
self, path: str, user: User = Depends(Authenticator.validate) | ||
): | ||
""" """ | ||
return Response( | ||
status_code=200, | ||
headers={ | ||
"Accept-Ranges": "bytes", | ||
}, | ||
) | ||
|
||
client = OpenEOCore( | ||
... | ||
files=extended_register, | ||
... | ||
) | ||
|
||
api = OpenEOApi(client=client, app=FastAPI()) | ||
|
||
So far, we have made the new code available to the api, but the api does not know how or when to execute the new code. The following snippet will register the new path to the api server, along with the functionality. | ||
|
||
api.app.router.add_api_route( | ||
name="file_headers", | ||
path=f"/{api.client.settings.OPENEO_VERSION}/files" + "/{path}", | ||
response_model=None, | ||
response_model_exclude_unset=False, | ||
response_model_exclude_none=True, | ||
methods=["HEAD"], | ||
endpoint=api.client.files.get_file_headers, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,99 @@ | ||
# Getting Started | ||
|
||
Everything on this page is all you need to go from 0 to having your OpenEO Api server running locally. | ||
|
||
## Environment Setup | ||
|
||
The openeo-fastapi package comes with a couple of assumptions. The first assumption, is the package is used to build a server side application. The second assumption is that the user is able to provide a connection to a psql database. The final assumption, is the user has provided the set of required environment variables which are needed for the application to run. | ||
|
||
The following steps will explain how the user can get the application skeleton to run. | ||
|
||
## Installation | ||
|
||
pip install openeo-fastapi | ||
Prior to installation, configure and activate a virtual environment for your new project, I can recommend using [poetry](https://python-poetry.org/docs/basic-usage/) for this. Once the environment is activate continue with installing openeo-fastapi. | ||
|
||
|
||
pip install openeo-fastapi | ||
|
||
## Command line interface | ||
|
||
The openeo-fastapi CLI can be used to set up a quick source directory for your deployment. | ||
|
||
openeo_fastapi new /path/to/source/directory | ||
|
||
The command line interface will create the following directory tree. | ||
|
||
src/ | ||
__init__.py | ||
app.py | ||
revise.py | ||
/psql | ||
alembic.ini | ||
models.py | ||
alembic/ | ||
env.py | ||
README | ||
script.py.mako | ||
versions/ | ||
|
||
|
||
The **app.py** file defines the minimal code to define the FastApi application to deploy the API. | ||
|
||
The **revise.py** file defines steps that can be used to generate and apply | ||
alembic revisions to the database deployment. This file is intended to be used after a new release of your deployment, but before your release has been deployed, i.e. as part of an initialization container. This file is optional and can be deleted if you want to complete these steps in a different way. | ||
|
||
The **/psql** directory is used for housing the alembic directory to record the alembic revision history and connectivity to the database. All files ( apart from models.py ) are generated from the alembic init command, so refer to the [alembic docs](https://alembic.sqlalchemy.org/en/latest/) for more information on those files. | ||
|
||
The **/psql/models.py** file defines the basis for importing the orm models from the OpenEO FastApi and defining the metadata class that can then be imported and used in the alembic *env.py* file. | ||
|
||
The **/psql/alembic/env.py** file needs a couple of edits. | ||
|
||
Set the main option for the psql connection. | ||
|
||
config.set_main_option( | ||
"sqlalchemy.url", | ||
f"postgresql://{environ.get('POSTGRES_USER')}:{environ.get('POSTGRES_PASSWORD')}" | ||
f"@{environ.get('POSTGRESQL_HOST')}:{environ.get('POSTGRESQL_PORT')}" | ||
f"/{environ.get('POSTGRES_DB')}", | ||
) | ||
|
||
Set the target metadata. In this example, I am importing from the **/psql/models.py** file. | ||
|
||
from openeo_api.psql.models import metadata | ||
target_metadata = metadata | ||
|
||
|
||
## Set the environment variables | ||
|
||
These variables need to be set in the environment of the deployment. Those marked required need to be set, and those set False, have some default value that only needs to be provided | ||
|
||
| Variable | Description | Required | | ||
| -------- | ------- | ------- | | ||
| API_DNS | The domain name hosting the API. | True | | ||
| API_TLS | Whether the API http scheme should be http or https. | True | | ||
| API_TITLE | The API title to be provided to FastAPI. | True | | ||
| API_DESCRIPTION | The API description to be provided to FastAPI. | True | | ||
| OPENEO_VERSION | The OpenEO Api specification version supported in this deployment of the API. Defaults to "1.1.0". | False | | ||
| OPENEO_PREFIX | The OpenEO prefix to be used when creating the endpoint urls. Defaults to the value of the openeo_version | True | | ||
| OIDC_URL | The URL of the OIDC provider used to authenticate tokens against. | True | | ||
| OIDC_ORGANISATION | The abbreviation of the OIDC provider's organisation name. | True | | ||
| OIDC_ROLES | The OIDC roles to check against when authenticating a user. | False | | ||
| STAC_VERSION | The STAC Version that is being supported by this deployments data discovery endpoints. Defaults to "1.0.0". | False | | ||
| STAC_API_URL | The STAC URL of the catalogue that the application deployment will proxy to. | True | | ||
| STAC_COLLECTIONS_WHITELIST | The collection ids to filter by when proxying to the Stac catalogue. | False | | ||
| POSTGRES_USER | The name of the postgres user. | True | | ||
| POSTGRES_PASSWORD | The pasword for the postgres user. | True | | ||
| POSTGRESQL_HOST | The host the database runs on. | True | | ||
| POSTGRESQL_PORT | The post on the host the database is available on. | True | | ||
| POSTGRES_DB | The name of the databse being used on the host. | True | | ||
| ALEMBIC_DIR | The path to the alembic directory for applying revisions. | True | | ||
|
||
## Deploy the application. | ||
|
||
1. Revise the database. | ||
|
||
python -m revise.py | ||
|
||
2. Deploy the uvicorn server | ||
|
||
uvicorn openeo_app.main:app --reload |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.