Skip to content

Commit

Permalink
Merge pull request #22 from DmitryNaumov/master
Browse files Browse the repository at this point in the history
use Grpc.Net.Client
  • Loading branch information
DmitryNaumov authored Mar 19, 2024
2 parents 3a67efb + f624ba2 commit 7b19c6a
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 23 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
.idea
dotnet.sln.DotSettings.user
dist
artifacts
**/bin
**/obj
Yandex.Cloud.Protos/google
Expand Down
1 change: 0 additions & 1 deletion Example/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using Yandex.Cloud.Endpoint;
using Yandex.Cloud.Resourcemanager.V1;
using Yandex.Cloud;
using Yandex.Cloud.Credentials;
Expand Down
18 changes: 10 additions & 8 deletions Yandex.Cloud.Protos/Yandex.Cloud.Protos.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,29 @@
<Title>Yandex.Cloud Protocol Buffer Library</Title>
<Summary>Auto-generated library from Protocol Buffer descriptors.</Summary>
<Description>See project site for more info.</Description>
<PackageVersion>1.3.0</PackageVersion>
<PackageVersion>1.4.0-pre1</PackageVersion>
<Authors>Yandex.Cloud</Authors>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageProjectUrl>https://github.com/yandex-cloud/dotnet-sdk/</PackageProjectUrl>
<PackageReleaseNotes>Yandex Cloud SDK</PackageReleaseNotes>
<Copyright>Copyright 2023, YANDEX LLC.</Copyright>
<Copyright>Copyright 2024, YANDEX LLC.</Copyright>
<PackageTags>yandex-cloud sdk proto proto3 protobuf grpc</PackageTags>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Google.Protobuf" Version="3.19.1" />
<PackageReference Include="Grpc" Version="2.41.1" />
<PackageReference Include="Grpc.Tools" Version="2.41.1" PrivateAssets="all" />
<PackageReference Include="Contrib.Grpc.Core.M1" Version="2.41.0" />
<PackageReference Include="Google.Protobuf" Version="3.26.0" />
<PackageReference Include="Grpc.Net.Client" Version="2.61.0" />
<PackageReference Include="Grpc.Tools" Version="2.62.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<Protobuf Include="../cloudapi/third_party/googleapis/google/**/*.proto" ProtoRoot="../cloudapi/third_party/googleapis" />
<Protobuf Include="../cloudapi/yandex/**/*.proto" ProtoRoot="../cloudapi" AdditionalImportDirs="../cloudapi/third_party/googleapis" GrpcServices="Client" />
</ItemGroup>


<Target Name="Replace1" AfterTargets="Protobuf_AfterCompile">
<PropertyGroup>
<InputFile>$(IntermediateOutputPath)/yandex/cloud/datatransfer/v1/endpoint/Kafka.cs</InputFile>
Expand Down
16 changes: 11 additions & 5 deletions Yandex.Cloud.SDK/ServiceRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ protected ServiceRegistry(Sdk sdk, string service)
this.service = service;
}

protected Channel GetChannel(string serviceOverride = null)
protected ChannelBase GetChannel(string serviceOverride = null)
{
return sdk.GetChannel(serviceOverride ?? service);
}
Expand All @@ -28,24 +28,30 @@ protected Sdk Sdk()

