Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,10 @@ namespace Example {
Credentials = new Credentials() {
Method = CredentialsMethod.ClientCredentials,
Config = new CredentialsConfig() {
// API Token Issuer can contain:
// - a scheme, defaults to https
// - a path, defaults to /oauth/token
// - a port
ApiTokenIssuer = Environment.GetEnvironmentVariable("FGA_API_TOKEN_ISSUER"),
ApiAudience = Environment.GetEnvironmentVariable("FGA_API_AUDIENCE"),
ClientId = Environment.GetEnvironmentVariable("FGA_CLIENT_ID"),
Expand Down
59 changes: 30 additions & 29 deletions src/OpenFga.Sdk.Test/Api/OpenFgaApiTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using System.Threading;
using System.Threading.Tasks;
using Xunit;
using SdkConfiguration = OpenFga.Sdk.Configuration.Configuration;

namespace OpenFga.Sdk.Test.Api {
/// <summary>
Expand All @@ -24,11 +25,11 @@ namespace OpenFga.Sdk.Test.Api {
public class OpenFgaApiTests : IDisposable {
private readonly string _storeId;
private readonly string _host = "api.fga.example";
private readonly Configuration.Configuration _config;
private readonly SdkConfiguration _config;

public OpenFgaApiTests() {
_storeId = "01H0H015178Y2V4CX10C2KGHF4";
_config = new Configuration.Configuration() { ApiHost = _host };
_config = new SdkConfiguration { ApiHost = _host };
}

private HttpResponseMessage GetCheckResponse(CheckResponse content, bool shouldRetry = false) {
Expand Down Expand Up @@ -56,7 +57,7 @@ public void Dispose() {
/// </summary>
[Fact]
public void StoreIdNotRequired() {
var storeIdRequiredConfig = new Configuration.Configuration() { ApiHost = _host };
var storeIdRequiredConfig = new SdkConfiguration { ApiHost = _host };
storeIdRequiredConfig.EnsureValid();
}

Expand All @@ -65,7 +66,7 @@ public void StoreIdNotRequired() {
/// </summary>
[Fact]
public async Task StoreIdRequiredWhenNeeded() {
var config = new Configuration.Configuration() { ApiHost = _host };
var config = new SdkConfiguration { ApiHost = _host };
var openFgaApi = new OpenFgaApi(config);

async Task<ReadAuthorizationModelsResponse> ActionMissingStoreId() => await openFgaApi.ReadAuthorizationModels(null, null);
Expand All @@ -78,7 +79,7 @@ public async Task StoreIdRequiredWhenNeeded() {
// /// </summary>
[Fact]
public void ValidHostRequired() {
var config = new Configuration.Configuration() { };
var config = new SdkConfiguration();
void ActionMissingHost() => config.EnsureValid();
var exception = Assert.Throws<FgaRequiredParamError>(ActionMissingHost);
Assert.Equal("Required parameter ApiUrl was not defined when calling Configuration.", exception.Message);
Expand All @@ -89,7 +90,7 @@ public void ValidHostRequired() {
// /// </summary>
[Fact]
public void ValidHostWellFormed() {
var config = new Configuration.Configuration() { ApiHost = "https://api.fga.example" };
var config = new SdkConfiguration { ApiHost = "https://api.fga.example" };
void ActionMalformedHost() => config.EnsureValid();
var exception = Assert.Throws<FgaValidationError>(ActionMalformedHost);
Assert.Equal("Configuration.ApiUrl (https://https://api.fga.example) does not form a valid URI (https://https://api.fga.example)", exception.Message);
Expand All @@ -100,7 +101,7 @@ public void ValidHostWellFormed() {
/// </summary>
[Fact]
public void ApiTokenRequired() {
var missingApiTokenConfig = new Configuration.Configuration() {
var missingApiTokenConfig = new SdkConfiguration {
ApiHost = _host,
Credentials = new Credentials() {
Method = CredentialsMethod.ApiToken,
Expand All @@ -119,14 +120,14 @@ void ActionMissingApiToken() =>
// /// </summary>
[Fact]
public void ValidApiTokenIssuerWellFormed() {
var config = new Configuration.Configuration() {
var config = new SdkConfiguration {
ApiHost = _host,
Credentials = new Credentials() {
Method = CredentialsMethod.ClientCredentials,
Config = new CredentialsConfig() {
ClientId = "some-id",
ClientSecret = "some-secret",
ApiTokenIssuer = "https://tokenissuer.fga.example",
ApiTokenIssuer = "https://https://tokenissuer.fga.example",
ApiAudience = "some-audience",
}
}
Expand All @@ -142,7 +143,7 @@ public void ValidApiTokenIssuerWellFormed() {
[Fact]
public async Task ApiTokenSentInHeader() {
var mockHandler = new Mock<HttpMessageHandler>(MockBehavior.Strict);
var config = new Configuration.Configuration() {
var config = new SdkConfiguration {
ApiHost = _host,
Credentials = new Credentials() {
Method = CredentialsMethod.ApiToken,
Expand Down Expand Up @@ -189,7 +190,7 @@ public async Task ApiTokenSentInHeader() {
/// </summary>
[Fact]
public void ApiTokenDoesNotSetReservedHeaderInDefaultHeaders() {
var config = new Configuration.Configuration() {
var config = new SdkConfiguration {
ApiHost = _host,
Credentials = new Credentials() {
Method = CredentialsMethod.ApiToken,
Expand All @@ -211,7 +212,7 @@ public void ApiTokenDoesNotSetReservedHeaderInDefaultHeaders() {
/// </summary>
[Fact]
public void ClientIdClientSecretRequired() {
var missingClientIdConfig = new Configuration.Configuration() {
var missingClientIdConfig = new SdkConfiguration {
StoreId = _storeId,
ApiHost = _host,
Credentials = new Credentials() {
Expand All @@ -230,7 +231,7 @@ void ActionMissingClientId() =>
Assert.Equal("Required parameter ClientId was not defined when calling Configuration.",
exceptionMissingClientId.Message);

var missingClientSecretConfig = new Configuration.Configuration() {
var missingClientSecretConfig = new SdkConfiguration {
StoreId = _storeId,
ApiHost = _host,
Credentials = new Credentials() {
Expand All @@ -249,7 +250,7 @@ void ActionMissingClientSecret() =>
Assert.Equal("Required parameter ClientSecret was not defined when calling Configuration.",
exceptionMissingClientSecret.Message);

var missingApiTokenIssuerConfig = new Configuration.Configuration() {
var missingApiTokenIssuerConfig = new SdkConfiguration {
StoreId = _storeId,
ApiHost = _host,
Credentials = new Credentials() {
Expand All @@ -268,7 +269,7 @@ void ActionMissingApiTokenIssuer() =>
Assert.Equal("Required parameter ApiTokenIssuer was not defined when calling Configuration.",
exceptionMissingApiTokenIssuer.Message);

var missingApiAudienceConfig = new Configuration.Configuration() {
var missingApiAudienceConfig = new SdkConfiguration {
StoreId = _storeId,
ApiHost = _host,
Credentials = new Credentials() {
Expand All @@ -294,7 +295,7 @@ void ActionMissingApiAudience() =>
/// </summary>
[Fact]
public async Task ExchangeCredentialsTest() {
var config = new Configuration.Configuration() {
var config = new SdkConfiguration {
StoreId = _storeId,
ApiHost = _host,
Credentials = new Credentials() {
Expand Down Expand Up @@ -394,7 +395,7 @@ public async Task ExchangeCredentialsTest() {
/// </summary>
[Fact]
public async Task ExchangeCredentialsAfterExpiryTest() {
var config = new Configuration.Configuration() {
var config = new SdkConfiguration {
ApiHost = _host,
Credentials = new Credentials() {
Method = CredentialsMethod.ClientCredentials,
Expand Down Expand Up @@ -493,7 +494,7 @@ public async Task ExchangeCredentialsAfterExpiryTest() {
/// </summary>
[Fact]
public async Task ExchangeCredentialsRetriesTest() {
var config = new Configuration.Configuration() {
var config = new SdkConfiguration {
ApiHost = _host,
Credentials = new Credentials() {
Method = CredentialsMethod.ClientCredentials,
Expand Down Expand Up @@ -658,7 +659,7 @@ public async Task FgaApiInternalErrorTest() {

var httpClient = new HttpClient(mockHandler.Object);

var config = new Configuration.Configuration() {
var config = new SdkConfiguration {
ApiHost = _host,
MaxRetry = 1,
};
Expand Down Expand Up @@ -856,7 +857,7 @@ public async Task FgaApiRateLimitExceededErrorTest() {
.ReturnsAsync(GetCheckResponse(new CheckResponse { Allowed = true }, true));

var httpClient = new HttpClient(mockHandler.Object);
var config = new Configuration.Configuration() {
var config = new SdkConfiguration {
StoreId = _storeId,
ApiHost = _host,
MaxRetry = 5,
Expand Down Expand Up @@ -954,7 +955,7 @@ public async Task RetryOnRateLimitTest() {

var httpClient = new HttpClient(mockHandler.Object);

var config = new Configuration.Configuration() {
var config = new SdkConfiguration {
StoreId = _storeId,
ApiHost = _host,
MaxRetry = 3,
Expand Down Expand Up @@ -2018,7 +2019,7 @@ public async Task RetryAfterIntegerFormatInErrorTest() {
});

var httpClient = new HttpClient(mockHandler.Object);
var config = new Configuration.Configuration() {
var config = new SdkConfiguration {
ApiHost = _host,
MaxRetry = 2,
};
Expand Down Expand Up @@ -2090,7 +2091,7 @@ public async Task RetryAfterHttpDateFormatInErrorTest() {
});

var httpClient = new HttpClient(mockHandler.Object);
var config = new Configuration.Configuration() {
var config = new SdkConfiguration {
ApiHost = _host,
MaxRetry = 1,
};
Expand Down Expand Up @@ -2198,7 +2199,7 @@ public async Task RetryAfterOutOfBoundsTooLargeTest() {
});

var httpClient = new HttpClient(mockHandler.Object);
var config = new Configuration.Configuration() {
var config = new SdkConfiguration {
ApiHost = _host,
MaxRetry = 1,
};
Expand Down Expand Up @@ -2257,7 +2258,7 @@ public async Task RetryAfterOutOfBoundsTooSmallTest() {
});

var httpClient = new HttpClient(mockHandler.Object);
var config = new Configuration.Configuration() {
var config = new SdkConfiguration {
ApiHost = _host,
MaxRetry = 1,
};
Expand Down Expand Up @@ -2320,7 +2321,7 @@ public async Task RetryAfterInvalidFormatTest() {
});

var httpClient = new HttpClient(mockHandler.Object);
var config = new Configuration.Configuration() {
var config = new SdkConfiguration {
ApiHost = _host,
MaxRetry = 1,
};
Expand Down Expand Up @@ -2383,7 +2384,7 @@ public async Task RetryAttemptTrackingTest() {
});

var httpClient = new HttpClient(mockHandler.Object);
var config = new Configuration.Configuration() {
var config = new SdkConfiguration {
ApiHost = _host,
MaxRetry = 2,
};
Expand Down Expand Up @@ -2437,7 +2438,7 @@ public async Task RetryAfterWith500ErrorTest() {
});

var httpClient = new HttpClient(mockHandler.Object);
var config = new Configuration.Configuration() {
var config = new SdkConfiguration {
ApiHost = _host,
MaxRetry = 1,
};
Expand Down Expand Up @@ -2611,7 +2612,7 @@ public async Task XRateLimitResetLegacyHeaderTest() {
});

var httpClient = new HttpClient(mockHandler.Object);
var config = new Configuration.Configuration() {
var config = new SdkConfiguration {
ApiHost = _host,
MaxRetry = 1,
};
Expand Down
13 changes: 7 additions & 6 deletions src/OpenFga.Sdk.Test/ApiClient/ApiClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
using System.Threading;
using System.Threading.Tasks;
using Xunit;
using SdkConfiguration = OpenFga.Sdk.Configuration.Configuration;

namespace OpenFga.Sdk.Test.ApiClient {
/// <summary>
Expand All @@ -42,12 +43,12 @@ public class ApiClientTests {
/// <summary>
/// Creates a Configuration with ApiToken credentials
/// </summary>
private static Configuration.Configuration CreateApiTokenConfiguration(string apiToken = TestApiToken) {
private static SdkConfiguration CreateApiTokenConfiguration(string apiToken = TestApiToken) {
var credentialsConfig = new CredentialsConfig {
ApiToken = apiToken
};

return new Configuration.Configuration {
return new SdkConfiguration {
ApiHost = TestHost,
Credentials = new Credentials {
Method = CredentialsMethod.ApiToken,
Expand All @@ -59,15 +60,15 @@ private static Configuration.Configuration CreateApiTokenConfiguration(string ap
/// <summary>
/// Creates a Configuration with OAuth ClientCredentials
/// </summary>
private static Configuration.Configuration CreateOAuthConfiguration() {
private static SdkConfiguration CreateOAuthConfiguration() {
var credentialsConfig = new CredentialsConfig {
ClientId = TestClientId,
ClientSecret = TestClientSecret,
ApiTokenIssuer = TestTokenIssuer,
ApiAudience = TestAudience
};

return new Configuration.Configuration {
return new SdkConfiguration {
ApiHost = TestHost,
Credentials = new Credentials {
Method = CredentialsMethod.ClientCredentials,
Expand All @@ -79,8 +80,8 @@ private static Configuration.Configuration CreateOAuthConfiguration() {
/// <summary>
/// Creates a Configuration with no credentials
/// </summary>
private static Configuration.Configuration CreateNoCredentialsConfiguration() {
return new Configuration.Configuration {
private static SdkConfiguration CreateNoCredentialsConfiguration() {
return new SdkConfiguration {
ApiHost = TestHost
};
}
Expand Down
Loading
Loading