def readings(): # print('REQUEST: ', request, request.args, request.data) if request.method == "POST": result = Reading.save_readings_from_json(request) # generate server response response = jsonify(result) response.status_code = 201 # Created return response # GET else: sid = request.args.get('sensor_id', '', type=str) count = request.args.get('count', -1, type=int) # check if query contains count if count != -1: # query does not specify sensor id if sid == '': sensor_ids = Sensor.get_all_ids() filtered = [] for id in sensor_ids: one_sensors_readings = Reading.get_for_sensor( id.sensor_id, count) for r in one_sensors_readings: filtered.append(r) return create_readings_response(filtered) # query contains sensor id & count else: # return count readings from sensor with given sensor_id filtered = Reading.get_for_sensor(sid, count) return create_readings_response(filtered) # check if query contains sensor id, and start and end times else: start = request.args.get('start_time', -1, type=int) end = request.args.get('end_time', -1, type=int) if (start != -1) and (end != -1): if sid != '': filtered = Reading.get_sensor_range(sid, start, end) else: filtered = Reading.get_range(start, end) return create_readings_response(filtered) # parameters are missing response = jsonify({ 'error': 'Only the following queries are supported: count, sensor_id & count, start_time & end_time, sensor_id & start_time & end_time' }) response.status_code = 400 # Bad Request return response
def test_sensor_get_ids(self): self.client.post('/sensors', data=self.sensor_fixed) self.client.post('/sensors', data=self.sensor_rover) res = Sensor.get_all_ids() self.assertTrue(len(res) is 2)