class CachingSdk : Sdk
{
private readonly Dictionary<string, Channel> _channels = new Dictionary<string, Channel>();
private readonly Dictionary<string, ChannelBase> _channels = new Dictionary<string, ChannelBase>();

public CachingSdk(ICredentialsProvider credentialsProvider) : base(credentialsProvider)
{
}

public Channel GetChannel(string service)
public ChannelBase GetChannel(string service)
{
var endpointAddress = base.GetEndpointAddress(service);

ChannelBase serviceChannel;
lock (_channels)
{
if (_channels.TryGetValue(service, out var serviceChannel) && serviceChannel.Target == endpointAddress)
if (_channels.TryGetValue(service, out serviceChannel) &&
serviceChannel.Target == endpointAddress)
{
return serviceChannel;
}
}

serviceChannel = new Channel(endpointAddress, base.GetCredentials());
serviceChannel = GetChannel(endpointAddress, base.GetCredentials());

lock (_channels)
{
_channels[service] = serviceChannel;

return serviceChannel;
Expand Down
6 changes: 3 additions & 3 deletions Yandex.Cloud.SDK/Yandex.Cloud.SDK.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
<Title>Yandex.Cloud SDK</Title>
<Summary>Software Development Kit designed to ease interaction with Yandex.Cloud API.</Summary>
<Description>See project site for more info.</Description>
<PackageVersion>1.3.0</PackageVersion>
<PackageVersion>1.4.0-pre1</PackageVersion>
<Authors>Yandex.Cloud</Authors>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageProjectUrl>https://github.com/yandex-cloud/dotnet-sdk/</PackageProjectUrl>
<PackageReleaseNotes>Yandex Cloud SDK</PackageReleaseNotes>
<Copyright>Copyright 2023, YANDEX LLC.</Copyright>
<Copyright>Copyright 2024, YANDEX LLC.</Copyright>
<PackageTags>yandex-cloud sdk proto proto3 protobuf grpc</PackageTags>
</PropertyGroup>

Expand All @@ -27,7 +27,7 @@
</ItemGroup>

<Target Name="GenerateServices" BeforeTargets="BeforeBuild">
<Exec Command="dotnet ../artifacts/bin/tools/$(Configuration)/tools.dll ./Generated.cs" />
<Exec Command="dotnet ../artifacts/bin/tools/$(configuration)/tools.dll ./Generated.cs" />
</Target>

</Project>
16 changes: 13 additions & 3 deletions Yandex.Cloud.SDK/YandexCloud.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System.Threading.Tasks;

using System.Threading.Tasks;
using Grpc.Core;
using Grpc.Net.Client;
using Yandex.Cloud.Credentials;
using Yandex.Cloud.Endpoint;
using Yandex.Cloud.Generated;
Expand All @@ -10,21 +12,29 @@ public class Sdk
{
private readonly ChannelCredentials _channelCredentials = new SslCredentials();
private readonly ICredentialsProvider _credentialsProvider;
private readonly Channel _apiEndpointChannel;
private readonly ChannelBase _apiEndpointChannel;
public readonly Services Services;


public Sdk(ICredentialsProvider credentialsProvider)
{
_credentialsProvider = credentialsProvider;
_apiEndpointChannel = new Channel("api.cloud.yandex.net:443", _channelCredentials);
_apiEndpointChannel = GetChannel("api.cloud.yandex.net:443", _channelCredentials);
Services = new Services(this);
}

public Sdk() : this(new MetadataCredentialsProvider())
{
}

protected internal static ChannelBase GetChannel(string endpoint, ChannelCredentials credentials)
{
return GrpcChannel.ForAddress($"https://{endpoint}", new GrpcChannelOptions
{
Credentials = credentials
});
}

public ChannelCredentials GetCredentials()
{
return ChannelCredentials.Create(
Expand Down
9 changes: 6 additions & 3 deletions Yandex.Cloud.SDK/credentials/OAuthCredentialsProvider.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;
using Grpc.Core;
using Yandex.Cloud.Endpoint;
using Grpc.Net.Client;
using Yandex.Cloud.Iam.V1;

namespace Yandex.Cloud.Credentials
Expand All @@ -19,14 +19,17 @@ public OAuthCredentialsProvider(string oauthToken)

private IamTokenService.IamTokenServiceClient TokenService()
{
var channel = new Channel("iam.api.cloud.yandex.net:443", new SslCredentials());
var channel = GrpcChannel.ForAddress("https://iam.api.cloud.yandex.net:443", new GrpcChannelOptions
{
Credentials = new SslCredentials()
});
return new IamTokenService.IamTokenServiceClient(channel);
}

public string GetToken()
{
var expiration = DateTimeOffset.Now.ToUnixTimeSeconds() + 300;
if (_iamToken == null || _iamToken.ExpiresAt.Seconds > expiration)
if (_iamToken == null || _iamToken.ExpiresAt.Seconds < expiration)
{
_iamToken = _tokenService.Create(new CreateIamTokenRequest()
{
Expand Down

0 comments on commit 7b19c6a

Please sign in to comment.