C++ APISession methods

get_current_license

Reads the current in-session license authorization context.

get_current_license() returns the current license authorization context as the public blaze::license result object.

Function variants

blaze::license get_current_license();
blaze::license get_current_license(std::error_code& ec) noexcept;
void async_get_current_license(std::function<void(std::error_code, blaze::license)> callback);
blaze::coro::awaitable_result<blaze::license> co_get_current_license();

Status vs error

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

Result

struct license {
  std::string key;
  std::vector<std::string> levels;
  std::string comment;
  std::string ip_address;
  std::chrono::seconds time_left{};
  blaze::status status{};

  bool good() const noexcept;
  bool paused() const noexcept;
  bool expired() const noexcept;
  bool blacklisted() const noexcept;
  bool already_authorized() const noexcept;
  std::string comment_or(std::string value) const;
};

Prop

Type

license.good() is equivalent to status == blaze::status::ok.

Checking returned fields

Missing fields are returned as default values. If license.status != blaze::status::ok, treat metadata fields as optional in practice and check them explicitly.

FieldPractical check
license.keylicense.key.empty()
license.levelslicense.levels.empty()
license.commentlicense.comment.empty()
license.ip_addresslicense.ip_address.empty()
license.time_leftlicense.time_left.count() == 0 means no expiration metadata was provided. license.time_left.count() <= 0 means there is no positive remaining time.

Behavior notes

  • Call get_current_license only after successful connect and initialize.
  • This method reads the current in-session license context. It does not perform a new authorization request.
  • It works only for license-based authorization context. If the session is not authorized by license, the operation returns blaze::status::not_authorized.
  • If the server omits levels, comment, ip_address, or time_left, the library returns default values instead of std::optional, because it targets C++14 compatibility.
  • If the server does not include expiration metadata, time_left remains 0s.
  • If license.status == blaze::status::license_expired, the library can still contain useful license metadata such as key, levels, and comment.

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 license_key = read_line("License");

  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::license auth_result = session.authorize(license_key);
    if (!auth_result.good()) {
      print_status("Authorize returned status", auth_result.status);
      return 1;
    }

    const blaze::license current_license = session.get_current_license();
    if (!current_license.good() && !current_license.expired()) {
      print_status("Get-current-license returned status", current_license.status);
      return 1;
    }

    std::cout << "Current license key: " << current_license.key << '\n';
    std::cout << "Levels count: " << current_license.levels.size() << '\n';
    std::cout << "Time left: " << current_license.time_left.count() << "s\n";

    if (current_license.expired()) {
      print_status("Current-license status", current_license.status);
    }

    session.shutdown();
  } catch (const std::system_error& e) {
    print_error_code("Get-current-license 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 license_key = read_line("License");

  blaze::session session;

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

  session.async_connect(
    [&session, &completion, websocket_api_key, client_id, license_key](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, license_key](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_authorize(license_key,
            [&session, &completion](std::error_code auth_ec, blaze::license auth_result) {
              if (auth_ec) {
                print_error_code("Authorize failed", auth_ec);
                completion.set_value(1);
                return;
              }

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

              session.async_get_current_license(
                [&session, &completion](std::error_code current_ec, blaze::license current_license) {
                  if (current_ec) {
                    print_error_code("Get-current-license failed", current_ec);
                    completion.set_value(1);
                    return;
                  }

                  if (!current_license.good() && !current_license.expired()) {
                    print_status("Get-current-license returned status", current_license.status);
                    completion.set_value(1);
                    return;
                  }

                  std::cout << "Current license key: " << current_license.key << '\n';
                  std::cout << "Levels count: " << current_license.levels.size() << '\n';
                  std::cout << "Time left: " << current_license.time_left.count() << "s\n";

                  if (current_license.expired()) {
                    print_status("Current-license status", current_license.status);
                  }

                  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& license_key
) {
  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 [auth_ec, auth_result] = co_await session.co_authorize(license_key);
  if (auth_ec) {
    co_return auth_ec;
  }

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

  const auto [current_ec, current_license] = co_await session.co_get_current_license();
  if (current_ec) {
    co_return current_ec;
  }

  if (!current_license.good() && !current_license.expired()) {
    co_return blaze::make_error_code(current_license.status);
  }

  std::cout << "Current license key: " << current_license.key << '\n';
  std::cout << "Levels count: " << current_license.levels.size() << '\n';
  std::cout << "Time left: " << current_license.time_left.count() << "s\n";

  if (current_license.expired()) {
    co_return blaze::make_error_code(current_license.status);
  }

  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 license_key = read_line("License");

  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, license_key]() -> blaze::coro::task<void> {
    const std::error_code ec = co_await run_example(session, websocket_api_key, client_id, license_key);
    completion.set_value(ec);
    co_return;
  }());

  const std::error_code ec = result.get();
  if (ec) {
    print_error_code("Get-current-license 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-current-license statuses

ValueStatusReturned when
1okCurrent license data was returned successfully.
3not_authorizedSession is not currently authorized by license.
103license_expiredCurrent license context exists, but the license is already expired.

Connection-level shutdowns

Normal current-license results are returned in license.status. The websocket may still be closed by session-level guards:

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

On this page