// Only needed for the LSP extension. // You can ignore this file until you reach that extension. // Course-supplied transport, using the starter's existing HTTP and JSON libraries. #pragma once #include #include #include #include #include #include class LSPClient { std::string url; public: nlohmann::json extension_to_language; static std::optional from_environment() { if (!std::getenv("CODECRAFTERS_LSP_URL")) return std::nullopt; return LSPClient{}; } LSPClient() { const char* value = std::getenv("CODECRAFTERS_LSP_URL"); if (!value) throw std::runtime_error("Run with the course LSP transport"); url = value; extension_to_language = call("configuration", nlohmann::json::object()) .at("extensionToLanguage"); } nlohmann::json call(const std::string& action, const nlohmann::json& payload) { auto response = cpr::Post(cpr::Url{url + "/" + action}, cpr::Header{{"Content-Type", "application/json"}}, cpr::Body{payload.dump()}, cpr::Timeout{10000}); if (response.status_code != 200) throw std::runtime_error(response.text); return nlohmann::json::parse(response.text); } };