comparison OrthancServer/Sources/OrthancRestApi/OrthancRestResources.cpp @ 4409:5784a9eaf502

cont openapi
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 25 Dec 2020 11:12:14 +0100
parents d2bfadc2948b
children a6abe5f512db
comparison
equal deleted inserted replaced
4408:d2bfadc2948b 4409:5784a9eaf502
210 210
211 211
212 AnswerListOfResources(call.GetOutput(), index, result, resourceType, call.HasArgument("expand")); 212 AnswerListOfResources(call.GetOutput(), index, result, resourceType, call.HasArgument("expand"));
213 } 213 }
214 214
215
216
217 static std::string GetDocumentationSampleResource(ResourceType type)
218 {
219 switch (type)
220 {
221 case Orthanc::ResourceType_Instance:
222 return "https://demo.orthanc-server.com/instances/d94d9a03-3003b047-a4affc69-322313b2-680530a2";
223 break;
224
225 case Orthanc::ResourceType_Series:
226 return "https://demo.orthanc-server.com/series/37836232-d13a2350-fa1dedc5-962b31aa-010f8e52";
227 break;
228
229 case Orthanc::ResourceType_Study:
230 return "https://demo.orthanc-server.com/studies/27f7126f-4f66fb14-03f4081b-f9341db2-53925988";
231 break;
232
233 case Orthanc::ResourceType_Patient:
234 return "https://demo.orthanc-server.com/patients/46e6332c-677825b6-202fcf7c-f787bc5f-7b07c382";
235 break;
236
237 default:
238 throw OrthancException(ErrorCode_ParameterOutOfRange);
239 }
240 }
241
242
215 template <enum ResourceType resourceType> 243 template <enum ResourceType resourceType>
216 static void GetSingleResource(RestApiGetCall& call) 244 static void GetSingleResource(RestApiGetCall& call)
217 { 245 {
218 if (call.IsDocumentation()) 246 if (call.IsDocumentation())
219 { 247 {
220 std::string sampleUrl;
221 switch (resourceType)
222 {
223 case Orthanc::ResourceType_Instance:
224 sampleUrl = "https://demo.orthanc-server.com/instances/d94d9a03-3003b047-a4affc69-322313b2-680530a2";
225 break;
226 case Orthanc::ResourceType_Series:
227 sampleUrl = "https://demo.orthanc-server.com/series/37836232-d13a2350-fa1dedc5-962b31aa-010f8e52";
228 break;
229 case Orthanc::ResourceType_Study:
230 sampleUrl = "https://demo.orthanc-server.com/studies/27f7126f-4f66fb14-03f4081b-f9341db2-53925988";
231 break;
232 case Orthanc::ResourceType_Patient:
233 sampleUrl = "https://demo.orthanc-server.com/patients/46e6332c-677825b6-202fcf7c-f787bc5f-7b07c382";
234 break;
235 default:
236 throw OrthancException(ErrorCode_ParameterOutOfRange);
237 }
238
239 const std::string resource = GetResourceTypeText(resourceType, false /* plural */, false /* lower case */); 248 const std::string resource = GetResourceTypeText(resourceType, false /* plural */, false /* lower case */);
240 call.GetDocumentation() 249 call.GetDocumentation()
241 .SetTag(GetResourceTypeText(resourceType, true /* plural */, true /* upper case */)) 250 .SetTag(GetResourceTypeText(resourceType, true /* plural */, true /* upper case */))
242 .SetSummary("Get information about some " + resource) 251 .SetSummary("Get information about some " + resource)
243 .SetDescription("Get detailed information about the DICOM " + resource + " of interest whose Orthanc identifier is provided in the URL") 252 .SetDescription("Get detailed information about the DICOM " + resource + " of interest whose Orthanc identifier is provided in the URL")
244 .SetUriArgument("id", "Orthanc identifier of the " + resource + " of interest") 253 .SetUriArgument("id", "Orthanc identifier of the " + resource + " of interest")
245 .SetHttpGetSample(sampleUrl, true); 254 .AddAnswerType(MimeType_Json, "Information about the DICOM " + resource)
255 .SetHttpGetSample(GetDocumentationSampleResource(resourceType), true);
246 return; 256 return;
247 } 257 }
248 258
249 Json::Value result; 259 Json::Value result;
250 if (OrthancRestApi::GetIndex(call).LookupResource(result, call.GetUriComponent("id", ""), resourceType)) 260 if (OrthancRestApi::GetIndex(call).LookupResource(result, call.GetUriComponent("id", ""), resourceType))
1306 1316
1307 // Handling of metadata ----------------------------------------------------- 1317 // Handling of metadata -----------------------------------------------------
1308 1318
1309 static void CheckValidResourceType(const RestApiCall& call) 1319 static void CheckValidResourceType(const RestApiCall& call)
1310 { 1320 {
1311 std::string resourceType = call.GetUriComponent("resourceType", ""); 1321 const std::string resourceType = call.GetFullUri() [0];
1312 StringToResourceType(resourceType.c_str()); 1322 StringToResourceType(resourceType.c_str());
1313 } 1323 }
1314 1324
1315 1325
1316 static void ListMetadata(RestApiGetCall& call) 1326 static void ListMetadata(RestApiGetCall& call)
1414 1424
1415 // Handling of attached files ----------------------------------------------- 1425 // Handling of attached files -----------------------------------------------
1416 1426
1417 static void ListAttachments(RestApiGetCall& call) 1427 static void ListAttachments(RestApiGetCall& call)
1418 { 1428 {
1419 std::string resourceType = call.GetUriComponent("resourceType", ""); 1429 if (call.IsDocumentation())
1420 std::string publicId = call.GetUriComponent("id", ""); 1430 {
1431 ResourceType t = StringToResourceType(call.GetFullUri()[0].c_str());
1432 std::string r = GetResourceTypeText(t, false /* plural */, false /* upper case */);
1433 call.GetDocumentation()
1434 .SetTag(GetResourceTypeText(t, true /* plural */, true /* upper case */))
1435 .SetSummary("List attachments")
1436 .SetDescription("Get the list of attachments that are associated with the given " + r)
1437 .SetUriArgument("id", "Orthanc identifier of the " + r + " of interest")
1438 .AddAnswerType(MimeType_Json, "JSON array containing the names of the attachments")
1439 .SetHttpGetSample(GetDocumentationSampleResource(t) + "/attachments", true);
1440 return;
1441 }
1442
1443 const std::string resourceType = call.GetFullUri() [0];
1444 const std::string publicId = call.GetUriComponent("id", "");
1421 std::list<FileContentType> attachments; 1445 std::list<FileContentType> attachments;
1422 OrthancRestApi::GetIndex(call).ListAvailableAttachments(attachments, publicId, StringToResourceType(resourceType.c_str())); 1446 OrthancRestApi::GetIndex(call).ListAvailableAttachments(attachments, publicId, StringToResourceType(resourceType.c_str()));
1423 1447
1424 Json::Value result = Json::arrayValue; 1448 Json::Value result = Json::arrayValue;
1425 1449
1445 } 1469 }
1446 1470
1447 1471
1448 static void GetAttachmentOperations(RestApiGetCall& call) 1472 static void GetAttachmentOperations(RestApiGetCall& call)
1449 { 1473 {
1474 if (call.IsDocumentation())
1475 {
1476 ResourceType t = StringToResourceType(call.GetFullUri()[0].c_str());
1477 std::string r = GetResourceTypeText(t, false /* plural */, false /* upper case */);
1478 call.GetDocumentation()
1479 .SetTag("Other")
1480 .SetSummary("List of operations on attachments")
1481 .SetDescription("Get the list of operations that are available for attachments associated with the given " + r)
1482 .SetUriArgument("id", "Orthanc identifier of the " + r + " of interest")
1483 .SetUriArgument("name", "The name of the attachment")
1484 .AddAnswerType(MimeType_Json, "List of the available operations")
1485 .SetHttpGetSample("https://demo.orthanc-server.com/instances/d94d9a03-3003b047-a4affc69-322313b2-680530a2/attachments/dicom", true);
1486 return;
1487 }
1488
1450 FileInfo info; 1489 FileInfo info;
1451 if (GetAttachmentInfo(info, call)) 1490 if (GetAttachmentInfo(info, call))
1452 { 1491 {
1453 Json::Value operations = Json::arrayValue; 1492 Json::Value operations = Json::arrayValue;
1454 1493
1484 1523
1485 1524
1486 template <int uncompress> 1525 template <int uncompress>
1487 static void GetAttachmentData(RestApiGetCall& call) 1526 static void GetAttachmentData(RestApiGetCall& call)
1488 { 1527 {
1528 if (call.IsDocumentation())
1529 {
1530 ResourceType t = StringToResourceType(call.GetFullUri()[0].c_str());
1531 std::string r = GetResourceTypeText(t, false /* plural */, false /* upper case */);
1532 call.GetDocumentation()
1533 .SetTag(GetResourceTypeText(t, true /* plural */, true /* upper case */))
1534 .SetSummary("Get attachment" + std::string(uncompress ? "" : " (no decompression)"))
1535 .SetDescription("Get the (binary) content of one attachment associated with the given " + r +
1536 std::string(uncompress ? "" : ". The attachment will not be decompressed if `StorageCompression` if `true`."))
1537 .SetUriArgument("id", "Orthanc identifier of the " + r + " of interest")
1538 .SetUriArgument("name", "The name of the attachment")
1539 .AddAnswerType(MimeType_Binary, "The attachment");
1540 return;
1541 }
1542
1489 ServerContext& context = OrthancRestApi::GetContext(call); 1543 ServerContext& context = OrthancRestApi::GetContext(call);
1490 1544
1491 CheckValidResourceType(call); 1545 CheckValidResourceType(call);
1492 1546
1493 std::string publicId = call.GetUriComponent("id", ""); 1547 std::string publicId = call.GetUriComponent("id", "");
1507 } 1561 }
1508 1562
1509 1563
1510 static void GetAttachmentSize(RestApiGetCall& call) 1564 static void GetAttachmentSize(RestApiGetCall& call)
1511 { 1565 {
1566 if (call.IsDocumentation())
1567 {
1568 ResourceType t = StringToResourceType(call.GetFullUri()[0].c_str());
1569 std::string r = GetResourceTypeText(t, false /* plural */, false /* upper case */);
1570 call.GetDocumentation()
1571 .SetTag(GetResourceTypeText(t, true /* plural */, true /* upper case */))
1572 .SetSummary("Get size of attachment")
1573 .SetDescription("Get the size of one attachment associated with the given " + r)
1574 .SetUriArgument("id", "Orthanc identifier of the " + r + " of interest")
1575 .SetUriArgument("name", "The name of the attachment")
1576 .AddAnswerType(MimeType_PlainText, "The size of the attachment");
1577 return;
1578 }
1579
1512 FileInfo info; 1580 FileInfo info;
1513 if (GetAttachmentInfo(info, call)) 1581 if (GetAttachmentInfo(info, call))
1514 { 1582 {
1515 call.GetOutput().AnswerBuffer(boost::lexical_cast<std::string>(info.GetUncompressedSize()), MimeType_PlainText); 1583 call.GetOutput().AnswerBuffer(boost::lexical_cast<std::string>(info.GetUncompressedSize()), MimeType_PlainText);
1516 } 1584 }
1517 } 1585 }
1518 1586
1519 1587
1520 static void GetAttachmentCompressedSize(RestApiGetCall& call) 1588 static void GetAttachmentCompressedSize(RestApiGetCall& call)
1521 { 1589 {
1590 if (call.IsDocumentation())
1591 {
1592 ResourceType t = StringToResourceType(call.GetFullUri()[0].c_str());
1593 std::string r = GetResourceTypeText(t, false /* plural */, false /* upper case */);
1594 call.GetDocumentation()
1595 .SetTag(GetResourceTypeText(t, true /* plural */, true /* upper case */))
1596 .SetSummary("Get size of attachment on the disk")
1597 .SetDescription("Get the size of one attachment associated with the given " + r + ", as stored on the disk. "
1598 "This is different from `.../size` if `EnableStorage` is `true`.")
1599 .SetUriArgument("id", "Orthanc identifier of the " + r + " of interest")
1600 .SetUriArgument("name", "The name of the attachment")
1601 .AddAnswerType(MimeType_PlainText, "The size of the attachment on the disk");
1602 return;
1603 }
1604
1522 FileInfo info; 1605 FileInfo info;
1523 if (GetAttachmentInfo(info, call)) 1606 if (GetAttachmentInfo(info, call))
1524 { 1607 {
1525 call.GetOutput().AnswerBuffer(boost::lexical_cast<std::string>(info.GetCompressedSize()), MimeType_PlainText); 1608 call.GetOutput().AnswerBuffer(boost::lexical_cast<std::string>(info.GetCompressedSize()), MimeType_PlainText);
1526 } 1609 }
2396 Register("/instances/{id}/header", GetInstanceHeader); 2479 Register("/instances/{id}/header", GetInstanceHeader);
2397 2480
2398 Register("/patients/{id}/protected", IsProtectedPatient); 2481 Register("/patients/{id}/protected", IsProtectedPatient);
2399 Register("/patients/{id}/protected", SetPatientProtection); 2482 Register("/patients/{id}/protected", SetPatientProtection);
2400 2483
2401 Register("/{resourceType}/{id}/metadata", ListMetadata); 2484 std::vector<std::string> resourceTypes;
2402 Register("/{resourceType}/{id}/metadata/{name}", DeleteMetadata); 2485 resourceTypes.push_back("patients");
2403 Register("/{resourceType}/{id}/metadata/{name}", GetMetadata); 2486 resourceTypes.push_back("studies");
2404 Register("/{resourceType}/{id}/metadata/{name}", SetMetadata); 2487 resourceTypes.push_back("series");
2405 2488 resourceTypes.push_back("instances");
2406 Register("/{resourceType}/{id}/attachments", ListAttachments); 2489
2407 Register("/{resourceType}/{id}/attachments/{name}", DeleteAttachment); 2490 for (size_t i = 0; i < resourceTypes.size(); i++)
2408 Register("/{resourceType}/{id}/attachments/{name}", GetAttachmentOperations); 2491 {
2409 Register("/{resourceType}/{id}/attachments/{name}", UploadAttachment); 2492 Register("/" + resourceTypes[i] + "/{id}/metadata", ListMetadata);
2410 Register("/{resourceType}/{id}/attachments/{name}/compress", ChangeAttachmentCompression<CompressionType_ZlibWithSize>); 2493 Register("/" + resourceTypes[i] + "/{id}/metadata/{name}", DeleteMetadata);
2411 Register("/{resourceType}/{id}/attachments/{name}/compressed-data", GetAttachmentData<0>); 2494 Register("/" + resourceTypes[i] + "/{id}/metadata/{name}", GetMetadata);
2412 Register("/{resourceType}/{id}/attachments/{name}/compressed-md5", GetAttachmentCompressedMD5); 2495 Register("/" + resourceTypes[i] + "/{id}/metadata/{name}", SetMetadata);
2413 Register("/{resourceType}/{id}/attachments/{name}/compressed-size", GetAttachmentCompressedSize); 2496
2414 Register("/{resourceType}/{id}/attachments/{name}/data", GetAttachmentData<1>); 2497 Register("/" + resourceTypes[i] + "/{id}/attachments", ListAttachments);
2415 Register("/{resourceType}/{id}/attachments/{name}/is-compressed", IsAttachmentCompressed); 2498 Register("/" + resourceTypes[i] + "/{id}/attachments/{name}", DeleteAttachment);
2416 Register("/{resourceType}/{id}/attachments/{name}/md5", GetAttachmentMD5); 2499 Register("/" + resourceTypes[i] + "/{id}/attachments/{name}", GetAttachmentOperations);
2417 Register("/{resourceType}/{id}/attachments/{name}/size", GetAttachmentSize); 2500 Register("/" + resourceTypes[i] + "/{id}/attachments/{name}", UploadAttachment);
2418 Register("/{resourceType}/{id}/attachments/{name}/uncompress", ChangeAttachmentCompression<CompressionType_None>); 2501 Register("/" + resourceTypes[i] + "/{id}/attachments/{name}/compress", ChangeAttachmentCompression<CompressionType_ZlibWithSize>);
2419 Register("/{resourceType}/{id}/attachments/{name}/verify-md5", VerifyAttachment); 2502 Register("/" + resourceTypes[i] + "/{id}/attachments/{name}/compressed-data", GetAttachmentData<0>);
2503 Register("/" + resourceTypes[i] + "/{id}/attachments/{name}/compressed-md5", GetAttachmentCompressedMD5);
2504 Register("/" + resourceTypes[i] + "/{id}/attachments/{name}/compressed-size", GetAttachmentCompressedSize);
2505 Register("/" + resourceTypes[i] + "/{id}/attachments/{name}/data", GetAttachmentData<1>);
2506 Register("/" + resourceTypes[i] + "/{id}/attachments/{name}/is-compressed", IsAttachmentCompressed);
2507 Register("/" + resourceTypes[i] + "/{id}/attachments/{name}/md5", GetAttachmentMD5);
2508 Register("/" + resourceTypes[i] + "/{id}/attachments/{name}/size", GetAttachmentSize);
2509 Register("/" + resourceTypes[i] + "/{id}/attachments/{name}/uncompress", ChangeAttachmentCompression<CompressionType_None>);
2510 Register("/" + resourceTypes[i] + "/{id}/attachments/{name}/verify-md5", VerifyAttachment);
2511 }
2420 2512
2421 Register("/tools/invalidate-tags", InvalidateTags); 2513 Register("/tools/invalidate-tags", InvalidateTags);
2422 Register("/tools/lookup", Lookup); 2514 Register("/tools/lookup", Lookup);
2423 Register("/tools/find", Find); 2515 Register("/tools/find", Find);
2424 2516