comparison OrthancFramework/Sources/RestApi/RestApiCallDocumentation.cpp @ 4403:ad646ff506d0

cont openapi
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 23 Dec 2020 18:32:13 +0100
parents 354ea95b294a
children f34634916d8c
comparison
equal deleted inserted replaced
4402:b651989194d3 4403:ad646ff506d0
56 } 56 }
57 57
58 58
59 RestApiCallDocumentation& RestApiCallDocumentation::SetRequestField(const std::string& name, 59 RestApiCallDocumentation& RestApiCallDocumentation::SetRequestField(const std::string& name,
60 Type type, 60 Type type,
61 const std::string& description) 61 const std::string& description,
62 bool required)
62 { 63 {
63 if (method_ != HttpMethod_Post && 64 if (method_ != HttpMethod_Post &&
64 method_ != HttpMethod_Put) 65 method_ != HttpMethod_Put)
65 { 66 {
66 throw OrthancException(ErrorCode_BadParameterType, "Request body is only allowed on POST and PUT"); 67 throw OrthancException(ErrorCode_BadParameterType, "Request body is only allowed on POST and PUT");
75 { 76 {
76 throw OrthancException(ErrorCode_ParameterOutOfRange, "Field \"" + name + "\" of JSON request is already documented"); 77 throw OrthancException(ErrorCode_ParameterOutOfRange, "Field \"" + name + "\" of JSON request is already documented");
77 } 78 }
78 else 79 else
79 { 80 {
80 Parameter p; 81 requestFields_[name] = Parameter(type, description, required);
81 p.type_ = type;
82 p.description_ = description;
83 requestFields_[name] = p;
84 return *this; 82 return *this;
85 } 83 }
86 } 84 }
87 85
88 86
112 { 110 {
113 throw OrthancException(ErrorCode_ParameterOutOfRange, "URI argument \"" + name + "\" is already documented"); 111 throw OrthancException(ErrorCode_ParameterOutOfRange, "URI argument \"" + name + "\" is already documented");
114 } 112 }
115 else 113 else
116 { 114 {
117 Parameter p; 115 uriArguments_[name] = Parameter(type, description, true);
118 p.type_ = type;
119 p.description_ = description;
120 uriArguments_[name] = p;
121 return *this; 116 return *this;
122 } 117 }
123 } 118 }
124 119
125 120
130 { 125 {
131 throw OrthancException(ErrorCode_ParameterOutOfRange, "HTTP header \"" + name + "\" is already documented"); 126 throw OrthancException(ErrorCode_ParameterOutOfRange, "HTTP header \"" + name + "\" is already documented");
132 } 127 }
133 else 128 else
134 { 129 {
135 Parameter p; 130 httpHeaders_[name] = Parameter(Type_String, description, false);
136 p.type_ = Type_String;
137 p.description_ = description;
138 httpHeaders_[name] = p;
139 return *this; 131 return *this;
140 } 132 }
141 } 133 }
142 134
143 135
144 RestApiCallDocumentation& RestApiCallDocumentation::SetHttpGetArgument(const std::string& name, 136 RestApiCallDocumentation& RestApiCallDocumentation::SetHttpGetArgument(const std::string& name,
145 Type type, 137 Type type,
146 const std::string& description) 138 const std::string& description,
139 bool required)
147 { 140 {
148 if (method_ != HttpMethod_Get) 141 if (method_ != HttpMethod_Get)
149 { 142 {
150 throw OrthancException(ErrorCode_InternalError, "Cannot set a HTTP GET argument on HTTP method: " + 143 throw OrthancException(ErrorCode_InternalError, "Cannot set a HTTP GET argument on HTTP method: " +
151 std::string(EnumerationToString(method_))); 144 std::string(EnumerationToString(method_)));
154 { 147 {
155 throw OrthancException(ErrorCode_ParameterOutOfRange, "GET argument \"" + name + "\" is already documented"); 148 throw OrthancException(ErrorCode_ParameterOutOfRange, "GET argument \"" + name + "\" is already documented");
156 } 149 }
157 else 150 else
158 { 151 {
159 Parameter p; 152 getArguments_[name] = Parameter(type, description, required);
160 p.type_ = type;
161 p.description_ = description;
162 getArguments_[name] = p;
163 return *this; 153 return *this;
164 } 154 }
165 } 155 }
166 156
167 157
178 { 168 {
179 throw OrthancException(ErrorCode_ParameterOutOfRange, "Field \"" + name + "\" of JSON answer is already documented"); 169 throw OrthancException(ErrorCode_ParameterOutOfRange, "Field \"" + name + "\" of JSON answer is already documented");
180 } 170 }
181 else 171 else
182 { 172 {
183 Parameter p; 173 answerFields_[name] = Parameter(type, description, false);
184 p.type_ = type;
185 p.description_ = description;
186 answerFields_[name] = p;
187 return *this; 174 return *this;
188 } 175 }
189 } 176 }
190 177
191 178
239 226
240 case RestApiCallDocumentation::Type_Boolean: 227 case RestApiCallDocumentation::Type_Boolean:
241 return "boolean"; 228 return "boolean";
242 229
243 case RestApiCallDocumentation::Type_JsonObject: 230 case RestApiCallDocumentation::Type_JsonObject:
231 return "object";
232
244 case RestApiCallDocumentation::Type_JsonListOfStrings: 233 case RestApiCallDocumentation::Type_JsonListOfStrings:
245 return "object"; 234 case RestApiCallDocumentation::Type_JsonListOfObjects:
246 235 return "array";
236
247 default: 237 default:
248 throw OrthancException(ErrorCode_ParameterOutOfRange); 238 throw OrthancException(ErrorCode_ParameterOutOfRange);
249 } 239 }
250 } 240 }
251 241
298 { 288 {
299 for (Parameters::const_iterator field = requestFields_.begin(); 289 for (Parameters::const_iterator field = requestFields_.begin();
300 field != requestFields_.end(); ++field) 290 field != requestFields_.end(); ++field)
301 { 291 {
302 Json::Value p = Json::objectValue; 292 Json::Value p = Json::objectValue;
303 p["type"] = TypeToString(field->second.type_); 293 p["type"] = TypeToString(field->second.GetType());
304 p["description"] = field->second.description_; 294 p["description"] = field->second.GetDescription();
305 schema["properties"][field->first] = p; 295 schema["properties"][field->first] = p;
306 } 296 }
307 } 297 }
308 } 298 }
309 } 299 }
320 { 310 {
321 for (Parameters::const_iterator field = answerFields_.begin(); 311 for (Parameters::const_iterator field = answerFields_.begin();
322 field != answerFields_.end(); ++field) 312 field != answerFields_.end(); ++field)
323 { 313 {
324 Json::Value p = Json::objectValue; 314 Json::Value p = Json::objectValue;
325 p["type"] = TypeToString(field->second.type_); 315 p["type"] = TypeToString(field->second.GetType());
326 p["description"] = field->second.description_; 316 p["description"] = field->second.GetDescription();
327 schema["properties"][field->first] = p; 317 schema["properties"][field->first] = p;
328 } 318 }
329 } 319 }
330 } 320 }
331 321
349 it != getArguments_.end(); ++it) 339 it != getArguments_.end(); ++it)
350 { 340 {
351 Json::Value p = Json::objectValue; 341 Json::Value p = Json::objectValue;
352 p["name"] = it->first; 342 p["name"] = it->first;
353 p["in"] = "query"; 343 p["in"] = "query";
354 p["schema"]["type"] = TypeToString(it->second.type_); 344 p["required"] = it->second.IsRequired();
355 p["description"] = it->second.description_; 345 p["schema"]["type"] = TypeToString(it->second.GetType());
346 p["description"] = it->second.GetDescription();
356 parameters.append(p); 347 parameters.append(p);
357 } 348 }
358 349
359 for (Parameters::const_iterator it = httpHeaders_.begin(); 350 for (Parameters::const_iterator it = httpHeaders_.begin();
360 it != httpHeaders_.end(); ++it) 351 it != httpHeaders_.end(); ++it)
361 { 352 {
362 Json::Value p = Json::objectValue; 353 Json::Value p = Json::objectValue;
363 p["name"] = it->first; 354 p["name"] = it->first;
364 p["in"] = "header"; 355 p["in"] = "header";
365 p["schema"]["type"] = TypeToString(it->second.type_); 356 p["required"] = it->second.IsRequired();
366 p["description"] = it->second.description_; 357 p["schema"]["type"] = TypeToString(it->second.GetType());
358 p["description"] = it->second.GetDescription();
367 parameters.append(p); 359 parameters.append(p);
368 } 360 }
369 361
370 for (Parameters::const_iterator it = uriArguments_.begin(); 362 for (Parameters::const_iterator it = uriArguments_.begin();
371 it != uriArguments_.end(); ++it) 363 it != uriArguments_.end(); ++it)
376 } 368 }
377 369
378 Json::Value p = Json::objectValue; 370 Json::Value p = Json::objectValue;
379 p["name"] = it->first; 371 p["name"] = it->first;
380 p["in"] = "path"; 372 p["in"] = "path";
381 p["required"] = true; 373 p["required"] = it->second.IsRequired();
382 p["schema"]["type"] = TypeToString(it->second.type_); 374 p["schema"]["type"] = TypeToString(it->second.GetType());
383 p["description"] = it->second.description_; 375 p["description"] = it->second.GetDescription();
384 parameters.append(p); 376 parameters.append(p);
385 } 377 }
386 378
387 for (std::set<std::string>::const_iterator it = expectedUriArguments.begin(); 379 for (std::set<std::string>::const_iterator it = expectedUriArguments.begin();
388 it != expectedUriArguments.end(); ++it) 380 it != expectedUriArguments.end(); ++it)