openfga / dotnet-sdk

OpenFGA SDK for .NET - https://www.nuget.org/packages/OpenFga.Sdk
https://openfga.dev
Apache License 2.0
49 stars 7 forks source link

Example snippets in README don't compile #55

Open paulc-fugro opened 7 months ago

paulc-fugro commented 7 months ago

Checklist

Describe the problem you'd like to have solved

The current README examples don't compile as-is.

For example: https://github.com/openfga/dotnet-sdk?tab=readme-ov-file#client-credentials

The following errors when compiled:

ClientCredentials.cs(13,39): error CS0246: The type or namespace name 'Credentials' could not be found (are you missing a using directive or an assembly reference?) [okta.csproj] ClientCredentials.cs(14,34): error CS0103: The name 'CredentialsMethod' does not exist in the current context [okta.csproj] ClientCredentials.cs(15,38): error CS0246: The type or namespace name 'CredentialsConfig' could not be found (are you missing a using directive or an assembly reference?) [okta.csproj] ClientCredentials.cs(25,22): error CS0246: The type or namespace name 'ApiException' could not be found (are you missing a using directive or an assembly reference?) [okta.csproj] ClientCredentials.cs(26,18): error CS0103: The name 'Debug' does not exist in the current context [okta.csproj]

Describe the ideal solution

The experienced C# programmer will probably know which using statements to add straight away, or know where to go look. Being new to C# I have no idea which packages these belong to, or what using statements to use.

Alternatives and current workarounds

The example code seem to have everything and compiles, so I might start with that.

https://github.com/openfga/dotnet-sdk/blob/main/example/Example1/Example1.cs

I known it's on the top-level repository, but it might be worth mentioning in the readme.

References

No response

Additional context

No response

vicheanath commented 2 months ago

I have fix some param and run it it show runtime error result, if you can identify it it good if have proper ApiTokenIssuer it ready to go, i'm not really sure what dose code did, just fix compile error.

Unhandled exception. System.Net.Http.HttpRequestException: Connection refused (localhost:8080)
 ---> System.Net.Sockets.SocketException (61): Connection refused
   at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.ThrowException(SocketError error, CancellationToken cancellationToken)
   at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.System.Threading.Tasks.Sources.IValueTaskSource.GetResult(Int16 token)
   at System.Net.Sockets.Socket.<ConnectAsync>g__WaitForConnectWithCancellation|285_0(AwaitableSocketAsyncEventArgs saea, ValueTask connectTask, CancellationToken cancellationToken)
   at System.Net.Http.HttpConnectionPool.ConnectToTcpHostAsync(String host, Int32 port, HttpRequestMessage initialRequest, Boolean async, CancellationToken cancellationToken)
   --- End of inner exception stack trace ---
   at System.Net.Http.HttpConnectionPool.ConnectToTcpHostAsync(String host, Int32 port, HttpRequestMessage initialRequest, Boolean async, CancellationToken cancellationToken)
   at System.Net.Http.HttpConnectionPool.ConnectAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
   at System.Net.Http.HttpConnectionPool.CreateHttp11ConnectionAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
   at System.Net.Http.HttpConnectionPool.AddHttp11ConnectionAsync(QueueItem queueItem)
   at System.Threading.Tasks.TaskCompletionSourceWithCancellation`1.WaitWithCancellationAsync(CancellationToken cancellationToken)
   at System.Net.Http.HttpConnectionPool.SendWithVersionDetectionAndRetryAsync(HttpRequestMessage request, Boolean async, Boolean doRequestAuth, CancellationToken cancellationToken)
   at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)
   at System.Net.Http.HttpClient.<SendAsync>g__Core|83_0(HttpRequestMessage request, HttpCompletionOption completionOption, CancellationTokenSource cts, Boolean disposeCts, CancellationTokenSource pendingRequestsCts, CancellationToken originalCancellationToken)
   at OpenFga.Sdk.ApiClient.BaseClient.SendRequestAsync[T](HttpRequestMessage request, IDictionary`2 additionalHeaders, String apiName, CancellationToken cancellationToken)
   at OpenFga.Sdk.ApiClient.BaseClient.SendRequestAsync[T](RequestBuilder requestBuilder, IDictionary`2 additionalHeaders, String apiName, CancellationToken cancellationToken)
   at OpenFga.Sdk.ApiClient.OAuth2Client.ExchangeTokenAsync(CancellationToken cancellationToken)
   at OpenFga.Sdk.ApiClient.OAuth2Client.GetAccessTokenAsync()
   at OpenFga.Sdk.ApiClient.ApiClient.SendRequestAsync[T](RequestBuilder requestBuilder, String apiName, CancellationToken cancellationToken)
   at OpenFga.Sdk.Api.OpenFgaApi.ReadAuthorizationModels(Nullable`1 pageSize, String continuationToken, CancellationToken cancellationToken)
   at OpenFga.Sdk.Client.OpenFgaClient.ReadAuthorizationModels(IClientReadAuthorizationModelsOptions options, CancellationToken cancellationToken)
   at Example.Example.Main() in /Users/vichea/dotnet-sdk/example/Example1/Example1.cs:line 297
   at Example.Example.<Main>()

Fixed Code

using OpenFga.Sdk.Client;
using OpenFga.Sdk.Configuration;
using OpenFga.Sdk.Exceptions;
using System.Diagnostics;

namespace Example {
    public class Example {
        public static async Task Main() {
            try {
                var configuration = new ClientConfiguration() {
                    ApiScheme = "https", // required, e.g. https
                    ApiHost = Environment.GetEnvironmentVariable("FGA_API_URL") ?? "localhost:8080", // required, e.g. https://api.fga.example
                    StoreId = Environment.GetEnvironmentVariable("FGA_STORE_ID") ?? "test-store", // not needed when calling `CreateStore` or `ListStores`
                    AuthorizationModelId = Environment.GetEnvironmentVariable("FGA_MODEL_ID"), // Optional, can be overridden per request
                    Credentials = new Credentials() {
                        Method = CredentialsMethod.ClientCredentials,
                        Config = new CredentialsConfig() {
                            ApiTokenIssuer = Environment.GetEnvironmentVariable("FGA_API_TOKEN_ISSUER") ?? "localhost:8080",
                            ApiAudience = Environment.GetEnvironmentVariable("FGA_API_AUDIENCE") ?? "test-audience",
                            ClientId = Environment.GetEnvironmentVariable("FGA_CLIENT_ID") ?? "test-client",
                            ClientSecret = Environment.GetEnvironmentVariable("FGA_CLIENT_SECRET") ?? "test-secret"
                        }
                    }
                };
                var fgaClient = new OpenFgaClient(configuration);
                var response = await fgaClient.ReadAuthorizationModels();
            } catch (ApiException e) {
                 Debug.Print("Error: "+ e);
            }
        }
    }
}