def setUp(self): # avoid cluttered console output (for instance logging all the http requests) logging.disable(logging.WARNING) self.response = self.client.get(self.endpoint) content = json.loads(self.response.content.decode('utf8')) self.total = content['collection'].pop('total', None) # remove the non-standard 'total' property self.collection = Collection.from_json(json.dumps(content))
def test_get(self): test_url = self.test_url + "/devices/7324ef5a-b1a6-4ff4-9d97-61b4dde35924" test_response = get(test_url) collection = Collection.from_json(test_response.text) print collection.version print collection.href print [data.name for data in collection.items[0].data]
def test_get_template(self): test_url = self.test_url + "/readings/temperature/template/7324ef5a-b1a6-4ff4-9d97-61b4dde35924" test_response = get(test_url) collection = Collection.from_json(test_response.text) print collection.version print collection.href data = collection.template.data print[item.name for item in data]
def _get_device_links(self): if self._device_settings.device_url is not None: get_url = self._device_settings.device_url link_response = get(url=get_url) if link_response.status_code == 200: collection = Collection.from_json(link_response.text) device_id = [data.value for data in collection.items[0].data if data.name == 'device_id'][0] if device_id is not None: self._device_settings.device_id = device_id for link in collection.links: if link.rel == 'latest': self._device_settings.latest_url = link.href else: resp = get(url=link.href) if resp.status_code == 200: link_coll = Collection.from_json(resp.text) self._device_settings.process_measurement_template(link_coll, link.rel)
def test_get_template(self): test_url = self.test_url + "/readings/temperature/template/7324ef5a-b1a6-4ff4-9d97-61b4dde35924" test_response = get(test_url) collection = Collection.from_json(test_response.text) print collection.version print collection.href data = collection.template.data print [item.name for item in data]
def test_get_by_date_device(self): test_url = self.test_url + "/devices/0c89fded-76ea-4890-a8a1-25c25b24986d/2013-06-13T13:49:23-04:00" test_response = get(test_url) collection = Collection.from_json(test_response.text) self.assertIsNotNone(collection.items, """ Api_ClientTestCase::test_get_by_date_device: Expected '{expected}' got '{returned}' value instead """.format(expected='values', returned='None'))
def test_get_by_date_device(self): test_url = self.test_url + "/devices/0c89fded-76ea-4890-a8a1-25c25b24986d/2013-06-13T13:49:23-04:00" test_response = get(test_url) collection = Collection.from_json(test_response.text) self.assertIsNotNone( collection.items, """ Api_ClientTestCase::test_get_by_date_device: Expected '{expected}' got '{returned}' value instead """.format(expected='values', returned='None'))
def test_from_json_minimal(self): collection = Collection.from_json( '{"collection": {"href": "http://example.org"}}') self.assertEqual(collection.version, '1.0') self.assertEqual(collection.href, 'http://example.org') self.assertEqual(collection.links, Array(Link, 'links', [])) self.assertEqual(collection.items, Array(Item, 'items', [])) self.assertEqual(collection.queries, Array(Query, 'queries', [])) self.assertEqual(collection.template, None) self.assertEqual(collection.error, None)
def _get_collection_from_response(response): """ Internal method to get the collection object from a response object. """ content = json.loads(response.text) total = content['collection'].pop('total', None) collection = Collection.from_json(json.dumps(content)) if collection.error: raise StoreRequestException(collection.error.message) if total is not None: collection.total = total return collection
def test_get(self): test_url = self.test_url + "/devices/7324ef5a-b1a6-4ff4-9d97-61b4dde35924" test_response = get(test_url) collection = Collection.from_json(test_response.text) print collection.version print collection.href print[data.name for data in collection.items[0].data] device_id = [ data.value for data in collection.items[0].data if data.name == 'device_id' ] print "DEVICE: ", device_id
def _get_device_links(self): if self._device_settings.device_url is not None: get_url = self._device_settings.device_url link_response = get(url=get_url) if link_response.status_code == 200: collection = Collection.from_json(link_response.text) device_id = [ data.value for data in collection.items[0].data if data.name == 'device_id' ][0] if device_id is not None: self._device_settings.device_id = device_id for link in collection.links: if link.rel == 'latest': self._device_settings.latest_url = link.href else: resp = get(url=link.href) if resp.status_code == 200: link_coll = Collection.from_json(resp.text) self._device_settings.process_measurement_template( link_coll, link.rel)
def test_from_json_with_template_data(self): data = json.dumps({ 'collection': { 'version': '1.0', 'href': 'http://example.com', 'template': { 'data': [ {'name': 'name', 'value': 'value', 'prompt': 'prompt'} ] } } }) collection = Collection.from_json(data) self.assertEqual(collection.template, Template([Data('name', 'value', 'prompt')]))
def test_from_json_with_error_data(self): data = json.dumps({ 'collection': { 'version': '1.0', 'href': 'http://example.org', 'error': { 'code': 'code', 'message': 'message', 'title': 'title', } } }) collection = Collection.from_json(data) self.assertEqual(collection.error, Error('code', 'message', 'title'))
def test_from_json_with_links_data(self): data = json.dumps({ 'collection': { 'version': '1.0', 'href': 'http://example.com', 'links': [ { 'rel': 'rel', 'href': 'href', } ], } }) collection = Collection.from_json(data) link = Link('href', 'rel') self.assertEqual(collection.links, Array(Link, 'links', [link]))
def _get(self, url, params=None): """ Internal method to make a GET request to the ChRIS server. """ try: r = requests.get(url, params=params, auth=(self.username, self.password), timeout=self.timeout) except (requests.exceptions.Timeout, requests.exceptions.RequestException) as e: raise ChrisRequestException(str(e)) collection = Collection.from_json(r.text) if collection.error: raise ChrisRequestException(collection.error.message) return collection.items
def test_from_json_with_queries_data(self): data = json.dumps({ 'collection': { 'version': '1.0', 'href': 'http://example.com', 'queries': [ { 'rel': 'rel', 'href': 'href', } ], } }) collection = Collection.from_json(data) query = Query('href', 'rel') self.assertEqual(collection.queries, Array(Query, 'queries', [query]))
def test_from_json_with_inline_data(self): data = json.dumps({ 'collection': { 'version': '1.0', 'href': 'http://example.com', 'links': [ { 'href': 'href-inline', 'rel': 'rel-inline', 'length': 1, 'inline': True, } ], 'inline': { 'href-inline': { 'collection': { 'version': '1.0', 'href': 'href-inline', 'items': [ { 'href': 'href', 'data': [ {'name': 'name'} ], 'links': [ {'href': 'href', 'rel': 'rel'} ] } ] } } } } }) collection = Collection.from_json(data) data = Data('name') link = Link('href', 'rel') item = Item('href', [data], [link]) inline = [Collection(href='href-inline', items=[item])] link = Link('href-inline', 'rel-inline', length=1, inline=True) self.assertEqual(collection.inline, Array(Collection, 'inline', inline)) self.assertEqual(collection.links, Array(Link, 'links', [link]))
def test_from_json_with_items_data(self): data = json.dumps({ 'collection': { 'version': '1.0', 'href': 'http://example.com', 'items': [ { 'href': 'href', 'data': [ {'name': 'name'} ], 'links': [ {'href': 'href', 'rel': 'rel'} ] } ], } }) collection = Collection.from_json(data) data = Data('name') link = Link('href', 'rel') item = Item('href', [data], [link]) self.assertEqual(collection.items, Array(Item, 'items', [item]))
def setUp(self): self.response = self.client.get(self.endpoint) self.collection = Collection.from_json(self.response.content.decode('utf8'))
def test_from_json_invalid_document(self): with self.assertRaises(ValueError): Collection.from_json('{}')
def test_from_json_invaid_uri(self): with self.assertRaises(ValueError): Collection.from_json('{"collection": {}}')
def test_from_json_invalid_data(self): with self.assertRaises(ValueError): Collection.from_json('')
def setUp(self): # avoid cluttered console output (for instance logging all the http requests) logging.disable(logging.CRITICAL) self.response = self.client.get(self.endpoint) self.collection = Collection.from_json(self.response.content.decode('utf8'))
#SampleCollection+Json test #https://pypi.python.org/pypi/collection-json/0.1.0 # import urllib.request as requests from collection_json import Collection data=requests.get('http://www.youtypeitwepostit.com/api/').text collection=Collection.from_json(data) print collection print data