From 3f821919cc66781d69adf0f3a79d7b37fe9c4edf Mon Sep 17 00:00:00 2001 From: Tom Kralidis Date: Fri, 5 Jun 2020 07:38:52 -0400 Subject: [PATCH] OGC API updates and test fixes (#693) * add function annotations * fix OARec tests * fix test --- owslib/ogcapi/__init__.py | 21 +++++++++++---------- owslib/ogcapi/features.py | 8 +++++--- owslib/ogcapi/records.py | 4 +++- tests/test_ogcapi_records_pygeoapi.py | 22 +++++++++++----------- 4 files changed, 30 insertions(+), 25 deletions(-) diff --git a/owslib/ogcapi/__init__.py b/owslib/ogcapi/__init__.py index ce1d1c12b..851008c19 100644 --- a/owslib/ogcapi/__init__.py +++ b/owslib/ogcapi/__init__.py @@ -14,7 +14,7 @@ import yaml from owslib import __version__ -from owslib.util import http_get +from owslib.util import Authentication, http_get LOGGER = logging.getLogger(__name__) @@ -24,9 +24,10 @@ class API(object): - """Abstraction for OGC API Common version 1.0""" + """Abstraction for OGC API - Common version 1.0""" - def __init__(self, url, json_=None, timeout=30, headers=None, auth=None): + def __init__(self, url: str, json_: str = None, timeout: int = 30, + headers: dict = None, auth: Authentication = None): """ Initializer; implements / @@ -62,7 +63,7 @@ def __init__(self, url, json_=None, timeout=30, headers=None, auth=None): response = http_get(url, headers=self.headers, auth=self.auth).json() self.links = response['links'] - def api(self): + def api(self) -> dict: """ implements /api @@ -102,7 +103,7 @@ def api(self): LOGGER.error(msg) raise RuntimeError(msg) - def conformance(self): + def conformance(self) -> dict: """ implements /conformance @@ -112,7 +113,7 @@ def conformance(self): path = 'conformance' return self._request(path) - def collections(self): + def collections(self) -> dict: """ implements /collections @@ -122,7 +123,7 @@ def collections(self): path = 'collections' return self._request(path) - def collection(self, collection_id): + def collection(self, collection_id) -> dict: """ implements /collections/{collectionId} @@ -135,7 +136,7 @@ def collection(self, collection_id): path = 'collections/{}'.format(collection_id) return self._request(path) - def collection_queryables(self, collection_id): + def collection_queryables(self, collection_id) -> dict: """ implements /collections/{collectionId}/queryables @@ -148,7 +149,7 @@ def collection_queryables(self, collection_id): path = 'collections/{}/queryables'.format(collection_id) return self._request(path) - def _build_url(self, path=None): + def _build_url(self, path=None) -> str: """ helper function to build an OGC API URL @@ -170,7 +171,7 @@ def _build_url(self, path=None): return url - def _request(self, path=None, kwargs={}): + def _request(self, path=None, kwargs={}) -> dict: """ helper function for request/response patterns against OGC API endpoints diff --git a/owslib/ogcapi/features.py b/owslib/ogcapi/features.py index 52f686ca2..54075f4b1 100644 --- a/owslib/ogcapi/features.py +++ b/owslib/ogcapi/features.py @@ -9,6 +9,7 @@ import logging from owslib.ogcapi import API +from owslib.util import Authentication LOGGER = logging.getLogger(__name__) @@ -16,11 +17,12 @@ class Features(API): """Abstraction for OGC API - Features""" - def __init__(self, url, json_=None, timeout=30, headers=None, auth=None): + def __init__(self, url: str, json_: str = None, timeout: int = 30, + headers: dict = None, auth: Authentication = None): __doc__ = API.__doc__ # noqa super().__init__(url, json_, timeout, headers, auth) - def collection_items(self, collection_id, **kwargs): + def collection_items(self, collection_id: str, **kwargs: dict) -> dict: """ implements /collection/{collectionId}/items @@ -46,7 +48,7 @@ def collection_items(self, collection_id, **kwargs): path = 'collections/{}/items'.format(collection_id) return self._request(path, kwargs) - def collection_item(self, collection_id, identifier): + def collection_item(self, collection_id: str, identifier: str) -> dict: """ implements /collections/{collectionId}/items/{featureId} diff --git a/owslib/ogcapi/records.py b/owslib/ogcapi/records.py index 6d9c98bee..249cea766 100644 --- a/owslib/ogcapi/records.py +++ b/owslib/ogcapi/records.py @@ -9,6 +9,7 @@ import logging from owslib.ogcapi.features import Features +from owslib.util import Authentication LOGGER = logging.getLogger(__name__) @@ -16,6 +17,7 @@ class Records(Features): """Abstraction for OGC API - Records""" - def __init__(self, url, json_=None, timeout=30, headers=None, auth=None): + def __init__(self, url: str, json_: str = None, timeout: int = 30, + headers: dict = None, auth: Authentication = None): __doc__ = Features.__doc__ # noqa super().__init__(url, json_, timeout, headers, auth) diff --git a/tests/test_ogcapi_records_pygeoapi.py b/tests/test_ogcapi_records_pygeoapi.py index 35170b7f1..1c6f7d891 100644 --- a/tests/test_ogcapi_records_pygeoapi.py +++ b/tests/test_ogcapi_records_pygeoapi.py @@ -4,7 +4,7 @@ from owslib.ogcapi.records import Records -SERVICE_URL = 'http://52.170.144.218:8000' +SERVICE_URL = 'https://dev.api.weather.gc.ca/msc-wis-dcpc' @pytest.mark.online @@ -13,14 +13,14 @@ def test_ogcapi_records_pygeoapi(): w = Records(SERVICE_URL) - assert w.url == 'http://52.170.144.218:8000/' + assert w.url == 'https://dev.api.weather.gc.ca/msc-wis-dcpc/' assert w.url_query_string is None api = w.api() assert api['components']['parameters'] is not None paths = api['paths'] assert paths is not None - assert paths['/collections/msc-wis-dcpc'] is not None + assert paths['/collections/discovery-metadata'] is not None conformance = w.conformance() assert len(conformance['conformsTo']) == 8 @@ -28,24 +28,24 @@ def test_ogcapi_records_pygeoapi(): collections = w.collections() assert len(collections) > 0 - msc_wis_dcpc = w.collection('msc-wis-dcpc') - assert msc_wis_dcpc['id'] == 'msc-wis-dcpc' - assert msc_wis_dcpc['title'] == 'MSC WIS DCPC' - assert msc_wis_dcpc['description'] == 'MSC WIS DCPC' + msc_wis_dcpc = w.collection('discovery-metadata') + assert msc_wis_dcpc['id'] == 'discovery-metadata' + assert msc_wis_dcpc['title'] == 'MSC discovery metadata' + assert msc_wis_dcpc['description'] == 'MSC discovery metadata' - msc_wis_dcpc_queryables = w.collection_queryables('msc-wis-dcpc') + msc_wis_dcpc_queryables = w.collection_queryables('discovery-metadata') assert len(msc_wis_dcpc_queryables['queryables']) == 7 # Minimum of limit param is 1 with pytest.raises(RuntimeError): - msc_wis_dcpc_query = w.collection_items('msc-wis-dcpc', limit=0) + msc_wis_dcpc_query = w.collection_items('discovery-metadata', limit=0) - msc_wis_dcpc_query = w.collection_items('msc-wis-dcpc', limit=1) + msc_wis_dcpc_query = w.collection_items('discovery-metadata', limit=1) assert msc_wis_dcpc_query['numberMatched'] == 178 assert msc_wis_dcpc_query['numberReturned'] == 1 assert len(msc_wis_dcpc_query['features']) == 1 - msc_wis_dcpc_query = w.collection_items('msc-wis-dcpc', q='metar') + msc_wis_dcpc_query = w.collection_items('discovery-metadata', q='metar') assert msc_wis_dcpc_query['numberMatched'] == 2 assert msc_wis_dcpc_query['numberReturned'] == 2 assert len(msc_wis_dcpc_query['features']) == 2