voicevox_client_cpp  0.0.0
A client library for VOICEVOX implemented in C++.
client.hpp
Go to the documentation of this file.
1 #ifndef VOICEVOX_CLIENT_CPP_CLIEHT_HPP_
2 #define VOICEVOX_CLIENT_CPP_CLIEHT_HPP_
3 #include <functional>
4 #include <memory>
5 #include <optional>
6 
7 #include <pplx/pplxtasks.h>
8 #include <cpprest/http_msg.h>
9 #include <cpprest/json.h>
10 
11 
13 {
14 
15 namespace request
16 {
17 class Builder;
18 }
19 
23 class Client
24 {
25 public:
29  using OptionalJson = std::optional<web::json::value>;
30 
34  using OptionalBinary = std::optional<std::vector<unsigned char>>;
35 
41  template<class T>
42  using CallbackType = std::function<void(const T&)>;
43 
49  Client(const Client&) = delete;
50 
58  Client& operator=(const Client&) = delete;
59 
67  static Client& GetInstance(const std::string& uri);
68 
77  pplx::task<void> Request(
78  const web::http::http_request& req,
79  const CallbackType<OptionalJson> callback_json);
80 
89  pplx::task<void> Request(
90  const web::http::http_request& req,
91  const CallbackType<OptionalBinary> callback_audio);
92 
101  template<class T>
102  T Request(const web::http::http_request& req)
103  {
104  T out;
105  auto callback = [&out](const T& in) { out = in; };
106  auto task = Request(req, callback);
107  task.wait();
108  return out;
109  }
110 
111 private:
117  Client(const std::string& uri);
118 
122  ~Client() = default;
123 
127  std::unique_ptr<web::http::client::http_client> client_;
128 };
129 
130 extern "C"
131 {
139 Client* GetClientInstance(const char* uri);
140 
151 const char* RequestJsonString(
152  Client* client,
153  request::Builder* req_builder);
154 
165 const char* RequestString(
166  Client* client,
167  request::Builder* req_builder);
168 
174 void FreeString(const char* str);
175 } // extern "C"
176 
177 } // namespace voicevox_client_cpp
178 #endif // VOICEVOX_CLIENT_CPP_CLIEHT_HPP_
pplx::task< void > Request(const web::http::http_request &req, const CallbackType< OptionalJson > callback_json)
An interface for sending supplies to the server and receiving responses asynchronously.
pplx::task< void > Request(const web::http::http_request &req, const CallbackType< OptionalBinary > callback_audio)
An interface for sending a request to the server and receiving responses asynchronously.
Client(const Client &)=delete
Copy constructor (deleted)
std::function< void(const T &)> CallbackType
User callback type.
Definition: client.hpp:42
T Request(const web::http::http_request &req)
An interface for sending requests to the server and receiving responses synchronously.
Definition: client.hpp:102
Client & operator=(const Client &)=delete
Copy constructor (deleted)
std::optional< web::json::value > OptionalJson
Responses from the client (in JSON format).
Definition: client.hpp:29
std::optional< std::vector< unsigned char > > OptionalBinary
Responses from the client (in String format)
Definition: client.hpp:34
static Client & GetInstance(const std::string &uri)
The only interface for obtaining the client.
A client class for handlingk requests and responses to/from VOICEVOX.
Definition: client.hpp:24
A base class for a group of builder classes.
Definition: request.hpp:16
const char * RequestString(Client *client, request::Builder *req_builder)
An interface for sending requests to the server and receiving responses synchronously.
const char * RequestJsonString(Client *client, request::Builder *req_builder)
An interface for sending requests to the server and receiving responses synchronously.
void FreeString(const char *str)
Free memories of string object.
Client * GetClientInstance(const char *uri)
[extern "C"] Get instance of the client
Definition: client.hpp:13