Skip to content
Snippets Groups Projects
Commit 5b786e3a authored by Robert Goldmann's avatar Robert Goldmann
Browse files

#8 - post route for device

parent f6ab5b72
No related branches found
No related tags found
No related merge requests found
......@@ -103,6 +103,34 @@ paths:
type: array
items:
$ref: '#/components/schemas/Sensor'
/device:
post:
tags:
- device
summary: Adds a new device. If a device with the provided name already exists an error response is returned.
operationId: addDevice
security:
- bearerAuth: []
requestBody:
description: Device to add
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/NewDevice'
responses:
'200':
description: success response
content:
application/json:
schema:
$ref: '#/components/schemas/SuccessResponse'
default:
description: error response
content:
application/json:
schema:
$ref: '#/components/schemas/ErrorResponse'
/sensors:
get:
......@@ -416,6 +444,15 @@ components:
type: string
example: "20.15"
NewDevice:
type: object
required:
- device
properties:
device:
type: string
example: "myDevice"
SuccessResponse:
required:
- success
......
from flask import Blueprint, jsonify
from flask import Blueprint, jsonify, request
from logic.AuthenticationWrapper import require_api_key
from logic.Parameters import DeviceParameters
from logic.RequestValidator import RequestValidator, ValidationError
from logic.database.Database import Database
......@@ -42,4 +44,23 @@ def construct_blueprint(settings):
return jsonify({'success': True})
@devices.route('/device', methods=['POST'])
@require_api_key(password=settings['api']['key'])
def add_device():
try:
parameters = RequestValidator.validate(request, [DeviceParameters.DEVICE.value])
database = Database(settings['database']['databasePath'])
deviceName = parameters[DeviceParameters.DEVICE.value]
existingDevice = database.deviceAccess.get_device_by_name(deviceName)
if existingDevice:
return jsonify({'success': False,
'msg': f'A device called "{deviceName}" already exists (ID: {existingDevice["id"]})'})
database.deviceAccess.add_device(deviceName)
except ValidationError as e:
return e.response, 400
return jsonify({'success': True})
return devices
......@@ -23,7 +23,7 @@ def construct_blueprint(settings):
@measurements.route('/measurements', methods=['POST'])
@require_api_key(password=settings['api']['key'])
def addMeasurement():
def add_measurement():
try:
parameters = RequestValidator.validate(request, DeviceParameters.get_values())
database = Database(settings['database']['databasePath'])
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment