comparison OrthancFramework/Sources/RestApi/RestApi.cpp @ 5807:8279eaab0d1d attach-custom-data

merged default -> attach-custom-data
author Alain Mazy <am@orthanc.team>
date Tue, 24 Sep 2024 11:39:52 +0200
parents f7adfb22e20e
children
comparison
equal deleted inserted replaced
5085:79f98ee4f04b 5807:8279eaab0d1d
1 /** 1 /**
2 * Orthanc - A Lightweight, RESTful DICOM Store 2 * Orthanc - A Lightweight, RESTful DICOM Store
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics 3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
4 * Department, University Hospital of Liege, Belgium 4 * Department, University Hospital of Liege, Belgium
5 * Copyright (C) 2017-2022 Osimis S.A., Belgium 5 * Copyright (C) 2017-2023 Osimis S.A., Belgium
6 * Copyright (C) 2021-2022 Sebastien Jodogne, ICTEAM UCLouvain, Belgium 6 * Copyright (C) 2024-2024 Orthanc Team SRL, Belgium
7 * Copyright (C) 2021-2024 Sebastien Jodogne, ICTEAM UCLouvain, Belgium
7 * 8 *
8 * This program is free software: you can redistribute it and/or 9 * This program is free software: you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public License 10 * modify it under the terms of the GNU Lesser General Public License
10 * as published by the Free Software Foundation, either version 3 of 11 * as published by the Free Software Foundation, either version 3 of
11 * the License, or (at your option) any later version. 12 * the License, or (at your option) any later version.
136 size_t successPathsCount_; 137 size_t successPathsCount_;
137 size_t totalPathsCount_; 138 size_t totalPathsCount_;
138 139
139 protected: 140 protected:
140 virtual bool HandleCall(RestApiCall& call, 141 virtual bool HandleCall(RestApiCall& call,
142 const std::string& path,
141 const std::set<std::string>& uriArgumentsNames) = 0; 143 const std::set<std::string>& uriArgumentsNames) = 0;
142 144
143 public: 145 public:
144 explicit DocumentationVisitor(RestApi& restApi) : 146 explicit DocumentationVisitor(RestApi& restApi) :
145 restApi_(restApi), 147 restApi_(restApi),
155 const UriComponents& trailing) 157 const UriComponents& trailing)
156 { 158 {
157 std::string path = Toolbox::FlattenUri(uri); 159 std::string path = Toolbox::FlattenUri(uri);
158 if (hasTrailing) 160 if (hasTrailing)
159 { 161 {
160 path += "/{...}"; 162 path += "/{path}";
161 } 163 }
162 164
163 std::set<std::string> uriArgumentsNames; 165 std::set<std::string> uriArgumentsNames;
164 HttpToolbox::Arguments uriArguments; 166 HttpToolbox::Arguments uriArguments;
165 167
171 uriArguments[it->first] = ""; 173 uriArguments[it->first] = "";
172 } 174 }
173 175
174 if (hasTrailing) 176 if (hasTrailing)
175 { 177 {
176 uriArgumentsNames.insert("..."); 178 uriArgumentsNames.insert("path");
177 uriArguments["..."] = ""; 179 uriArguments["path"] = "";
178 } 180 }
179 181
180 if (resource.HasHandler(HttpMethod_Get)) 182 if (resource.HasHandler(HttpMethod_Get))
181 { 183 {
182 totalPathsCount_ ++; 184 totalPathsCount_ ++;
183 185
184 StringHttpOutput o1; 186 StringHttpOutput o1;
185 HttpOutput o2(o1, false); 187 HttpOutput o2(o1, false /* assume no keep-alive */, 0);
186 RestApiOutput o3(o2, HttpMethod_Get); 188 RestApiOutput o3(o2, HttpMethod_Get);
187 RestApiGetCall call(o3, restApi_, RequestOrigin_Documentation, "" /* remote IP */, 189 RestApiGetCall call(o3, restApi_, RequestOrigin_Documentation, "" /* remote IP */,
188 "" /* username */, HttpToolbox::Arguments() /* HTTP headers */, 190 "" /* username */, HttpToolbox::Arguments() /* HTTP headers */,
189 uriArguments, UriComponents() /* trailing */, 191 uriArguments, UriComponents() /* trailing */,
190 uri, HttpToolbox::Arguments() /* GET arguments */); 192 uri, HttpToolbox::Arguments() /* GET arguments */);
192 bool ok = false; 194 bool ok = false;
193 195
194 try 196 try
195 { 197 {
196 ok = (resource.Handle(call) && 198 ok = (resource.Handle(call) &&
197 HandleCall(call, uriArgumentsNames)); 199 HandleCall(call, path, uriArgumentsNames));
198 } 200 }
199 catch (OrthancException& e) 201 catch (OrthancException& e)
200 { 202 {
201 LOG(ERROR) << "Exception while documenting GET " << path << ": " << e.What(); 203 LOG(ERROR) << "Exception while documenting GET " << path << ": " << e.What();
202 } 204 }
218 if (resource.HasHandler(HttpMethod_Post)) 220 if (resource.HasHandler(HttpMethod_Post))
219 { 221 {
220 totalPathsCount_ ++; 222 totalPathsCount_ ++;
221 223
222 StringHttpOutput o1; 224 StringHttpOutput o1;
223 HttpOutput o2(o1, false); 225 HttpOutput o2(o1, false /* assume no keep-alive */, 0);
224 RestApiOutput o3(o2, HttpMethod_Post); 226 RestApiOutput o3(o2, HttpMethod_Post);
225 RestApiPostCall call(o3, restApi_, RequestOrigin_Documentation, "" /* remote IP */, 227 RestApiPostCall call(o3, restApi_, RequestOrigin_Documentation, "" /* remote IP */,
226 "" /* username */, HttpToolbox::Arguments() /* HTTP headers */, 228 "" /* username */, HttpToolbox::Arguments() /* HTTP headers */,
227 uriArguments, UriComponents() /* trailing */, 229 uriArguments, UriComponents() /* trailing */,
228 uri, NULL /* body */, 0 /* body size */); 230 uri, NULL /* body */, 0 /* body size */);
230 bool ok = false; 232 bool ok = false;
231 233
232 try 234 try
233 { 235 {
234 ok = (resource.Handle(call) && 236 ok = (resource.Handle(call) &&
235 HandleCall(call, uriArgumentsNames)); 237 HandleCall(call, path, uriArgumentsNames));
236 } 238 }
237 catch (OrthancException& e) 239 catch (OrthancException& e)
238 { 240 {
239 LOG(ERROR) << "Exception while documenting POST " << path << ": " << e.What(); 241 LOG(ERROR) << "Exception while documenting POST " << path << ": " << e.What();
240 } 242 }
256 if (resource.HasHandler(HttpMethod_Delete)) 258 if (resource.HasHandler(HttpMethod_Delete))
257 { 259 {
258 totalPathsCount_ ++; 260 totalPathsCount_ ++;
259 261
260 StringHttpOutput o1; 262 StringHttpOutput o1;
261 HttpOutput o2(o1, false); 263 HttpOutput o2(o1, false /* assume no keep-alive */, 0);
262 RestApiOutput o3(o2, HttpMethod_Delete); 264 RestApiOutput o3(o2, HttpMethod_Delete);
263 RestApiDeleteCall call(o3, restApi_, RequestOrigin_Documentation, "" /* remote IP */, 265 RestApiDeleteCall call(o3, restApi_, RequestOrigin_Documentation, "" /* remote IP */,
264 "" /* username */, HttpToolbox::Arguments() /* HTTP headers */, 266 "" /* username */, HttpToolbox::Arguments() /* HTTP headers */,
265 uriArguments, UriComponents() /* trailing */, uri); 267 uriArguments, UriComponents() /* trailing */, uri);
266 268
267 bool ok = false; 269 bool ok = false;
268 270
269 try 271 try
270 { 272 {
271 ok = (resource.Handle(call) && 273 ok = (resource.Handle(call) &&
272 HandleCall(call, uriArgumentsNames)); 274 HandleCall(call, path, uriArgumentsNames));
273 } 275 }
274 catch (OrthancException& e) 276 catch (OrthancException& e)
275 { 277 {
276 LOG(ERROR) << "Exception while documenting DELETE " << path << ": " << e.What(); 278 LOG(ERROR) << "Exception while documenting DELETE " << path << ": " << e.What();
277 } 279 }
293 if (resource.HasHandler(HttpMethod_Put)) 295 if (resource.HasHandler(HttpMethod_Put))
294 { 296 {
295 totalPathsCount_ ++; 297 totalPathsCount_ ++;
296 298
297 StringHttpOutput o1; 299 StringHttpOutput o1;
298 HttpOutput o2(o1, false); 300 HttpOutput o2(o1, false /* assume no keep-alive */, 0);
299 RestApiOutput o3(o2, HttpMethod_Put); 301 RestApiOutput o3(o2, HttpMethod_Put);
300 RestApiPutCall call(o3, restApi_, RequestOrigin_Documentation, "" /* remote IP */, 302 RestApiPutCall call(o3, restApi_, RequestOrigin_Documentation, "" /* remote IP */,
301 "" /* username */, HttpToolbox::Arguments() /* HTTP headers */, 303 "" /* username */, HttpToolbox::Arguments() /* HTTP headers */,
302 uriArguments, UriComponents() /* trailing */, uri, 304 uriArguments, UriComponents() /* trailing */, uri,
303 NULL /* body */, 0 /* body size */); 305 NULL /* body */, 0 /* body size */);
305 bool ok = false; 307 bool ok = false;
306 308
307 try 309 try
308 { 310 {
309 ok = (resource.Handle(call) && 311 ok = (resource.Handle(call) &&
310 HandleCall(call, uriArgumentsNames)); 312 HandleCall(call, path, uriArgumentsNames));
311 } 313 }
312 catch (OrthancException& e) 314 catch (OrthancException& e)
313 { 315 {
314 LOG(ERROR) << "Exception while documenting PUT " << path << ": " << e.What(); 316 LOG(ERROR) << "Exception while documenting PUT " << path << ": " << e.What();
315 } 317 }
364 private: 366 private:
365 Json::Value paths_; 367 Json::Value paths_;
366 368
367 protected: 369 protected:
368 virtual bool HandleCall(RestApiCall& call, 370 virtual bool HandleCall(RestApiCall& call,
371 const std::string& path,
369 const std::set<std::string>& uriArgumentsNames) ORTHANC_OVERRIDE 372 const std::set<std::string>& uriArgumentsNames) ORTHANC_OVERRIDE
370 { 373 {
371 const std::string path = Toolbox::FlattenUri(call.GetFullUri());
372
373 Json::Value v; 374 Json::Value v;
374 if (call.GetDocumentation().FormatOpenApi(v, uriArgumentsNames, path)) 375 if (call.GetDocumentation().FormatOpenApi(v, uriArgumentsNames, path))
375 { 376 {
376 std::string method; 377 std::string method;
377 378
682 683
683 Paths paths_; 684 Paths paths_;
684 685
685 protected: 686 protected:
686 virtual bool HandleCall(RestApiCall& call, 687 virtual bool HandleCall(RestApiCall& call,
688 const std::string& _path,
687 const std::set<std::string>& uriArgumentsNames) ORTHANC_OVERRIDE 689 const std::set<std::string>& uriArgumentsNames) ORTHANC_OVERRIDE
688 { 690 {
689 Path& path = paths_[ Toolbox::FlattenUri(call.GetFullUri()) ]; 691 Path& path = paths_[ _path ];
690 692
691 path.AddMethod(call.GetMethod(), call.GetDocumentation().GetTag(), call.GetDocumentation().IsDeprecated()); 693 path.AddMethod(call.GetMethod(), call.GetDocumentation().GetTag(), call.GetDocumentation().IsDeprecated());
692 694
693 if (call.GetDocumentation().HasSummary()) 695 if (call.GetDocumentation().HasSummary())
694 { 696 {