1 /*
2   Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
3 
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU General Public License, version 2.0,
6   as published by the Free Software Foundation.
7 
8   This program is also distributed with certain software (including
9   but not limited to OpenSSL) that is licensed under separate terms,
10   as designated in a particular file or component or in included license
11   documentation.  The authors of MySQL hereby grant you an additional
12   permission to link the program and your derivative works with the
13   separately licensed software that they have included with MySQL.
14 
15   This program is distributed in the hope that it will be useful,
16   but WITHOUT ANY WARRANTY; without even the implied warranty of
17   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18   GNU General Public License for more details.
19 
20   You should have received a copy of the GNU General Public License
21   along with this program; if not, write to the Free Software
22   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
23 */
24 #ifndef MYSQL_ROUTER_REST_CLIENT_H_INCLUDED
25 #define MYSQL_ROUTER_REST_CLIENT_H_INCLUDED
26 
27 #include "mysqlrouter/http_client.h"
28 
29 constexpr const char kRestAPIVersion[] = "20190715";
30 
31 class HTTP_CLIENT_EXPORT RestClient {
32  public:
RestClient(IOContext & io_ctx,const std::string & address,uint16_t port)33   RestClient(IOContext &io_ctx, const std::string &address, uint16_t port)
34       : http_client_{std::unique_ptr<HttpClient>{
35             new HttpClient(io_ctx, address, port)}} {}
36 
RestClient(IOContext & io_ctx,const std::string & address,uint16_t port,const std::string & username,const std::string & password)37   RestClient(IOContext &io_ctx, const std::string &address, uint16_t port,
38              const std::string &username, const std::string &password)
39       : username_{username},
40         password_{password},
41         http_client_{std::unique_ptr<HttpClient>{
42             new HttpClient(io_ctx, address, port)}} {}
43 
RestClient(IOContext & io_ctx,const HttpUri & u,const std::string & username,const std::string & password)44   RestClient(IOContext &io_ctx, const HttpUri &u, const std::string &username,
45              const std::string &password)
46       : username_{username},
47         password_{password},
48         http_client_{std::unique_ptr<HttpClient>{
49             new HttpClient(io_ctx, u.get_host(), u.get_port())}} {}
50 
51   // build a RestClient around an existing HttpClient object that's consumed
RestClient(std::unique_ptr<HttpClient> && http_client)52   RestClient(std::unique_ptr<HttpClient> &&http_client)
53       : http_client_{std::move(http_client)} {}
54 
55   HttpRequest request_sync(
56       HttpMethod::type method, const std::string &uri,
57       const std::string &request_body = {},
58       const std::string &content_type = "application/json");
59 
60   operator bool() const { return http_client_->operator bool(); }
61 
error_msg()62   std::string error_msg() const { return http_client_->error_msg(); }
63 
64  private:
65   std::string username_;
66   std::string password_;
67   std::unique_ptr<HttpClient> http_client_;
68 };
69 
70 #endif
71