Пример #1
0
def version_detail(name):
    version = Version.objects.get(version=name)
    links = [Link(rel, url) for (rel, url) in version.binaries.items()]
    return Document(data={
        'version': version.version
    },
                    links=Collection(*links)).to_dict()
Пример #2
0
    def get(self):
        '''
        Get all devices available
        '''

        devices_db = g.con.get_devices()

        if not devices_db:
            return create_error_response(404, "No devices found",
                                     'There is no devices in DB', 'Devices')

        # create collection of links
        links = Collection(
            Self(),
            Link('device', '/wind/api/device/{id}')
        )

        # links to dict
        l = links.to_dict()

        # combine links and speed to one dict and add items where the actual values are
        dump = l
        dump.update({'items': devices_db})

        # return Response
        return Response(json.dumps(dump), 200, mimetype=JSONHAL)
Пример #3
0
    def get(self, id, timestamp):
        '''
        :param id, timestamp:
        '''

        direction_db = g.con.get_direction(id, timestamp)

        if not direction_db:
            return create_error_response(404, "No direction data found",
                                         'There is no direction data on device id %s with timestamp %s' % (id, timestamp),
                                         'Direction')

        #create collection of links
        links = Collection(
            Self(),
            Link('device', '/wind/api/device/' + id + '/'),
            Link('list', '/wind/api/device/' + id + '/directions/'),
            Link('all-batteries', '/wind/api/device/' + id + '/batteries/')
        )

        #links to dict
        l = links.to_dict()

        #combine links and speed to one dict
        dump = dict(list(l.items()) + list(direction_db.items()))

        #return Response
        return Response(json.dumps(dump), 200, mimetype=JSONHAL)
Пример #4
0
    def get(self, id):
        '''
        Get device with given id from db
        :param id
        '''

        device_db = g.con.get_device(id)

        if not device_db:
            return create_error_response(404, "No device found",
                                     'There is no device info on given device id %s' % id, 'Device')

        #create collection of links
        links = Collection(
            Self(),
            Link('list', '/wind/api/devices/'),
            Link('data:speeds-all', request.path + '/speeds/')
        )

        #links to dict
        l = links.to_dict()

        #combine links and speed to one dict to be returned in response
        #dump = dict(list(device_db.items()) + list(l.items()))
        dump = dict(list(l.items()) + list(device_db.items()))

        #return Response
        return Response(json.dumps(dump), 200, mimetype=JSONHAL)
Пример #5
0
    def get(self, id):
        '''
        get all speeds
        params: id

        '''
        #extract speeds from db
        speeds_db = g.con.get_speeds(id)
        if not speeds_db:
            return create_error_response(404, "No speeds found",
                                     'There is no speeds data on given device id %s' % id, 'Speeds')

        # create collection of links
        links = Collection(
            Self(),
            Link('device', '/wind/api/device/' + id + '/'),
            Link('speed', '/wind/api/device/' + id + '/speed/{timestamp}')
        )

        # links to dict
        l = links.to_dict()

        # combine links and speed to one dict
        dump = l
        dump.update({'items': speeds_db})

        # return Response
        return Response(json.dumps(dump), 200, mimetype=JSONHAL)
Пример #6
0
    def get(self, id, timestamp):
        '''
        :param id, timestamp:
        '''

        speed_db = g.con.get_speed(id, timestamp)

        if not speed_db:
             return create_error_response(404, "No speed found",
                                'There is no speed data on device id %s with timestamp %s' % (id, timestamp), 'Speed')

        #create collection of links
        links = Collection(
            Self(),
            Link('device', '/wind/api/device/' + id + '/'),
            Link('list', '/wind/api/device/' + id + '/speeds/'),
            Link('temperatures-all', '/wind/api/device/' + id + '/temperatures/')
        )

        #links to dict
        l = links.to_dict()

        #combine links and speed to one dict
        dump = dict(list(l.items()) + list(speed_db.items()))

        #return Response
        return Response(json.dumps(dump), 200, mimetype=JSONHAL)
Пример #7
0
    def get(self, id, timestamp):
        '''
        :param id, timestamp:
        '''

        battery_db = g.con.get_battery(id, timestamp)

        if not battery_db:
            return create_error_response(404, "No battery data found",
                                         'There is no battery data on device id %s with timestamp %s' % (id, timestamp),
                                         'Battery')

        #create collection of links
        links = Collection(
            Self(),
            Link('device', '/wind/api/device/' + id + '/'),
            Link('list', '/wind/api/device/' + id + '/batteries/')
            # last one, no links to other resources except device and collection
        )

        #links to dict
        l = links.to_dict()

        #combine links and speed to one dict
        dump = dict(list(l.items()) + list(battery_db.items()))

        #return Response
        return Response(json.dumps(dump), 200, mimetype=JSONHAL)
