Skip to content

Commit

Permalink
fixup! EP-3352 Open-EO#34 encapsulate service metadata to simplify AP…
Browse files Browse the repository at this point in the history
…I compliance
  • Loading branch information
soxofaan committed Apr 9, 2020
1 parent 4b23e74 commit 3a379e6
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
20 changes: 20 additions & 0 deletions openeo_driver/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,23 @@
from datetime import datetime

from openeo import ImageCollection
from openeo.util import date_to_rfc3339
from openeo_driver.errors import OpenEOApiException, CollectionNotFoundException
from openeo_driver.utils import read_json


class ServiceMetadata(NamedTuple):
# TODO: move this to openeo-python-client?
# TODO: also add user metadata?

# Required fields (no default)
id: str
process: dict # TODO: also encapsulate this "process graph with metadata" struct (instead of free-form dict)?
url: str
type: str
enabled: bool
attributes: dict

# Optional fields (with default)
title: str = None
description: str = None
Expand All @@ -37,6 +42,21 @@ class ServiceMetadata(NamedTuple):
costs: float = None
budget: float = None

def prepare_for_json(self) -> dict:
"""Prepare metadata for JSON serialization"""
d = self._asdict()
d["created"] = date_to_rfc3339(self.created) if self.created else None
return d

@classmethod
def from_dict(cls, d:dict) -> 'ServiceMetadata':
"""Load ServiceMetadata from dict (e.g. parsed JSON dump)."""
created = d.get("created")
if isinstance(created, str):
d = d.copy()
d["created"] = datetime.strptime(created, '%Y-%m-%dT%H:%M:%SZ')
return cls(**d)


class SecondaryServices:
"""
Expand Down
3 changes: 1 addition & 2 deletions openeo_driver/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -591,8 +591,7 @@ def services_post():

def _service_metadata_to_json(metadata: ServiceMetadata, full=True) -> dict:
"""API-version-aware conversion of service metadata to jsonable dict"""
d = metadata._asdict()
d["created"] = date_to_rfc3339(metadata.created) if metadata.created else None
d = metadata.prepare_for_json()
if not full:
d.pop("process")
d.pop("attributes")
Expand Down

0 comments on commit 3a379e6

Please sign in to comment.