Skip to content

Commit

Permalink
OGC API updates and test fixes (#693)
Browse files Browse the repository at this point in the history
* add function annotations

* fix OARec tests

* fix test
  • Loading branch information
tomkralidis authored Jun 5, 2020
1 parent 9991d84 commit 3f82191
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 25 deletions.
21 changes: 11 additions & 10 deletions owslib/ogcapi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand All @@ -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 /
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -102,7 +103,7 @@ def api(self):
LOGGER.error(msg)
raise RuntimeError(msg)

def conformance(self):
def conformance(self) -> dict:
"""
implements /conformance
Expand All @@ -112,7 +113,7 @@ def conformance(self):
path = 'conformance'
return self._request(path)

def collections(self):
def collections(self) -> dict:
"""
implements /collections
Expand All @@ -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}
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
8 changes: 5 additions & 3 deletions owslib/ogcapi/features.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,20 @@
import logging

from owslib.ogcapi import API
from owslib.util import Authentication

LOGGER = logging.getLogger(__name__)


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
Expand All @@ -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}
Expand Down
4 changes: 3 additions & 1 deletion owslib/ogcapi/records.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@
import logging

from owslib.ogcapi.features import Features
from owslib.util import Authentication

LOGGER = logging.getLogger(__name__)


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)
22 changes: 11 additions & 11 deletions tests/test_ogcapi_records_pygeoapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -13,39 +13,39 @@
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

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

0 comments on commit 3f82191

Please sign in to comment.