C++ APISession methods

get_variable

Returns application variable content by name for the current initialized session.

get_variable(std::string name) returns blaze::variable for the requested application variable.

Function variants

blaze::variable get_variable(std::string name);
blaze::variable get_variable(std::string name, std::error_code& ec) noexcept;
void async_get_variable(std::string name, std::function<void(std::error_code, blaze::variable)> callback);
blaze::coro::awaitable_result<blaze::variable> co_get_variable(std::string name);

Status vs error

variable.status is the API result. std::error_code and std::system_error are used for transport/runtime failures only.

Parameters

Prop

Type

Result

struct variable {
  std::string content;
  blaze::status status;

  bool good() const noexcept;
};

Prop

Type

Checking returned fields

If the server omits variable content, the library returns a default value instead of std::optional, because it targets C++14 compatibility.

FieldPractical check
result.contentUse result.good() or result.status == blaze::status::ok before using it. result.content.empty() is not a reliable failure check because an empty string can be valid content and is also the default on non-success statuses.

Behavior notes

  • Call get_variable only after successful connect and initialize.
  • Variable access strategy is defined by the application configuration:
    • Public variables can be read by any initialized session.
    • Protected variables require an authorized session and return blaze::status::not_authorized otherwise.
  • On non-success statuses, content defaults to an empty string.

Example

Compilation note

Synchronous and Asynchronous examples compile in the same form from C++14 to C++26.

#include "blazeauth/api/api.hpp"

#include <iostream>
#include <string>
#include <system_error>

void print_error_code(const std::string& context, const std::error_code& ec) {
  std::cout << context << ": " << ec.message() << " ("
            << ec.value() << " : " << ec.category().name() << ")\n";
}

std::string read_line(const std::string& label) {
  std::string value;
  std::cout << label << ": ";
  std::getline(std::cin, value);
  return value;
}

void print_status(const std::string& context, blaze::status status) {
  std::cout << context << ": "
            << static_cast<unsigned int>(status)
            << " (" << blaze::to_string(status) << ")\n";
}

int main() {
  const std::string websocket_api_key = read_line("Websocket API key");
  const std::string client_id = read_line("Client ID");
  const std::string variable_name = read_line("Variable name");

  blaze::session session;

  try {
    const blaze::api_server server = session.connect();
    std::cout << "Connected to server location: " << server.location << '\n';

    const blaze::application app = session.initialize(websocket_api_key, client_id);
    if (!app.good()) {
      print_status("Initialize returned status", app.status);
      return 1;
    }

    const blaze::variable result = session.get_variable(variable_name);
    if (!result.good()) {
      print_status("Get-variable returned status", result.status);
      return 1;
    }

    std::cout << "Variable content: " << result.content << '\n';
    session.shutdown();
  } catch (const std::system_error& e) {
    print_error_code("Get-variable failed", e.code());
    return 1;
  }

  return 0;
}
#include "blazeauth/api/api.hpp"

#include <future>
#include <iostream>
#include <string>
#include <system_error>

void print_error_code(const std::string& context, const std::error_code& ec) {
  std::cout << context << ": " << ec.message() << " ("
            << ec.value() << " : " << ec.category().name() << ")\n";
}

std::string read_line(const std::string& label) {
  std::string value;
  std::cout << label << ": ";
  std::getline(std::cin, value);
  return value;
}

void print_status(const std::string& context, blaze::status status) {
  std::cout << context << ": "
            << static_cast<unsigned int>(status)
            << " (" << blaze::to_string(status) << ")\n";
}

