From d10fa1b84ddaff0f87628bd4088b27920607a273 Mon Sep 17 00:00:00 2001 From: pcain-usgs <pcain@usgs.gov> Date: Tue, 16 Feb 2021 11:16:40 -0700 Subject: [PATCH] factory entrypoint for requests --- geomagio/geomagioapi/__init__.py | 3 + geomagio/geomagioapi/metadata.py | 54 ++++++++++++++++ geomagio/metadata/MetadataFactory.py | 93 ++++++++++++---------------- geomagio/processing/__init__.py | 2 +- setup.py | 1 + 5 files changed, 99 insertions(+), 54 deletions(-) create mode 100644 geomagio/geomagioapi/__init__.py create mode 100644 geomagio/geomagioapi/metadata.py diff --git a/geomagio/geomagioapi/__init__.py b/geomagio/geomagioapi/__init__.py new file mode 100644 index 000000000..20b3da7f7 --- /dev/null +++ b/geomagio/geomagioapi/__init__.py @@ -0,0 +1,3 @@ +from .metadata import client + +__all__ = ["client"] diff --git a/geomagio/geomagioapi/metadata.py b/geomagio/geomagioapi/metadata.py new file mode 100644 index 000000000..9d56037c7 --- /dev/null +++ b/geomagio/geomagioapi/metadata.py @@ -0,0 +1,54 @@ +import os +from typing import Literal, Optional + +from obspy import UTCDateTime +import typer + +from ..api.secure.MetadataQuery import MetadataQuery +from ..metadata import MetadataCategory, MetadataFactory + + +def main(): + typer.run(client) + + +def client( + action: str, + url: str = "http://{}/ws/secure/metadata".format( + os.getenv("EDGE_HOST", "127.0.0.1:8000") + ), + category: Optional[MetadataCategory] = None, + starttime: Optional[str] = None, + endtime: Optional[str] = None, + created_after: Optional[str] = None, + created_before: Optional[str] = None, + network: Optional[str] = None, + station: Optional[str] = None, + channel: Optional[str] = None, + location: Optional[str] = None, + data_valid: Optional[bool] = None, + metadata_valid: Optional[bool] = True, +): + query = MetadataQuery( + category=category, + starttime=UTCDateTime(starttime) if starttime else None, + endtime=UTCDateTime(endtime) if endtime else None, + created_after=UTCDateTime(created_after) if created_after else None, + created_before=UTCDateTime(created_before) if created_before else None, + network=network, + station=station, + channel=channel, + location=location, + data_valid=data_valid, + metadata_valid=metadata_valid, + ) + factory = MetadataFactory(url=url) + if action == "delete": + factory.delete_metadata(query=query) + elif action == "get": + metadata = factory.get_metadata(query=query) + if action == "post": + factory.post_metadata(query=query) + if action == "update": + factory.update_metadata(query=query) + return metadata diff --git a/geomagio/metadata/MetadataFactory.py b/geomagio/metadata/MetadataFactory.py index 31578aa0c..dc3fefe09 100644 --- a/geomagio/metadata/MetadataFactory.py +++ b/geomagio/metadata/MetadataFactory.py @@ -7,75 +7,62 @@ from typing import Dict, List, Optional from obspy import UTCDateTime from pydantic import parse_obj_as +from ..api.secure import MetadataQuery from ..residual import Reading from .Metadata import Metadata from .MetadataCategory import MetadataCategory -HOST = os.getenv("EDGE_HOST", "127.0.0.1:8000") class MetadataFactory(object): - def __init__(self, url:str = None): - self.url = url or f"http://{HOST}/ws/secure/metadata" - - def get_metadata( + def __init__( self, - category: Optional[MetadataCategory] = None, - starttime: Optional[UTCDateTime] = None, - endtime: Optional[UTCDateTime] = None, - created_after: Optional[UTCDateTime] = None, - created_before: Optional[UTCDateTime] = None, - network: Optional[str] = None, - station: Optional[str] = None, - channel: Optional[str] = None, - location: Optional[str] = None, - data_valid: Optional[bool] = None, - metadata_valid: Optional[bool] = True, - # returns interior base models from metadata.metadata - return_objects: str = False, - ) -> List[Metadata]: - args = parse_params(params=locals()) + url: str = "http://{}/ws/secure/metadata".format( + os.getenv("EDGE_HOST", "127.0.0.1:8000") + ), + ): + self.url = url + + def delete_metadata(self, query: MetadataQuery): + raise NotImplementedError + + def format_metadata(self, data: Dict): + # formats responses as Metadata objects + return parse_obj_as(List[Metadata], data) + + def get_metadata(self, query: MetadataQuery) -> List[Metadata]: + args = parse_params(query=query) response = web_request(url=f"{self.url}?{args}") - metadata = self.format_metadata(data=response, return_objects=return_objects) + metadata = self.format_metadata(data=response) return metadata - def put_metadata(self): + def post_metadata(self, query: MetadataQuery): raise NotImplementedError - def format_metadata(self, data: Dict, return_objects: bool = False): - # formats responses as Metadata objects - data = parse_obj_as(List[Metadata], data) - if not return_objects: - return data - formatted_metadata = [] - for metadata in data: - category = metadata.category - if category == MetadataCategory.ADJUSTED_MATRIX: - raise NotImplementedError - elif category == MetadataCategory.FLAG: - raise NotImplementedError - elif category == MetadataCategory.READING: - formatted_metadata.append(parse_obj_as(Reading, metadata.metadata)) - # observatory and instrument metadata do not have a confining object other than metadata - return formatted_metadata + def update_metadata(self, query: MetadataQuery): + raise NotImplementedError -def web_request(url:str) -> Dict: - client_id=os.getenv("OPENID_CLIENT_ID") - client_secret=os.getenv("OPENID_CLIENT_SECRET") - response = requests.get(url, data={'grant_type' : 'client_credentials'}, auth = (client_id, client_secret)) +def web_request(url: str) -> Dict: + client_id = os.getenv("OPENID_CLIENT_ID") + client_secret = os.getenv("OPENID_CLIENT_SECRET") + response = requests.get( + url, data={"grant_type": "client_credentials"}, auth=(client_id, client_secret) + ) metadata = json.loads(response.text) return metadata -def parse_params(params: Dict): + +def parse_params(query: MetadataQuery): + d = query.dict() data = {} - for param in params.keys(): - if params[param] is not None and param != "self": - p = params[param] - # convert times to strings - if type(p) == UTCDateTime: - p=p.isoformat() - data[param] = p + for key in d.keys(): + if d[key] is None: + continue + # convert times to strings + if type(d[key]) == UTCDateTime: + d[key] = d[key].isoformat() + if key == "category": + d[key] = d[key].value + data[key] = d[key] - return urllib.parse.urlencode( - data - ) \ No newline at end of file + return urllib.parse.urlencode(data) diff --git a/geomagio/processing/__init__.py b/geomagio/processing/__init__.py index b17686a9e..be97cfbd5 100644 --- a/geomagio/processing/__init__.py +++ b/geomagio/processing/__init__.py @@ -17,7 +17,7 @@ __all__ = [ "obsrio_minute", "obsrio_second", "obsrio_temperatures", - "obsrid_tenhertz", + "obsrio_tenhertz", "rotate", "sqdist_minute", ] diff --git a/setup.py b/setup.py index 6216d647e..8009d4f78 100644 --- a/setup.py +++ b/setup.py @@ -28,6 +28,7 @@ setuptools.setup( "magproc-prepfiles=geomagio.processing.magproc:main", "generate-matrix=geomagio.processing.adjusted:main", "obsrio-filter=geomagio.processing.obsrio:main", + "metadata-client=geomagio.geomagioapi.metadata:main", ], }, ) -- GitLab