author | František Kučera <franta-hg@frantovo.cz> |
Tue, 22 Mar 2022 22:08:03 +0100 | |
branch | v_0 |
changeset 19 | 0fc76872a921 |
parent 11 | 6b913e82f52a |
permissions | -rw-r--r-- |
5
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
1 |
/** |
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
2 |
* Relational pipes |
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
3 |
* Copyright © 2022 František Kučera (Frantovo.cz, GlobalCode.info) |
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
4 |
* |
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
5 |
* This program is free software: you can redistribute it and/or modify |
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
6 |
* it under the terms of the GNU General Public License as published by |
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
7 |
* the Free Software Foundation, version 3 of the License. |
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
8 |
* |
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
9 |
* This program is distributed in the hope that it will be useful, |
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
12 |
* GNU General Public License for more details. |
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
13 |
* |
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
14 |
* You should have received a copy of the GNU General Public License |
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
15 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
16 |
*/ |
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
17 |
#pragma once |
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
18 |
|
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
19 |
#include <string> |
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
20 |
#include <vector> |
9
9fdbfbe24161
throw and report exception on error (e.g. 'Couldn't connect to server')
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
21 |
#include <stdexcept> |
5
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
22 |
|
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
23 |
#include <relpipe/common/type/typedefs.h> |
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
24 |
|
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
25 |
|
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
26 |
namespace relpipe { |
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
27 |
namespace tr { |
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
28 |
namespace http { |
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
29 |
|
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
30 |
/** |
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
31 |
* Simple synchronous client for the HTTP and HTTPS protocol. |
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
32 |
* Is not thread-safe – must not be called from multiple threads simultaneously. |
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
33 |
*/ |
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
34 |
class HTTPClient { |
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
35 |
private: |
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
36 |
class HTTPClientImpl; |
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
37 |
HTTPClientImpl* impl; |
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
38 |
|
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
39 |
HTTPClient(HTTPClientImpl* impl) : impl(impl) { |
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
40 |
} |
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
41 |
|
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
42 |
|
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
43 |
public: |
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
44 |
|
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
45 |
enum class Method { |
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
46 |
GET, |
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
47 |
HEAD, |
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
48 |
POST, |
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
49 |
PUT, |
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
50 |
DELETE, |
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
51 |
// CONNECT, |
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
52 |
// OPTIONS, |
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
53 |
// TRACE, |
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
54 |
PATCH, |
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
55 |
}; |
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
56 |
|
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
57 |
struct Request { |
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
58 |
Method method = Method::GET; |
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
59 |
std::string url; |
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
60 |
std::vector<std::string> headers; |
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
61 |
std::string body; |
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
62 |
}; |
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
63 |
|
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
64 |
struct Response { |
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
65 |
int responseCode = 0; |
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
66 |
std::vector<std::string> headers; |
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
67 |
std::string body; |
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
68 |
}; |
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
69 |
|
9
9fdbfbe24161
throw and report exception on error (e.g. 'Couldn't connect to server')
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
70 |
class Exception : public std::runtime_error { |
11
6b913e82f52a
provide also error details in exception and output
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
71 |
private: |
6b913e82f52a
provide also error details in exception and output
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
72 |
std::string details; |
9
9fdbfbe24161
throw and report exception on error (e.g. 'Couldn't connect to server')
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
73 |
public: |
9fdbfbe24161
throw and report exception on error (e.g. 'Couldn't connect to server')
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
74 |
|
11
6b913e82f52a
provide also error details in exception and output
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
75 |
Exception(const std::string& message, const std::string& details) : runtime_error(message), details(details) { |
6b913e82f52a
provide also error details in exception and output
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
76 |
} |
6b913e82f52a
provide also error details in exception and output
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
77 |
|
9
9fdbfbe24161
throw and report exception on error (e.g. 'Couldn't connect to server')
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
78 |
Exception(const std::string& message) : runtime_error(message) { |
9fdbfbe24161
throw and report exception on error (e.g. 'Couldn't connect to server')
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
79 |
} |
9fdbfbe24161
throw and report exception on error (e.g. 'Couldn't connect to server')
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
80 |
|
9fdbfbe24161
throw and report exception on error (e.g. 'Couldn't connect to server')
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
81 |
Exception(const char* message) : runtime_error(message) { |
9fdbfbe24161
throw and report exception on error (e.g. 'Couldn't connect to server')
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
82 |
} |
9fdbfbe24161
throw and report exception on error (e.g. 'Couldn't connect to server')
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
83 |
|
11
6b913e82f52a
provide also error details in exception and output
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
84 |
std::string getDetails() const { |
6b913e82f52a
provide also error details in exception and output
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
85 |
return details; |
6b913e82f52a
provide also error details in exception and output
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
86 |
} |
6b913e82f52a
provide also error details in exception and output
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
87 |
|
6b913e82f52a
provide also error details in exception and output
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
88 |
std::string getFullMessage() const { |
6b913e82f52a
provide also error details in exception and output
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
89 |
return std::string(what()) + ": " + details; |
6b913e82f52a
provide also error details in exception and output
František Kučera <franta-hg@frantovo.cz>
parents:
9
diff
changeset
|
90 |
} |
9
9fdbfbe24161
throw and report exception on error (e.g. 'Couldn't connect to server')
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
91 |
}; |
9fdbfbe24161
throw and report exception on error (e.g. 'Couldn't connect to server')
František Kučera <franta-hg@frantovo.cz>
parents:
6
diff
changeset
|
92 |
|
5
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
93 |
virtual ~HTTPClient(); |
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
94 |
HTTPClient(const HTTPClient&) = delete; |
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
95 |
HTTPClient& operator=(const HTTPClient&) = delete; |
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
96 |
|
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
97 |
static HTTPClient* open(); |
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
98 |
|
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
99 |
const Response exchange(const Request& request); |
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
100 |
|
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
101 |
}; |
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
102 |
|
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
103 |
} |
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
104 |
} |
165f6162524d
introduce HTTPClient wrapper around CURL
František Kučera <franta-hg@frantovo.cz>
parents:
diff
changeset
|
105 |
} |