예제 #1
0
 public static IServiceCollection AddSynthbotClients(this IServiceCollection services, IConfiguration config)
 {
     return(services
            .AddTransient <ISynthbotAuthenticator>(provider =>
                                                   new SynthbotDiscordUserAuthenticator(
                                                       provider.GetService <DiscordContextAccessor>()?.User?.Id.ToString() ?? "",
                                                       config["synthbot.token.sharedsecret"],
                                                       provider.GetService <DiscordContextAccessor>()?.User?.Username ?? ""))
            .AddTransient <SynthbotRestClient>(provider =>
                                               new SynthbotRestClient(
                                                   new Uri($"{config["synthbot.webapp.protocol"]}://{config["synthbot.webapp.host"]}"),
                                                   provider.GetRequiredService <ISynthbotAuthenticator>()))
            .AddSingleton <SynthbotSignalrClient>(provider =>
     {
         var builder = new HubConnectionBuilder()
                       .WithUrl($"{config["synthbot.webapp.protocol"]}://{config["synthbot.webapp.host"]}/bot-hub",
                                options =>
         {
             options.Transports = HttpTransportType.WebSockets;
             options.AccessTokenProvider = () =>
             {
                 return Task.FromResult(JwtBuilder.BuildDiscordJwtForSignalR(
                                            config["synthbot.token.sharedsecret"], SignalrUsernames.BotUsername));
             };
         })
                       .ConfigureLogging(logging => { logging.AddSerilog(); });
         //.AddMessagePackProtocol();
         return new SynthbotSignalrClient(
             provider.GetService <ILogger <SynthbotSignalrClient> >(),
             builder,
             provider.GetService <SpotifyWebAPI>(),
             config,
             provider.GetService <DiscordSocketClient>());
     }));
 }