Mercurial > hg > orthanc
changeset 4622:9086aeb9d9d2 db-changes
RestApiCallDocumentation::SetAnswerHeader() and Toolbox::RemoveSurroundingQuotes()
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Fri, 16 Apr 2021 10:48:57 +0200 |
parents | 82dbe45c947a |
children | 95ffe3b6ef7c |
files | OrthancFramework/Sources/RestApi/RestApiCallDocumentation.cpp OrthancFramework/Sources/RestApi/RestApiCallDocumentation.h OrthancFramework/Sources/RestApi/RestApiOutput.h OrthancFramework/Sources/Toolbox.cpp OrthancFramework/Sources/Toolbox.h OrthancFramework/UnitTestsSources/FrameworkTests.cpp |
diffstat | 6 files changed, 72 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- a/OrthancFramework/Sources/RestApi/RestApiCallDocumentation.cpp Thu Apr 15 11:29:57 2021 +0200 +++ b/OrthancFramework/Sources/RestApi/RestApiCallDocumentation.cpp Fri Apr 16 10:48:57 2021 +0200 @@ -176,6 +176,21 @@ } + RestApiCallDocumentation& RestApiCallDocumentation::SetAnswerHeader(const std::string& name, + const std::string& description) + { + if (answerHeaders_.find(name) != answerHeaders_.end()) + { + throw OrthancException(ErrorCode_ParameterOutOfRange, "Answer HTTP header \"" + name + "\" is already documented"); + } + else + { + answerHeaders_[name] = Parameter(Type_String, description, false); + return *this; + } + } + + void RestApiCallDocumentation::SetHttpGetSample(const std::string& url, bool isJson) { @@ -430,6 +445,20 @@ target["responses"]["200"]["content"][EnumerationToString(MimeType_PlainText)]["example"] = sampleText_; } + if (!answerHeaders_.empty()) + { + Json::Value answerHeaders = Json::objectValue; + + for (Parameters::const_iterator it = answerHeaders_.begin(); it != answerHeaders_.end(); ++it) + { + Json::Value h = Json::objectValue; + h["description"] = it->second.GetDescription(); + answerHeaders[it->first] = h; + } + + target["responses"]["200"]["headers"] = answerHeaders; + } + Json::Value parameters = Json::arrayValue; for (Parameters::const_iterator it = getArguments_.begin();
--- a/OrthancFramework/Sources/RestApi/RestApiCallDocumentation.h Thu Apr 15 11:29:57 2021 +0200 +++ b/OrthancFramework/Sources/RestApi/RestApiCallDocumentation.h Fri Apr 16 10:48:57 2021 +0200 @@ -102,6 +102,7 @@ AllowedTypes answerTypes_; Parameters answerFields_; // Only if JSON object std::string answerDescription_; + Parameters answerHeaders_; bool hasSampleText_; std::string sampleText_; Json::Value sampleJson_; @@ -172,6 +173,9 @@ Type type, const std::string& description); + RestApiCallDocumentation& SetAnswerHeader(const std::string& name, + const std::string& description); + void SetHttpGetSample(const std::string& url, bool isJson);
--- a/OrthancFramework/Sources/RestApi/RestApiOutput.h Thu Apr 15 11:29:57 2021 +0200 +++ b/OrthancFramework/Sources/RestApi/RestApiOutput.h Fri Apr 16 10:48:57 2021 +0200 @@ -59,6 +59,11 @@ return convertJsonToXml_; } + HttpOutput& GetLowLevelOutput() const + { + return output_; + } + void AnswerStream(IHttpStreamAnswer& stream); void AnswerJson(const Json::Value& value);
--- a/OrthancFramework/Sources/Toolbox.cpp Thu Apr 15 11:29:57 2021 +0200 +++ b/OrthancFramework/Sources/Toolbox.cpp Fri Apr 16 10:48:57 2021 +0200 @@ -2385,6 +2385,17 @@ target = Json::writeString(builder, source); #endif } + + + void Toolbox::RemoveSurroundingQuotes(std::string& value) + { + if (!value.empty() && + value[0] == '\"' && + value[value.size() - 1] == '\"') + { + value = value.substr(1, value.size() - 2); + } + } }
--- a/OrthancFramework/Sources/Toolbox.h Thu Apr 15 11:29:57 2021 +0200 +++ b/OrthancFramework/Sources/Toolbox.h Fri Apr 16 10:48:57 2021 +0200 @@ -280,6 +280,8 @@ static void WriteStyledJson(std::string& target, const Json::Value& source); + + static void RemoveSurroundingQuotes(std::string& value); }; }
--- a/OrthancFramework/UnitTestsSources/FrameworkTests.cpp Thu Apr 15 11:29:57 2021 +0200 +++ b/OrthancFramework/UnitTestsSources/FrameworkTests.cpp Fri Apr 16 10:48:57 2021 +0200 @@ -401,6 +401,27 @@ ASSERT_EQ("coucou", Toolbox::StripSpaces(" coucou \t \r \n ")); ASSERT_EQ("cou cou", Toolbox::StripSpaces(" cou cou \n ")); ASSERT_EQ("c", Toolbox::StripSpaces(" \n\t c\r \n ")); + + std::string s = "\" abd \""; + Toolbox::RemoveSurroundingQuotes(s); ASSERT_EQ(" abd ", s); + + s = " \" abd \" "; + Toolbox::RemoveSurroundingQuotes(s); ASSERT_EQ(" \" abd \" ", s); + + s = Toolbox::StripSpaces(s); + Toolbox::RemoveSurroundingQuotes(s); ASSERT_EQ(" abd ", s); + + s = "\""; + Toolbox::RemoveSurroundingQuotes(s); ASSERT_EQ("", s); + + s = "\"\""; + Toolbox::RemoveSurroundingQuotes(s); ASSERT_EQ("", s); + + s = "\"_\""; + Toolbox::RemoveSurroundingQuotes(s); ASSERT_EQ("_", s); + + s = "\"\"\""; + Toolbox::RemoveSurroundingQuotes(s); ASSERT_EQ("\"", s); } TEST(Toolbox, Case)