comparison OrthancFramework/Sources/RestApi/RestApi.cpp @ 4414:d928dfcacb4b

cont openapi
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 28 Dec 2020 14:46:51 +0100
parents 22a1352a0823
children a4518adede59
comparison
equal deleted inserted replaced
4413:22a1352a0823 4414:d928dfcacb4b
432 std::string getTag_; 432 std::string getTag_;
433 std::string postTag_; 433 std::string postTag_;
434 std::string deleteTag_; 434 std::string deleteTag_;
435 std::string putTag_; 435 std::string putTag_;
436 std::string summary_; 436 std::string summary_;
437 bool getDeprecated_;
438 bool postDeprecated_;
439 bool deleteDeprecated_;
440 bool putDeprecated_;
437 HttpMethod summaryOrigin_; 441 HttpMethod summaryOrigin_;
438 442
439 public: 443 public:
440 Path() : 444 Path() :
441 hasGet_(false), 445 hasGet_(false),
442 hasPost_(false), 446 hasPost_(false),
443 hasDelete_(false), 447 hasDelete_(false),
444 hasPut_(false), 448 hasPut_(false),
449 getDeprecated_(false),
450 postDeprecated_(false),
451 deleteDeprecated_(false),
452 putDeprecated_(false),
445 summaryOrigin_(HttpMethod_Get) // Dummy initialization 453 summaryOrigin_(HttpMethod_Get) // Dummy initialization
446 { 454 {
447 } 455 }
448 456
449 void AddMethod(HttpMethod method, 457 void AddMethod(HttpMethod method,
450 const std::string& tag) 458 const std::string& tag,
459 bool deprecated)
451 { 460 {
452 switch (method) 461 switch (method)
453 { 462 {
454 case HttpMethod_Get: 463 case HttpMethod_Get:
455 if (hasGet_) 464 if (hasGet_)
457 throw OrthancException(ErrorCode_InternalError); 466 throw OrthancException(ErrorCode_InternalError);
458 } 467 }
459 468
460 hasGet_ = true; 469 hasGet_ = true;
461 getTag_ = tag; 470 getTag_ = tag;
471 getDeprecated_ = deprecated;
462 break; 472 break;
463 473
464 case HttpMethod_Post: 474 case HttpMethod_Post:
465 if (hasPost_) 475 if (hasPost_)
466 { 476 {
467 throw OrthancException(ErrorCode_InternalError); 477 throw OrthancException(ErrorCode_InternalError);
468 } 478 }
469 479
470 hasPost_ = true; 480 hasPost_ = true;
471 postTag_ = tag; 481 postTag_ = tag;
482 postDeprecated_ = deprecated;
472 break; 483 break;
473 484
474 case HttpMethod_Delete: 485 case HttpMethod_Delete:
475 if (hasDelete_) 486 if (hasDelete_)
476 { 487 {
477 throw OrthancException(ErrorCode_InternalError); 488 throw OrthancException(ErrorCode_InternalError);
478 } 489 }
479 490
480 hasDelete_ = true; 491 hasDelete_ = true;
481 deleteTag_ = tag; 492 deleteTag_ = tag;
493 deleteDeprecated_ = deprecated;
482 break; 494 break;
483 495
484 case HttpMethod_Put: 496 case HttpMethod_Put:
485 if (hasPut_) 497 if (hasPut_)
486 { 498 {
487 throw OrthancException(ErrorCode_InternalError); 499 throw OrthancException(ErrorCode_InternalError);
488 } 500 }
489 501
490 hasPut_ = true; 502 hasPut_ = true;
491 putTag_ = tag; 503 putTag_ = tag;
504 putDeprecated_ = deprecated;
492 break; 505 break;
493 506
494 default: 507 default:
495 throw OrthancException(ErrorCode_ParameterOutOfRange); 508 throw OrthancException(ErrorCode_ParameterOutOfRange);
496 } 509 }
589 HttpMethod method, 602 HttpMethod method,
590 const std::string& uri) const 603 const std::string& uri) const
591 { 604 {
592 std::string p = uri; 605 std::string p = uri;
593 boost::replace_all(p, "/", "~1"); 606 boost::replace_all(p, "/", "~1");
607
608 std::string verb;
609 std::string url;
594 610
595 switch (method) 611 switch (method)
596 { 612 {
597 case HttpMethod_Get: 613 case HttpMethod_Get:
598 if (hasGet_) 614 if (hasGet_)
599 { 615 {
600 if (openApiUrl.empty()) 616 verb = (getDeprecated_ ? "(get)" : "GET");
601 { 617 url = openApiUrl + "#tag/" + FormatTag(getTag_) + "/paths/" + p + "/get";
602 return "GET";
603 }
604 else
605 {
606 return ("`GET <" + openApiUrl + "#tag/" + FormatTag(getTag_) + "/paths/" + p + "/get>`__");
607 }
608 } 618 }
609 break; 619 break;
610 620
611 case HttpMethod_Post: 621 case HttpMethod_Post:
612 if (hasPost_) 622 if (hasPost_)
613 { 623 {
614 if (openApiUrl.empty()) 624 verb = (postDeprecated_ ? "(post)" : "POST");
615 { 625 url = openApiUrl + "#tag/" + FormatTag(postTag_) + "/paths/" + p + "/post";
616 return "POST";
617 }
618 else
619 {
620 return ("`POST <" + openApiUrl + "#tag/" + FormatTag(postTag_) + "/paths/" + p + "/post>`__");
621 }
622 } 626 }
623 break; 627 break;
624 628
625 case HttpMethod_Delete: 629 case HttpMethod_Delete:
626 if (hasDelete_) 630 if (hasDelete_)
627 { 631 {
628 if (openApiUrl.empty()) 632 verb = (deleteDeprecated_ ? "(delete)" : "DELETE");
629 { 633 url = openApiUrl + "#tag/" + FormatTag(deleteTag_) + "/paths/" + p + "/delete";
630 return "DELETE";
631 }
632 else
633 {
634 return ("`DELETE <" + openApiUrl + "#tag/" + FormatTag(deleteTag_) + "/paths/" + p + "/delete>`__");
635 }
636 } 634 }
637 break; 635 break;
638 636
639 case HttpMethod_Put: 637 case HttpMethod_Put:
640 if (hasPut_) 638 if (hasPut_)
641 { 639 {
642 if (openApiUrl.empty()) 640 verb = (putDeprecated_ ? "(put)" : "PUT");
643 { 641 url = openApiUrl + "#tag/" + FormatTag(putTag_) + "/paths/" + p + "/put";
644 return "GET"; 642 }
645 } 643 break;
646 else
647 {
648 return ("`PUT <" + openApiUrl + "#tag/" + FormatTag(putTag_) + "/paths/" + p + "/put>`__");
649 }
650 }
651 break;
652 644
653 default: 645 default:
654 throw OrthancException(ErrorCode_InternalError); 646 throw OrthancException(ErrorCode_InternalError);
655 } 647 }
656 648
657 return ""; 649 if (verb.empty())
650 {
651 return "";
652 }
653 else if (openApiUrl.empty())
654 {
655 return verb;
656 }
657 else
658 {
659 return "`" + verb + " <" + url + ">`__";
660 }
661 }
662
663 bool HasDeprecated() const
664 {
665 return ((hasGet_ && getDeprecated_) ||
666 (hasPost_ && postDeprecated_) ||
667 (hasDelete_ && deleteDeprecated_) ||
668 (hasPut_ && putDeprecated_));
658 } 669 }
659 }; 670 };
660 671
661 typedef std::map<std::string, Path> Paths; 672 typedef std::map<std::string, Path> Paths;
662 673
666 virtual bool HandleCall(RestApiCall& call, 677 virtual bool HandleCall(RestApiCall& call,
667 const std::set<std::string> uriArgumentsNames) ORTHANC_OVERRIDE 678 const std::set<std::string> uriArgumentsNames) ORTHANC_OVERRIDE
668 { 679 {
669 Path& path = paths_[ Toolbox::FlattenUri(call.GetFullUri()) ]; 680 Path& path = paths_[ Toolbox::FlattenUri(call.GetFullUri()) ];
670 681
671 path.AddMethod(call.GetMethod(), call.GetDocumentation().GetTag()); 682 path.AddMethod(call.GetMethod(), call.GetDocumentation().GetTag(), call.GetDocumentation().IsDeprecated());
672 683
673 if (call.GetDocumentation().HasSummary()) 684 if (call.GetDocumentation().HasSummary())
674 { 685 {
675 path.SetSummary(call.GetDocumentation().GetSummary(), call.GetMethod()); 686 path.SetSummary(call.GetDocumentation().GetSummary(), call.GetMethod());
676 } 687 }
693 target += "``" + it->first + "``,"; 704 target += "``" + it->first + "``,";
694 target += it->second.Format(openApiUrl, HttpMethod_Get, it->first) + ","; 705 target += it->second.Format(openApiUrl, HttpMethod_Get, it->first) + ",";
695 target += it->second.Format(openApiUrl, HttpMethod_Post, it->first) + ","; 706 target += it->second.Format(openApiUrl, HttpMethod_Post, it->first) + ",";
696 target += it->second.Format(openApiUrl, HttpMethod_Delete, it->first) + ","; 707 target += it->second.Format(openApiUrl, HttpMethod_Delete, it->first) + ",";
697 target += it->second.Format(openApiUrl, HttpMethod_Put, it->first) + ","; 708 target += it->second.Format(openApiUrl, HttpMethod_Put, it->first) + ",";
709
710 if (it->second.HasDeprecated())
711 {
712 target += "*(deprecated)* ";
713 }
714
698 target += it->second.GetSummary() + "\n"; 715 target += it->second.GetSummary() + "\n";
699 } 716 }
700 } 717 }
701 }; 718 };
702 } 719 }