From a72200d2d043f0d4702a998ebda2b1109a6c14de Mon Sep 17 00:00:00 2001 From: f4exb Date: Sat, 23 Dec 2017 07:28:02 +0100 Subject: [PATCH] Web API: /sdrangel/deviceset/{deviceSetIndex}/device PUT: let client specify only the necessary fields. Inernally set integer value default (-1) if not specified --- sdrbase/resources/index.html | 21 +++--- sdrbase/webapi/webapirequestmapper.cpp | 79 +++++++++++++++++++++-- sdrbase/webapi/webapirequestmapper.h | 1 + swagger/sdrangel/api/swagger/swagger.yaml | 4 +- swagger/sdrangel/code/html2/index.html | 21 +++--- 5 files changed, 94 insertions(+), 32 deletions(-) diff --git a/sdrbase/resources/index.html b/sdrbase/resources/index.html index 0c487d0de..e6b193b85 100644 --- a/sdrbase/resources/index.html +++ b/sdrbase/resources/index.html @@ -888,7 +888,6 @@ margin-bottom: 20px; "description" : "DV serial device details" }; defs.DeviceListItem = { - "required" : [ "tx" ], "properties" : { "displayedName" : { "type" : "string", @@ -4253,7 +4252,7 @@ public class DeviceSetApiExample { DeviceSetApi apiInstance = new DeviceSetApi(); Integer deviceSetIndex = 56; // Integer | Index of device set in the device set list - DeviceListItem body = ; // DeviceListItem | Device item to look for. Give values for only the fields you want to search for among displayedName, hwType, serial, sequence and deviceSetIndex. For integers except tx set to -1 to ignore the field. It will match the first device found. + DeviceListItem body = ; // DeviceListItem | Device item to look for. Specify only the fields you want to search for. You must at least specify one among displayedName, hwType or serial. index and nbStreams are ignored. Rx is assumed if tx is not specified. try { DeviceListItem result = apiInstance.devicesetDevicePut(deviceSetIndex, body); System.out.println(result); @@ -4273,7 +4272,7 @@ public class DeviceSetApiExample { public static void main(String[] args) { DeviceSetApi apiInstance = new DeviceSetApi(); Integer deviceSetIndex = 56; // Integer | Index of device set in the device set list - DeviceListItem body = ; // DeviceListItem | Device item to look for. Give values for only the fields you want to search for among displayedName, hwType, serial, sequence and deviceSetIndex. For integers except tx set to -1 to ignore the field. It will match the first device found. + DeviceListItem body = ; // DeviceListItem | Device item to look for. Specify only the fields you want to search for. You must at least specify one among displayedName, hwType or serial. index and nbStreams are ignored. Rx is assumed if tx is not specified. try { DeviceListItem result = apiInstance.devicesetDevicePut(deviceSetIndex, body); System.out.println(result); @@ -4290,7 +4289,7 @@ public class DeviceSetApiExample { -->
Integer *deviceSetIndex = 56; // Index of device set in the device set list
-DeviceListItem *body = ; // Device item to look for. Give values for only the fields you want to search for among displayedName, hwType, serial, sequence and deviceSetIndex. For integers except tx set to -1 to ignore the field. It will match the first device found.
+DeviceListItem *body = ; // Device item to look for. Specify only the fields you want to search for. You must at least specify one among displayedName, hwType or serial. index and nbStreams are ignored. Rx is assumed if tx is not specified.
 
 DeviceSetApi *apiInstance = [[DeviceSetApi alloc] init];
 
@@ -4314,7 +4313,7 @@ var api = new SdRangel.DeviceSetApi()
 
 var deviceSetIndex = 56; // {Integer} Index of device set in the device set list
 
-var body = ; // {DeviceListItem} Device item to look for. Give values for only the fields you want to search for among displayedName, hwType, serial, sequence and deviceSetIndex. For integers except tx set to -1 to ignore the field. It will match the first device found.
+var body = ; // {DeviceListItem} Device item to look for. Specify only the fields you want to search for. You must at least specify one among displayedName, hwType or serial. index and nbStreams are ignored. Rx is assumed if tx is not specified.
 
 
 var callback = function(error, data, response) {
@@ -4347,7 +4346,7 @@ namespace Example
             
             var apiInstance = new DeviceSetApi();
             var deviceSetIndex = 56;  // Integer | Index of device set in the device set list
-            var body = new DeviceListItem(); // DeviceListItem | Device item to look for. Give values for only the fields you want to search for among displayedName, hwType, serial, sequence and deviceSetIndex. For integers except tx set to -1 to ignore the field. It will match the first device found.
+            var body = new DeviceListItem(); // DeviceListItem | Device item to look for. Specify only the fields you want to search for. You must at least specify one among displayedName, hwType or serial. index and nbStreams are ignored. Rx is assumed if tx is not specified.
 
             try
             {
@@ -4370,7 +4369,7 @@ require_once(__DIR__ . '/vendor/autoload.php');
 
 $api_instance = new Swagger\Client\Api\DeviceSetApi();
 $deviceSetIndex = 56; // Integer | Index of device set in the device set list
-$body = ; // DeviceListItem | Device item to look for. Give values for only the fields you want to search for among displayedName, hwType, serial, sequence and deviceSetIndex. For integers except tx set to -1 to ignore the field. It will match the first device found.
+$body = ; // DeviceListItem | Device item to look for. Specify only the fields you want to search for. You must at least specify one among displayedName, hwType or serial. index and nbStreams are ignored. Rx is assumed if tx is not specified.
 
 try {
     $result = $api_instance->devicesetDevicePut($deviceSetIndex, $body);
@@ -4388,7 +4387,7 @@ use SWGSDRangel::DeviceSetApi;
 
 my $api_instance = SWGSDRangel::DeviceSetApi->new();
 my $deviceSetIndex = 56; # Integer | Index of device set in the device set list
-my $body = SWGSDRangel::Object::DeviceListItem->new(); # DeviceListItem | Device item to look for. Give values for only the fields you want to search for among displayedName, hwType, serial, sequence and deviceSetIndex. For integers except tx set to -1 to ignore the field. It will match the first device found.
+my $body = SWGSDRangel::Object::DeviceListItem->new(); # DeviceListItem | Device item to look for. Specify only the fields you want to search for. You must at least specify one among displayedName, hwType or serial. index and nbStreams are ignored. Rx is assumed if tx is not specified.
 
 eval { 
     my $result = $api_instance->devicesetDevicePut(deviceSetIndex => $deviceSetIndex, body => $body);
@@ -4409,7 +4408,7 @@ from pprint import pprint
 # create an instance of the API class
 api_instance = swagger_sdrangel.DeviceSetApi()
 deviceSetIndex = 56 # Integer | Index of device set in the device set list
-body =  # DeviceListItem | Device item to look for. Give values for only the fields you want to search for among displayedName, hwType, serial, sequence and deviceSetIndex. For integers except tx set to -1 to ignore the field. It will match the first device found.
+body =  # DeviceListItem | Device item to look for. Specify only the fields you want to search for. You must at least specify one among displayedName, hwType or serial. index and nbStreams are ignored. Rx is assumed if tx is not specified.
 
 try: 
     api_response = api_instance.deviceset_device_put(deviceSetIndex, body)
@@ -4468,7 +4467,7 @@ $(document).ready(function() {
   var schemaWrapper = {
   "in" : "body",
   "name" : "body",
-  "description" : "Device item to look for. Give values for only the fields you want to search for among displayedName, hwType, serial, sequence and deviceSetIndex. For integers except tx set to -1 to ignore the field. It will match the first device found.",
+  "description" : "Device item to look for. Specify only the fields you want to search for. You must at least specify one among displayedName, hwType or serial. index and nbStreams are ignored. Rx is assumed if tx is not specified.",
   "required" : true,
   "schema" : {
     "$ref" : "#/definitions/DeviceListItem"
@@ -16201,7 +16200,7 @@ except ApiException as e:
           
- Generated 2017-12-22T13:37:01.264+01:00 + Generated 2017-12-23T07:26:19.634+01:00
diff --git a/sdrbase/webapi/webapirequestmapper.cpp b/sdrbase/webapi/webapirequestmapper.cpp index 68ea9cb65..23d6f03ce 100644 --- a/sdrbase/webapi/webapirequestmapper.cpp +++ b/sdrbase/webapi/webapirequestmapper.cpp @@ -823,20 +823,29 @@ void WebAPIRequestMapper::devicesetDeviceService(const std::string& indexStr, qt if (request.getMethod() == "PUT") { - SWGSDRangel::SWGDeviceListItem normalResponse; QString jsonStr = request.getBody(); QJsonObject jsonObject; if (parseJsonBody(jsonStr, jsonObject, response)) { - normalResponse.fromJson(jsonStr); + SWGSDRangel::SWGDeviceListItem normalResponse; - int status = m_adapter->devicesetDevicePut(deviceSetIndex, normalResponse, errorResponse); - response.setStatus(status); + if (validateDeviceListItem(normalResponse, jsonObject)) + { + int status = m_adapter->devicesetDevicePut(deviceSetIndex, normalResponse, errorResponse); + response.setStatus(status); - if (status/100 == 2) { - response.write(normalResponse.asJson().toUtf8()); - } else { + if (status/100 == 2) { + response.write(normalResponse.asJson().toUtf8()); + } else { + response.write(errorResponse.asJson().toUtf8()); + } + } + else + { + response.setStatus(400,"Missing device identification"); + errorResponse.init(); + *errorResponse.getMessage() = "Missing device identification"; response.write(errorResponse.asJson().toUtf8()); } } @@ -1285,6 +1294,62 @@ bool WebAPIRequestMapper::validatePresetExport(SWGSDRangel::SWGPresetExport& pre return validatePresetIdentifer(*presetIdentifier); } +bool WebAPIRequestMapper::validateDeviceListItem(SWGSDRangel::SWGDeviceListItem& deviceListItem, QJsonObject& jsonObject) +{ + if (jsonObject.contains("tx")) { + deviceListItem.setTx(jsonObject["tx"].toInt()); + } else { + deviceListItem.setTx(0); // assume Rx + } + + bool identified = false; + + if (jsonObject.contains("displayedName") && jsonObject["displayedName"].isString()) + { + deviceListItem.setDisplayedName(new QString(jsonObject["displayedName"].toString())); + identified = true; + } else { + deviceListItem.setDisplayedName(0); + } + + + if (jsonObject.contains("hwType") && jsonObject["hwType"].isString()) + { + deviceListItem.setHwType(new QString(jsonObject["hwType"].toString())); + identified = true; + } else { + deviceListItem.setHwType(0); + } + + if (jsonObject.contains("serial") && jsonObject["serial"].isString()) + { + deviceListItem.setSerial(new QString(jsonObject["serial"].toString())); + identified = true; + } else { + deviceListItem.setSerial(0); + } + + if (jsonObject.contains("index")) { + deviceListItem.setIndex(jsonObject["index"].toInt(-1)); + } else { + deviceListItem.setIndex(-1); + } + + if (jsonObject.contains("sequence")){ + deviceListItem.setSequence(jsonObject["sequence"].toInt(-1)); + } else { + deviceListItem.setSequence(-1); + } + + if (jsonObject.contains("streamIndex")) { + deviceListItem.setStreamIndex(jsonObject["streamIndex"].toInt(-1)); + } else { + deviceListItem.setStreamIndex(-1); + } + + return identified; +} + bool WebAPIRequestMapper::validateDeviceSettings(SWGSDRangel::SWGDeviceSettings& deviceSettings, QJsonObject& jsonObject) { if (jsonObject.contains("tx")) { diff --git a/sdrbase/webapi/webapirequestmapper.h b/sdrbase/webapi/webapirequestmapper.h index 54b6de536..a85f7c0ce 100644 --- a/sdrbase/webapi/webapirequestmapper.h +++ b/sdrbase/webapi/webapirequestmapper.h @@ -69,6 +69,7 @@ private: bool validatePresetTransfer(SWGSDRangel::SWGPresetTransfer& presetTransfer); bool validatePresetIdentifer(SWGSDRangel::SWGPresetIdentifier& presetIdentifier); bool validatePresetExport(SWGSDRangel::SWGPresetExport& presetExport); + bool validateDeviceListItem(SWGSDRangel::SWGDeviceListItem& deviceListItem, QJsonObject& jsonObject); bool validateDeviceSettings(SWGSDRangel::SWGDeviceSettings& deviceSettings, QJsonObject& jsonObject); bool validateChannelSettings(SWGSDRangel::SWGChannelSettings& deviceSettings, QJsonObject& jsonObject); diff --git a/swagger/sdrangel/api/swagger/swagger.yaml b/swagger/sdrangel/api/swagger/swagger.yaml index 7b75343c8..664dc29d6 100644 --- a/swagger/sdrangel/api/swagger/swagger.yaml +++ b/swagger/sdrangel/api/swagger/swagger.yaml @@ -582,7 +582,7 @@ paths: description: Index of device set in the device set list - name: body in: body - description: Device item to look for. Give values for only the fields you want to search for among displayedName, hwType, serial, sequence and deviceSetIndex. For integers except tx set to -1 to ignore the field. It will match the first device found. + description: Device item to look for. Specify only the fields you want to search for. You must at least specify one among displayedName, hwType or serial. index and nbStreams are ignored. Rx is assumed if tx is not specified. required: true schema: $ref: "#/definitions/DeviceListItem" @@ -1035,8 +1035,6 @@ definitions: DeviceListItem: description: "Summarized information about attached hardware device" - required: - - tx properties: displayedName: description: "Displayable name that uniquely identifies this device instance" diff --git a/swagger/sdrangel/code/html2/index.html b/swagger/sdrangel/code/html2/index.html index 0c487d0de..e6b193b85 100644 --- a/swagger/sdrangel/code/html2/index.html +++ b/swagger/sdrangel/code/html2/index.html @@ -888,7 +888,6 @@ margin-bottom: 20px; "description" : "DV serial device details" }; defs.DeviceListItem = { - "required" : [ "tx" ], "properties" : { "displayedName" : { "type" : "string", @@ -4253,7 +4252,7 @@ public class DeviceSetApiExample { DeviceSetApi apiInstance = new DeviceSetApi(); Integer deviceSetIndex = 56; // Integer | Index of device set in the device set list - DeviceListItem body = ; // DeviceListItem | Device item to look for. Give values for only the fields you want to search for among displayedName, hwType, serial, sequence and deviceSetIndex. For integers except tx set to -1 to ignore the field. It will match the first device found. + DeviceListItem body = ; // DeviceListItem | Device item to look for. Specify only the fields you want to search for. You must at least specify one among displayedName, hwType or serial. index and nbStreams are ignored. Rx is assumed if tx is not specified. try { DeviceListItem result = apiInstance.devicesetDevicePut(deviceSetIndex, body); System.out.println(result); @@ -4273,7 +4272,7 @@ public class DeviceSetApiExample { public static void main(String[] args) { DeviceSetApi apiInstance = new DeviceSetApi(); Integer deviceSetIndex = 56; // Integer | Index of device set in the device set list - DeviceListItem body = ; // DeviceListItem | Device item to look for. Give values for only the fields you want to search for among displayedName, hwType, serial, sequence and deviceSetIndex. For integers except tx set to -1 to ignore the field. It will match the first device found. + DeviceListItem body = ; // DeviceListItem | Device item to look for. Specify only the fields you want to search for. You must at least specify one among displayedName, hwType or serial. index and nbStreams are ignored. Rx is assumed if tx is not specified. try { DeviceListItem result = apiInstance.devicesetDevicePut(deviceSetIndex, body); System.out.println(result); @@ -4290,7 +4289,7 @@ public class DeviceSetApiExample { -->
Integer *deviceSetIndex = 56; // Index of device set in the device set list
-DeviceListItem *body = ; // Device item to look for. Give values for only the fields you want to search for among displayedName, hwType, serial, sequence and deviceSetIndex. For integers except tx set to -1 to ignore the field. It will match the first device found.
+DeviceListItem *body = ; // Device item to look for. Specify only the fields you want to search for. You must at least specify one among displayedName, hwType or serial. index and nbStreams are ignored. Rx is assumed if tx is not specified.
 
 DeviceSetApi *apiInstance = [[DeviceSetApi alloc] init];
 
@@ -4314,7 +4313,7 @@ var api = new SdRangel.DeviceSetApi()
 
 var deviceSetIndex = 56; // {Integer} Index of device set in the device set list
 
-var body = ; // {DeviceListItem} Device item to look for. Give values for only the fields you want to search for among displayedName, hwType, serial, sequence and deviceSetIndex. For integers except tx set to -1 to ignore the field. It will match the first device found.
+var body = ; // {DeviceListItem} Device item to look for. Specify only the fields you want to search for. You must at least specify one among displayedName, hwType or serial. index and nbStreams are ignored. Rx is assumed if tx is not specified.
 
 
 var callback = function(error, data, response) {
@@ -4347,7 +4346,7 @@ namespace Example
             
             var apiInstance = new DeviceSetApi();
             var deviceSetIndex = 56;  // Integer | Index of device set in the device set list
-            var body = new DeviceListItem(); // DeviceListItem | Device item to look for. Give values for only the fields you want to search for among displayedName, hwType, serial, sequence and deviceSetIndex. For integers except tx set to -1 to ignore the field. It will match the first device found.
+            var body = new DeviceListItem(); // DeviceListItem | Device item to look for. Specify only the fields you want to search for. You must at least specify one among displayedName, hwType or serial. index and nbStreams are ignored. Rx is assumed if tx is not specified.
 
             try
             {
@@ -4370,7 +4369,7 @@ require_once(__DIR__ . '/vendor/autoload.php');
 
 $api_instance = new Swagger\Client\Api\DeviceSetApi();
 $deviceSetIndex = 56; // Integer | Index of device set in the device set list
-$body = ; // DeviceListItem | Device item to look for. Give values for only the fields you want to search for among displayedName, hwType, serial, sequence and deviceSetIndex. For integers except tx set to -1 to ignore the field. It will match the first device found.
+$body = ; // DeviceListItem | Device item to look for. Specify only the fields you want to search for. You must at least specify one among displayedName, hwType or serial. index and nbStreams are ignored. Rx is assumed if tx is not specified.
 
 try {
     $result = $api_instance->devicesetDevicePut($deviceSetIndex, $body);
@@ -4388,7 +4387,7 @@ use SWGSDRangel::DeviceSetApi;
 
 my $api_instance = SWGSDRangel::DeviceSetApi->new();
 my $deviceSetIndex = 56; # Integer | Index of device set in the device set list
-my $body = SWGSDRangel::Object::DeviceListItem->new(); # DeviceListItem | Device item to look for. Give values for only the fields you want to search for among displayedName, hwType, serial, sequence and deviceSetIndex. For integers except tx set to -1 to ignore the field. It will match the first device found.
+my $body = SWGSDRangel::Object::DeviceListItem->new(); # DeviceListItem | Device item to look for. Specify only the fields you want to search for. You must at least specify one among displayedName, hwType or serial. index and nbStreams are ignored. Rx is assumed if tx is not specified.
 
 eval { 
     my $result = $api_instance->devicesetDevicePut(deviceSetIndex => $deviceSetIndex, body => $body);
@@ -4409,7 +4408,7 @@ from pprint import pprint
 # create an instance of the API class
 api_instance = swagger_sdrangel.DeviceSetApi()
 deviceSetIndex = 56 # Integer | Index of device set in the device set list
-body =  # DeviceListItem | Device item to look for. Give values for only the fields you want to search for among displayedName, hwType, serial, sequence and deviceSetIndex. For integers except tx set to -1 to ignore the field. It will match the first device found.
+body =  # DeviceListItem | Device item to look for. Specify only the fields you want to search for. You must at least specify one among displayedName, hwType or serial. index and nbStreams are ignored. Rx is assumed if tx is not specified.
 
 try: 
     api_response = api_instance.deviceset_device_put(deviceSetIndex, body)
@@ -4468,7 +4467,7 @@ $(document).ready(function() {
   var schemaWrapper = {
   "in" : "body",
   "name" : "body",
-  "description" : "Device item to look for. Give values for only the fields you want to search for among displayedName, hwType, serial, sequence and deviceSetIndex. For integers except tx set to -1 to ignore the field. It will match the first device found.",
+  "description" : "Device item to look for. Specify only the fields you want to search for. You must at least specify one among displayedName, hwType or serial. index and nbStreams are ignored. Rx is assumed if tx is not specified.",
   "required" : true,
   "schema" : {
     "$ref" : "#/definitions/DeviceListItem"
@@ -16201,7 +16200,7 @@ except ApiException as e:
           
- Generated 2017-12-22T13:37:01.264+01:00 + Generated 2017-12-23T07:26:19.634+01:00