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 
79  { }
80 
81  Reply(int StatusCode, unsigned int NetworkErrorCode, const QString& NetworkErrorString, const QString& Content):
82  m_StatusCode(StatusCode),
83  m_NetworkErrorCode(NetworkErrorCode),
84  m_NetworkErrorString(NetworkErrorString),
85  m_Content(Content)
86  { }
87 
88  int getStatusCode() const
89  { return m_StatusCode; }
90 
91  int getNetworkErrorCode() const
92  { return m_NetworkErrorCode; }
93 
94  QString getNetworkErrorString() const
95  { return m_NetworkErrorString; }
96 
97  QString getContent() const
98  { return m_Content; }
99 
100  bool isOK() const
101  { return m_NetworkErrorCode == 0 && m_StatusCode >= 200 && m_StatusCode < 300; }
102 
103  void clear()
104  {
105  m_StatusCode = -1;
106  m_Content.clear();
107  }
108  };
109 
111  {
112  private:
113 
114  QSslSocket::PeerVerifyMode m_CertificateVerifyMode;
115 
116  public:
117 
118  SSLConfiguration() : m_CertificateVerifyMode(QSslSocket::AutoVerifyPeer)
119  { }
120 
121  QSslSocket::PeerVerifyMode getCertificateVerifyMode() const
122  { return m_CertificateVerifyMode; }
123 
124  void setCertificateVerifyMode(QSslSocket::PeerVerifyMode Mode)
125  { m_CertificateVerifyMode = Mode; }
126  };
127 
128 
129  private:
130 
131  QString m_BaseURL;
132 
133  SSLConfiguration m_SSLConfiguration;
134 
135  Reply performRequest(const QString& Path, const QString& Method, const QString& Data = "") const;
136 
137 
138  public:
139 
140  RESTClient();
141 
143  { }
144 
145  void setBaseURL(const QString& URL);
146 
147  QString getBaseURL() const
148  { return m_BaseURL; }
149 
151  { m_SSLConfiguration = Config; }
152 
154  { return m_SSLConfiguration; }
155 
156  Reply getResource(const QString& Path) const;
157 
158  Reply postResource(const QString& Path, const QString& Data) const;
159 
160  Reply putResource(const QString& Path, const QString& Data) const;
161 
162  Reply patchResource(const QString& Path, const QString& Data) const;
163 
164  Reply deleteResource(const QString& Path, const QString& Data) const;
165 
166 };
167 
168 
169 } } // namespaces
170 
171 
172 #endif /* __OPENFLUID_UTILS_RESTCLIENT_HPP__ */
QString getNetworkErrorString() const
Definition: RESTClient.hpp:94
Definition: RESTClient.hpp:58
QSslSocket::PeerVerifyMode getCertificateVerifyMode() const
Definition: RESTClient.hpp:121
void clear()
Definition: RESTClient.hpp:103
SSLConfiguration()
Definition: RESTClient.hpp:118
void setCertificateVerifyMode(QSslSocket::PeerVerifyMode Mode)
Definition: RESTClient.hpp:124
int getStatusCode() const
Definition: RESTClient.hpp:88
QString getContent() const
Definition: RESTClient.hpp:97
Reply()
Definition: RESTClient.hpp:78
#define OPENFLUID_API
Definition: dllexport.hpp:87
Definition: ApplicationException.hpp:47
int getNetworkErrorCode() const
Definition: RESTClient.hpp:91
bool isOK() const
Definition: RESTClient.hpp:100
void setSSLConfiguration(const SSLConfiguration &Config)
Definition: RESTClient.hpp:150
SSLConfiguration getSSLConfiguration() const
Definition: RESTClient.hpp:153
~RESTClient()
Definition: RESTClient.hpp:142
Definition: RESTClient.hpp:63
QString getBaseURL() const
Definition: RESTClient.hpp:147
Reply(int StatusCode, unsigned int NetworkErrorCode, const QString &NetworkErrorString, const QString &Content)
Definition: RESTClient.hpp:81