Skip to content

Commit

Permalink
Merge pull request #55 from ecmwf-projects/555-update-post-process-ex…
Browse files Browse the repository at this point in the history
…ecution

Refactorization
  • Loading branch information
mcucchi9 authored Dec 22, 2022
2 parents 7da57f6 + 3554793 commit cf0ade0
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 110 deletions.
30 changes: 15 additions & 15 deletions ogc_api_processes_fastapi/clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import fastapi

from . import responses
from . import models


class BaseClient(abc.ABC):
Expand All @@ -28,7 +28,7 @@ class BaseClient(abc.ABC):
@abc.abstractmethod
def get_processes(
self, limit: Optional[int] = fastapi.Query(None)
) -> responses.ProcessList:
) -> models.ProcessList:
"""Get all available processes.
Called with `GET /processes`.
Expand All @@ -40,15 +40,15 @@ def get_processes(
Returns
-------
responses.ProcessList
models.ProcessList
List of available processes summaries.
"""
...

@abc.abstractmethod
def get_process(
self, process_id: str = fastapi.Path(...)
) -> responses.ProcessDescription:
) -> models.ProcessDescription:
"""Get description of the process identified by `process_id`.
Called with `GET /processes/{process_id}`.
Expand All @@ -60,7 +60,7 @@ def get_process(
Returns
-------
responses.schema["ProcessDescription"]
models.ProcessDescription
Description of the process.
Raises
Expand All @@ -75,7 +75,7 @@ def post_process_execution(
self,
process_id: str = fastapi.Path(...),
execution_content: Dict[str, Any] = fastapi.Body(...),
) -> responses.StatusInfo:
) -> models.StatusInfo:
"""Post request for execution of the process identified by `process_id`.
Called with `POST /processes/{process_id}/execution`.
Expand All @@ -90,7 +90,7 @@ def post_process_execution(
Returns
-------
responses.StatusInfo
models.StatusInfo
Information on the status of the submitted job.
Raises
Expand All @@ -106,7 +106,7 @@ def get_jobs(
processID: Optional[List[str]] = fastapi.Query(None),
status: Optional[List[str]] = fastapi.Query(None),
limit: Optional[int] = fastapi.Query(10, ge=1, le=10000),
) -> responses.JobList:
) -> models.JobList:
"""Get the list of submitted jobs.
Called with `GET /jobs`.
Expand All @@ -127,12 +127,12 @@ def get_jobs(
Returns
-------
responses.JobList
models.JobList
List of jobs.
"""

@abc.abstractmethod
def get_job(self, job_id: str = fastapi.Path(...)) -> responses.StatusInfo:
def get_job(self, job_id: str = fastapi.Path(...)) -> models.StatusInfo:
"""Get status information of the job identified by `job_id`.
Called with `GET /jobs/{job_id}`.
Expand All @@ -144,7 +144,7 @@ def get_job(self, job_id: str = fastapi.Path(...)) -> responses.StatusInfo:
Returns
-------
responses.StatusInfo
models.StatusInfo
Information on the status of the job.
Raises
Expand All @@ -155,7 +155,7 @@ def get_job(self, job_id: str = fastapi.Path(...)) -> responses.StatusInfo:
...

@abc.abstractmethod
def get_job_results(self, job_id: str = fastapi.Path(...)) -> responses.Results:
def get_job_results(self, job_id: str = fastapi.Path(...)) -> models.Results:
"""Get results of the job identified by `job_id`.
Called with `GET /jobs/{job_id}/results`.
Expand All @@ -167,7 +167,7 @@ def get_job_results(self, job_id: str = fastapi.Path(...)) -> responses.Results:
Returns
-------
responses.Results
models.Results
Job results.
Raises
Expand All @@ -182,7 +182,7 @@ def get_job_results(self, job_id: str = fastapi.Path(...)) -> responses.Results:
...

