comparison OrthancServer/Sources/OrthancRestApi/OrthancRestResources.cpp @ 4399:80fd140b12ba

New command-line option: "--openapi" to write the OpenAPI documentation of the REST API to a file
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 23 Dec 2020 12:21:03 +0100
parents a01b1c9cbef4
children 354ea95b294a
comparison
equal deleted inserted replaced
4398:38c22715bb56 4399:80fd140b12ba
159 159
160 160
161 template <enum ResourceType resourceType> 161 template <enum ResourceType resourceType>
162 static void ListResources(RestApiGetCall& call) 162 static void ListResources(RestApiGetCall& call)
163 { 163 {
164 if (call.IsDocumentation())
165 {
166 const std::string resources = GetResourceTypeText(resourceType, true /* plural */, false /* lower case */);
167 call.GetDocumentation()
168 .SetTag(GetResourceTypeText(resourceType, true /* plural */, true /* upper case */))
169 .SetSummary("List the available " + resources)
170 .SetDescription("List the Orthanc identifiers of all the available DICOM " + resources)
171 .SetHttpGetArgument("limit", RestApiCallDocumentation::Type_Number, "Limit the number of results")
172 .SetHttpGetArgument("since", RestApiCallDocumentation::Type_Number, "Show only the resources since the provided index")
173 .SetHttpGetArgument("expand", RestApiCallDocumentation::Type_String,
174 "If present, retrieve detailed information about the individual " + resources)
175 .SetHttpGetSample("https://demo.orthanc-server.com/" + resources + "?since=0&limit=2");
176 return;
177 }
178
164 ServerIndex& index = OrthancRestApi::GetIndex(call); 179 ServerIndex& index = OrthancRestApi::GetIndex(call);
165 180
166 std::list<std::string> result; 181 std::list<std::string> result;
167 182
168 if (call.HasArgument("limit") || 183 if (call.HasArgument("limit") ||
196 } 211 }
197 212
198 template <enum ResourceType resourceType> 213 template <enum ResourceType resourceType>
199 static void GetSingleResource(RestApiGetCall& call) 214 static void GetSingleResource(RestApiGetCall& call)
200 { 215 {
216 if (call.IsDocumentation())
217 {
218 std::string sampleUrl;
219 switch (resourceType)
220 {
221 case Orthanc::ResourceType_Instance:
222 sampleUrl = "https://demo.orthanc-server.com/instances/d94d9a03-3003b047-a4affc69-322313b2-680530a2";
223 break;
224 case Orthanc::ResourceType_Series:
225 sampleUrl = "https://demo.orthanc-server.com/series/37836232-d13a2350-fa1dedc5-962b31aa-010f8e52";
226 break;
227 case Orthanc::ResourceType_Study:
228 sampleUrl = "https://demo.orthanc-server.com/studies/27f7126f-4f66fb14-03f4081b-f9341db2-53925988";
229 break;
230 case Orthanc::ResourceType_Patient:
231 sampleUrl = "https://demo.orthanc-server.com/patients/46e6332c-677825b6-202fcf7c-f787bc5f-7b07c382";
232 break;
233 default:
234 throw OrthancException(ErrorCode_ParameterOutOfRange);
235 }
236
237 const std::string resource = GetResourceTypeText(resourceType, false /* plural */, false /* lower case */);
238 call.GetDocumentation()
239 .SetTag(GetResourceTypeText(resourceType, true /* plural */, true /* upper case */))
240 .SetSummary("Get information about some " + resource)
241 .SetDescription("Get information about the DICOM " + resource + " of interest whose Orthanc identifier is provided in the URL")
242 .SetUriComponent("id", RestApiCallDocumentation::Type_String, "Orthanc identifier of the " + resource + " of interest")
243 .SetHttpGetSample(sampleUrl);
244 return;
245 }
246
201 Json::Value result; 247 Json::Value result;
202 if (OrthancRestApi::GetIndex(call).LookupResource(result, call.GetUriComponent("id", ""), resourceType)) 248 if (OrthancRestApi::GetIndex(call).LookupResource(result, call.GetUriComponent("id", ""), resourceType))
203 { 249 {
204 call.GetOutput().AnswerJson(result); 250 call.GetOutput().AnswerJson(result);
205 } 251 }
206 } 252 }
207 253
208 template <enum ResourceType resourceType> 254 template <enum ResourceType resourceType>
209 static void DeleteSingleResource(RestApiDeleteCall& call) 255 static void DeleteSingleResource(RestApiDeleteCall& call)
210 { 256 {
257 if (call.IsDocumentation())
258 {
259 const std::string resource = GetResourceTypeText(resourceType, false /* plural */, false /* lower case */);
260 call.GetDocumentation()
261 .SetTag(GetResourceTypeText(resourceType, true /* plural */, true /* upper case */))
262 .SetSummary("Delete some " + resource)
263 .SetDescription("Delete the DICOM " + resource + " of interest whose Orthanc identifier is provided in the URL")
264 .SetUriComponent("id", RestApiCallDocumentation::Type_String, "Orthanc identifier of the " + resource + " of interest");
265 return;
266 }
267
211 Json::Value result; 268 Json::Value result;
212 if (OrthancRestApi::GetContext(call).DeleteResource(result, call.GetUriComponent("id", ""), resourceType)) 269 if (OrthancRestApi::GetContext(call).DeleteResource(result, call.GetUriComponent("id", ""), resourceType))
213 { 270 {
214 call.GetOutput().AnswerJson(result); 271 call.GetOutput().AnswerJson(result);
215 } 272 }
218 275
219 // Get information about a single patient ----------------------------------- 276 // Get information about a single patient -----------------------------------
220 277
221 static void IsProtectedPatient(RestApiGetCall& call) 278 static void IsProtectedPatient(RestApiGetCall& call)
222 { 279 {
280 if (call.IsDocumentation())
281 {
282 call.GetDocumentation()
283 .SetTag("Patients")
284 .SetSummary("Is the patient protected against recycling?")
285 .SetUriComponent("id", RestApiCallDocumentation::Type_String, "Orthanc identifier of the patient of interest")
286 .AddAnswerType(MimeType_PlainText, "\"1\" if protected, \"0\" if not protected");
287 return;
288 }
289
223 std::string publicId = call.GetUriComponent("id", ""); 290 std::string publicId = call.GetUriComponent("id", "");
224 bool isProtected = OrthancRestApi::GetIndex(call).IsProtectedPatient(publicId); 291 bool isProtected = OrthancRestApi::GetIndex(call).IsProtectedPatient(publicId);
225 call.GetOutput().AnswerBuffer(isProtected ? "1" : "0", MimeType_PlainText); 292 call.GetOutput().AnswerBuffer(isProtected ? "1" : "0", MimeType_PlainText);
226 } 293 }
227 294
228 295
229 static void SetPatientProtection(RestApiPutCall& call) 296 static void SetPatientProtection(RestApiPutCall& call)
230 { 297 {
298 if (call.IsDocumentation())
299 {
300 call.GetDocumentation()
301 .SetTag("Patients")
302 .SetSummary("Protect one patient against recycling")
303 .SetDescription("Check out configuration options \"MaximumStorageSize\" and \"MaximumPatientCount\"")
304 .SetUriComponent("id", RestApiCallDocumentation::Type_String, "Orthanc identifier of the patient of interest");
305 return;
306 }
307
231 ServerContext& context = OrthancRestApi::GetContext(call); 308 ServerContext& context = OrthancRestApi::GetContext(call);
232 309
233 std::string publicId = call.GetUriComponent("id", ""); 310 std::string publicId = call.GetUriComponent("id", "");
234 311
235 std::string body; 312 std::string body;