Skip to content
Snippets Groups Projects
Commit ffc7b77e authored by James Graham's avatar James Graham
Browse files

Add small amount of extra code to handle Cisco HyperCat with tests - #5

Will need to be rolled into a single HyperCat class
parent 1a3b653d
No related branches found
No related tags found
No related merge requests found
...@@ -25,5 +25,9 @@ ubuntu-bionic-18.04-cloudimg-console.log ...@@ -25,5 +25,9 @@ ubuntu-bionic-18.04-cloudimg-console.log
/docs/build/ /docs/build/
/docs/source/apidoc/ /docs/source/apidoc/
# Notes
/data/
/notes/
# Experimental # Experimental
/applications/connectors/ /applications/connectors/
...@@ -86,12 +86,38 @@ class HyperCat(DataConnectorContainsDatasets, DataConnectorHasMetadata, BaseData ...@@ -86,12 +86,38 @@ class HyperCat(DataConnectorContainsDatasets, DataConnectorHasMetadata, BaseData
return matches[0] return matches[0]
def _get_response(self, query_params: typing.Optional[typing.Mapping[str, str]] = None): def _get_response(self, query_params: typing.Optional[typing.Mapping[str, str]] = None):
r = requests.get(self.location, params=query_params) # r = requests.get(self.location, params=query_params)
r = self._get_auth_request(self.location,
query_params=query_params)
return r.json() return r.json()
def _get_auth_request(self, url, **kwargs):
return requests.get(url,
auth=requests.auth.HTTPBasicAuth(self.api_key, ''),
**kwargs)
def __enter__(self): def __enter__(self):
self._response = self._get_response() self._response = self._get_response()
return self return self
def __exit__(self, exc_type, exc_val, exc_tb): def __exit__(self, exc_type, exc_val, exc_tb):
pass pass
class HyperCatCisco(HyperCat):
def __init__(self, location: str,
api_key: typing.Optional[str] = None,
entity_url: str = None):
super().__init__(location, api_key=api_key)
self.entity_url = entity_url
def get_entities(self):
r = self._get_auth_request(self.entity_url)
return r.json()
def _get_auth_request(self, url, **kwargs):
return requests.get(url,
# Doesn't accept HttpBasicAuth
headers={'Authorization': self.api_key},
**kwargs)
...@@ -136,3 +136,75 @@ class ConnectorHyperCatTest(TestCase): ...@@ -136,3 +136,75 @@ class ConnectorHyperCatTest(TestCase):
self.assertIsInstance(result, str) self.assertIsInstance(result, str)
self.assertGreaterEqual(len(result), 1) self.assertGreaterEqual(len(result), 1)
class ConnectorHyperCatCiscoTest(TestCase):
url = 'https://api.cityverve.org.uk/v1/cat'
entity_url = 'https://api.cityverve.org.uk/v1/entity'
dataset = 'weather-observations-wind'
def setUp(self):
from decouple import config
BaseDataConnector.load_plugins('datasources/connectors')
self.plugin = BaseDataConnector.get_plugin('HyperCatCisco')
self.api_key = config('HYPERCAT_CISCO_API_KEY')
def test_get_plugin(self):
self.assertIsNotNone(self.plugin)
def test_plugin_init(self):
connection = self.plugin(self.url)
self.assertEqual(connection.location, self.url)
def test_plugin_get_entities(self):
connection = self.plugin(self.url,
api_key=self.api_key,
entity_url=self.entity_url)
result = connection.get_entities()
self.assertGreaterEqual(len(result), 1)
for entity in result:
self.assertIn('id', entity)
self.assertIn('uri', entity)
def test_plugin_get_catalogue_metadata(self):
connection = self.plugin(self.url)
result = connection.get_metadata()
self.assertIn('application/vnd.hypercat.catalogue+json',
result['urn:X-hypercat:rels:isContentType'])
self.assertIn('CityVerve',
result['urn:X-hypercat:rels:hasDescription:en'][0])
self.assertEqual('https://developer.cityverve.org.uk',
result['urn:X-hypercat:rels:hasHomepage'])
def test_plugin_get_dataset_metadata(self):
connection = self.plugin(self.url)
result = connection.get_metadata(dataset=self.dataset)
for property in [
'urn:X-bt:rels:feedTitle',
'urn:X-hypercat:rels:hasDescription:en',
'urn:X-bt:rels:feedTag',
'urn:X-bt:rels:hasSensorStream',
'urn:X-hypercat:rels:isContentType',
]:
self.assertIn(property, result)
self.assertIn('Met Office',
result['urn:X-bt:rels:feedTitle'][0])
self.assertIn('Met Office',
result['urn:X-hypercat:rels:hasDescription:en'][0])
self.assertEqual(len(result['urn:X-bt:rels:feedTag']), 1)
self.assertEqual(result['urn:X-bt:rels:feedTag'][0], 'weather')
self.assertGreaterEqual(len(result['urn:X-bt:rels:hasSensorStream']), 1)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment