Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example snippets in README don't compile #55

Open
1 task done
paulc-fugro opened this issue Apr 8, 2024 · 1 comment
Open
1 task done

Example snippets in README don't compile #55

paulc-fugro opened this issue Apr 8, 2024 · 1 comment
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@paulc-fugro
Copy link

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

@paulc-fugro paulc-fugro added the enhancement New feature or request label Apr 8, 2024
@rhamzeh rhamzeh added the good first issue Good for newcomers label Apr 11, 2024
@rhamzeh rhamzeh moved this from Backlog to Ready in SDKs and Tooling Apr 24, 2024
@vicheanath
Copy link

vicheanath commented Sep 28, 2024

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);
            }
        }
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
Status: Ready
Development

No branches or pull requests

3 participants