Skip to content

Commit

Permalink
feat: handle optional schema prefix in SpiceDbClient
Browse files Browse the repository at this point in the history
This commit introduces the ability to handle an optional schema prefix in the `SpiceDbClient` constructors. This change allows for more flexibility when initializing a new instance of the `SpiceDbClient` class, as it no longer requires a schema prefix. The handling of empty or null prefixes has been added across various methods and classes to ensure consistent behavior.

BREAKING CHANGE: The constructors for `SpiceDbClient` now accept an optional parameter for `schemaPrefix`. If not provided, it defaults to null.
  • Loading branch information
tanczosm committed Jul 9, 2024
1 parent 9748f08 commit 0417b8f
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 11 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ SpiceDb.net was created by Michael Tanczos and has contributions from Pavel Akim

## What's New?

1.5.3
- BREAKING CHANGE: The constructors for `SpiceDbClient` now accept a nullable `schemaPrefix` instead of a required string. If not provided, it defaults to null.
- @williamgraver added empty schema prefix handling for dedicated version of SpiceDb
- @dystopiandev exposed ReadSchemaAsync method

1.5.2
- BREAKING CHANGE: ReadRelationships now accepts a SubjectFilter (thanks to @epbensimpson) for the second parameter along with an optional relationship limit and cursor
- BREAKING CHANGE: DeleteRelationshipsAsync utilizes an optional SubjectFilter (thanks to @epbensimpson) instead of a RelationshipFilter
Expand Down Expand Up @@ -113,7 +118,7 @@ The `SpiceDbClient` class provides a client for interacting with Authzed's Spice

## Constructors

### SpiceDbClient(string token, string schemaPrefix)
### SpiceDbClient(string token, string? schemaPrefix = null)

Initializes a new instance of the `SpiceDbClient` class using the default Authzed server address.

Expand All @@ -122,7 +127,7 @@ Initializes a new instance of the `SpiceDbClient` class using the default Authze
- `token` - Token with admin privileges for manipulating the desired permission system.
- `schemaPrefix` - Schema prefix used for the permission system.

### SpiceDbClient(string serverAddress, string token, string schemaPrefix)
### SpiceDbClient(string serverAddress, string token, string? schemaPrefix = null)

Initializes a new instance of the `SpiceDbClient` class with the specified server address, token, and schema prefix.

Expand Down
2 changes: 1 addition & 1 deletion SpiceDb/Models/ResourceReference.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ private void ProcessId()
/// <returns></returns>
public ResourceReference WithSubjectRelation(string relation) => new ResourceReference(this.Type, this.Id, relation);

public ResourceReference EnsurePrefix(string prefix)
public ResourceReference EnsurePrefix(string? prefix)
{
var type = this.Type;
if (string.IsNullOrEmpty(prefix))
Expand Down
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.5.2</Version>
<Version>1.5.3</Version>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
Expand Down
15 changes: 8 additions & 7 deletions SpiceDb/SpiceDbClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ public sealed class SpiceDbClient : ISpiceDbClient
/// Create a new client with the default Authzed server address
/// </summary>
/// <param name="token">Token with admin privileges that can manipulate the desired permission system</param>
/// <param name="schemaPrefix">Schema prefix used for permission system</param>
public SpiceDbClient(string token, string schemaPrefix) : this("https://grpc.authzed.com", token, schemaPrefix)
/// <param name="schemaPrefix">(Optional) Schema prefix used for permission system</param>
public SpiceDbClient(string token, string? schemaPrefix = null) : this("https://grpc.authzed.com", token, schemaPrefix)
{
}

Expand All @@ -32,9 +32,9 @@ public SpiceDbClient(string token, string schemaPrefix) : this("https://grpc.aut
/// </summary>
/// <param name="serverAddress">The server address of the Authzed server.</param>
/// <param name="token">The token with admin privileges for manipulating the desired permission system.</param>
/// <param name="schemaPrefix">The schema prefix used for the permission system.</param>
/// <param name="schemaPrefix">(Optional) The schema prefix used for the permission system.</param>
/// <exception cref="Exception">Thrown when the server address or token is null or empty, or if the schema prefix does not meet the required format.</exception>
public SpiceDbClient(string serverAddress, string token, string? schemaPrefix)
public SpiceDbClient(string serverAddress, string token, string? schemaPrefix = null)
{
if (string.IsNullOrEmpty(serverAddress) || !serverAddress.StartsWith("http"))
throw new ArgumentException("Expecting http:// or https:// in the SpiceDb endpoint.");
Expand All @@ -55,9 +55,9 @@ public SpiceDbClient(string serverAddress, string token, string? schemaPrefix)
/// <param name="channel">The grpc channel used to connect to server</param>
/// <param name="schemaPrefix">The schema prefix used for the permission system.</param>
/// <exception cref="Exception">Thrown when the server address or token is null or empty, or if the schema prefix does not meet the required format.</exception>
public SpiceDbClient(ChannelBase channel, string schemaPrefix)
public SpiceDbClient(ChannelBase channel, string? schemaPrefix = null)
{
if (!Regex.IsMatch(schemaPrefix, @"^[a-zA-Z0-9_]{3,63}[a-zA-Z0-9]$"))
if (schemaPrefix is not null && !Regex.IsMatch(schemaPrefix, @"^[a-zA-Z0-9_]{3,63}[a-zA-Z0-9]$"))
throw new Exception(
"Schema prefixes must be alphanumeric, lowercase, between 4-64 characters and not end in an underscore");

Expand Down Expand Up @@ -288,6 +288,7 @@ public async Task<DeleteRelationshipsResponse> DeleteRelationshipsAsync(Relation
/// <param name="permission">Permission relationship to evaluate</param>
/// <param name="context">Additional context information that may be needed for evaluating caveats</param>
/// <param name="zedToken"></param>
/// <param name="cacheFreshness"></param>
/// <returns></returns>
public async Task<PermissionResponse> CheckPermissionAsync(Models.Permission permission,
Dictionary<string, object>? context = null, ZedToken? zedToken = null,
Expand Down Expand Up @@ -542,7 +543,7 @@ public async Task ImportSchemaFromFileAsync(string filePath)
/// <returns></returns>
public async Task ImportSchemaFromStringAsync(string schema)
{
var prefix = _prefix;
var prefix = _prefix ?? string.Empty;

if (prefix.Length > 0 && !prefix.EndsWith("/")) prefix += "/";

Expand Down

0 comments on commit 0417b8f

Please sign in to comment.