comparison OrthancFramework/Sources/RestApi/RestApiHierarchy.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 029366f95217
comparison
equal deleted inserted replaced
4398:38c22715bb56 4399:80fd140b12ba
37 deleteHandler_(NULL) 37 deleteHandler_(NULL)
38 { 38 {
39 } 39 }
40 40
41 41
42 bool RestApiHierarchy::Resource::HasHandler(HttpMethod method) const 42 bool RestApiHierarchy::Resource::HasMethod(HttpMethod method) const
43 { 43 {
44 switch (method) 44 switch (method)
45 { 45 {
46 case HttpMethod_Get: 46 case HttpMethod_Get:
47 return getHandler_ != NULL; 47 return getHandler_ != NULL;
184 { 184 {
185 if (path.GetLevelCount() == level) 185 if (path.GetLevelCount() == level)
186 { 186 {
187 if (path.IsUniversalTrailing()) 187 if (path.IsUniversalTrailing())
188 { 188 {
189 universalHandlers_.Register(handler); 189 handlersWithTrailing_.Register(handler);
190 } 190 }
191 else 191 else
192 { 192 {
193 handlers_.Register(handler); 193 handlers_.Register(handler);
194 } 194 }
219 level > uri.size()) 219 level > uri.size())
220 { 220 {
221 return false; 221 return false;
222 } 222 }
223 223
224 UriComponents trailing;
225
226 // Look for an exact match on the resource of interest 224 // Look for an exact match on the resource of interest
227 if (uri.size() == 0 || 225 if (uri.size() == 0 ||
228 level == uri.size()) 226 level == uri.size())
229 { 227 {
228 UriComponents noTrailing;
229
230 if (!handlers_.IsEmpty() && 230 if (!handlers_.IsEmpty() &&
231 visitor.Visit(handlers_, uri, components, trailing)) 231 visitor.Visit(handlers_, uri, false, components, noTrailing))
232 { 232 {
233 return true; 233 return true;
234 } 234 }
235 } 235 }
236 236
261 } 261 }
262 } 262 }
263 263
264 264
265 // As a last resort, call the universal handlers, if any 265 // As a last resort, call the universal handlers, if any
266 if (!universalHandlers_.IsEmpty()) 266 if (!handlersWithTrailing_.IsEmpty())
267 { 267 {
268 UriComponents trailing;
268 trailing.resize(uri.size() - level); 269 trailing.resize(uri.size() - level);
269 size_t pos = 0; 270 size_t pos = 0;
270 for (size_t i = level; i < uri.size(); i++, pos++) 271 for (size_t i = level; i < uri.size(); i++, pos++)
271 { 272 {
272 trailing[pos] = uri[i]; 273 trailing[pos] = uri[i];
273 } 274 }
274 275
275 assert(pos == trailing.size()); 276 assert(pos == trailing.size());
276 277
277 if (visitor.Visit(universalHandlers_, uri, components, trailing)) 278 if (visitor.Visit(handlersWithTrailing_, uri, true, components, trailing))
278 { 279 {
279 return true; 280 return true;
280 } 281 }
281 } 282 }
282 283
284 } 285 }
285 286
286 287
287 bool RestApiHierarchy::CanGenerateDirectory() const 288 bool RestApiHierarchy::CanGenerateDirectory() const
288 { 289 {
289 return (universalHandlers_.IsEmpty() && 290 return (handlersWithTrailing_.IsEmpty() &&
290 wildcardChildren_.empty()); 291 wildcardChildren_.empty());
291 } 292 }
292 293
293 294
294 bool RestApiHierarchy::GetDirectory(Json::Value& result, 295 bool RestApiHierarchy::GetDirectory(Json::Value& result,
374 void RestApiHierarchy::CreateSiteMap(Json::Value& target) const 375 void RestApiHierarchy::CreateSiteMap(Json::Value& target) const
375 { 376 {
376 target = Json::objectValue; 377 target = Json::objectValue;
377 378
378 /*std::string s = " "; 379 /*std::string s = " ";
379 if (handlers_.HasHandler(HttpMethod_Get)) 380 if (handlers_.HasMethod(HttpMethod_Get))
380 { 381 {
381 s += "GET "; 382 s += "GET ";
382 } 383 }
383 384
384 if (handlers_.HasHandler(HttpMethod_Post)) 385 if (handlers_.HasMethod(HttpMethod_Post))
385 { 386 {
386 s += "POST "; 387 s += "POST ";
387 } 388 }
388 389
389 if (handlers_.HasHandler(HttpMethod_Put)) 390 if (handlers_.HasMethod(HttpMethod_Put))
390 { 391 {
391 s += "PUT "; 392 s += "PUT ";
392 } 393 }
393 394
394 if (handlers_.HasHandler(HttpMethod_Delete)) 395 if (handlers_.HasMethod(HttpMethod_Delete))
395 { 396 {
396 s += "DELETE "; 397 s += "DELETE ";
397 } 398 }
398 399
399 target = s;*/ 400 target = s;*/
441 { 442 {
442 } 443 }
443 444
444 virtual bool Visit(const RestApiHierarchy::Resource& resource, 445 virtual bool Visit(const RestApiHierarchy::Resource& resource,
445 const UriComponents& uri, 446 const UriComponents& uri,
447 bool hasTrailing,
446 const HttpToolbox::Arguments& components, 448 const HttpToolbox::Arguments& components,
447 const UriComponents& trailing) 449 const UriComponents& trailing)
448 { 450 {
449 if (trailing.size() == 0) // Ignore universal handlers 451 if (!hasTrailing) // Ignore universal handlers
450 { 452 {
451 if (resource.HasHandler(HttpMethod_Get)) 453 if (resource.HasMethod(HttpMethod_Get))
452 { 454 {
453 methods_.insert(HttpMethod_Get); 455 methods_.insert(HttpMethod_Get);
454 } 456 }
455 457
456 if (resource.HasHandler(HttpMethod_Post)) 458 if (resource.HasMethod(HttpMethod_Post))
457 { 459 {
458 methods_.insert(HttpMethod_Post); 460 methods_.insert(HttpMethod_Post);
459 } 461 }
460 462
461 if (resource.HasHandler(HttpMethod_Put)) 463 if (resource.HasMethod(HttpMethod_Put))
462 { 464 {
463 methods_.insert(HttpMethod_Put); 465 methods_.insert(HttpMethod_Put);
464 } 466 }
465 467
466 if (resource.HasHandler(HttpMethod_Delete)) 468 if (resource.HasMethod(HttpMethod_Delete))
467 { 469 {
468 methods_.insert(HttpMethod_Delete); 470 methods_.insert(HttpMethod_Delete);
469 } 471 }
470 } 472 }
471 473
487 methods.insert(HttpMethod_Get); 489 methods.insert(HttpMethod_Get);
488 } 490 }
489 } 491 }
490 } 492 }
491 493
494 void RestApiHierarchy::ExploreAllResources(IVisitor& visitor,
495 const UriComponents& path) const
496 {
497 if (!handlers_.IsEmpty())
498 {
499 visitor.Visit(handlers_, path, false, HttpToolbox::Arguments(), UriComponents());
500 }
501
502 if (!handlersWithTrailing_.IsEmpty())
503 {
504 visitor.Visit(handlersWithTrailing_, path, true, HttpToolbox::Arguments(), UriComponents());
505 }
506
507 for (Children::const_iterator
508 it = children_.begin(); it != children_.end(); ++it)
509 {
510 assert(it->second != NULL);
511 UriComponents c = path;
512 c.push_back(it->first);
513 it->second->ExploreAllResources(visitor, c);
514 }
515
516 for (Children::const_iterator
517 it = wildcardChildren_.begin(); it != wildcardChildren_.end(); ++it)
518 {
519 assert(it->second != NULL);
520 UriComponents c = path;
521 c.push_back("{" + it->first + "}");
522 it->second->ExploreAllResources(visitor, c);
523 }
524 }
492 } 525 }