using Capnp.Rpc; using FabAccessAPI.Schema; using System; using System.Collections.Generic; using System.Threading.Tasks; namespace FabAccessAPI { class Connection { private TcpRpcClient? _rpcClient = null; private IBootstrap? _bootstrapCap = null; private AuthUser? _authUser = null; private Auth? _auth = null; private Machines? _machines = null; /// /// /// /// Should be an already configured and connected TcpRpcClient public Connection(TcpRpcClient rpcClient) { _rpcClient = rpcClient; _bootstrapCap = _rpcClient.GetMain(); } /// /// Authenticate this connection. /// Calling this more then once is UB /// /// The desired authentication mechanism /// Key-Value data specific to the mechanism /// async Task Auth(string mech, Dictionary kvs) { _auth = new Auth(await _bootstrapCap.Auth()); var mechs = await _auth.GetMechanisms().ConfigureAwait(false); Console.WriteLine("The Server supports the following auth mechs:"); Console.Write(mechs); // TODO: Check that the requested auth mech is actually available. await _auth.Authenticate(mech, kvs).ConfigureAwait(false); } /// /// Get a wrapped capability to interact with machines /// /// A wrapped capability to interact with machines async Task AccessMachines() { _machines ??= new Machines((await _bootstrapCap.Machines().ConfigureAwait(false))); return _machines; } } }