BitShares-Core  6.1.0
BitShares blockchain implementation and command-line interface software
elasticsearch.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018 oxarbitrage, and contributors.
3  *
4  * The MIT License
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24 #pragma once
25 #include <cstddef>
26 #include <string>
27 #include <vector>
28 
29 #include <curl/curl.h>
30 
31 #include <fc/variant_object.hpp>
32 
33 namespace graphene { namespace utilities {
34 
36 {
37 public:
38  curl_wrapper();
39 
40  // Note: the numbers are used in the request() function. If we need to update or add, please check the function
42  {
43  HTTP_GET = 0,
44  HTTP_POST = 1,
45  HTTP_HEAD = 2,
46  HTTP_PUT = 3,
47  HTTP_DELETE = 4,
48  HTTP_PATCH = 5,
49  HTTP_OPTIONS = 6
50  };
51 
53  {
54  static constexpr uint16_t HTTP_200 = 200;
55  static constexpr uint16_t HTTP_401 = 401;
56  static constexpr uint16_t HTTP_413 = 413;
57  };
58 
60  {
61  uint16_t code;
62  std::string content;
63  bool is_200() const;
64  };
65 
67  const std::string& url,
68  const std::string& auth,
69  const std::string& query ) const;
70 
71  http_response get( const std::string& url, const std::string& auth ) const;
72  http_response del( const std::string& url, const std::string& auth ) const;
73  http_response post( const std::string& url, const std::string& auth, const std::string& query ) const;
74  http_response put( const std::string& url, const std::string& auth, const std::string& query ) const;
75 
76 private:
77 
78  static CURL* init_curl();
79  static curl_slist* init_request_headers();
80 
81  struct curl_deleter
82  {
83  void operator()( CURL* p_curl ) const;
84  };
85 
86  struct curl_slist_deleter
87  {
88  void operator()( curl_slist* slist ) const;
89  };
90 
91  std::unique_ptr<CURL, curl_deleter> curl { init_curl() };
92  std::unique_ptr<curl_slist, curl_slist_deleter> request_headers { init_request_headers() };
93 };
94 
95 class es_client
96 {
97 public:
98  es_client( const std::string& p_base_url, const std::string& p_auth ) : base_url(p_base_url), auth(p_auth) {}
99 
100  bool check_status() const;
101  std::string get_version() const;
102  void check_version_7_or_above( bool& result ) const noexcept;
103 
104  bool send_bulk( const std::vector<std::string>& bulk_lines ) const;
105  bool del( const std::string& path ) const;
106  std::string get( const std::string& path ) const;
107  std::string query( const std::string& path, const std::string& query ) const;
108 
110  static constexpr size_t request_size_threshold = 4 * 1024 * 1024; // 4MB
111 private:
112  std::string base_url;
113  std::string auth;
114  curl_wrapper curl;
115 };
116 
117 std::vector<std::string> createBulk(const fc::mutable_variant_object& bulk_header, std::string&& data);
118 
120 {
121  enum class data_type
122  {
123  static_variant_type,
124  map_type,
125  array_type // can be simple arrays, object arrays, static_variant arrays, or even nested arrays
126  };
127 
128  static fc::variant adapt( const fc::variant_object& op, uint16_t max_depth );
129 
130  static fc::variant adapt( const fc::variants& v, data_type type, uint16_t max_depth );
131 
132  static fc::variant adapt_map_item( const fc::variants& v, uint16_t max_depth );
133 
134  static fc::variant adapt_static_variant( const fc::variants& v, uint16_t max_depth );
135 
137  static void in_situ_adapt( fc::variants& v, uint16_t max_depth );
138 
140  static void extract_data_from_variant( const fc::variant& v,
142  const std::string& prefix,
143  uint16_t max_depth );
144 
145 };
146 
147 } } // end namespace graphene::utilities
http_response request(http_request_method method, const std::string &url, const std::string &auth, const std::string &query) const
http_response del(const std::string &url, const std::string &auth) const
es_client(const std::string &p_base_url, const std::string &p_auth)
An order-perserving dictionary of variant&#39;s.
Definition: api.cpp:48
std::vector< variant > variants
Definition: variant.hpp:170
std::vector< std::string > createBulk(const fc::mutable_variant_object &bulk_header, std::string &&data)
http_response post(const std::string &url, const std::string &auth, const std::string &query) const
http_response put(const std::string &url, const std::string &auth, const std::string &query) const
stores null, int64, uint64, double, bool, string, std::vector<variant>, and variant_object&#39;s.
Definition: variant.hpp:198
An order-perserving dictionary of variant&#39;s.