@abc.abstractmethod
def delete_job(self, job_id: str = fastapi.Path(...)) -> responses.StatusInfo:
def delete_job(self, job_id: str = fastapi.Path(...)) -> models.StatusInfo:
"""Cancel the job identified by `job_id`.
Called with `DELETE /jobs/{job_id}`.
Expand All @@ -194,7 +194,7 @@ def delete_job(self, job_id: str = fastapi.Path(...)) -> responses.StatusInfo:
Returns
-------
responses.StatusInfo
models.StatusInfo
Information on the status of the job.
Raises
Expand Down
96 changes: 47 additions & 49 deletions ogc_api_processes_fastapi/endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,22 @@

import fastapi

from . import clients, responses
from . import clients, models


def create_links_to_job(
request: fastapi.Request, job: responses.StatusInfo
) -> List[responses.Link]:
request: fastapi.Request, job: models.StatusInfo
) -> List[models.Link]:
"""Create links to attach to provided the job.
Parameters
----------
job : schema["StatusInfo"]
job : models.StatusInfo
Job to create links for.
Returns
-------
List[responses.Link]
List[models.Link]
Links to attach to job.
"""
rel_job_link = "self"
Expand All @@ -29,7 +29,7 @@ def create_links_to_job(
rel_job_link = "monitor"
title_job_link = "job status info"
links = [
responses.Link(
models.Link(
href=urllib.parse.urljoin(str(request.base_url), f"jobs/{job.jobID}"),
rel=rel_job_link,
type="application/json",
Expand All @@ -38,7 +38,7 @@ def create_links_to_job(
]
if job.status.value in ("successful", "failed"):
links.append(
responses.Link(
models.Link(
href=urllib.parse.urljoin(
str(request.base_url), f"jobs/{job.jobID}/results"
),
Expand All @@ -50,8 +50,8 @@ def create_links_to_job(

def create_self_link(
request_url: str, title: Optional[str] = None, type: Optional[str] = None
) -> responses.Link:
self_link = responses.Link(href=str(request_url), rel="self")
) -> models.Link:
self_link = models.Link(href=str(request_url), rel="self")
if type:
self_link.type = type
if title:
Expand All @@ -60,8 +60,8 @@ def create_self_link(


def create_page_link(
request_url: str, page: str, pagination_qs: responses.PaginationQueryParameters
) -> responses.Link:
request_url: str, page: str, pagination_qs: models.PaginationQueryParameters
) -> models.Link:
if page not in ("next", "prev"):
raise ValueError(f"{page} is not a valid value for ``page`` parameter")
request_parsed = urllib.parse.urlsplit(request_url)
Expand All @@ -72,13 +72,13 @@ def create_page_link(
query_string = urllib.parse.urlencode(queries_page, doseq=True)
parsed_page_request = request_parsed._replace(query=query_string)
url_page = parsed_page_request.geturl()
link_page = responses.Link(href=url_page, rel=page)
link_page = models.Link(href=url_page, rel=page)
return link_page


def create_pagination_links(
request_url: str, pagination_qs: Optional[responses.PaginationQueryParameters]
) -> List[responses.Link]:
request_url: str, pagination_qs: Optional[models.PaginationQueryParameters]
) -> List[models.Link]:
pagination_links = []
if pagination_qs:
if pagination_qs.next:
Expand All @@ -92,32 +92,32 @@ def create_pagination_links(

def create_get_landing_page_endpoint(
client: clients.BaseClient,
) -> Callable[[fastapi.Request], responses.LandingPage]:
) -> Callable[[fastapi.Request], models.LandingPage]:
def get_landing_page(
request: fastapi.Request,
) -> responses.LandingPage:
) -> models.LandingPage:
"""Get the API landing page."""
links = [
responses.Link(
models.Link(
href=urllib.parse.urljoin(str(request.base_url), "openapi.json"),
rel="service-desc",
type="application/vnd.oai.openapi+json;version=3.0",
title="OpenAPI service description",
),
responses.Link(
models.Link(
href=urllib.parse.urljoin(str(request.base_url), "conformance"),
rel="http://www.opengis.net/def/rel/ogc/1.0/conformance",
type="application/json",
title="Conformance declaration",
),
responses.Link(
models.Link(
href=urllib.parse.urljoin(str(request.base_url), "processes"),
rel="http://www.opengis.net/def/rel/ogc/1.0/processes",
type="application/json",
title="Metadata about the processes",
),
]
landing_page = responses.LandingPage(links=links)
landing_page = models.LandingPage(links=links)

return landing_page

Expand All @@ -126,10 +126,10 @@ def get_landing_page(

def create_get_conformance_endpoint(
client: clients.BaseClient,
) -> Callable[[fastapi.Request], responses.ConfClass]:
def get_conformance(request: fastapi.Request) -> responses.ConfClass:
) -> Callable[[fastapi.Request], models.ConfClass]:
def get_conformance(request: fastapi.Request) -> models.ConfClass:
"""Get the API conformance declaration page."""
conformance = responses.ConfClass(
conformance = models.ConfClass(
conformsTo=[
"http://www.opengis.net/spec/ogcapi-processes-1/1.0/conf/core",
"http://www.opengis.net/spec/ogcapi-processes-1/1.0/conf/ogc-process-description",
Expand All @@ -146,11 +146,11 @@ def get_conformance(request: fastapi.Request) -> responses.ConfClass:

def create_get_processes_endpoint(
client: clients.BaseClient,
) -> Callable[[fastapi.Request], responses.ProcessList]:
) -> Callable[[fastapi.Request], models.ProcessList]:
def get_processes(
request: fastapi.Request,
process_list: responses.ProcessList = fastapi.Depends(client.get_processes),
) -> responses.ProcessList:
process_list: models.ProcessList = fastapi.Depends(client.get_processes),
) -> models.ProcessList:
"""Get the list of available processes.
The list of processes contains a summary of each process
Expand All @@ -159,7 +159,7 @@ def get_processes(
"""
for process in process_list.processes:
process.links = [
responses.Link(
models.Link(
href=urllib.parse.urljoin(
str(request.base_url), f"processes/{process.id}"
),
Expand All @@ -184,11 +184,11 @@ def get_processes(

def create_get_process_endpoint(
client: clients.BaseClient,
) -> Callable[[fastapi.Request], responses.ProcessDescription]:
) -> Callable[[fastapi.Request], models.ProcessDescription]:
def get_process(
request: fastapi.Request,
process: responses.ProcessDescription = fastapi.Depends(client.get_process),
) -> responses.ProcessDescription:
process: models.ProcessDescription = fastapi.Depends(client.get_process),
) -> models.ProcessDescription:
"""Get the description of a specific process.
The list of processes contains a summary of each process
Expand All @@ -197,7 +197,7 @@ def get_process(
"""
process.links = [
create_self_link(str(request.url)),
responses.Link(
models.Link(
href=urllib.parse.urljoin(
str(request.base_url), f"processes/{process.id}/execution"
),
Expand All @@ -214,18 +214,16 @@ def get_process(

def create_post_process_execution_endpoint(
client: clients.BaseClient,
) -> Callable[[fastapi.Request, fastapi.Response], responses.StatusInfo]:
) -> Callable[[fastapi.Request, fastapi.Response], models.StatusInfo]:
def post_process_execution(
request: fastapi.Request,
response: fastapi.Response,
status_info: responses.StatusInfo = fastapi.Depends(
client.post_process_execution
),
) -> responses.StatusInfo:
status_info: models.StatusInfo = fastapi.Depends(client.post_process_execution),
) -> models.StatusInfo:
"""Create a new job."""
status_info.links = [
create_self_link(str(request.url)),
responses.Link(
models.Link(
href=urllib.parse.urljoin(
str(request.base_url), f"jobs/{status_info.jobID}"
),
Expand All @@ -245,11 +243,11 @@ def post_process_execution(

def create_get_jobs_endpoint(
client: clients.BaseClient,
) -> Callable[[fastapi.Request], responses.JobList]:
) -> Callable[[fastapi.Request], models.JobList]:
def get_jobs(
request: fastapi.Request,
job_list: responses.JobList = fastapi.Depends(client.get_jobs),
) -> responses.JobList:
job_list: models.JobList = fastapi.Depends(client.get_jobs),
) -> models.JobList:
"""Show the list of submitted jobs."""
for job in job_list.jobs:
job.links = create_links_to_job(job=job, request=request)
Expand All @@ -269,11 +267,11 @@ def get_jobs(

def create_get_job_endpoint(
client: clients.BaseClient,
) -> Callable[[fastapi.Request], responses.StatusInfo]:
) -> Callable[[fastapi.Request], models.StatusInfo]:
def get_job(
request: fastapi.Request,
job: responses.StatusInfo = fastapi.Depends(client.get_job),
) -> responses.StatusInfo:
job: models.StatusInfo = fastapi.Depends(client.get_job),
) -> models.StatusInfo:
"""Show the status of a job."""
job.links = create_links_to_job(job=job, request=request)

Expand All @@ -284,10 +282,10 @@ def get_job(

def create_get_job_results_endpoint(
client: clients.BaseClient,
) -> Callable[[], responses.Results]:
) -> Callable[[], models.Results]:
def get_job_results(
job_results: responses.Results = fastapi.Depends(client.get_job_results),
) -> responses.Results:
job_results: models.Results = fastapi.Depends(client.get_job_results),
) -> models.Results:
"""Show results of a job."""
return job_results

Expand All @@ -296,10 +294,10 @@ def get_job_results(

def create_delete_job_endpoint(
client: clients.BaseClient,
) -> Callable[[], responses.StatusInfo]:
) -> Callable[[], models.StatusInfo]:
def delete_job(
job: responses.StatusInfo = fastapi.Depends(client.delete_job),
) -> responses.StatusInfo:
job: models.StatusInfo = fastapi.Depends(client.delete_job),
) -> models.StatusInfo:
"""Cancel a job."""
return job

Expand Down
Loading

0 comments on commit cf0ade0

Please sign in to comment.