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))
示例#2
0
 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]
示例#3
0
 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]
示例#4
0
 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)
示例#5
0
 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]
示例#6
0
 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'))
示例#7
0
 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)
示例#9
0
 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
示例#10
0
 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
示例#11
0
 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)
示例#12
0
 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')]))
示例#13
0
 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'))
示例#14
0
 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]))
示例#15
0
 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
示例#16
0
 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]))
示例#17
0
 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]))
示例#18
0
 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]))
示例#19
0
 def setUp(self):
     self.response = self.client.get(self.endpoint)
     self.collection = Collection.from_json(self.response.content.decode('utf8'))
示例#20
0
 def test_from_json_invalid_document(self):
     with self.assertRaises(ValueError):
         Collection.from_json('{}')
示例#21
0
 def test_from_json_invaid_uri(self):
     with self.assertRaises(ValueError):
         Collection.from_json('{"collection": {}}')
示例#22
0
 def setUp(self):
     self.response = self.client.get(self.endpoint)
     self.collection = Collection.from_json(self.response.content.decode('utf8'))
示例#23
0
 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