RESTClient.hpp
Go to the documentation of this file.
1 /*
2 
3  This file is part of OpenFLUID software
4  Copyright(c) 2007, INRA - Montpellier SupAgro
5 
6 
7  == GNU General Public License Usage ==
8 
9  OpenFLUID is free software: you can redistribute it and/or modify
10  it under the terms of the GNU General Public License as published by
11  the Free Software Foundation, either version 3 of the License, or
12  (at your option) any later version.
13 
14  OpenFLUID is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU General Public License for more details.
18 
19  You should have received a copy of the GNU General Public License
20  along with OpenFLUID. If not, see <http://www.gnu.org/licenses/>.
21 
22 
23  == Other Usage ==
24 
25  Other Usage means a use of OpenFLUID that is inconsistent with the GPL
26  license, and requires a written agreement between You and INRA.
27  Licensees for Other Usage of OpenFLUID may use this file in accordance
28  with the terms contained in the written agreement between You and INRA.
29 
30 */
31 
32 /**
33  @file RESTClient.hpp
34 
35  @author Jean-Christophe FABRE <jean-christophe.fabre@inra.fr>
36 */
37 
38 
39 #ifndef __OPENFLUID_UTILS_RESTCLIENT_HPP__
40 #define __OPENFLUID_UTILS_RESTCLIENT_HPP__
41 
42 
43 #if QT_VERSION >= QT_VERSION_CHECK(5, 6, 0)
44 # define OPENFLUID_REST_URL_REDIRECT 1
45 #else
46 # define OPENFLUID_REST_URL_REDIRECT 0
47 #endif
48 
49 
50 #include <QSslSocket>
51 
52 #include <openfluid/dllexport.hpp>
53 
54 
55 namespace openfluid { namespace utils {
56 
57 
59 {
60 
61  public:
62 
63  class Reply
64  {
65  private:
66 
67  int m_StatusCode = -1;
68 
69  unsigned int m_NetworkErrorCode = 0;
70 
71  QString m_NetworkErrorString;
72 
73  QString m_Content;
74 
75 
76  public:
77 
78  Reply() = default;
79 
80  Reply(int StatusCode, unsigned int NetworkErrorCode, const QString& NetworkErrorString, const QString& Content):
81  m_StatusCode(StatusCode),
82  m_NetworkErrorCode(NetworkErrorCode),
83  m_NetworkErrorString(NetworkErrorString),
84  m_Content(Content)
85  { }
86 
87  int getStatusCode() const
88  { return m_StatusCode; }
89 
90  int getNetworkErrorCode() const
91  { return m_NetworkErrorCode; }
92 
93  QString getNetworkErrorString() const
94  { return m_NetworkErrorString; }
95 
96  QString getContent() const
97  { return m_Content; }
98 
99  bool isOK() const
100  { return m_NetworkErrorCode == 0 && m_StatusCode >= 200 && m_StatusCode < 300; }
101 
102  void clear()
103  {
104  m_StatusCode = -1;
105  m_Content.clear();
106  }
107  };
108 
110  {
111  private:
112 
113  QSslSocket::PeerVerifyMode m_CertificateVerifyMode;
114 
115  public:
116 
117  SSLConfiguration() : m_CertificateVerifyMode(QSslSocket::AutoVerifyPeer)
118  { }
119 
120  QSslSocket::PeerVerifyMode getCertificateVerifyMode() const
121  { return m_CertificateVerifyMode; }
122 
123  void setCertificateVerifyMode(QSslSocket::PeerVerifyMode Mode)
124  { m_CertificateVerifyMode = Mode; }
125  };
126 
127 
128  private:
129 
130  QString m_BaseURL;
131 
132  SSLConfiguration m_SSLConfiguration;
133 
134  Reply performRequest(const QString& Path, const QString& Method, const QString& Data = "") const;
135 
136 
137  public:
138 
139  RESTClient();
140 
142  { }
143 
144  void setBaseURL(const QString& URL);
145 
146  QString getBaseURL() const
147  { return m_BaseURL; }
148 
150  { m_SSLConfiguration = Config; }
151 
153  { return m_SSLConfiguration; }
154 
155  Reply getResource(const QString& Path) const;
156 
157  Reply postResource(const QString& Path, const QString& Data) const;
158 
159  Reply putResource(const QString& Path, const QString& Data) const;
160 
161  Reply patchResource(const QString& Path, const QString& Data) const;
162 
163  Reply deleteResource(const QString& Path, const QString& Data) const;
164 
165 };
166 
167 
168 } } // namespaces
169 
170 
171 #endif /* __OPENFLUID_UTILS_RESTCLIENT_HPP__ */
void setSSLConfiguration(const SSLConfiguration &Config)
Definition: RESTClient.hpp:149
QSslSocket::PeerVerifyMode getCertificateVerifyMode() const
Definition: RESTClient.hpp:120
SSLConfiguration getSSLConfiguration() const
Definition: RESTClient.hpp:152
~RESTClient()
Definition: RESTClient.hpp:141
void setCertificateVerifyMode(QSslSocket::PeerVerifyMode Mode)
Definition: RESTClient.hpp:123
QString getBaseURL() const
Definition: RESTClient.hpp:146
int getStatusCode() const
Definition: RESTClient.hpp:87
SSLConfiguration()
Definition: RESTClient.hpp:117
Definition: RESTClient.hpp:63
QString getNetworkErrorString() const
Definition: RESTClient.hpp:93
void clear()
Definition: RESTClient.hpp:102
int getNetworkErrorCode() const
Definition: RESTClient.hpp:90
bool isOK() const
Definition: RESTClient.hpp:99
QString getContent() const
Definition: RESTClient.hpp:96
Definition: ApplicationException.hpp:47
Reply(int StatusCode, unsigned int NetworkErrorCode, const QString &NetworkErrorString, const QString &Content)
Definition: RESTClient.hpp:80
#define OPENFLUID_API
Definition: dllexport.hpp:86
Definition: RESTClient.hpp:58