ORE Studio 0.0.4
Loading...
Searching...
No Matches
nats_client.hpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 *
3 * Copyright (C) 2026 Marco Craveiro <marco.craveiro@gmail.com>
4 *
5 * This program is free software; you can redistribute it and/or modify it under
6 * the terms of the GNU General Public License as published by the Free Software
7 * Foundation; either version 3 of the License, or (at your option) any later
8 * version.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13 * details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 51
17 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 *
19 */
20#ifndef ORES_NATS_SERVICE_NATS_CLIENT_HPP
21#define ORES_NATS_SERVICE_NATS_CLIENT_HPP
22
23#include <chrono>
24#include <functional>
25#include <memory>
26#include <optional>
27#include <span>
28#include <string>
29#include <string_view>
30#include "ores.logging/make_logger.hpp"
31#include "ores.nats/config/nats_options.hpp"
32#include "ores.nats/domain/message.hpp"
33#include "ores.nats/service/session_expired_error.hpp"
34
35namespace ores::nats::service { class client; }
36
37namespace ores::nats::service {
38
72private:
73 inline static std::string_view logger_name = "ores.nats.service.nats_client";
74
75 [[nodiscard]] static auto& lg() {
76 using namespace ores::logging;
77 static auto instance = make_logger(logger_name);
78 return instance;
79 }
80
81public:
87 struct login_info {
88 std::string jwt;
89 std::string username;
90 std::string tenant_id;
91 std::string tenant_name;
92 };
93
104 using token_provider = std::function<std::string(bool force)>;
105
106 // -- Interactive path --
107
111 nats_client() = default;
112
118 void connect(config::nats_options opts);
119
123 void disconnect();
124
125 [[nodiscard]] bool is_connected() const noexcept;
126
130 void set_auth(login_info info);
131
135 void clear_auth();
136
141 [[nodiscard]] const login_info& auth() const;
142
143 // -- Service path --
144
152 nats_client(client& nats, token_provider provider);
153
154 // -- Common --
155
156 [[nodiscard]] bool is_logged_in() const noexcept;
157
163 [[nodiscard]] message request(std::string_view subject,
164 std::string_view json_body);
165
171 [[nodiscard]] message authenticated_request(std::string_view subject,
172 std::string_view json_body,
173 std::chrono::milliseconds timeout = std::chrono::seconds(30));
174
180 [[nodiscard]] message authenticated_request(std::string_view subject,
181 std::span<const std::byte> body,
182 std::chrono::milliseconds timeout = std::chrono::seconds(10));
183
189 [[nodiscard]] std::shared_ptr<client> get_client() const;
190
191private:
192 [[nodiscard]] client& active_client() const;
193
194 [[nodiscard]] message do_authenticated_request(std::string_view subject,
195 std::span<const std::byte> body, std::chrono::milliseconds timeout);
196
197 // Interactive path state
198 std::shared_ptr<client> owned_client_;
199 std::optional<login_info> auth_;
200
201 // Service path state
202 client* external_client_ = nullptr;
203 token_provider token_provider_;
204};
205
206} // namespace ores::nats::service
207
208#endif
STL namespace.
Implements logging infrastructure for ORE Studio.
Definition boost_severity.hpp:28
@ client
Indicates that the span describes a request to some remote service. This is often the client-side of ...
Configuration for a NATS connection.
Definition nats_options.hpp:30
A received NATS message.
Definition message.hpp:40
NATS client: connection, pub/sub, request/reply, and JetStream.
Definition client.hpp:73
Authenticated NATS client for both interactive and service-to-service use.
Definition nats_client.hpp:71
const login_info & auth() const
Return current auth info.
Definition nats_client.cpp:75
void clear_auth()
Clear authentication info on logout.
Definition nats_client.cpp:67
std::function< std::string(bool force)> token_provider
Callable that returns a current Bearer token.
Definition nats_client.hpp:104
nats_client()=default
Default-construct for interactive use. Call connect() next.
void set_auth(login_info info)
Store authentication info after a successful login.
Definition nats_client.cpp:63
void disconnect()
Disconnect and clear all auth state.
Definition nats_client.cpp:48
std::shared_ptr< client > get_client() const
Return the underlying client (interactive path only).
Definition nats_client.cpp:147
void connect(config::nats_options opts)
Connect to NATS and take ownership of the connection.
Definition nats_client.cpp:33
message authenticated_request(std::string_view subject, std::string_view json_body, std::chrono::milliseconds timeout=std::chrono::seconds(30))
Authenticated synchronous request — string body overload.
Definition nats_client.cpp:137
Login state for the interactive path.
Definition nats_client.hpp:87