MySQL 9.1.0
Source Code Documentation
rest_client.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2018, 2024, Oracle and/or its affiliates.
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 designed to work 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 either included with
14 the program or referenced in the documentation.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24*/
25#ifndef MYSQL_ROUTER_REST_CLIENT_H_INCLUDED
26#define MYSQL_ROUTER_REST_CLIENT_H_INCLUDED
27
28#include <cstdint>
29#include <memory>
30#include <string>
31#include <utility>
32
33#include "http/base/method.h"
34#include "http/base/uri.h"
35#include "http/client/client.h"
36#include "http/client/request.h"
39
41
42constexpr const char kRestAPIVersion[] = "20190715";
43
45
47 public:
50
51 public:
52 RestClient(IOContext &io_ctx, const std::string &default_host,
53 uint16_t default_port, const std::string &default_username = {},
54 const std::string &default_password = {},
55 const bool use_http2 = false);
56
57 // The path, query, fragment parts of the URI, are ignored
58 // (overwritten when specifying the request).
60 const HttpUri &default_uri = HttpUri{"http://127.0.0.1"},
61 const bool use_http2 = false)
62 : io_context_{io_ctx},
63 uri_{default_uri},
64 http_client_{std::make_unique<http::client::Client>(io_ctx, use_http2)},
65 use_http2_{use_http2} {}
66
67 RestClient(IOContext &io_ctx, TlsClientContext &&tls_context,
68 const HttpUri &default_uri = HttpUri{"http://127.0.0.1"},
69 const bool use_http2 = false)
70 : io_context_{io_ctx},
71 uri_{default_uri},
72 http_client_{std::make_unique<http::client::Client>(
73 io_ctx, std::move(tls_context), use_http2)},
74 use_http2_{use_http2} {}
75
76 // Request might be send to different host than the default one.
77 // 'uri' parameter overrides default uri settings.
78 Request request_sync(http::base::method::key_type method, const HttpUri &uri,
79 const std::string &request_body = {},
80 const std::string &content_type = "application/json");
81
82 // Use default host, for this request.
83 Request request_sync(http::base::method::key_type method,
84 const std::string &path,
85 const std::string &request_body = {},
86 const std::string &content_type = "application/json");
87
88 operator bool() const { return http_client_->operator bool(); }
89
90 std::string error_msg() const { return http_client_->error_message(); }
91
92 private:
93 static std::string make_userinfo(const std::string &user,
94 const std::string &password) {
95 if (password.empty()) return user;
96
97 std::string result = user;
98
99 result += ':';
100 result += password;
101 return result;
102 }
103
105 HttpUri uri_{"/"};
106 std::unique_ptr<http::client::Client> http_client_;
108};
109
110#endif // MYSQL_ROUTER_REST_CLIENT_H_INCLUDED
Definition: rest_client.h:46
std::string error_msg() const
Definition: rest_client.h:90
net::io_context & io_context_
Definition: rest_client.h:104
static std::string make_userinfo(const std::string &user, const std::string &password)
Definition: rest_client.h:93
RestClient(IOContext &io_ctx, TlsClientContext &&tls_context, const HttpUri &default_uri=HttpUri{"http://127.0.0.1"}, const bool use_http2=false)
Definition: rest_client.h:67
RestClient(IOContext &io_ctx, const HttpUri &default_uri=HttpUri{"http://127.0.0.1"}, const bool use_http2=false)
Definition: rest_client.h:59
bool use_http2_
Definition: rest_client.h:107
std::unique_ptr< http::client::Client > http_client_
Definition: rest_client.h:106
Client TLS Context.
Definition: tls_client_context.h:43
Definition: uri.h:38
Definition: request.h:39
Definition: io_context.h:61
#define HTTP_CLIENT_EXPORT
Definition: http_client_export.h:15
static char * password
Definition: mysql_secure_installation.cc:58
char * user
Definition: mysqladmin.cc:66
static char * path
Definition: mysqldump.cc:149
int key_type
Definition: method.h:38
Request::Uri Uri
Definition: request.cc:37
struct result result
Definition: result.h:34
RestClient::Request Request
Definition: rest_client.cc:29
constexpr const char kRestAPIVersion[]
Definition: rest_client.h:42
Definition: result.h:30