mirror of
https://github.com/azaion/annotations.git
synced 2026-04-22 09:46:30 +00:00
62623b7123
rewrite zmq to DEALER and ROUTER add GET_USER command to get CurrentUser from Python all auth is on the python side inference run and validate annotations on python
61 lines
1.7 KiB
C#
61 lines
1.7 KiB
C#
using System.Text;
|
|
using Azaion.CommonSecurity.DTO;
|
|
using Azaion.CommonSecurity.DTO.Commands;
|
|
using MessagePack;
|
|
using NetMQ;
|
|
using NetMQ.Sockets;
|
|
|
|
namespace Azaion.CommonSecurity.Services;
|
|
|
|
public interface IResourceLoader
|
|
{
|
|
Task<MemoryStream> LoadFile(string fileName, CancellationToken ct = default);
|
|
}
|
|
|
|
public interface IAuthProvider
|
|
{
|
|
User CurrentUser { get; }
|
|
}
|
|
|
|
|
|
public class PythonResourceLoader : IResourceLoader, IAuthProvider
|
|
{
|
|
private readonly DealerSocket _dealer = new();
|
|
private readonly Guid _clientId = Guid.NewGuid();
|
|
|
|
public User CurrentUser { get; }
|
|
|
|
public PythonResourceLoader(ApiCredentials credentials)
|
|
{
|
|
//Run python by credentials
|
|
_dealer.Options.Identity = Encoding.UTF8.GetBytes(_clientId.ToString("N"));
|
|
_dealer.Connect($"tcp://{SecurityConstants.ZMQ_HOST}:{SecurityConstants.ZMQ_PORT}");
|
|
|
|
_dealer.SendFrame(MessagePackSerializer.Serialize(new RemoteCommand(CommandType.GetUser)));
|
|
var user = _dealer.Get<User>(out _);
|
|
if (user == null)
|
|
throw new Exception("Can't get user from Auth provider");
|
|
|
|
CurrentUser = user;
|
|
}
|
|
|
|
|
|
public async Task<MemoryStream> LoadFile(string fileName, CancellationToken ct = default)
|
|
{
|
|
try
|
|
{
|
|
_dealer.SendFrame(MessagePackSerializer.Serialize(new RemoteCommand(CommandType.Load, fileName)));
|
|
|
|
if (!_dealer.TryReceiveFrameBytes(TimeSpan.FromMilliseconds(1000), out var bytes))
|
|
throw new Exception($"Unable to receive {fileName}");
|
|
|
|
return await Task.FromResult(new MemoryStream(bytes));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw new Exception($"Failed to load fil0e '{fileName}': {ex.Message}", ex);
|
|
}
|
|
}
|
|
|
|
}
|