3#ifndef REST_RL_ENV_CLIENT_H
4#define REST_RL_ENV_CLIENT_H
8#include "bitrl/extern/HTTPRequest.hpp"
9#include "bitrl/extern/nlohmann/json/json.hpp"
14#include <unordered_map>
34 explicit RESTRLEnvClient(
const std::string &url =
"http://0.0.0.0:8001/api",
35 const bool initialize =
true);
45 std::string
get_url() const noexcept {
return url_; }
58 std::string
get_env_url(
const std::string &name)
const noexcept;
65 std::string
get_uri(
const std::string &name)
const noexcept;
71 void register_new(
const std::string &name,
const std::string &uri);
90 nlohmann::json
is_alive(
const std::string &env_name,
const std::string &idx)
const;
97 nlohmann::json
close(
const std::string &env_name,
const std::string &idx)
const;
106 template <
typename ActionType>
107 nlohmann::json
step(
const std::string &env_name,
const std::string &idx,
108 const ActionType &action)
const;
116 nlohmann::json
reset(
const std::string &env_name,
const std::string &idx,
const uint_t seed,
117 const nlohmann::json &options)
const;
125 nlohmann::json
make(
const std::string &env_name,
const std::string &version,
126 const nlohmann::json &options)
const;
132 nlohmann::json dynamics(
const std::string &env_name,
const std::string &idx,
const uint_t sidx,
142 const std::string url_;
148 bool is_init_{
false};
154 std::unordered_map<std::string, std::string> envs_;
162template <
typename ActionType>
164 const ActionType &action)
const
172 throw std::logic_error(
"Environment: " + env_name +
" is not registered");
175 const auto request_url = url_ +
"/" + idx +
"/step";
176 http::Request request{request_url};
179 body[
"action"] = action;
181 std::cout <<
"Sending body: " << body.dump() << std::endl;
183 const auto response = request.send(
"POST", body.dump());
184 if (response.status.code != 202)
186 throw std::runtime_error(
"Environment server failed to step environment");
189 auto str_response = std::string(response.body.begin(), response.body.end());
190 nlohmann::json j = nlohmann::json::parse(str_response);
Utility class to facilitate HTTP requests between the environments REST API and C++ drivers.
Definition rest_rl_env_client.h:29
nlohmann::json step(const std::string &env_name, const std::string &idx, const ActionType &action) const
Step in the environment with the given name and the given copy index executing action....
Definition rest_rl_env_client.h:163
std::string get_url() const noexcept
Returns the remote url.
Definition rest_rl_env_client.h:45
bool is_env_registered(const std::string &env_name) const noexcept
Definition rest_rl_env_client.cpp:39
std::string get_uri(const std::string &name) const noexcept
Returns the URI of the environment with the given name Returns INVALID_STR if the environment is not ...
Definition rest_rl_env_client.cpp:74
nlohmann::json close(const std::string &env_name, const std::string &idx) const
Close the environment with the given name. Throws std::logic_error is the environment is not register...
Definition rest_rl_env_client.cpp:117
bool is_inisialised() const noexcept
Returns true if the server is initialised.
Definition rest_rl_env_client.h:40
nlohmann::json n_copies(const std::string &name, const std::string &version=bitrl::consts::INVALID_STR) const
Definition rest_rl_env_client.cpp:141
std::string get_env_url(const std::string &name) const noexcept
Return the url for the environment with the given name.
Definition rest_rl_env_client.cpp:86
void register_if_not(const std::string &name, const std::string &uri)
Same as register_new but swallows the thrown exception.
Definition rest_rl_env_client.cpp:62
bool has_gymnasium() const
Definition rest_rl_env_client.cpp:250
nlohmann::json is_alive(const std::string &env_name, const std::string &idx) const
Queries the remote server if the environment with the given cidx is alive Throws std::logic_error is ...
Definition rest_rl_env_client.cpp:98
nlohmann::json make(const std::string &env_name, const std::string &version, const nlohmann::json &options) const
Make the the environment with the given name and the given copy index executing action....
Definition rest_rl_env_client.cpp:191
std::vector< std::string > gymnasium_envs() const
Definition rest_rl_env_client.cpp:260
nlohmann::json reset(const std::string &env_name, const std::string &idx, const uint_t seed, const nlohmann::json &options) const
Reset the the environment with the given name and the given copy index executing action....
Definition rest_rl_env_client.cpp:159
void register_new(const std::string &name, const std::string &uri)
Register a new environment. Throws std::logic_error if the environment name already exists.
Definition rest_rl_env_client.cpp:49
const std::string INVALID_STR
Invalid string.
Definition bitrl_consts.h:26
Definition bitrl_consts.h:14
std::size_t uint_t
uint_t
Definition bitrl_types.h:43