Manual for OpenFLUID 2.1.11

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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  @author Armel THONI <armel.thoni@inrae.fr>
37 */
38 
39 
40 #ifndef __OPENFLUID_UTILS_RESTCLIENT_HPP__
41 #define __OPENFLUID_UTILS_RESTCLIENT_HPP__
42 
43 
44 #if QT_VERSION >= QT_VERSION_CHECK(5, 6, 0)
45 # define OPENFLUID_REST_URL_REDIRECT 1
46 #else
47 # define OPENFLUID_REST_URL_REDIRECT 0
48 #endif
49 
50 
51 #include <QSslSocket>
52 
53 #include <openfluid/dllexport.hpp>
54 
55 
56 namespace openfluid { namespace utils {
57 
58 /* List instead of map since several HTTP headers can have the same key:
59  https://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.2 */
60 typedef std::list<std::pair<QByteArray,QByteArray>> t_HeadersList;
61 
63 {
64 
65  public:
66 
67  class Reply
68  {
69  private:
70 
71  int m_StatusCode = -1;
72 
73  unsigned int m_NetworkErrorCode = 0;
74 
75  QString m_NetworkErrorString;
76 
77  QString m_Content;
78 
79 
80  public:
81 
82  Reply() = default;
83 
84  Reply(int StatusCode, unsigned int NetworkErrorCode, const QString& NetworkErrorString, const QString& Content):
85  m_StatusCode(StatusCode),
86  m_NetworkErrorCode(NetworkErrorCode),
87  m_NetworkErrorString(NetworkErrorString),
88  m_Content(Content)
89  { }
90 
91  int getStatusCode() const
92  { return m_StatusCode; }
93 
94  int getNetworkErrorCode() const
95  { return m_NetworkErrorCode; }
96 
97  QString getNetworkErrorString() const
98  { return m_NetworkErrorString; }
99 
100  QString getContent() const
101  { return m_Content; }
102 
103  bool isOK() const
104  { return m_NetworkErrorCode == 0 && m_StatusCode >= 200 && m_StatusCode < 300; }
105 
106  void clear()
107  {
108  m_StatusCode = -1;
109  m_Content.clear();
110  }
111  };
112 
114  {
115  private:
116 
117  QSslSocket::PeerVerifyMode m_CertificateVerifyMode;
118 
119  public:
120 
121  SSLConfiguration() : m_CertificateVerifyMode(QSslSocket::AutoVerifyPeer)
122  { }
123 
124  QSslSocket::PeerVerifyMode getCertificateVerifyMode() const
125  { return m_CertificateVerifyMode; }
126 
127  void setCertificateVerifyMode(QSslSocket::PeerVerifyMode Mode)
128  { m_CertificateVerifyMode = Mode; }
129  };
130 
131 
132  private:
133 
134  QString m_BaseURL;
135 
136  SSLConfiguration m_SSLConfiguration;
137 
138  t_HeadersList m_RawHeaders;
139 
140  Reply performRequest(const QString& Path, const QString& Method, const QString& Data = "") const;
141 
142 
143  public:
144 
145  RESTClient();
146 
148  { }
149 
150  void setBaseURL(const QString& URL);
151 
152  QString getBaseURL() const
153  { return m_BaseURL; }
154 
156  { m_SSLConfiguration = Config; }
157 
158  void resetRawHeaders();
159 
160  void removeRawHeader(const QByteArray Key);
161 
162  bool hasRawHeader(const QByteArray Key);
163 
164  void addRawHeader(const QByteArray Key, const QByteArray Value);
165 
167  { return m_SSLConfiguration; }
168 
169  Reply getResource(const QString& Path) const;
170 
171  Reply postResource(const QString& Path, const QString& Data) const;
172 
173  Reply putResource(const QString& Path, const QString& Data) const;
174 
175  Reply patchResource(const QString& Path, const QString& Data) const;
176 
177  Reply deleteResource(const QString& Path, const QString& Data) const;
178 
179 };
180 
181 
182 } } // namespaces
183 
184 
185 #endif /* __OPENFLUID_UTILS_RESTCLIENT_HPP__ */
openfluid::utils::RESTClient::SSLConfiguration
Definition: RESTClient.hpp:113
OPENFLUID_API
#define OPENFLUID_API
Definition: dllexport.hpp:86
openfluid::utils::RESTClient::Reply::getContent
QString getContent() const
Definition: RESTClient.hpp:100
openfluid::utils::RESTClient::Reply::Reply
Reply(int StatusCode, unsigned int NetworkErrorCode, const QString &NetworkErrorString, const QString &Content)
Definition: RESTClient.hpp:84
openfluid::utils::RESTClient::getBaseURL
QString getBaseURL() const
Definition: RESTClient.hpp:152
openfluid::utils::RESTClient::SSLConfiguration::getCertificateVerifyMode
QSslSocket::PeerVerifyMode getCertificateVerifyMode() const
Definition: RESTClient.hpp:124
openfluid::utils::RESTClient::SSLConfiguration::SSLConfiguration
SSLConfiguration()
Definition: RESTClient.hpp:121
openfluid::utils::RESTClient::Reply::getNetworkErrorCode
int getNetworkErrorCode() const
Definition: RESTClient.hpp:94
openfluid::utils::RESTClient::Reply::clear
void clear()
Definition: RESTClient.hpp:106
openfluid::utils::RESTClient::Reply::getStatusCode
int getStatusCode() const
Definition: RESTClient.hpp:91
openfluid::utils::RESTClient::Reply::isOK
bool isOK() const
Definition: RESTClient.hpp:103
openfluid::utils::RESTClient::Reply::getNetworkErrorString
QString getNetworkErrorString() const
Definition: RESTClient.hpp:97
openfluid
Definition: ApplicationException.hpp:47
openfluid::utils::RESTClient::~RESTClient
~RESTClient()
Definition: RESTClient.hpp:147
openfluid::utils::RESTClient::Reply
Definition: RESTClient.hpp:67
openfluid::utils::RESTClient::setSSLConfiguration
void setSSLConfiguration(const SSLConfiguration &Config)
Definition: RESTClient.hpp:155
openfluid::utils::RESTClient::SSLConfiguration::setCertificateVerifyMode
void setCertificateVerifyMode(QSslSocket::PeerVerifyMode Mode)
Definition: RESTClient.hpp:127
openfluid::utils::RESTClient::getSSLConfiguration
SSLConfiguration getSSLConfiguration() const
Definition: RESTClient.hpp:166
dllexport.hpp
openfluid::utils::t_HeadersList
std::list< std::pair< QByteArray, QByteArray > > t_HeadersList
Definition: RESTClient.hpp:60
openfluid::utils::RESTClient
Definition: RESTClient.hpp:62