Skip to content

Commit

Permalink
refactor: Simplify server address handling in Core class
Browse files Browse the repository at this point in the history
Removed unnecessary variable duplication and asynchronous token retrieval in the Core class. The server address is now directly used in condition checks and channel creation, eliminating the need for an extra variable. Also, the pre-shared key is used directly as a token instead of being retrieved asynchronously. This simplifies the code and potentially improves performance by reducing async overhead.

Additionally, updated SpiceDb version from 1.4.8 to 1.4.9 in project file to reflect these changes.
  • Loading branch information
tanczosm committed Apr 11, 2024
1 parent f6837b1 commit 7494437
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 17 deletions.
25 changes: 9 additions & 16 deletions SpiceDb/Api/Core.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,9 @@ internal class Core
public Core(string serverAddress, string preSharedKey)
{
CallOptions callOptions;
var serverAddress1 = serverAddress;
_preSharedKey = preSharedKey;

if (serverAddress1.StartsWith("http:"))
if (serverAddress.StartsWith("http:"))
{
_headers = new()
{
Expand All @@ -36,7 +35,7 @@ public Core(string serverAddress, string preSharedKey)

callOptions = new CallOptions(_headers);
}
else if (serverAddress1.StartsWith("https:"))
else if (serverAddress.StartsWith("https:"))
{
callOptions = new CallOptions();
}
Expand All @@ -45,17 +44,17 @@ public Core(string serverAddress, string preSharedKey)
throw new ArgumentException("Expecting http or https in the authzed endpoint.");
}

var channel = CreateAuthenticatedChannelAsync(serverAddress1).GetAwaiter().GetResult();
var channel = CreateAuthenticatedChannel(serverAddress);

Permissions = new SpiceDbPermissions(channel, callOptions, _headers);
Watch = new SpiceDbWatch(channel, _headers);
Schema = new SpiceDbSchema(channel, callOptions);
Experimental = new SpiceDbExperimental(channel, callOptions);
}

private async Task<ChannelBase> CreateAuthenticatedChannelAsync(string address)
private ChannelBase CreateAuthenticatedChannel(string address)
{
var token = await GetTokenAsync();
var token = _preSharedKey;
var credentials = CallCredentials.FromInterceptor((context, metadata) =>
{
if (!string.IsNullOrEmpty(token))
Expand All @@ -67,16 +66,15 @@ private async Task<ChannelBase> CreateAuthenticatedChannelAsync(string address)

//Support proxy by setting webproxy on httpClient
HttpClient.DefaultProxy = new WebProxy();

// SslCredentials is used here because this channel is using TLS.
// CallCredentials can't be used with ChannelCredentials.Insecure on non-TLS channels.
ChannelBase channel;
if (address.StartsWith("http:"))
{
Uri baseUri = new Uri(address);
channel = new Grpc.Core.Channel(baseUri.Host, baseUri.Port, ChannelCredentials.Insecure);
}
else
channel = GrpcChannel.ForAddress(address);
}
else
{
channel = GrpcChannel.ForAddress(address, new GrpcChannelOptions
{
Expand All @@ -86,11 +84,6 @@ private async Task<ChannelBase> CreateAuthenticatedChannelAsync(string address)
}
return channel;
}

private Task<string> GetTokenAsync()
{
return Task.FromResult(_preSharedKey);
}
}


2 changes: 1 addition & 1 deletion SpiceDb/SpiceDb.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<PackageIcon>logo.png</PackageIcon>
<PackageLicenseFile>LICENSE.txt</PackageLicenseFile>
<PackageReadmeFile>README.md</PackageReadmeFile>
<Version>1.4.8</Version>
<Version>1.4.9</Version>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
Expand Down

0 comments on commit 7494437

Please sign in to comment.