Documentation for OpenFLUID 2.2.0
HTTPClient.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 /**
34  @file HTTPClient.hpp
35 
36  @author Jean-Christophe FABRE <jean-christophe.fabre@inra.fr>
37  */
38 
39 
40 #ifndef __OPENFLUID_UTILS_HTTPCLIENT_HPP__
41 #define __OPENFLUID_UTILS_HTTPCLIENT_HPP__
42 
43 
44 #include <string>
45 #include <map>
46 #include <list>
47 
49 #include <openfluid/dllexport.hpp>
50 
51 
52 namespace openfluid { namespace utils {
53 
54 
56 {
57  public:
58 
60  {
61  std::list<std::pair<std::string,std::string>> Data;
62 
63  bool exists(const std::string& Key) const;
64 
65  void add(const std::string& Key, const std::string& Value);
66 
67  void remove(const std::string& Key);
68  };
69 
70  /**
71  Request data structure for HTTPClient
72  */
74  {
75  /**
76  @brief Path of the request, relative to the base URL of the client
77  */
78  std::string Path;
79 
80  /**
81  @brief Parameters list to pass in URL
82  */
84 
85  /**
86  @brief Request headers
87  */
89 
90  /**
91  @brief Request body
92  */
93  std::string Body;
94  };
95 
96  /**
97  Response data structure for HTTPClient
98  */
100  {
101  /**
102  @brief Status code of the performed request
103  */
104  int StatusCode = -1;
105 
106  /**
107  @brief Network error during request (0 = no error)
108  */
109  unsigned int NetworkError = 0;
110 
111  /**
112  @brief Other error during request
113  */
114  bool OtherError = false;
115 
116  /**
117  @brief Error string if an error occurred
118  */
119  std::string ErrorStr;
120 
121  /**
122  @brief Response content
123  */
124  std::string Content;
125 
126  /**
127  Returns true if the request was successfully performed
128  */
129  bool isOK();
130  };
131 
132 
133  private:
134 
135  std::string m_BaseURL;
136 
137  KeyValueList m_DefaultHeaders;
138 
139  int m_AllowedRedirections = -1; // redirections not allowed by default
140 
141  bool m_VerifyCertificates = true;
142 
143 
144  Response performRequest(const std::string& ReqMethod, const HTTPClient::Request& Req,
145  const std::string& ContentOutputPath = std::string()) const;
146 
147 
148  public:
149 
151 
152  /**
153  Constructs an HTTP client with the given base URL
154  @param[in] BaseURL the base URL used by the client that will prefix requests paths
155  */
156  HTTPClient(const std::string& BaseURL);
157 
158  /**
159  Returns the base URL of the client
160  @return the base URL used by the client that will prefix requests paths
161  */
162  std::string getBaseURL() const
163  {
164  return m_BaseURL;
165  }
166 
167  /**
168  Sets the base URL of the client
169  @param[in] BaseURL the base URL used by the client that will prefix requests paths
170  */
171  void setBaseURL(const std::string& BaseURL)
172  {
173  m_BaseURL = BaseURL;
174  }
175 
176  /**
177  The default headers added to the headers of any request
178  @return a reference to the default headers
179  */
181  {
182  return m_DefaultHeaders;
183  }
184 
185  /**
186  Sets maximum allowed redirections
187  @param[in] MaxRedir maximum allowed redirections
188  */
189  void setAllowedRedirections(int MaxRedir);
190 
191  /**
192  Disallows redirections (sets allowed redirections to -1)
193  */
195  {
196  setAllowedRedirections(-1);
197  }
198 
199  /**
200  Enables or disables SSL certicates verification
201  @param[in] Verify enable or disable verification
202  */
203  void setCertificateVerify(bool Verify);
204 
205  /**
206  Performs a GET request
207  @param[in] Req the request data
208  @return the response to the request
209  */
210  Response getResource(const Request& Req) const;
211 
212  /**
213  Performs a POST request
214  @param[in] Req the request data
215  @return the response to the request
216  */
217  Response postResource(const Request& Req) const;
218 
219  /**
220  Performs a PUT request
221  @param[in] Req the request data
222  @return the response to the request
223  @warning not implemented
224  */
225  Response putResource(const Request& Req) const;
226 
227  /**
228  Performs a PATCH request
229  @param[in] Req the request data
230  @return the response to the request
231  @warning not implemented
232  */
233  Response patchResource(const Request& Req) const;
234 
235  /**
236  Performs a DELETE request
237  @param[in] Req the request data
238  @return the response to the request
239  @warning not implemented
240  */
241  Response deleteResource(const Request& Req) const;
242 
243  /**
244  Downloads URL contents to string
245  @param[in] URL The URL for the download
246  @param[out] Content The string containing the downloaded contents
247  */
248  static bool downloadToString(const std::string& URL, std::string& Content);
249 
250  /**
251  Downloads URL contents to file
252  @param[in] URL The URL for the download
253  @param[out] FilePath The path of the file to store the downloaded contents
254  */
255  static bool downloadToFile(const std::string& URL, const std::string& FilePath);
256 };
257 
258 
259 } } // namespaces
260 
261 
262 #endif /* __OPENFLUID_UTILS_HTTPCLIENT_HPP__ */
Definition: HTTPClient.hpp:56
Response deleteResource(const Request &Req) const
Response getResource(const Request &Req) const
std::string getBaseURL() const
Definition: HTTPClient.hpp:162
void setCertificateVerify(bool Verify)
void disallowRedirections()
Definition: HTTPClient.hpp:194
void setBaseURL(const std::string &BaseURL)
Definition: HTTPClient.hpp:171
Response postResource(const Request &Req) const
HTTPClient(const std::string &BaseURL)
static bool downloadToString(const std::string &URL, std::string &Content)
KeyValueList & defaultHeaders()
Definition: HTTPClient.hpp:180
static bool downloadToFile(const std::string &URL, const std::string &FilePath)
Response patchResource(const Request &Req) const
Response putResource(const Request &Req) const
void setAllowedRedirections(int MaxRedir)
#define OPENFLUID_API
Definition: dllexport.hpp:86
Definition: ApplicationException.hpp:47
Definition: HTTPClient.hpp:60
void add(const std::string &Key, const std::string &Value)
void remove(const std::string &Key)
std::list< std::pair< std::string, std::string > > Data
Definition: HTTPClient.hpp:61
bool exists(const std::string &Key) const
Definition: HTTPClient.hpp:74
std::string Path
Path of the request, relative to the base URL of the client.
Definition: HTTPClient.hpp:78
std::string Body
Request body.
Definition: HTTPClient.hpp:93
KeyValueList Parameters
Parameters list to pass in URL.
Definition: HTTPClient.hpp:83
KeyValueList Headers
Request headers.
Definition: HTTPClient.hpp:88
Definition: HTTPClient.hpp:100
std::string ErrorStr
Error string if an error occurred.
Definition: HTTPClient.hpp:119
std::string Content
Response content.
Definition: HTTPClient.hpp:124