Mercurial > hg > orthanc-python
annotate Sources/RestCallbacks.cpp @ 22:04b8a44b2717
adding patch.exe command-line tool for MSVC
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Fri, 03 Apr 2020 10:47:01 +0200 |
parents | 5b3dd10cecc3 |
children | fd58eb5749ed |
rev | line source |
---|---|
0 | 1 /** |
2 * Python plugin for Orthanc | |
3 * Copyright (C) 2017-2020 Osimis S.A., Belgium | |
4 * | |
5 * This program is free software: you can redistribute it and/or | |
6 * modify it under the terms of the GNU Affero General Public License | |
7 * as published by the Free Software Foundation, either version 3 of | |
8 * the License, or (at your option) any later version. | |
9 * | |
10 * This program is distributed in the hope that it will be useful, but | |
11 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 * Affero General Public License for more details. | |
14 * | |
15 * You should have received a copy of the GNU Affero General Public License | |
16 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
17 **/ | |
18 | |
19 | |
20 #include "RestCallbacks.h" | |
21 | |
22 #include "PythonObject.h" | |
23 #include "Autogenerated/sdk.h" | |
24 | |
25 #include <OrthancPluginCppWrapper.h> | |
26 | |
27 #include <boost/regex.hpp> | |
28 | |
29 | |
30 class RestCallback : public boost::noncopyable | |
31 { | |
32 private: | |
33 boost::regex regex_; | |
34 PyObject* callback_; | |
35 | |
36 public: | |
37 RestCallback(const std::string& uri, | |
38 PyObject* callback) : | |
39 regex_(uri), | |
40 callback_(callback) | |
41 { | |
42 Py_XINCREF(callback_); | |
43 } | |
44 | |
45 ~RestCallback() | |
46 { | |
47 Py_XDECREF(callback_); | |
48 } | |
49 | |
50 bool IsMatch(const std::string& uri) const | |
51 { | |
52 return boost::regex_match(uri, regex_); | |
53 } | |
54 | |
55 PyObject* GetCallback() | |
56 { | |
57 return callback_; | |
58 } | |
59 }; | |
60 | |
61 | |
62 // Concurrent accesses to the callbacks are protected by the | |
63 // "PythonLock" (GIL mutex) | |
64 static std::list<RestCallback*> restCallbacks_; | |
65 | |
66 | |
67 static void RestCallbackHandler(OrthancPluginRestOutput* output, | |
68 const char* uri, | |
69 const OrthancPluginHttpRequest* request) | |
70 { | |
71 PythonLock lock; | |
72 | |
73 for (std::list<RestCallback*>::const_iterator it = restCallbacks_.begin(); | |
74 it != restCallbacks_.end(); ++it) | |
75 { | |
76 assert(*it != NULL); | |
77 if ((*it)->IsMatch(uri)) | |
78 { | |
79 /** | |
80 * Construct an instance object of the "orthanc.RestOutput" | |
81 * class. This is done by calling the constructor function | |
82 * "sdk_OrthancPluginRestOutput_Type". | |
83 **/ | |
84 PythonObject args(lock, PyTuple_New(2)); | |
85 PyTuple_SetItem(args.GetPyObject(), 0, PyLong_FromSsize_t((intptr_t) output)); | |
86 PyTuple_SetItem(args.GetPyObject(), 1, PyBool_FromLong(true /* borrowed, don't destruct */)); | |
87 PyObject *pInst = PyObject_CallObject(GetOrthancPluginRestOutputType(), args.GetPyObject()); | |
88 | |
89 | |
90 /** | |
91 * Construct the arguments tuple (output, uri) | |
92 **/ | |
93 PythonObject args2(lock, PyTuple_New(2)); | |
94 PyTuple_SetItem(args2.GetPyObject(), 0, pInst); | |
95 PyTuple_SetItem(args2.GetPyObject(), 1, PyUnicode_FromString(uri)); | |
96 // No need to decrement refcount with "PyTuple_SetItem()" | |
97 | |
98 /** | |
99 * Construct the named arguments from the "request" argument | |
100 **/ | |
101 const char* method; | |
102 switch (request->method) | |
103 { | |
104 case OrthancPluginHttpMethod_Get: | |
105 method = "GET"; | |
106 break; | |
107 | |
108 case OrthancPluginHttpMethod_Post: | |
109 method = "POST"; | |
110 break; | |
111 | |
112 case OrthancPluginHttpMethod_Put: | |
113 method = "PUT"; | |
114 break; | |
115 | |
116 case OrthancPluginHttpMethod_Delete: | |
117 method = "DELETE"; | |
118 break; | |
119 | |
120 default: | |
121 ORTHANC_PLUGINS_THROW_EXCEPTION(ParameterOutOfRange); | |
122 } | |
123 | |
124 PythonObject kw(lock, PyDict_New()); | |
125 PyDict_SetItemString(kw.GetPyObject(), "method", PyUnicode_FromString(method)); | |
126 | |
127 { | |
128 PythonObject groups(lock, PyTuple_New(request->groupsCount)); | |
129 | |
130 for (uint32_t i = 0; i < request->groupsCount; i++) | |
131 { | |
132 PyTuple_SetItem(groups.GetPyObject(), i, PyUnicode_FromString(request->groups[i])); | |
133 } | |
134 | |
135 PyDict_SetItemString(kw.GetPyObject(), "groups", groups.Release()); | |
136 } | |
137 | |
138 if (request->method == OrthancPluginHttpMethod_Get) | |
139 { | |
140 PythonObject get(lock, PyDict_New()); | |
141 | |
142 for (uint32_t i = 0; i < request->getCount; i++) | |
143 { | |
144 PyDict_SetItemString(get.GetPyObject(), request->getKeys[i], | |
145 PyUnicode_FromString(request->getValues[i])); | |
146 } | |
147 | |
148 PyDict_SetItemString(kw.GetPyObject(), "get", get.Release()); | |
149 } | |
150 | |
151 { | |
152 PythonObject headers(lock, PyDict_New()); | |
153 | |
154 for (uint32_t i = 0; i < request->headersCount; i++) | |
155 { | |
156 PyDict_SetItemString(headers.GetPyObject(), request->headersKeys[i], | |
157 PyUnicode_FromString(request->headersValues[i])); | |
158 } | |
159 | |
160 PyDict_SetItemString(kw.GetPyObject(), "headers", headers.Release()); | |
161 } | |
162 | |
163 if (request->method == OrthancPluginHttpMethod_Post || | |
164 request->method == OrthancPluginHttpMethod_Put) | |
165 { | |
166 PyDict_SetItemString(kw.GetPyObject(), "body", PyBytes_FromStringAndSize( | |
167 reinterpret_cast<const char*>(request->body), request->bodySize)); | |
168 } | |
169 | |
170 /** | |
171 * Call the user-defined function | |
172 **/ | |
173 PythonObject result(lock, PyObject_Call( | |
174 (*it)->GetCallback(), args2.GetPyObject(), kw.GetPyObject())); | |
3
26762eb9d704
reporting of exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
175 |
26762eb9d704
reporting of exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
176 std::string traceback; |
26762eb9d704
reporting of exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
177 if (lock.HasErrorOccurred(traceback)) |
26762eb9d704
reporting of exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
178 { |
26762eb9d704
reporting of exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
179 OrthancPlugins::LogError("Error in the REST callback, traceback:\n" + traceback); |
26762eb9d704
reporting of exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
180 ORTHANC_PLUGINS_THROW_EXCEPTION(Plugin); |
26762eb9d704
reporting of exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
181 } |
26762eb9d704
reporting of exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
182 |
0 | 183 return; |
184 } | |
185 } | |
186 | |
187 // Should never happen | |
188 OrthancPlugins::LogError("Unable to find the Python handler for URI: " + std::string(uri)); | |
189 ORTHANC_PLUGINS_THROW_EXCEPTION(InternalError); | |
190 } | |
191 | |
192 | |
193 | |
194 PyObject* RegisterRestCallback(PyObject* module, PyObject* args) | |
195 { | |
196 // The GIL is locked at this point (no need to create "PythonLock") | |
197 | |
198 // https://docs.python.org/3/extending/extending.html#calling-python-functions-from-c | |
199 const char* uri = NULL; | |
200 PyObject* callback = NULL; | |
201 | |
202 if (!PyArg_ParseTuple(args, "sO", &uri, &callback) || | |
203 uri == NULL || | |
204 callback == NULL) | |
205 { | |
206 PyErr_SetString(PyExc_ValueError, "Expected a string (URI) and a callback function"); | |
207 return NULL; | |
208 } | |
209 | |
210 OrthancPlugins::LogInfo("Registering a Python REST callback on URI: " + std::string(uri)); | |
211 OrthancPlugins::RegisterRestCallback<RestCallbackHandler>(uri, true /* thread safe */); | |
212 | |
213 restCallbacks_.push_back(new RestCallback(uri, callback)); | |
214 | |
215 Py_INCREF(Py_None); | |
216 return Py_None; | |
217 } | |
218 | |
219 | |
220 | |
221 void FinalizeRestCallbacks() | |
222 { | |
223 PythonLock lock; | |
224 | |
225 for (std::list<RestCallback*>::iterator it = restCallbacks_.begin(); | |
226 it != restCallbacks_.end(); ++it) | |
227 { | |
228 assert(*it != NULL); | |
229 delete *it; | |
230 } | |
231 | |
232 restCallbacks_.clear(); | |
233 } |