Mercurial > hg > orthanc
comparison Plugins/Samples/Common/OrthancPluginCppWrapper.cpp @ 3667:c8d8c3b5f47c
OrthancJob::SubmitFromRestApiPost()
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 13 Feb 2020 18:05:51 +0100 |
parents | 94f4a18a79cc |
children | adb6d8b49283 445bda258245 |
comparison
equal
deleted
inserted
replaced
3663:b7cf2e32aafb | 3667:c8d8c3b5f47c |
---|---|
2157 } | 2157 } |
2158 } | 2158 } |
2159 } | 2159 } |
2160 } | 2160 } |
2161 | 2161 |
2162 | |
2163 void OrthancJob::SubmitFromRestApiPost(OrthancPluginRestOutput* output, | |
2164 const Json::Value& body, | |
2165 OrthancJob* job) | |
2166 { | |
2167 static const char* KEY_SYNCHRONOUS = "Synchronous"; | |
2168 static const char* KEY_ASYNCHRONOUS = "Asynchronous"; | |
2169 static const char* KEY_PRIORITY = "Priority"; | |
2170 | |
2171 std::auto_ptr<OrthancJob> protection(job); | |
2172 | |
2173 if (body.type() != Json::objectValue) | |
2174 { | |
2175 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat, | |
2176 "Expected a JSON object in the body"); | |
2177 } | |
2178 | |
2179 bool synchronous = true; | |
2180 | |
2181 if (body.isMember(KEY_SYNCHRONOUS)) | |
2182 { | |
2183 if (body[KEY_SYNCHRONOUS].type() != Json::booleanValue) | |
2184 { | |
2185 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat, | |
2186 "Option \"" + std::string(KEY_SYNCHRONOUS) + | |
2187 "\" must be Boolean"); | |
2188 } | |
2189 else | |
2190 { | |
2191 synchronous = body[KEY_SYNCHRONOUS].asBool(); | |
2192 } | |
2193 } | |
2194 | |
2195 if (body.isMember(KEY_ASYNCHRONOUS)) | |
2196 { | |
2197 if (body[KEY_ASYNCHRONOUS].type() != Json::booleanValue) | |
2198 { | |
2199 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat, | |
2200 "Option \"" + std::string(KEY_ASYNCHRONOUS) + | |
2201 "\" must be Boolean"); | |
2202 } | |
2203 else | |
2204 { | |
2205 synchronous = !body[KEY_ASYNCHRONOUS].asBool(); | |
2206 } | |
2207 } | |
2208 | |
2209 int priority = 0; | |
2210 | |
2211 if (body.isMember(KEY_PRIORITY)) | |
2212 { | |
2213 if (body[KEY_PRIORITY].type() != Json::booleanValue) | |
2214 { | |
2215 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat, | |
2216 "Option \"" + std::string(KEY_PRIORITY) + | |
2217 "\" must be an integer"); | |
2218 } | |
2219 else | |
2220 { | |
2221 priority = !body[KEY_PRIORITY].asInt(); | |
2222 } | |
2223 } | |
2224 | |
2225 Json::Value result; | |
2226 | |
2227 if (synchronous) | |
2228 { | |
2229 OrthancPlugins::OrthancJob::SubmitAndWait(result, protection.release(), priority); | |
2230 } | |
2231 else | |
2232 { | |
2233 std::string id = OrthancPlugins::OrthancJob::Submit(protection.release(), priority); | |
2234 | |
2235 result = Json::objectValue; | |
2236 result["ID"] = id; | |
2237 result["Path"] = "/jobs/" + id; | |
2238 } | |
2239 | |
2240 std::string s = result.toStyledString(); | |
2241 OrthancPluginAnswerBuffer(OrthancPlugins::GetGlobalContext(), output, s.c_str(), | |
2242 s.size(), "application/json"); | |
2243 } | |
2244 | |
2162 #endif | 2245 #endif |
2163 | 2246 |
2164 | 2247 |
2165 | 2248 |
2166 | 2249 |