MySQL 9.0.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
56 // The path, query, fragment parts of the URI, are ignored
57 // (overwritten when specifying the request).
59 const HttpUri &default_uri = HttpUri{"http://127.0.0.1"})
60 : io_context_{io_ctx},
61 uri_{default_uri},
62 http_client_{std::make_unique<http::client::Client>(io_ctx)} {}
63
64 RestClient(IOContext &io_ctx, TlsClientContext &&tls_context,
65 const HttpUri &default_uri = HttpUri{"http://127.0.0.1"})
66 : io_context_{io_ctx},
67 uri_{default_uri},
68 http_client_{std::make_unique<http::client::Client>(
69 io_ctx, std::move(tls_context))} {}
70
71 // Request might be send to different host than the default one.
72 // 'uri' parameter overrides default uri settings.
73 Request request_sync(http::base::method::key_type method, const HttpUri &uri,
74 const std::string &request_body = {},
75 const std::string &content_type = "application/json");
76
77 // Use default host, for this request.
78 Request request_sync(http::base::method::key_type method,
79 const std::string &path,
80 const std::string &request_body = {},
81 const std::string &content_type = "application/json");
82
83 operator bool() const { return http_client_->operator bool(); }
84
85 std::string error_msg() const { return http_client_->error_message(); }
86
87 private:
88 static std::string make_userinfo(const std::string &user,
89 const std::string &password) {
90 if (password.empty()) return user;
91
92 std::string result = user;
93
94 result += ':';
96 return result;
97 }
98
100 HttpUri uri_{"/"};
101 std::unique_ptr<http::client::Client> http_client_;
102};
103
104#endif // MYSQL_ROUTER_REST_CLIENT_H_INCLUDED
Definition: rest_client.h:46
std::string error_msg() const
Definition: rest_client.h:85
net::io_context & io_context_
Definition: rest_client.h:99
static std::string make_userinfo(const std::string &user, const std::string &password)
Definition: rest_client.h:88
RestClient(IOContext &io_ctx, TlsClientContext &&tls_context, const HttpUri &default_uri=HttpUri{"http://127.0.0.1"})
Definition: rest_client.h:64
RestClient(IOContext &io_ctx, const HttpUri &default_uri=HttpUri{"http://127.0.0.1"})
Definition: rest_client.h:58
std::unique_ptr< http::client::Client > http_client_
Definition: rest_client.h:101
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