changeset 4415:b50410d0e98c

cont openapi
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 28 Dec 2020 15:32:01 +0100
parents d928dfcacb4b
children 0b27841950d5
files OrthancFramework/Sources/RestApi/RestApiCallDocumentation.cpp OrthancFramework/Sources/RestApi/RestApiCallDocumentation.h OrthancServer/Sources/OrthancRestApi/OrthancRestResources.cpp
diffstat 3 files changed, 116 insertions(+), 28 deletions(-) [+]
line wrap: on
line diff
--- a/OrthancFramework/Sources/RestApi/RestApiCallDocumentation.cpp	Mon Dec 28 14:46:51 2020 +0100
+++ b/OrthancFramework/Sources/RestApi/RestApiCallDocumentation.cpp	Mon Dec 28 15:32:01 2020 +0100
@@ -179,6 +179,8 @@
   void RestApiCallDocumentation::SetHttpGetSample(const std::string& url,
                                                   bool isJson)
   {
+    //return;  // TODO -REMOVE
+    
 #if ORTHANC_ENABLE_CURL == 1
     HttpClient client;
     client.SetUrl(url);
@@ -210,6 +212,59 @@
   }
 
 
+  static void Truncate(Json::Value& value,
+                       size_t size)
+  {
+    if (value.type() == Json::arrayValue)
+    {
+      if (value.size() > size)
+      {
+        value.resize(size);
+        value.append("...");
+      }
+
+      for (Json::Value::ArrayIndex i = 0; i < value.size(); i++)
+      {
+        Truncate(value[i], size);
+      }
+    }
+    else if (value.type() == Json::objectValue)
+    {
+      std::vector<std::string> members = value.getMemberNames();
+      if (members.size() > size)
+      {
+        members.resize(size);
+
+        Json::Value v = Json::objectValue;
+        for (size_t i = 0; i < members.size(); i++)
+        {
+          v[members[i]] = value[members[i]];
+        }
+
+        // We use the "{" symbol, as it the last in the 7bit ASCII
+        // table, which places "..." at the end of the object in OpenAPI
+        v["{...}"] = "...";
+        
+        value = v;
+      }
+
+      for (size_t i = 0; i < members.size(); i++)
+      {
+        Truncate(value[members[i]], size);
+      }
+    }
+  }
+
+  
+  void RestApiCallDocumentation::SetTruncatedJsonHttpGetSample(const std::string& url,
+                                                               size_t size)
+  {
+    SetHttpGetSample(url, true);
+    Truncate(sampleJson_, size);
+  }
+
+
+
   static void TypeToSchema(Json::Value& target,
                            RestApiCallDocumentation::Type type)
   {
--- a/OrthancFramework/Sources/RestApi/RestApiCallDocumentation.h	Mon Dec 28 14:46:51 2020 +0100
+++ b/OrthancFramework/Sources/RestApi/RestApiCallDocumentation.h	Mon Dec 28 15:32:01 2020 +0100
@@ -175,6 +175,9 @@
     void SetHttpGetSample(const std::string& url,
                           bool isJson);
 
+    void SetTruncatedJsonHttpGetSample(const std::string& url,
+                                       size_t size);
+
     void SetSample(const Json::Value& sample)
     {
       sampleJson_ = sample;
--- a/OrthancServer/Sources/OrthancRestApi/OrthancRestResources.cpp	Mon Dec 28 14:46:51 2020 +0100
+++ b/OrthancServer/Sources/OrthancRestApi/OrthancRestResources.cpp	Mon Dec 28 15:32:01 2020 +0100
@@ -67,6 +67,32 @@
 
 namespace Orthanc
 {
+  static std::string GetDocumentationSampleResource(ResourceType type)
+  {
+    switch (type)
+    {
+      case Orthanc::ResourceType_Instance:
+        return "https://demo.orthanc-server.com/instances/d94d9a03-3003b047-a4affc69-322313b2-680530a2";
+        break;
+        
+      case Orthanc::ResourceType_Series:
+        return "https://demo.orthanc-server.com/series/37836232-d13a2350-fa1dedc5-962b31aa-010f8e52";
+        break;
+        
+      case Orthanc::ResourceType_Study:
+        return "https://demo.orthanc-server.com/studies/27f7126f-4f66fb14-03f4081b-f9341db2-53925988";
+        break;
+        
+      case Orthanc::ResourceType_Patient:
+        return "https://demo.orthanc-server.com/patients/46e6332c-677825b6-202fcf7c-f787bc5f-7b07c382";
+        break;
+        
+      default:
+        throw OrthancException(ErrorCode_ParameterOutOfRange);
+    }
+  }
+  
+
   static void AnswerDicomAsJson(RestApiCall& call,
                                 const Json::Value& dicom,
                                 DicomToJsonFormat mode)
@@ -214,32 +240,6 @@
 
 
 
-  static std::string GetDocumentationSampleResource(ResourceType type)
-  {
-    switch (type)
-    {
-      case Orthanc::ResourceType_Instance:
-        return "https://demo.orthanc-server.com/instances/d94d9a03-3003b047-a4affc69-322313b2-680530a2";
-        break;
-        
-      case Orthanc::ResourceType_Series:
-        return "https://demo.orthanc-server.com/series/37836232-d13a2350-fa1dedc5-962b31aa-010f8e52";
-        break;
-        
-      case Orthanc::ResourceType_Study:
-        return "https://demo.orthanc-server.com/studies/27f7126f-4f66fb14-03f4081b-f9341db2-53925988";
-        break;
-        
-      case Orthanc::ResourceType_Patient:
-        return "https://demo.orthanc-server.com/patients/46e6332c-677825b6-202fcf7c-f787bc5f-7b07c382";
-        break;
-        
-      default:
-        throw OrthancException(ErrorCode_ParameterOutOfRange);
-    }
-  }
-  
-
   template <enum ResourceType resourceType>
   static void GetSingleResource(RestApiGetCall& call)
   {
@@ -436,7 +436,7 @@
           .SetDescription("Get the DICOM tags in human-readable format")
           .SetUriArgument("id", "Orthanc identifier of the DICOM instance of interest")
           .AddAnswerType(MimeType_Json, "JSON object containing the DICOM tags and their associated value")
-          .SetHttpGetSample("https://demo.orthanc-server.com/instances/7c92ce8e-bbf67ed2-ffa3b8c1-a3b35d94-7ff3ae26/simplified-tags", true);
+          .SetTruncatedJsonHttpGetSample("https://demo.orthanc-server.com/instances/7c92ce8e-bbf67ed2-ffa3b8c1-a3b35d94-7ff3ae26/simplified-tags", 10);
         return;
       }
       else
@@ -487,7 +487,7 @@
         .SetHttpGetArgument("short", RestApiCallDocumentation::Type_String,
                             "If present, report the DICOM tags indexed in hexadecimal format", false)
         .AddAnswerType(MimeType_Json, "JSON object containing the DICOM tags and their associated value")
-        .SetHttpGetSample("https://demo.orthanc-server.com/instances/7c92ce8e-bbf67ed2-ffa3b8c1-a3b35d94-7ff3ae26/tags", true);      
+        .SetTruncatedJsonHttpGetSample("https://demo.orthanc-server.com/instances/7c92ce8e-bbf67ed2-ffa3b8c1-a3b35d94-7ff3ae26/tags", 10);
       return;
     }
 
@@ -2377,6 +2377,21 @@
             enum ResourceType end>
   static void GetChildResources(RestApiGetCall& call)
   {
+    if (call.IsDocumentation())
+    {
+      const std::string children = GetResourceTypeText(end, true /* plural */, false /* lower case */);
+      const std::string resource = GetResourceTypeText(start, false /* plural */, false /* lower case */);
+      call.GetDocumentation()
+        .SetTag(GetResourceTypeText(start, true /* plural */, true /* upper case */))
+        .SetSummary("Get child " + children)
+        .SetDescription("Get detailed information about the child " + children + " of the DICOM " +
+                        resource + " of interest whose Orthanc identifier is provided in the URL")
+        .SetUriArgument("id", "Orthanc identifier of the " + resource + " of interest")
+        .AddAnswerType(MimeType_Json, "JSON array containing information about the child DICOM " + children)
+        .SetTruncatedJsonHttpGetSample(GetDocumentationSampleResource(start) + "/" + children, 5);
+      return;
+    }
+
     ServerIndex& index = OrthancRestApi::GetIndex(call);
 
     std::list<std::string> a, b, c;
@@ -2463,6 +2478,21 @@
   {
     assert(start > end);
 
+    if (call.IsDocumentation())
+    {
+      const std::string parent = GetResourceTypeText(end, false /* plural */, false /* lower case */);
+      const std::string resource = GetResourceTypeText(start, false /* plural */, false /* lower case */);
+      call.GetDocumentation()
+        .SetTag(GetResourceTypeText(start, true /* plural */, true /* upper case */))
+        .SetSummary("Get parent " + parent)
+        .SetDescription("Get detailed information about the parent " + parent + " of the DICOM " +
+                        resource + " of interest whose Orthanc identifier is provided in the URL")
+        .SetUriArgument("id", "Orthanc identifier of the " + resource + " of interest")
+        .AddAnswerType(MimeType_Json, "Information about the parent DICOM " + parent)
+        .SetTruncatedJsonHttpGetSample(GetDocumentationSampleResource(start) + "/" + parent, 10);
+      return;
+    }
+
     ServerIndex& index = OrthancRestApi::GetIndex(call);
     
     std::string current = call.GetUriComponent("id", "");