grpc client factory bot manager

This commit is contained in:
Borja García Rodríguez 2020-12-03 15:21:17 +01:00
Родитель 57d8fcd380
Коммит a048303fea
3 изменённых файлов: 13 добавлений и 9 удалений

Просмотреть файл

@ -9,11 +9,11 @@ namespace RPSLS.Game.Client.Clients
{ {
public class BotGameManagerClient : IBotGameManagerClient public class BotGameManagerClient : IBotGameManagerClient
{ {
private readonly GrpcChannel _grpcChannel; private readonly BotGameManager.BotGameManagerClient _botGameManagerClient;
public BotGameManagerClient(GrpcChannel GrpcChannel) public BotGameManagerClient(BotGameManager.BotGameManagerClient botGameManagerClient)
{ {
_grpcChannel = GrpcChannel; _botGameManagerClient = botGameManagerClient;
} }
public async Task<ResultDto> Play(string challenger, string username, int pick, bool twitterLogged) public async Task<ResultDto> Play(string challenger, string username, int pick, bool twitterLogged)
@ -26,8 +26,7 @@ namespace RPSLS.Game.Client.Clients
Pick = pick Pick = pick
}; };
var client = new BotGameManager.BotGameManagerClient(_grpcChannel); var result = await _botGameManagerClient.DoPlayAsync(request);
var result = await client.DoPlayAsync(request);
return new ResultDto() return new ResultDto()
{ {
Challenger = result.Challenger, Challenger = result.Challenger,
@ -41,8 +40,7 @@ namespace RPSLS.Game.Client.Clients
public async Task<IEnumerable<ChallengerDto>> Challengers() public async Task<IEnumerable<ChallengerDto>> Challengers()
{ {
var client = new BotGameManager.BotGameManagerClient(_grpcChannel); var result = await _botGameManagerClient.GetChallengersAsync(new Empty());
var result = await client.GetChallengersAsync(new Empty());
return result.Challengers.Select(ChallengerDto.FromProtoResponse).ToList(); return result.Challengers.Select(ChallengerDto.FromProtoResponse).ToList();
} }
} }

Просмотреть файл

@ -27,9 +27,14 @@ namespace RPSLS.Game.Client
var httpClient = new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) }; var httpClient = new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) };
builder.Services.AddScoped(sp => httpClient); builder.Services.AddScoped(sp => httpClient);
var httpHandler = new GrpcWebHandler(new HttpClientHandler()); var httpHandler = new GrpcWebHandler(GrpcWebMode.GrpcWebText, new HttpClientHandler());
var grpcClient = GrpcChannel.ForAddress(builder.HostEnvironment.BaseAddress, new GrpcChannelOptions { HttpHandler = httpHandler }); var grpcClient = GrpcChannel.ForAddress(builder.HostEnvironment.BaseAddress, new GrpcChannelOptions { HttpHandler = httpHandler });
builder.Services.AddSingleton(services => grpcClient);
builder.Services.AddGrpcClient<BotGameManager.BotGameManagerClient>((services, options) =>
{
options.Address = new Uri(builder.HostEnvironment.BaseAddress);
})
.ConfigurePrimaryHttpMessageHandler(() => httpHandler);
var gameSettingsManager = new GameSettingsManager.GameSettingsManagerClient(grpcClient); var gameSettingsManager = new GameSettingsManager.GameSettingsManagerClient(grpcClient);
var settings = await gameSettingsManager.GetSettingsAsync(new Empty()); var settings = await gameSettingsManager.GetSettingsAsync(new Empty());

Просмотреть файл

@ -9,6 +9,7 @@
<PackageReference Include="Google.Protobuf" Version="3.14.0" /> <PackageReference Include="Google.Protobuf" Version="3.14.0" />
<PackageReference Include="Grpc.Net.Client" Version="2.33.1" /> <PackageReference Include="Grpc.Net.Client" Version="2.33.1" />
<PackageReference Include="Grpc.Net.Client.Web" Version="2.33.1" /> <PackageReference Include="Grpc.Net.Client.Web" Version="2.33.1" />
<PackageReference Include="Grpc.Net.ClientFactory" Version="2.33.1" />
<PackageReference Include="Grpc.Tools" Version="2.33.1"> <PackageReference Include="Grpc.Tools" Version="2.33.1">
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>