Program Listing for File apiserver.h

Return to documentation for file (src/rlenvs/envs/api_server/apiserver.h)

#ifndef APISERVER_H
#define APISERVER_H

#include "rlenvs/rlenvs_types_v2.h"
#include "rlenvs/rlenvs_consts.h"
#include "rlenvs/extern/nlohmann/json/json.hpp"
#include "rlenvs/extern/HTTPRequest.hpp"
#include <string>
#include <vector>
#include <any>
#include <unordered_map>

namespace rlenvscpp{
namespace envs{

class RESTApiServerWrapper
{
public:

    explicit RESTApiServerWrapper(const std::string& url="http://0.0.0.0:8001/api",
                                  const bool initialize=true);

    bool is_inisialised()const noexcept{return is_init_;}

    std::string get_url()const noexcept{return url_;}

    std::string get_env_url(const std::string& name)const noexcept;

    std::string get_uri(const std::string& name)const noexcept;

    void register_new(const std::string& name, const std::string& uri);

    void register_if_not(const std::string& name, const std::string& uri);


    nlohmann::json is_alive(const std::string& env_name,
                            const uint_t cidx)const;

    nlohmann::json close(const std::string& env_name,
                         const uint_t cidx)const;

    template<typename ActionType>
    nlohmann::json step(const std::string& env_name,
                        const uint_t cidx,
                        const ActionType& action)const;

    nlohmann::json reset(const std::string& env_name,
                         const uint_t cidx,
                         const uint_t seed,
                         const nlohmann::json& options)const;

    nlohmann::json make(const std::string& env_name,
                        const uint_t cidx,
                        const std::string& version,
                        const nlohmann::json& options)const;


    nlohmann::json dynamics(const std::string& env_name,
                            const uint_t cidx,
                            const uint_t sidx,
                            const uint_t aidx)const;

    nlohmann::json copy(const std::string& env_name,
                        const uint_t cidx,
                        const std::string& version,
                        const nlohmann::json& options)const{return make(env_name, cidx, version, options);}

    bool has_gymnasium()const;

    std::vector<std::string> gymnasium_envs()const;

private:

    const std::string url_;

    bool is_init_{false};

    std::unordered_map<std::string, std::string> envs_;

    void init_();
};

template<typename ActionType>
nlohmann::json
RESTApiServerWrapper::step(const std::string& env_name, const uint_t cidx,
                           const ActionType& action)const{


    // find the source
    auto url_ = get_env_url(env_name);

    if(url_ == rlenvscpp::consts::INVALID_STR){
        throw std::logic_error("Environment: " + env_name + " is not registered");
    }

    const auto request_url = url_ + "/step";
    http::Request request{request_url};

    nlohmann::json body;
    body["cidx"] = cidx;
    body["action"] = action;

    const auto response = request.send("POST", body.dump());

    if(response.status.code != 202){
        throw std::runtime_error("Environment server failed to step environment");
    }

    auto str_response = std::string(response.body.begin(), response.body.end());
    nlohmann::json j = nlohmann::json::parse(str_response);
    return j;

}


}
}
#endif // APISERVER_H