comparison Framework/Deprecated/Toolbox/IWebService.h @ 732:c35e98d22764

move Deprecated classes to a separate folder
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 21 May 2019 14:27:35 +0200
parents Framework/Toolbox/IWebService.h@4f2416d519b4
children 861c080ef47b
comparison
equal deleted inserted replaced
729:529189f399ec 732:c35e98d22764
1 /**
2 * Stone of Orthanc
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
4 * Department, University Hospital of Liege, Belgium
5 * Copyright (C) 2017-2019 Osimis S.A., Belgium
6 *
7 * This program is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU Affero General Public License
9 * as published by the Free Software Foundation, either version 3 of
10 * the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Affero General Public License for more details.
16 *
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 **/
20
21
22 #pragma once
23
24 #include "../../Messages/IObserver.h"
25 #include "../../Messages/ICallable.h"
26
27 #include <Core/IDynamicObject.h>
28 #include <Core/Logging.h>
29
30 #include <string>
31 #include <map>
32
33 namespace Deprecated
34 {
35 // The IWebService performs HTTP requests.
36 // Since applications can run in native or WASM environment and, since
37 // in a WASM environment, the WebService is asynchronous, the IWebservice
38 // also implements an asynchronous interface: you must schedule a request
39 // and you'll be notified when the response/error is ready.
40 class IWebService : public boost::noncopyable
41 {
42 protected:
43 OrthancStone::MessageBroker& broker_;
44
45 public:
46 typedef std::map<std::string, std::string> HttpHeaders;
47
48 class HttpRequestSuccessMessage : public OrthancStone::IMessage
49 {
50 ORTHANC_STONE_MESSAGE(__FILE__, __LINE__);
51
52 private:
53 const std::string& uri_;
54 const void* answer_;
55 size_t answerSize_;
56 const HttpHeaders& answerHeaders_;
57 const Orthanc::IDynamicObject* payload_;
58
59 public:
60 HttpRequestSuccessMessage(const std::string& uri,
61 const void* answer,
62 size_t answerSize,
63 const HttpHeaders& answerHeaders,
64 const Orthanc::IDynamicObject* payload) :
65 uri_(uri),
66 answer_(answer),
67 answerSize_(answerSize),
68 answerHeaders_(answerHeaders),
69 payload_(payload)
70 {
71 }
72
73 const std::string& GetUri() const
74 {
75 return uri_;
76 }
77
78 const void* GetAnswer() const
79 {
80 return answer_;
81 }
82
83 size_t GetAnswerSize() const
84 {
85 return answerSize_;
86 }
87
88 const HttpHeaders& GetAnswerHttpHeaders() const
89 {
90 return answerHeaders_;
91 }
92
93 bool HasPayload() const
94 {
95 return payload_ != NULL;
96 }
97
98 const Orthanc::IDynamicObject& GetPayload() const;
99 };
100
101
102 class HttpRequestErrorMessage : public OrthancStone::IMessage
103 {
104 ORTHANC_STONE_MESSAGE(__FILE__, __LINE__);
105
106 private:
107 const std::string& uri_;
108 const Orthanc::IDynamicObject* payload_;
109
110 public:
111 HttpRequestErrorMessage(const std::string& uri,
112 const Orthanc::IDynamicObject* payload) :
113 uri_(uri),
114 payload_(payload)
115 {
116 }
117
118 const std::string& GetUri() const
119 {
120 return uri_;
121 }
122
123 bool HasPayload() const
124 {
125 return payload_ != NULL;
126 }
127
128 const Orthanc::IDynamicObject& GetPayload() const;
129 };
130
131
132 IWebService(OrthancStone::MessageBroker& broker) :
133 broker_(broker)
134 {
135 }
136
137
138 virtual ~IWebService()
139 {
140 }
141
142 virtual void EnableCache(bool enable) = 0;
143
144 virtual void GetAsync(const std::string& uri,
145 const HttpHeaders& headers,
146 Orthanc::IDynamicObject* payload /* takes ownership */,
147 OrthancStone::MessageHandler<IWebService::HttpRequestSuccessMessage>* successCallback,
148 OrthancStone::MessageHandler<IWebService::HttpRequestErrorMessage>* failureCallback = NULL,
149 unsigned int timeoutInSeconds = 60) = 0;
150
151 virtual void PostAsync(const std::string& uri,
152 const HttpHeaders& headers,
153 const std::string& body,
154 Orthanc::IDynamicObject* payload /* takes ownership */,
155 OrthancStone::MessageHandler<IWebService::HttpRequestSuccessMessage>* successCallback,
156 OrthancStone::MessageHandler<IWebService::HttpRequestErrorMessage>* failureCallback = NULL,
157 unsigned int timeoutInSeconds = 60) = 0;
158
159 virtual void DeleteAsync(const std::string& uri,
160 const HttpHeaders& headers,
161 Orthanc::IDynamicObject* payload /* takes ownership */,
162 OrthancStone::MessageHandler<IWebService::HttpRequestSuccessMessage>* successCallback,
163 OrthancStone::MessageHandler<IWebService::HttpRequestErrorMessage>* failureCallback = NULL,
164 unsigned int timeoutInSeconds = 60) = 0;
165 };
166 }