Пример #8
0
    def get(self, id):
        '''
        get all battery values on device
        :param id:
        '''

        batteries_db = g.con.get_batteries(id)

        if not batteries_db:
            return create_error_response(404, "No batteries found",
                                     'There is no batteries data on given device id %s' % id, 'Batteries')

        # create collection of links
        links = Collection(
            Self(),
            Link('device', '/wind/api/device/' + id + '/'),
            Link('battery', '/wind/api/device/' + id + '/battery/{timestamp}')
        )

        # links to dict
        l = links.to_dict()

        # combine links and speed to one dict
        dump = l
        dump.update({'items': batteries_db})

        # return Response
        return Response(json.dumps(dump), 200, mimetype=JSONHAL)
Пример #9
0
def version_detail(name):
    version = Version.objects.get(version=name)
    binaries = {}
    for (platform, links) in version.binaries.items():
        if 'x86_64' in links:
            binaries[platform] = links['x86_64']

    links = [Link(rel, url) for (rel, url) in binaries.items()]
    return Document(data={
        'version': version.version
    },
                    links=Collection(*links)).to_dict()
Пример #10
0
    def test_to_json_repeat_relation(self):
        c = Collection(Link('foo', '/foo'), Link('foo', '/bar'))

        expected = json.dumps(
            {'_links': {
                'foo': [{
                    'href': '/foo'
                }, {
                    'href': '/bar'
                }]
            }})

        assert c.to_json() == expected
Пример #11
0
    def test_to_json(self):
        c = Collection(Link('foo', '/foo'), Link('bar', '/bar'))

        expected = json.dumps(
            {'_links': {
                'foo': {
                    'href': '/foo'
                },
                'bar': {
                    'href': '/bar'
                }
            }})

        assert c.to_json() == expected
Пример #12
0
    def test_to_dict(self):
        c = Collection(Link('foo', '/foo'), Link('bar', '/bar'))

        expected = {
            '_links': {
                'foo': {
                    'href': '/foo'
                },
                'bar': {
                    'href': '/bar'
                }
            }
        }

        assert c.to_dict() == expected
Пример #13
0
    def get(self, id, timestamp):
        humidity_db = g.con.get_humidity(id, timestamp)
        if not humidity_db:
            return create_error_response(404, "No humidity found",
                                         'There is no humidity data on device id %s with timestamp %s' % (id, timestamp),
                                         'Humidity')

        #create collection of links
        links = Collection(
            Self(),
            Link('device', '/wind/api/device/' + id + '/'),
            Link('list', '/wind/api/humidities/'),
            Link('directions-all', '/wind/api/device/' + id + '/directions/')
        )

        #links to dict
        l = links.to_dict()

        #combine links and speed to one dict
        dump = dict(list(l.items()) + list(humidity_db.items()))

        #return Response
        return Response(json.dumps(dump), 200, mimetype=JSONHAL)
Пример #14
0
    def get(self, id):
        humidities_db = g.con.get_humidities(id)

        if not humidities_db:
            return create_error_response(404, "No humidities found",
                                     'There is no humidities data on given device id %s' % id, 'Humidities')

        # create collection of links
        links = Collection(
            Self(),
            Link('device', '/wind/api/device/' + id + '/'),
            Link('humidity', '/wind/api/device/' + id + '/humidity/{timestamp}')
        )

        # links to dict
        l = links.to_dict()

        # combine links and speed to one dict
        dump = l
        dump.update({'items': humidities_db})

        # return Response
        return Response(json.dumps(dump), 200, mimetype=JSONHAL)
Пример #15
0
    def test_to_dict_repeat_relation(self):
        c = Collection(Link('foo', '/foo'), Link('foo', '/bar'))

        expected = {'_links': {'foo': [{'href': '/foo'}, {'href': '/bar'}]}}

        assert c.to_dict() == expected
Пример #16
0
def root():
    return Document(links=Collection(Link('versions', '/versions'), ))
Пример #17
0
 def to_embedded(v):
     return Embedded(
         links=Collection(Link('self', '/versions/{}'.format(v.version))))
Пример #18
0
    def test_init_raises_type_error(self):
        with pytest.raises(TypeError) as excinfo:
            Collection(Link('foo', '/foo'), 'Foo', Link('bar', '/bar'))

        assert 'Foo is not a valid flask_hal.link.Link instance' in str(
            excinfo.value)