Elasticlient
client.h
Go to the documentation of this file.
1 
6 #pragma once
7 
8 #include <string>
9 #include <memory>
10 #include <cstdint>
11 #include <stdexcept>
12 #include <vector>
13 #include <utility>
14 #include <initializer_list>
15 #include <type_traits>
16 
17 
18 // Forward cpr::Response existence.
19 namespace cpr {
20  class Response;
21 }
22 
23 
25 namespace elasticlient {
26 
27 
28 class ConnectionException: public std::runtime_error {
29  public:
30  explicit ConnectionException(const std::string &message) : std::runtime_error(message) {}
31 };
32 
33 
35 class Client {
36  class Implementation;
38  std::unique_ptr<Implementation> impl;
39 
40  public:
42  enum class HTTPMethod {
43  GET = 0,
44  POST = 1,
45  PUT = 2,
46  DELETE = 3,
47  HEAD = 4
48  };
49 
51  struct ClientOption {
52  virtual ~ClientOption() {}
57  virtual void accept(Implementation &) const = 0;
58  };
59 
61  template <typename T>
63  T value;
64 
65  explicit ClientOptionValue(const T& value): value(value) {}
66  explicit ClientOptionValue(T&& value): value(std::move(value)) {}
68  virtual ~ClientOptionValue() = default;
69 
70  T getValue() const {
71  return value;
72  }
73  };
74 
76  struct TimeoutOption: public ClientOptionValue<std::int32_t> {
77  explicit TimeoutOption(std::int32_t timeoutMs)
78  : ClientOptionValue(timeoutMs) {}
79  protected:
80  void accept(Implementation &) const override;
81  };
82 
84  struct ConnectTimeoutOption: public ClientOptionValue<std::int32_t> {
85  explicit ConnectTimeoutOption(std::int32_t timeoutMs)
86  : ClientOptionValue(timeoutMs) {}
87  protected:
88  void accept(Implementation &) const override;
89  };
90 
92  struct ProxiesOption: public ClientOption {
94  class ProxiesOptionImplementation;
95  std::unique_ptr<ProxiesOptionImplementation> impl;
96 
101  explicit ProxiesOption(
102  const std::initializer_list<std::pair<const std::string, std::string>> &proxies);
103  ~ProxiesOption();
104  protected:
105  void accept(Implementation &) const override;
106  };
107 
109  struct SSLOption: public ClientOption {
111  class SSLOptionImplementation;
112  std::unique_ptr<SSLOptionImplementation> impl;
113 
114  SSLOption();
115  SSLOption(SSLOption &&);
116  ~SSLOption();
117 
123  template <typename... T>
124  SSLOption(T... args): SSLOption() {
125  optSetter(std::move(args)...);
126  }
127 
129  struct SSLOptionType {
130  virtual ~SSLOptionType() {}
132  virtual void accept(SSLOptionImplementation &) const = 0;
133  };
134 
136  struct CertFile: public SSLOptionType {
137  explicit CertFile(std::string path): path(std::move(path)) {}
138  void accept(SSLOptionImplementation &) const override;
139  std::string path;
140  };
141 
143  struct KeyFile: public SSLOptionType {
144  explicit KeyFile(std::string path, std::string password = {})
145  : path(std::move(path)), password(std::move(password))
146  {}
147  void accept(SSLOptionImplementation &) const override;
148  std::string path;
149  std::string password;
150  };
151 
153  struct CaInfo: public SSLOptionType {
154  explicit CaInfo(std::string path): path(std::move(path)) {}
155  void accept(SSLOptionImplementation &) const override;
156  std::string path;
157  };
158 
160  struct VerifyHost: public SSLOptionType {
161  explicit VerifyHost(bool verify): verify(verify) {}
162  void accept(SSLOptionImplementation &) const override;
163  bool verify;
164  };
165 
167  struct VerifyPeer: public SSLOptionType {
168  explicit VerifyPeer(bool verify): verify(verify) {}
169  void accept(SSLOptionImplementation &) const override;
170  bool verify;
171  };
172 
174  void setSslOption(const SSLOptionType &);
175 
176  protected:
177  void accept(Implementation &) const override;
178 
180  template <typename T>
181  void optSetter(T&& t) {
182  static_assert(std::is_base_of<SSLOptionType, T>::value,
183  "Record must be derived from SSLOptionType");
184  setSslOption(std::forward<T>(t));
185  }
186 
188  template<typename T, typename... TRest>
189  void optSetter(T&& t, TRest&&... ts) {
190  static_assert(std::is_base_of<SSLOptionType, T>::value,
191  "Record must be derived from SSLOptionType");
192  optSetter(std::forward<T>(t));
193  optSetter(std::move(ts)...);
194  }
195  };
196 
197 
204  explicit Client(const std::vector<std::string> &hostUrlList,
205  std::int32_t timeout = 6000);
206 
214  explicit Client(
215  const std::vector<std::string> &hostUrlList,
216  std::int32_t timeout,
217  const std::initializer_list<std::pair<const std::string, std::string>>& proxyUrlList);
218 
227  template <typename... Opts>
228  Client(const std::vector<std::string> &hostUrlList,
229  Opts&&... opts)
230  : Client(hostUrlList)
231  {
232  optionConstructHelper(std::move(opts)...);
233  }
234 
235  Client(Client &&);
236 
237  ~Client();
238 
243  void setClientOption(const ClientOption &opt);
244 
255  cpr::Response performRequest(HTTPMethod method,
256  const std::string &urlPath,
257  const std::string &body);
258 
270  cpr::Response search(const std::string &indexName,
271  const std::string &docType,
272  const std::string &body,
273  const std::string &routing = std::string());
274 
286  cpr::Response get(const std::string &indexName,
287  const std::string &docType,
288  const std::string &id = std::string(),
289  const std::string &routing = std::string());
290 
303  cpr::Response index(const std::string &indexName,
304  const std::string &docType,
305  const std::string &id,
306  const std::string &body,
307  const std::string &routing = std::string());
308 
320  cpr::Response remove(const std::string &indexName,
321  const std::string &docType,
322  const std::string &id,
323  const std::string &routing = std::string());
324  private:
326  template <typename T>
327  void optionConstructHelper(T&& opt) {
328  static_assert(std::is_base_of<ClientOption, T>::value,
329  "Record must be derived from ClientOption");
330  setClientOption(std::forward<T>(opt));
331  }
332 
334  template <typename T, typename... TRest>
335  void optionConstructHelper(T&& opt, TRest&&... rest) {
336  static_assert(std::is_base_of<ClientOption, T>::value,
337  "Record must be derived from ClientOption");
338  optionConstructHelper(std::forward<T>(opt));
339  optionConstructHelper(std::move(rest)...);
340  }
341 };
342 
343 
344 } // namespace elasticlient
elasticlient::Client::Client
Client(const std::vector< std::string > &hostUrlList, Opts &&... opts)
Definition: client.h:228
elasticlient::Client::ClientOptionValue
Helper class holding single value for ClientOption implementations.
Definition: client.h:62
elasticlient::Client::SSLOption::KeyFile::accept
void accept(SSLOptionImplementation &) const override
Visitor initiation to setup specific option by derived classes.
elasticlient::Client::Client
Client(const std::vector< std::string > &hostUrlList, std::int32_t timeout=6000)
elasticlient::Client::SSLOption::KeyFile
Path to the certificate key file.
Definition: client.h:143
elasticlient::Client::ProxiesOption::ProxiesOption
ProxiesOption(const std::initializer_list< std::pair< const std::string, std::string >> &proxies)
elasticlient::Client::SSLOption::CaInfo::accept
void accept(SSLOptionImplementation &) const override
Visitor initiation to setup specific option by derived classes.
elasticlient::Client::TimeoutOption
The timeout [ms] setting for client connection.
Definition: client.h:76
elasticlient::Client::SSLOption
Options to setup SSL for client connection.
Definition: client.h:109
elasticlient::Client::SSLOption::setSslOption
void setSslOption(const SSLOptionType &)
Set single option - see SSLOptionType derived structs above.
elasticlient::Client::SSLOption::VerifyPeer::accept
void accept(SSLOptionImplementation &) const override
Visitor initiation to setup specific option by derived classes.
elasticlient::ConnectionException
Definition: client.h:28
elasticlient::Client::ClientOption::accept
virtual void accept(Implementation &) const =0
elasticlient::Client::HTTPMethod
HTTPMethod
Enum for kinds of supported HTTP methods in performRequest.
Definition: client.h:42
elasticlient::Client::ConnectTimeoutOption::accept
void accept(Implementation &) const override
elasticlient::Client::SSLOption::SSLOption
SSLOption(T... args)
Definition: client.h:124
elasticlient::Client::remove
cpr::Response remove(const std::string &indexName, const std::string &docType, const std::string &id, const std::string &routing=std::string())
elasticlient::Client::SSLOption::VerifyPeer
Flag whether to verify peer.
Definition: client.h:167
elasticlient::Client::SSLOption::VerifyHost
Flag whether to verify host.
Definition: client.h:160
elasticlient::Client::index
cpr::Response index(const std::string &indexName, const std::string &docType, const std::string &id, const std::string &body, const std::string &routing=std::string())
elasticlient::Client::search
cpr::Response search(const std::string &indexName, const std::string &docType, const std::string &body, const std::string &routing=std::string())
elasticlient::Client::SSLOption::SSLOptionType::accept
virtual void accept(SSLOptionImplementation &) const =0
Visitor initiation to setup specific option by derived classes.
elasticlient::Client::SSLOption::optSetter
void optSetter(T &&t)
Helper method to setup ssl options with SSLOptionType instances.
Definition: client.h:181
elasticlient
The elasticlient namespace.
Definition: bulk.h:15
elasticlient::Client::ProxiesOption
Proxies option for client connection.
Definition: client.h:92
elasticlient::Client::SSLOption::CertFile::accept
void accept(SSLOptionImplementation &) const override
Visitor initiation to setup specific option by derived classes.
elasticlient::Client::performRequest
cpr::Response performRequest(HTTPMethod method, const std::string &urlPath, const std::string &body)
elasticlient::Client::SSLOption::accept
void accept(Implementation &) const override
elasticlient::Client::get
cpr::Response get(const std::string &indexName, const std::string &docType, const std::string &id=std::string(), const std::string &routing=std::string())
elasticlient::Client::SSLOption::CaInfo
Path to the custom CA bundle.
Definition: client.h:153
elasticlient::Client::SSLOption::CertFile
Path to the certificate.
Definition: client.h:136
elasticlient::Client::ClientOption
Abstract class for various options passed to Client constructor.
Definition: client.h:51
elasticlient::Client::ConnectTimeoutOption
The connection timeout [ms] for client.
Definition: client.h:84
elasticlient::Client::SSLOption::SSLOptionType
Abstract base class for specific SSL options.
Definition: client.h:129
elasticlient::Client::TimeoutOption::accept
void accept(Implementation &) const override
elasticlient::Client::SSLOption::VerifyHost::accept
void accept(SSLOptionImplementation &) const override
Visitor initiation to setup specific option by derived classes.
elasticlient::Client
Class for managing Elasticsearch connection in one Elasticsearch cluster.
Definition: client.h:35
elasticlient::Client::SSLOption::optSetter
void optSetter(T &&t, TRest &&... ts)
Helper method to setup ssl options with SSLOptionType instances.
Definition: client.h:189
elasticlient::Client::setClientOption
void setClientOption(const ClientOption &opt)
elasticlient::Client::ProxiesOption::accept
void accept(Implementation &) const override