int main() {
  const std::string websocket_api_key = read_line("Websocket API key");
  const std::string client_id = read_line("Client ID");
  const std::string variable_name = read_line("Variable name");

  blaze::session session;

  std::promise<int> completion;
  std::future<int> result = completion.get_future();

  session.async_connect(
    [&session, &completion, websocket_api_key, client_id, variable_name](std::error_code connect_ec, blaze::api_server server) {
      if (connect_ec) {
        print_error_code("Connect failed", connect_ec);
        completion.set_value(1);
        return;
      }

      std::cout << "Connected to server location: " << server.location << '\n';

      session.async_initialize(websocket_api_key, client_id,
        [&session, &completion, variable_name](std::error_code init_ec, blaze::application app) {
          if (init_ec) {
            print_error_code("Initialize failed", init_ec);
            completion.set_value(1);
            return;
          }

          if (!app.good()) {
            print_status("Initialize returned status", app.status);
            completion.set_value(1);
            return;
          }

          session.async_get_variable(variable_name,
            [&session, &completion](std::error_code variable_ec, blaze::variable result) {
              if (variable_ec) {
                print_error_code("Get-variable failed", variable_ec);
                completion.set_value(1);
                return;
              }

              if (!result.good()) {
                print_status("Get-variable returned status", result.status);
                completion.set_value(1);
                return;
              }

              std::cout << "Variable content: " << result.content << '\n';

              session.async_shutdown([&completion](std::error_code shutdown_ec) {
                if (shutdown_ec) {
                  print_error_code("Shutdown failed", shutdown_ec);
                  completion.set_value(1);
                  return;
                }

                completion.set_value(0);
              });
            });
        });
    });

  return result.get();
}

Project must be built with C++20 support and with coroutine support available both in the language mode and in the standard library implementation for this example to compile correctly.

#include "blazeauth/api/api.hpp"

#include <future>
#include <iostream>
#include <string>
#include <system_error>

void print_error_code(const std::string& context, const std::error_code& ec) {
  std::cout << context << ": " << ec.message() << " ("
            << ec.value() << " : " << ec.category().name() << ")\n";
}

std::string read_line(const std::string& label) {
  std::string value;
  std::cout << label << ": ";
  std::getline(std::cin, value);
  return value;
}

#if BLAZEAUTH_HAS_COROUTINES

blaze::coro::task<std::error_code> run_example(
  blaze::session& session,
  const std::string& websocket_api_key,
  const std::string& client_id,
  const std::string& variable_name
) {
  const auto [connect_ec, server] = co_await session.co_connect();
  if (connect_ec) {
    co_return connect_ec;
  }

  std::cout << "Connected to server location: " << server.location << '\n';

  const auto [init_ec, app] = co_await session.co_initialize(websocket_api_key, client_id);
  if (init_ec) {
    co_return init_ec;
  }

  if (!app.good()) {
    co_return blaze::make_error_code(app.status);
  }

  const auto [variable_ec, result] = co_await session.co_get_variable(variable_name);
  if (variable_ec) {
    co_return variable_ec;
  }

  if (!result.good()) {
    co_return blaze::make_error_code(result.status);
  }

  std::cout << "Variable content: " << result.content << '\n';

  const auto [shutdown_ec] = co_await session.co_shutdown();
  co_return shutdown_ec;
}

int main() {
  const std::string websocket_api_key = read_line("Websocket API key");
  const std::string client_id = read_line("Client ID");
  const std::string variable_name = read_line("Variable name");

  blaze::session session;

  std::promise<std::error_code> completion;
  std::future<std::error_code> result = completion.get_future();

  blaze::coro::co_spawn([&session, &completion, websocket_api_key, client_id, variable_name]() -> blaze::coro::task<void> {
    const std::error_code ec = co_await run_example(session, websocket_api_key, client_id, variable_name);
    completion.set_value(ec);
    co_return;
  }());

  const std::error_code ec = result.get();
  if (ec) {
    print_error_code("Get-variable failed", ec);
    return 1;
  }

  return 0;
}

#else

int main() {
  std::cout << "This example requires coroutine support in the Blazeauth library build.\n";
  return 0;
}

#endif

Get-variable statuses

ValueStatusReturned when
1okVariable content was returned successfully.
3not_authorizedVariable is protected and the current session is not authorized.
400variable_not_foundVariable does not exist in the current application.
401variable_invalid_namename is shorter than the minimum allowed length.

Connection-level shutdowns

Normal get-variable results are returned in result.status. The websocket may still be closed by session-level guards:

Close codeMeaningWhen it can happen
4200not_initializedget_variable was called before a successful initialize.
4201rate_limitedSession or IP rate limiting closed the websocket around this operation.

On this page