diff --git a/modules/openapi-generator/src/main/resources/csharp/libraries/generichost/ApiKeyToken.mustache b/modules/openapi-generator/src/main/resources/csharp/libraries/generichost/ApiKeyToken.mustache index 3bee0750d5..194bb8d293 100644 --- a/modules/openapi-generator/src/main/resources/csharp/libraries/generichost/ApiKeyToken.mustache +++ b/modules/openapi-generator/src/main/resources/csharp/libraries/generichost/ApiKeyToken.mustache @@ -15,14 +15,21 @@ namespace {{packageName}}.{{clientPackage}} { private string _raw; + /// + /// The header that this token will be used with. + /// + public string Header { get; } + /// /// Constructs an ApiKeyToken object. /// /// + /// /// /// - public ApiKeyToken(string value, string prefix = "Bearer ", TimeSpan? timeout = null) : base(timeout) + public ApiKeyToken(string value, string header, string prefix = "Bearer ", TimeSpan? timeout = null) : base(timeout) { + Header = header; _raw = $"{ prefix }{ value }"; } @@ -30,22 +37,20 @@ namespace {{packageName}}.{{clientPackage}} /// Places the token in the header. /// /// - /// - public virtual void UseInHeader(System.Net.Http.HttpRequestMessage request, string headerName) + public virtual void UseInHeader(System.Net.Http.HttpRequestMessage request) { - request.Headers.Add(headerName, _raw); + request.Headers.Add(Header, _raw); } - + /// /// Places the token in the query. /// /// /// /// - /// - public virtual void UseInQuery(System.Net.Http.HttpRequestMessage request, UriBuilder uriBuilder, System.Collections.Specialized.NameValueCollection parseQueryString, string parameterName) + public virtual void UseInQuery(System.Net.Http.HttpRequestMessage request, UriBuilder uriBuilder, System.Collections.Specialized.NameValueCollection parseQueryString) { - parseQueryString[parameterName] = Uri.EscapeDataString(_raw).ToString(){{nrt!}}; + parseQueryString[Header] = Uri.EscapeDataString(_raw).ToString(){{nrt!}}; } } } \ No newline at end of file diff --git a/modules/openapi-generator/src/main/resources/csharp/libraries/generichost/RateLimitProvider`1.mustache b/modules/openapi-generator/src/main/resources/csharp/libraries/generichost/RateLimitProvider`1.mustache index 5de00d6acf..e9438003c4 100644 --- a/modules/openapi-generator/src/main/resources/csharp/libraries/generichost/RateLimitProvider`1.mustache +++ b/modules/openapi-generator/src/main/resources/csharp/libraries/generichost/RateLimitProvider`1.mustache @@ -4,14 +4,17 @@ #nullable enable {{/nrt}} -using System;{{^netStandard}} -using System.Threading.Channels;{{/netStandard}}{{#netStandard}} -using System.Collections.Concurrent; +using System; +using System.Collections.Generic; using System.Linq; -using System.Threading; -using System.Threading.Tasks;{{/netStandard}} +{{^netStandard}} +using System.Threading.Channels; +{{/netStandard}} +{{#netStandard}} +using System.Threading.Channels; +{{/netStandard}} -namespace {{packageName}}.{{clientPackage}} {{^netStandard}} +namespace {{packageName}}.{{clientPackage}} { /// /// Provides a token to the api clients. Tokens will be rate limited based on the provided TimeSpan. @@ -19,7 +22,7 @@ namespace {{packageName}}.{{clientPackage}} {{^netStandard}} /// {{>visibility}} class RateLimitProvider : TokenProvider where TTokenBase : TokenBase { - internal Channel AvailableTokens { get; } + internal Dictionary> AvailableTokens { get; } = new{{^net70OrLater}} Dictionary>{{/net70OrLater}}(); /// /// Instantiates a ThrottledTokenProvider. Your tokens will be rate limited based on the token's timeout. @@ -30,78 +33,49 @@ namespace {{packageName}}.{{clientPackage}} {{^netStandard}} foreach(TTokenBase token in _tokens) token.StartTimer(token.Timeout ?? TimeSpan.FromMilliseconds(40)); - BoundedChannelOptions options = new BoundedChannelOptions(_tokens.Length) - { - FullMode = BoundedChannelFullMode.DropWrite + {{#lambda.copy}} + BoundedChannelOptions options = new BoundedChannelOptions(_tokens.Length) + { + FullMode = BoundedChannelFullMode.DropWrite }; - AvailableTokens = Channel.CreateBounded(options); - - for (int i = 0; i < _tokens.Length; i++) - _tokens[i].TokenBecameAvailable += ((sender) => AvailableTokens.Writer.TryWrite((TTokenBase) sender)); - } - internal override async System.Threading.Tasks.ValueTask GetAsync(System.Threading.CancellationToken cancellation = default{{^netstandard20OrLater}}(System.Threading.CancellationToken){{/netstandard20OrLater}}) - => await AvailableTokens.Reader.ReadAsync(cancellation).ConfigureAwait(false); - } -} {{/netStandard}}{{#netStandard}} -{ - /// - /// Provides a token to the api clients. Tokens will be rate limited based on the provided TimeSpan. - /// - /// - public class RateLimitProvider : TokenProvider where TTokenBase : TokenBase - { - internal ConcurrentDictionary AvailableTokens = new ConcurrentDictionary(); - private SemaphoreSlim _semaphore; - - /// - /// Instantiates a ThrottledTokenProvider. Your tokens will be rate limited based on the token's timeout. - /// - /// - public RateLimitProvider(TokenContainer container) : base(container.Tokens) - { - _semaphore = new SemaphoreSlim(1, 1); - - foreach(TTokenBase token in _tokens) - token.StartTimer(token.Timeout ?? TimeSpan.FromMilliseconds(40)); - - for (int i = 0; i < _tokens.Length; i++) + AvailableTokens.Add(string.Empty, Channel.CreateBounded(options)); + {{/lambda.copy}} + {{#hasApiKeyMethods}} + if (container is TokenContainer apiKeyTokenContainer) { - _tokens[i].TokenBecameAvailable += ((sender) => + string[] headers = apiKeyTokenContainer.Tokens.Select(t => t.Header).Distinct().ToArray(); + + foreach (string header in headers) { - TTokenBase token = (TTokenBase)sender; + BoundedChannelOptions options = new BoundedChannelOptions(apiKeyTokenContainer.Tokens.Count(t => t.Header.Equals(header))) + { + FullMode = BoundedChannelFullMode.DropWrite + }; - AvailableTokens.TryAdd(token, token); - }); - } - } - - internal override async System.Threading.Tasks.ValueTask GetAsync(System.Threading.CancellationToken cancellation = default{{^netstandard20OrLater}}(System.Threading.CancellationToken){{/netstandard20OrLater}}) - { - await _semaphore.WaitAsync().ConfigureAwait(false); - - try - { - TTokenBase result = null; - - while (result == null) - { - TTokenBase tokenToRemove = AvailableTokens.FirstOrDefault().Value; - - if (tokenToRemove != null && AvailableTokens.TryRemove(tokenToRemove, out result)) - return result; - - await Task.Delay(40).ConfigureAwait(false); - - tokenToRemove = AvailableTokens.FirstOrDefault().Value; + AvailableTokens.Add(header, Channel.CreateBounded(options)); } - - return result; } - finally + else { - _semaphore.Release(); + {{#lambda.indent1}}{{#lambda.pasteLine}}{{/lambda.pasteLine}}{{/lambda.indent1}} } + {{/hasApiKeyMethods}} + {{^hasApiKeyMethods}} + {{#lambda.pasteLine}}{{/lambda.pasteLine}} + {{/hasApiKeyMethods}} + + foreach(Channel tokens in AvailableTokens.Values) + for (int i = 0; i < _tokens.Length; i++) + _tokens[i].TokenBecameAvailable += ((sender) => tokens.Writer.TryWrite((TTokenBase) sender)); + } + + internal override async System.Threading.Tasks.ValueTask GetAsync(string header = "", System.Threading.CancellationToken cancellation = default{{^netstandard20OrLater}}(System.Threading.CancellationToken){{/netstandard20OrLater}}) + { + if (!AvailableTokens.TryGetValue(header, out Channel{{nrt?}} tokens)) + throw new KeyNotFoundException($"Could not locate a token for header '{header}'."); + + return await tokens.Reader.ReadAsync(cancellation).ConfigureAwait(false); } } -}{{/netStandard}} +} diff --git a/modules/openapi-generator/src/main/resources/csharp/libraries/generichost/TokenProvider`1.mustache b/modules/openapi-generator/src/main/resources/csharp/libraries/generichost/TokenProvider`1.mustache index 5ab2143e9a..5297254675 100644 --- a/modules/openapi-generator/src/main/resources/csharp/libraries/generichost/TokenProvider`1.mustache +++ b/modules/openapi-generator/src/main/resources/csharp/libraries/generichost/TokenProvider`1.mustache @@ -21,7 +21,7 @@ namespace {{packageName}} /// protected TTokenBase[] _tokens; - internal abstract System.Threading.Tasks.ValueTask GetAsync(System.Threading.CancellationToken cancellation = default{{^netstandard20OrLater}}(System.Threading.CancellationToken){{/netstandard20OrLater}}); + internal abstract System.Threading.Tasks.ValueTask GetAsync(string header = "", System.Threading.CancellationToken cancellation = default{{^netstandard20OrLater}}(System.Threading.CancellationToken){{/netstandard20OrLater}}); /// /// Instantiates a TokenProvider. diff --git a/modules/openapi-generator/src/main/resources/csharp/libraries/generichost/api.mustache b/modules/openapi-generator/src/main/resources/csharp/libraries/generichost/api.mustache index 6be1cb371d..54bc94f3c6 100644 --- a/modules/openapi-generator/src/main/resources/csharp/libraries/generichost/api.mustache +++ b/modules/openapi-generator/src/main/resources/csharp/libraries/generichost/api.mustache @@ -497,28 +497,26 @@ namespace {{packageName}}.{{apiPackage}} {{/-first}} {{#isApiKey}} {{^isKeyInCookie}} - ApiKeyToken apiKeyTokenLocalVar{{-index}} = (ApiKeyToken) await ApiKeyProvider.GetAsync(cancellationToken).ConfigureAwait(false); + ApiKeyToken apiKeyTokenLocalVar{{-index}} = (ApiKeyToken) await ApiKeyProvider.GetAsync("{{keyParamName}}", cancellationToken).ConfigureAwait(false); tokenBaseLocalVars.Add(apiKeyTokenLocalVar{{-index}}); {{#isKeyInHeader}} - apiKeyTokenLocalVar{{-index}}.UseInHeader(httpRequestMessageLocalVar, "{{keyParamName}}"); + apiKeyTokenLocalVar{{-index}}.UseInHeader(httpRequestMessageLocalVar); {{/isKeyInHeader}} {{/isKeyInCookie}} {{#isKeyInQuery}} - apiKeyTokenLocalVar{{-index}}.UseInQuery(httpRequestMessageLocalVar, uriBuilderLocalVar, parseQueryStringLocalVar, "{{keyParamName}}"); + apiKeyTokenLocalVar{{-index}}.UseInQuery(httpRequestMessageLocalVar, uriBuilderLocalVar, parseQueryStringLocalVar); uriBuilderLocalVar.Query = parseQueryStringLocalVar.ToString(); {{/isKeyInQuery}} {{/isApiKey}} {{/authMethods}} - - {{! below line must be after any UseInQuery calls, but before using the HttpSignatureToken}} httpRequestMessageLocalVar.RequestUri = uriBuilderLocalVar.Uri; {{#authMethods}} {{#isBasicBasic}} - BasicToken basicTokenLocalVar{{-index}} = (BasicToken) await BasicTokenProvider.GetAsync(cancellationToken).ConfigureAwait(false); + BasicToken basicTokenLocalVar{{-index}} = (BasicToken) await BasicTokenProvider.GetAsync(cancellation: cancellationToken).ConfigureAwait(false); tokenBaseLocalVars.Add(basicTokenLocalVar{{-index}}); @@ -526,7 +524,7 @@ namespace {{packageName}}.{{apiPackage}} {{/isBasicBasic}} {{#isBasicBearer}} - BearerToken bearerTokenLocalVar{{-index}} = (BearerToken) await BearerTokenProvider.GetAsync(cancellationToken).ConfigureAwait(false); + BearerToken bearerTokenLocalVar{{-index}} = (BearerToken) await BearerTokenProvider.GetAsync(cancellation: cancellationToken).ConfigureAwait(false); tokenBaseLocalVars.Add(bearerTokenLocalVar{{-index}}); @@ -534,7 +532,7 @@ namespace {{packageName}}.{{apiPackage}} {{/isBasicBearer}} {{#isOAuth}} - OAuthToken oauthTokenLocalVar{{-index}} = (OAuthToken) await OauthTokenProvider.GetAsync(cancellationToken).ConfigureAwait(false); + OAuthToken oauthTokenLocalVar{{-index}} = (OAuthToken) await OauthTokenProvider.GetAsync(cancellation: cancellationToken).ConfigureAwait(false); tokenBaseLocalVars.Add(oauthTokenLocalVar{{-index}}); @@ -542,7 +540,7 @@ namespace {{packageName}}.{{apiPackage}} {{/isOAuth}} {{#isHttpSignature}} - HttpSignatureToken httpSignatureTokenLocalVar{{-index}} = (HttpSignatureToken) await HttpSignatureTokenProvider.GetAsync(cancellationToken).ConfigureAwait(false); + HttpSignatureToken httpSignatureTokenLocalVar{{-index}} = (HttpSignatureToken) await HttpSignatureTokenProvider.GetAsync(cancellation: cancellationToken).ConfigureAwait(false); tokenBaseLocalVars.Add(httpSignatureTokenLocalVar{{-index}}); diff --git a/modules/openapi-generator/src/main/resources/csharp/netcore_project.mustache b/modules/openapi-generator/src/main/resources/csharp/netcore_project.mustache index 8c62658d79..aa6c35c6d4 100644 --- a/modules/openapi-generator/src/main/resources/csharp/netcore_project.mustache +++ b/modules/openapi-generator/src/main/resources/csharp/netcore_project.mustache @@ -43,6 +43,9 @@ {{#net80OrLater}} {{/net80OrLater}} + {{#netStandard}} + + {{/netStandard}} {{/useGenericHost}} {{^useGenericHost}} {{#supportsRetry}} diff --git a/samples/client/petstore/csharp/OpenAPIClient-generichost-manual-tests/ManualTests.Latest.UseSourceGeneration/UnitTest1.cs b/samples/client/petstore/csharp/OpenAPIClient-generichost-manual-tests/ManualTests.Latest.UseSourceGeneration/UnitTest1.cs index 24989ea90d..0fcfdd2624 100644 --- a/samples/client/petstore/csharp/OpenAPIClient-generichost-manual-tests/ManualTests.Latest.UseSourceGeneration/UnitTest1.cs +++ b/samples/client/petstore/csharp/OpenAPIClient-generichost-manual-tests/ManualTests.Latest.UseSourceGeneration/UnitTest1.cs @@ -22,7 +22,7 @@ public class UnitTest1 IHostBuilder hostBuild = Host.CreateDefaultBuilder(Array.Empty()).ConfigureApi((context, services, options) => { string apiKeyTokenValue = context.Configuration[""] ?? "Token not found."; - ApiKeyToken apiKeyToken = new(apiKeyTokenValue, timeout: TimeSpan.FromSeconds(1)); + ApiKeyToken apiKeyToken = new(apiKeyTokenValue, "session", timeout: TimeSpan.FromSeconds(1)); options.AddTokens(apiKeyToken); string bearerTokenValue = context.Configuration[""] ?? "Token not found."; diff --git a/samples/client/petstore/csharp/OpenAPIClient-generichost-manual-tests/OpenAPIClient-generichost-manual-tests/UnitTest1.cs b/samples/client/petstore/csharp/OpenAPIClient-generichost-manual-tests/OpenAPIClient-generichost-manual-tests/UnitTest1.cs index 60f9abd940..ad2f79f8d9 100644 --- a/samples/client/petstore/csharp/OpenAPIClient-generichost-manual-tests/OpenAPIClient-generichost-manual-tests/UnitTest1.cs +++ b/samples/client/petstore/csharp/OpenAPIClient-generichost-manual-tests/OpenAPIClient-generichost-manual-tests/UnitTest1.cs @@ -18,7 +18,7 @@ namespace OpenAPIClient_generichost_manual_tests IHostBuilder hostBuild = Host.CreateDefaultBuilder(Array.Empty()).ConfigureApi((context, services, options) => { string apiKeyTokenValue = context.Configuration[""] ?? "Token not found."; - ApiKeyToken apiKeyToken = new(apiKeyTokenValue, timeout: TimeSpan.FromSeconds(1)); + ApiKeyToken apiKeyToken = new(apiKeyTokenValue, "session", timeout: TimeSpan.FromSeconds(1)); options.AddTokens(apiKeyToken); string bearerTokenValue = context.Configuration[""] ?? "Token not found."; diff --git a/samples/client/petstore/csharp/OpenAPIClient-generichost-net6.0-nrt-useSourceGeneration/src/UseSourceGeneration/Api/FakeApi.cs b/samples/client/petstore/csharp/OpenAPIClient-generichost-net6.0-nrt-useSourceGeneration/src/UseSourceGeneration/Api/FakeApi.cs index 9f4ceb68bd..02be116408 100644 --- a/samples/client/petstore/csharp/OpenAPIClient-generichost-net6.0-nrt-useSourceGeneration/src/UseSourceGeneration/Api/FakeApi.cs +++ b/samples/client/petstore/csharp/OpenAPIClient-generichost-net6.0-nrt-useSourceGeneration/src/UseSourceGeneration/Api/FakeApi.cs @@ -3245,10 +3245,9 @@ namespace UseSourceGeneration.Api formParameterLocalVars.Add(new KeyValuePair("dateTime", ClientUtils.ParameterToString(dateTime.Value))); List tokenBaseLocalVars = new List(); - httpRequestMessageLocalVar.RequestUri = uriBuilderLocalVar.Uri; - BasicToken basicTokenLocalVar1 = (BasicToken) await BasicTokenProvider.GetAsync(cancellationToken).ConfigureAwait(false); + BasicToken basicTokenLocalVar1 = (BasicToken) await BasicTokenProvider.GetAsync(cancellation: cancellationToken).ConfigureAwait(false); tokenBaseLocalVars.Add(basicTokenLocalVar1); @@ -3770,10 +3769,9 @@ namespace UseSourceGeneration.Api httpRequestMessageLocalVar.Headers.Add("boolean_group", ClientUtils.ParameterToString(booleanGroup.Value)); List tokenBaseLocalVars = new List(); - httpRequestMessageLocalVar.RequestUri = uriBuilderLocalVar.Uri; - BearerToken bearerTokenLocalVar1 = (BearerToken) await BearerTokenProvider.GetAsync(cancellationToken).ConfigureAwait(false); + BearerToken bearerTokenLocalVar1 = (BearerToken) await BearerTokenProvider.GetAsync(cancellation: cancellationToken).ConfigureAwait(false); tokenBaseLocalVars.Add(bearerTokenLocalVar1); diff --git a/samples/client/petstore/csharp/OpenAPIClient-generichost-net6.0-nrt-useSourceGeneration/src/UseSourceGeneration/Api/FakeClassnameTags123Api.cs b/samples/client/petstore/csharp/OpenAPIClient-generichost-net6.0-nrt-useSourceGeneration/src/UseSourceGeneration/Api/FakeClassnameTags123Api.cs index c2ee396027..830c72645d 100644 --- a/samples/client/petstore/csharp/OpenAPIClient-generichost-net6.0-nrt-useSourceGeneration/src/UseSourceGeneration/Api/FakeClassnameTags123Api.cs +++ b/samples/client/petstore/csharp/OpenAPIClient-generichost-net6.0-nrt-useSourceGeneration/src/UseSourceGeneration/Api/FakeClassnameTags123Api.cs @@ -280,13 +280,12 @@ namespace UseSourceGeneration.Api : httpRequestMessageLocalVar.Content = new StringContent(JsonSerializer.Serialize(modelClient, _jsonSerializerOptions)); List tokenBaseLocalVars = new List(); - ApiKeyToken apiKeyTokenLocalVar1 = (ApiKeyToken) await ApiKeyProvider.GetAsync(cancellationToken).ConfigureAwait(false); + ApiKeyToken apiKeyTokenLocalVar1 = (ApiKeyToken) await ApiKeyProvider.GetAsync("api_key_query", cancellationToken).ConfigureAwait(false); tokenBaseLocalVars.Add(apiKeyTokenLocalVar1); - apiKeyTokenLocalVar1.UseInQuery(httpRequestMessageLocalVar, uriBuilderLocalVar, parseQueryStringLocalVar, "api_key_query"); + apiKeyTokenLocalVar1.UseInQuery(httpRequestMessageLocalVar, uriBuilderLocalVar, parseQueryStringLocalVar); uriBuilderLocalVar.Query = parseQueryStringLocalVar.ToString(); - httpRequestMessageLocalVar.RequestUri = uriBuilderLocalVar.Uri; string[] contentTypes = new string[] { diff --git a/samples/client/petstore/csharp/OpenAPIClient-generichost-net6.0-nrt-useSourceGeneration/src/UseSourceGeneration/Api/PetApi.cs b/samples/client/petstore/csharp/OpenAPIClient-generichost-net6.0-nrt-useSourceGeneration/src/UseSourceGeneration/Api/PetApi.cs index 7998650179..bb5bbfb8b0 100644 --- a/samples/client/petstore/csharp/OpenAPIClient-generichost-net6.0-nrt-useSourceGeneration/src/UseSourceGeneration/Api/PetApi.cs +++ b/samples/client/petstore/csharp/OpenAPIClient-generichost-net6.0-nrt-useSourceGeneration/src/UseSourceGeneration/Api/PetApi.cs @@ -768,16 +768,15 @@ namespace UseSourceGeneration.Api : httpRequestMessageLocalVar.Content = new StringContent(JsonSerializer.Serialize(pet, _jsonSerializerOptions)); List tokenBaseLocalVars = new List(); - httpRequestMessageLocalVar.RequestUri = uriBuilderLocalVar.Uri; - OAuthToken oauthTokenLocalVar1 = (OAuthToken) await OauthTokenProvider.GetAsync(cancellationToken).ConfigureAwait(false); + OAuthToken oauthTokenLocalVar1 = (OAuthToken) await OauthTokenProvider.GetAsync(cancellation: cancellationToken).ConfigureAwait(false); tokenBaseLocalVars.Add(oauthTokenLocalVar1); oauthTokenLocalVar1.UseInHeader(httpRequestMessageLocalVar, ""); - HttpSignatureToken httpSignatureTokenLocalVar2 = (HttpSignatureToken) await HttpSignatureTokenProvider.GetAsync(cancellationToken).ConfigureAwait(false); + HttpSignatureToken httpSignatureTokenLocalVar2 = (HttpSignatureToken) await HttpSignatureTokenProvider.GetAsync(cancellation: cancellationToken).ConfigureAwait(false); tokenBaseLocalVars.Add(httpSignatureTokenLocalVar2); @@ -986,10 +985,9 @@ namespace UseSourceGeneration.Api httpRequestMessageLocalVar.Headers.Add("api_key", ClientUtils.ParameterToString(apiKey.Value)); List tokenBaseLocalVars = new List(); - httpRequestMessageLocalVar.RequestUri = uriBuilderLocalVar.Uri; - OAuthToken oauthTokenLocalVar1 = (OAuthToken) await OauthTokenProvider.GetAsync(cancellationToken).ConfigureAwait(false); + OAuthToken oauthTokenLocalVar1 = (OAuthToken) await OauthTokenProvider.GetAsync(cancellation: cancellationToken).ConfigureAwait(false); tokenBaseLocalVars.Add(oauthTokenLocalVar1); @@ -1180,16 +1178,15 @@ namespace UseSourceGeneration.Api uriBuilderLocalVar.Query = parseQueryStringLocalVar.ToString(); List tokenBaseLocalVars = new List(); - httpRequestMessageLocalVar.RequestUri = uriBuilderLocalVar.Uri; - OAuthToken oauthTokenLocalVar1 = (OAuthToken) await OauthTokenProvider.GetAsync(cancellationToken).ConfigureAwait(false); + OAuthToken oauthTokenLocalVar1 = (OAuthToken) await OauthTokenProvider.GetAsync(cancellation: cancellationToken).ConfigureAwait(false); tokenBaseLocalVars.Add(oauthTokenLocalVar1); oauthTokenLocalVar1.UseInHeader(httpRequestMessageLocalVar, ""); - HttpSignatureToken httpSignatureTokenLocalVar2 = (HttpSignatureToken) await HttpSignatureTokenProvider.GetAsync(cancellationToken).ConfigureAwait(false); + HttpSignatureToken httpSignatureTokenLocalVar2 = (HttpSignatureToken) await HttpSignatureTokenProvider.GetAsync(cancellation: cancellationToken).ConfigureAwait(false); tokenBaseLocalVars.Add(httpSignatureTokenLocalVar2); @@ -1432,16 +1429,15 @@ namespace UseSourceGeneration.Api uriBuilderLocalVar.Query = parseQueryStringLocalVar.ToString(); List tokenBaseLocalVars = new List(); - httpRequestMessageLocalVar.RequestUri = uriBuilderLocalVar.Uri; - OAuthToken oauthTokenLocalVar1 = (OAuthToken) await OauthTokenProvider.GetAsync(cancellationToken).ConfigureAwait(false); + OAuthToken oauthTokenLocalVar1 = (OAuthToken) await OauthTokenProvider.GetAsync(cancellation: cancellationToken).ConfigureAwait(false); tokenBaseLocalVars.Add(oauthTokenLocalVar1); oauthTokenLocalVar1.UseInHeader(httpRequestMessageLocalVar, ""); - HttpSignatureToken httpSignatureTokenLocalVar2 = (HttpSignatureToken) await HttpSignatureTokenProvider.GetAsync(cancellationToken).ConfigureAwait(false); + HttpSignatureToken httpSignatureTokenLocalVar2 = (HttpSignatureToken) await HttpSignatureTokenProvider.GetAsync(cancellation: cancellationToken).ConfigureAwait(false); tokenBaseLocalVars.Add(httpSignatureTokenLocalVar2); @@ -1668,17 +1664,16 @@ namespace UseSourceGeneration.Api System.Collections.Specialized.NameValueCollection parseQueryStringLocalVar = System.Web.HttpUtility.ParseQueryString(string.Empty); List tokenBaseLocalVars = new List(); - ApiKeyToken apiKeyTokenLocalVar1 = (ApiKeyToken) await ApiKeyProvider.GetAsync(cancellationToken).ConfigureAwait(false); + ApiKeyToken apiKeyTokenLocalVar1 = (ApiKeyToken) await ApiKeyProvider.GetAsync("api_key", cancellationToken).ConfigureAwait(false); tokenBaseLocalVars.Add(apiKeyTokenLocalVar1); - apiKeyTokenLocalVar1.UseInHeader(httpRequestMessageLocalVar, "api_key"); + apiKeyTokenLocalVar1.UseInHeader(httpRequestMessageLocalVar); - ApiKeyToken apiKeyTokenLocalVar2 = (ApiKeyToken) await ApiKeyProvider.GetAsync(cancellationToken).ConfigureAwait(false); + ApiKeyToken apiKeyTokenLocalVar2 = (ApiKeyToken) await ApiKeyProvider.GetAsync("api_key_query", cancellationToken).ConfigureAwait(false); tokenBaseLocalVars.Add(apiKeyTokenLocalVar2); - apiKeyTokenLocalVar2.UseInQuery(httpRequestMessageLocalVar, uriBuilderLocalVar, parseQueryStringLocalVar, "api_key_query"); + apiKeyTokenLocalVar2.UseInQuery(httpRequestMessageLocalVar, uriBuilderLocalVar, parseQueryStringLocalVar); uriBuilderLocalVar.Query = parseQueryStringLocalVar.ToString(); - httpRequestMessageLocalVar.RequestUri = uriBuilderLocalVar.Uri; string[] acceptLocalVars = new string[] { @@ -1918,16 +1913,15 @@ namespace UseSourceGeneration.Api : httpRequestMessageLocalVar.Content = new StringContent(JsonSerializer.Serialize(pet, _jsonSerializerOptions)); List tokenBaseLocalVars = new List(); - httpRequestMessageLocalVar.RequestUri = uriBuilderLocalVar.Uri; - OAuthToken oauthTokenLocalVar1 = (OAuthToken) await OauthTokenProvider.GetAsync(cancellationToken).ConfigureAwait(false); + OAuthToken oauthTokenLocalVar1 = (OAuthToken) await OauthTokenProvider.GetAsync(cancellation: cancellationToken).ConfigureAwait(false); tokenBaseLocalVars.Add(oauthTokenLocalVar1); oauthTokenLocalVar1.UseInHeader(httpRequestMessageLocalVar, ""); - HttpSignatureToken httpSignatureTokenLocalVar2 = (HttpSignatureToken) await HttpSignatureTokenProvider.GetAsync(cancellationToken).ConfigureAwait(false); + HttpSignatureToken httpSignatureTokenLocalVar2 = (HttpSignatureToken) await HttpSignatureTokenProvider.GetAsync(cancellation: cancellationToken).ConfigureAwait(false); tokenBaseLocalVars.Add(httpSignatureTokenLocalVar2); @@ -2167,10 +2161,9 @@ namespace UseSourceGeneration.Api formParameterLocalVars.Add(new KeyValuePair("status", ClientUtils.ParameterToString(status.Value))); List tokenBaseLocalVars = new List(); - httpRequestMessageLocalVar.RequestUri = uriBuilderLocalVar.Uri; - OAuthToken oauthTokenLocalVar1 = (OAuthToken) await OauthTokenProvider.GetAsync(cancellationToken).ConfigureAwait(false); + OAuthToken oauthTokenLocalVar1 = (OAuthToken) await OauthTokenProvider.GetAsync(cancellation: cancellationToken).ConfigureAwait(false); tokenBaseLocalVars.Add(oauthTokenLocalVar1); @@ -2393,10 +2386,9 @@ namespace UseSourceGeneration.Api formParameterLocalVars.Add(new KeyValuePair("additionalMetadata", ClientUtils.ParameterToString(additionalMetadata.Value))); List tokenBaseLocalVars = new List(); - httpRequestMessageLocalVar.RequestUri = uriBuilderLocalVar.Uri; - OAuthToken oauthTokenLocalVar1 = (OAuthToken) await OauthTokenProvider.GetAsync(cancellationToken).ConfigureAwait(false); + OAuthToken oauthTokenLocalVar1 = (OAuthToken) await OauthTokenProvider.GetAsync(cancellation: cancellationToken).ConfigureAwait(false); tokenBaseLocalVars.Add(oauthTokenLocalVar1); @@ -2659,10 +2651,9 @@ namespace UseSourceGeneration.Api formParameterLocalVars.Add(new KeyValuePair("additionalMetadata", ClientUtils.ParameterToString(additionalMetadata.Value))); List tokenBaseLocalVars = new List(); - httpRequestMessageLocalVar.RequestUri = uriBuilderLocalVar.Uri; - OAuthToken oauthTokenLocalVar1 = (OAuthToken) await OauthTokenProvider.GetAsync(cancellationToken).ConfigureAwait(false); + OAuthToken oauthTokenLocalVar1 = (OAuthToken) await OauthTokenProvider.GetAsync(cancellation: cancellationToken).ConfigureAwait(false); tokenBaseLocalVars.Add(oauthTokenLocalVar1); diff --git a/samples/client/petstore/csharp/OpenAPIClient-generichost-net6.0-nrt-useSourceGeneration/src/UseSourceGeneration/Api/StoreApi.cs b/samples/client/petstore/csharp/OpenAPIClient-generichost-net6.0-nrt-useSourceGeneration/src/UseSourceGeneration/Api/StoreApi.cs index cfaa50757e..905e8201c5 100644 --- a/samples/client/petstore/csharp/OpenAPIClient-generichost-net6.0-nrt-useSourceGeneration/src/UseSourceGeneration/Api/StoreApi.cs +++ b/samples/client/petstore/csharp/OpenAPIClient-generichost-net6.0-nrt-useSourceGeneration/src/UseSourceGeneration/Api/StoreApi.cs @@ -621,9 +621,9 @@ namespace UseSourceGeneration.Api uriBuilderLocalVar.Path = ClientUtils.CONTEXT_PATH + "/store/inventory"; List tokenBaseLocalVars = new List(); - ApiKeyToken apiKeyTokenLocalVar1 = (ApiKeyToken) await ApiKeyProvider.GetAsync(cancellationToken).ConfigureAwait(false); + ApiKeyToken apiKeyTokenLocalVar1 = (ApiKeyToken) await ApiKeyProvider.GetAsync("api_key", cancellationToken).ConfigureAwait(false); tokenBaseLocalVars.Add(apiKeyTokenLocalVar1); - apiKeyTokenLocalVar1.UseInHeader(httpRequestMessageLocalVar, "api_key"); + apiKeyTokenLocalVar1.UseInHeader(httpRequestMessageLocalVar); httpRequestMessageLocalVar.RequestUri = uriBuilderLocalVar.Uri; diff --git a/samples/client/petstore/csharp/OpenAPIClient-generichost-net6.0-nrt-useSourceGeneration/src/UseSourceGeneration/Client/ApiKeyToken.cs b/samples/client/petstore/csharp/OpenAPIClient-generichost-net6.0-nrt-useSourceGeneration/src/UseSourceGeneration/Client/ApiKeyToken.cs index 532b5eaa2d..bffd96d4ad 100644 --- a/samples/client/petstore/csharp/OpenAPIClient-generichost-net6.0-nrt-useSourceGeneration/src/UseSourceGeneration/Client/ApiKeyToken.cs +++ b/samples/client/petstore/csharp/OpenAPIClient-generichost-net6.0-nrt-useSourceGeneration/src/UseSourceGeneration/Client/ApiKeyToken.cs @@ -13,14 +13,21 @@ namespace UseSourceGeneration.Client { private string _raw; + /// + /// The header that this token will be used with. + /// + public string Header { get; } + /// /// Constructs an ApiKeyToken object. /// /// + /// /// /// - public ApiKeyToken(string value, string prefix = "Bearer ", TimeSpan? timeout = null) : base(timeout) + public ApiKeyToken(string value, string header, string prefix = "Bearer ", TimeSpan? timeout = null) : base(timeout) { + Header = header; _raw = $"{ prefix }{ value }"; } @@ -28,22 +35,20 @@ namespace UseSourceGeneration.Client /// Places the token in the header. /// /// - /// - public virtual void UseInHeader(System.Net.Http.HttpRequestMessage request, string headerName) + public virtual void UseInHeader(System.Net.Http.HttpRequestMessage request) { - request.Headers.Add(headerName, _raw); + request.Headers.Add(Header, _raw); } - + /// /// Places the token in the query. /// /// /// /// - /// - public virtual void UseInQuery(System.Net.Http.HttpRequestMessage request, UriBuilder uriBuilder, System.Collections.Specialized.NameValueCollection parseQueryString, string parameterName) + public virtual void UseInQuery(System.Net.Http.HttpRequestMessage request, UriBuilder uriBuilder, System.Collections.Specialized.NameValueCollection parseQueryString) { - parseQueryString[parameterName] = Uri.EscapeDataString(_raw).ToString()!; + parseQueryString[Header] = Uri.EscapeDataString(_raw).ToString()!; } } } \ No newline at end of file diff --git a/samples/client/petstore/csharp/OpenAPIClient-generichost-net6.0-nrt-useSourceGeneration/src/UseSourceGeneration/Client/RateLimitProvider`1.cs b/samples/client/petstore/csharp/OpenAPIClient-generichost-net6.0-nrt-useSourceGeneration/src/UseSourceGeneration/Client/RateLimitProvider`1.cs index 0ad3cc6e6e..b0bc8c1bd6 100644 --- a/samples/client/petstore/csharp/OpenAPIClient-generichost-net6.0-nrt-useSourceGeneration/src/UseSourceGeneration/Client/RateLimitProvider`1.cs +++ b/samples/client/petstore/csharp/OpenAPIClient-generichost-net6.0-nrt-useSourceGeneration/src/UseSourceGeneration/Client/RateLimitProvider`1.cs @@ -11,9 +11,11 @@ #nullable enable using System; +using System.Collections.Generic; +using System.Linq; using System.Threading.Channels; -namespace UseSourceGeneration.Client +namespace UseSourceGeneration.Client { /// /// Provides a token to the api clients. Tokens will be rate limited based on the provided TimeSpan. @@ -21,7 +23,7 @@ namespace UseSourceGeneration.Client /// public class RateLimitProvider : TokenProvider where TTokenBase : TokenBase { - internal Channel AvailableTokens { get; } + internal Dictionary> AvailableTokens { get; } = new(); /// /// Instantiates a ThrottledTokenProvider. Your tokens will be rate limited based on the token's timeout. @@ -32,17 +34,41 @@ namespace UseSourceGeneration.Client foreach(TTokenBase token in _tokens) token.StartTimer(token.Timeout ?? TimeSpan.FromMilliseconds(40)); - BoundedChannelOptions options = new BoundedChannelOptions(_tokens.Length) - { - FullMode = BoundedChannelFullMode.DropWrite - }; + if (container is TokenContainer apiKeyTokenContainer) + { + string[] headers = apiKeyTokenContainer.Tokens.Select(t => t.Header).Distinct().ToArray(); - AvailableTokens = Channel.CreateBounded(options); + foreach (string header in headers) + { + BoundedChannelOptions options = new BoundedChannelOptions(apiKeyTokenContainer.Tokens.Count(t => t.Header.Equals(header))) + { + FullMode = BoundedChannelFullMode.DropWrite + }; - for (int i = 0; i < _tokens.Length; i++) - _tokens[i].TokenBecameAvailable += ((sender) => AvailableTokens.Writer.TryWrite((TTokenBase) sender)); + AvailableTokens.Add(header, Channel.CreateBounded(options)); + } + } + else + { + BoundedChannelOptions options = new BoundedChannelOptions(_tokens.Length) + { + FullMode = BoundedChannelFullMode.DropWrite + }; + + AvailableTokens.Add(string.Empty, Channel.CreateBounded(options)); + } + + foreach(Channel tokens in AvailableTokens.Values) + for (int i = 0; i < _tokens.Length; i++) + _tokens[i].TokenBecameAvailable += ((sender) => tokens.Writer.TryWrite((TTokenBase) sender)); + } + + internal override async System.Threading.Tasks.ValueTask GetAsync(string header = "", System.Threading.CancellationToken cancellation = default) + { + if (!AvailableTokens.TryGetValue(header, out Channel? tokens)) + throw new KeyNotFoundException($"Could not locate a token for header '{header}'."); + + return await tokens.Reader.ReadAsync(cancellation).ConfigureAwait(false); } - internal override async System.Threading.Tasks.ValueTask GetAsync(System.Threading.CancellationToken cancellation = default) - => await AvailableTokens.Reader.ReadAsync(cancellation).ConfigureAwait(false); } -} +} diff --git a/samples/client/petstore/csharp/OpenAPIClient-generichost-net6.0-nrt-useSourceGeneration/src/UseSourceGeneration/Client/TokenProvider`1.cs b/samples/client/petstore/csharp/OpenAPIClient-generichost-net6.0-nrt-useSourceGeneration/src/UseSourceGeneration/Client/TokenProvider`1.cs index 1044bc055f..ec6b4ff035 100644 --- a/samples/client/petstore/csharp/OpenAPIClient-generichost-net6.0-nrt-useSourceGeneration/src/UseSourceGeneration/Client/TokenProvider`1.cs +++ b/samples/client/petstore/csharp/OpenAPIClient-generichost-net6.0-nrt-useSourceGeneration/src/UseSourceGeneration/Client/TokenProvider`1.cs @@ -27,7 +27,7 @@ namespace UseSourceGeneration /// protected TTokenBase[] _tokens; - internal abstract System.Threading.Tasks.ValueTask GetAsync(System.Threading.CancellationToken cancellation = default); + internal abstract System.Threading.Tasks.ValueTask GetAsync(string header = "", System.Threading.CancellationToken cancellation = default); /// /// Instantiates a TokenProvider. diff --git a/samples/client/petstore/csharp/OpenAPIClient-generichost-net6.0-nrt/src/Org.OpenAPITools/Api/FakeApi.cs b/samples/client/petstore/csharp/OpenAPIClient-generichost-net6.0-nrt/src/Org.OpenAPITools/Api/FakeApi.cs index 9f9841049e..b7ff2c9530 100644 --- a/samples/client/petstore/csharp/OpenAPIClient-generichost-net6.0-nrt/src/Org.OpenAPITools/Api/FakeApi.cs +++ b/samples/client/petstore/csharp/OpenAPIClient-generichost-net6.0-nrt/src/Org.OpenAPITools/Api/FakeApi.cs @@ -3245,10 +3245,9 @@ namespace Org.OpenAPITools.Api formParameterLocalVars.Add(new KeyValuePair("dateTime", ClientUtils.ParameterToString(dateTime.Value))); List tokenBaseLocalVars = new List(); - httpRequestMessageLocalVar.RequestUri = uriBuilderLocalVar.Uri; - BasicToken basicTokenLocalVar1 = (BasicToken) await BasicTokenProvider.GetAsync(cancellationToken).ConfigureAwait(false); + BasicToken basicTokenLocalVar1 = (BasicToken) await BasicTokenProvider.GetAsync(cancellation: cancellationToken).ConfigureAwait(false); tokenBaseLocalVars.Add(basicTokenLocalVar1); @@ -3770,10 +3769,9 @@ namespace Org.OpenAPITools.Api httpRequestMessageLocalVar.Headers.Add("boolean_group", ClientUtils.ParameterToString(booleanGroup.Value)); List tokenBaseLocalVars = new List(); - httpRequestMessageLocalVar.RequestUri = uriBuilderLocalVar.Uri; - BearerToken bearerTokenLocalVar1 = (BearerToken) await BearerTokenProvider.GetAsync(cancellationToken).ConfigureAwait(false); + BearerToken bearerTokenLocalVar1 = (BearerToken) await BearerTokenProvider.GetAsync(cancellation: cancellationToken).ConfigureAwait(false); tokenBaseLocalVars.Add(bearerTokenLocalVar1); diff --git a/samples/client/petstore/csharp/OpenAPIClient-generichost-net6.0-nrt/src/Org.OpenAPITools/Api/FakeClassnameTags123Api.cs b/samples/client/petstore/csharp/OpenAPIClient-generichost-net6.0-nrt/src/Org.OpenAPITools/Api/FakeClassnameTags123Api.cs index 6105d99128..d7264a7338 100644 --- a/samples/client/petstore/csharp/OpenAPIClient-generichost-net6.0-nrt/src/Org.OpenAPITools/Api/FakeClassnameTags123Api.cs +++ b/samples/client/petstore/csharp/OpenAPIClient-generichost-net6.0-nrt/src/Org.OpenAPITools/Api/FakeClassnameTags123Api.cs @@ -280,13 +280,12 @@ namespace Org.OpenAPITools.Api : httpRequestMessageLocalVar.Content = new StringContent(JsonSerializer.Serialize(modelClient, _jsonSerializerOptions)); List tokenBaseLocalVars = new List(); - ApiKeyToken apiKeyTokenLocalVar1 = (ApiKeyToken) await ApiKeyProvider.GetAsync(cancellationToken).ConfigureAwait(false); + ApiKeyToken apiKeyTokenLocalVar1 = (ApiKeyToken) await ApiKeyProvider.GetAsync("api_key_query", cancellationToken).ConfigureAwait(false); tokenBaseLocalVars.Add(apiKeyTokenLocalVar1); - apiKeyTokenLocalVar1.UseInQuery(httpRequestMessageLocalVar, uriBuilderLocalVar, parseQueryStringLocalVar, "api_key_query"); + apiKeyTokenLocalVar1.UseInQuery(httpRequestMessageLocalVar, uriBuilderLocalVar, parseQueryStringLocalVar); uriBuilderLocalVar.Query = parseQueryStringLocalVar.ToString(); - httpRequestMessageLocalVar.RequestUri = uriBuilderLocalVar.Uri; string[] contentTypes = new string[] { diff --git a/samples/client/petstore/csharp/OpenAPIClient-generichost-net6.0-nrt/src/Org.OpenAPITools/Api/PetApi.cs b/samples/client/petstore/csharp/OpenAPIClient-generichost-net6.0-nrt/src/Org.OpenAPITools/Api/PetApi.cs index 746440b610..8547a7806d 100644 --- a/samples/client/petstore/csharp/OpenAPIClient-generichost-net6.0-nrt/src/Org.OpenAPITools/Api/PetApi.cs +++ b/samples/client/petstore/csharp/OpenAPIClient-generichost-net6.0-nrt/src/Org.OpenAPITools/Api/PetApi.cs @@ -768,16 +768,15 @@ namespace Org.OpenAPITools.Api : httpRequestMessageLocalVar.Content = new StringContent(JsonSerializer.Serialize(pet, _jsonSerializerOptions)); List tokenBaseLocalVars = new List(); - httpRequestMessageLocalVar.RequestUri = uriBuilderLocalVar.Uri; - OAuthToken oauthTokenLocalVar1 = (OAuthToken) await OauthTokenProvider.GetAsync(cancellationToken).ConfigureAwait(false); + OAuthToken oauthTokenLocalVar1 = (OAuthToken) await OauthTokenProvider.GetAsync(cancellation: cancellationToken).ConfigureAwait(false); tokenBaseLocalVars.Add(oauthTokenLocalVar1); oauthTokenLocalVar1.UseInHeader(httpRequestMessageLocalVar, ""); - HttpSignatureToken httpSignatureTokenLocalVar2 = (HttpSignatureToken) await HttpSignatureTokenProvider.GetAsync(cancellationToken).ConfigureAwait(false); + HttpSignatureToken httpSignatureTokenLocalVar2 = (HttpSignatureToken) await HttpSignatureTokenProvider.GetAsync(cancellation: cancellationToken).ConfigureAwait(false); tokenBaseLocalVars.Add(httpSignatureTokenLocalVar2); @@ -986,10 +985,9 @@ namespace Org.OpenAPITools.Api httpRequestMessageLocalVar.Headers.Add("api_key", ClientUtils.ParameterToString(apiKey.Value)); List tokenBaseLocalVars = new List(); - httpRequestMessageLocalVar.RequestUri = uriBuilderLocalVar.Uri; - OAuthToken oauthTokenLocalVar1 = (OAuthToken) await OauthTokenProvider.GetAsync(cancellationToken).ConfigureAwait(false); + OAuthToken oauthTokenLocalVar1 = (OAuthToken) await OauthTokenProvider.GetAsync(cancellation: cancellationToken).ConfigureAwait(false); tokenBaseLocalVars.Add(oauthTokenLocalVar1); @@ -1180,16 +1178,15 @@ namespace Org.OpenAPITools.Api uriBuilderLocalVar.Query = parseQueryStringLocalVar.ToString(); List tokenBaseLocalVars = new List(); - httpRequestMessageLocalVar.RequestUri = uriBuilderLocalVar.Uri; - OAuthToken oauthTokenLocalVar1 = (OAuthToken) await OauthTokenProvider.GetAsync(cancellationToken).ConfigureAwait(false); + OAuthToken oauthTokenLocalVar1 = (OAuthToken) await OauthTokenProvider.GetAsync(cancellation: cancellationToken).ConfigureAwait(false); tokenBaseLocalVars.Add(oauthTokenLocalVar1); oauthTokenLocalVar1.UseInHeader(httpRequestMessageLocalVar, ""); - HttpSignatureToken httpSignatureTokenLocalVar2 = (HttpSignatureToken) await HttpSignatureTokenProvider.GetAsync(cancellationToken).ConfigureAwait(false); + HttpSignatureToken httpSignatureTokenLocalVar2 = (HttpSignatureToken) await HttpSignatureTokenProvider.GetAsync(cancellation: cancellationToken).ConfigureAwait(false); tokenBaseLocalVars.Add(httpSignatureTokenLocalVar2); @@ -1432,16 +1429,15 @@ namespace Org.OpenAPITools.Api uriBuilderLocalVar.Query = parseQueryStringLocalVar.ToString(); List tokenBaseLocalVars = new List(); - httpRequestMessageLocalVar.RequestUri = uriBuilderLocalVar.Uri; - OAuthToken oauthTokenLocalVar1 = (OAuthToken) await OauthTokenProvider.GetAsync(cancellationToken).ConfigureAwait(false); + OAuthToken oauthTokenLocalVar1 = (OAuthToken) await OauthTokenProvider.GetAsync(cancellation: cancellationToken).ConfigureAwait(false); tokenBaseLocalVars.Add(oauthTokenLocalVar1); oauthTokenLocalVar1.UseInHeader(httpRequestMessageLocalVar, ""); - HttpSignatureToken httpSignatureTokenLocalVar2 = (HttpSignatureToken) await HttpSignatureTokenProvider.GetAsync(cancellationToken).ConfigureAwait(false); + HttpSignatureToken httpSignatureTokenLocalVar2 = (HttpSignatureToken) await HttpSignatureTokenProvider.GetAsync(cancellation: cancellationToken).ConfigureAwait(false); tokenBaseLocalVars.Add(httpSignatureTokenLocalVar2); @@ -1668,17 +1664,16 @@ namespace Org.OpenAPITools.Api System.Collections.Specialized.NameValueCollection parseQueryStringLocalVar = System.Web.HttpUtility.ParseQueryString(string.Empty); List tokenBaseLocalVars = new List(); - ApiKeyToken apiKeyTokenLocalVar1 = (ApiKeyToken) await ApiKeyProvider.GetAsync(cancellationToken).ConfigureAwait(false); + ApiKeyToken apiKeyTokenLocalVar1 = (ApiKeyToken) await ApiKeyProvider.GetAsync("api_key", cancellationToken).ConfigureAwait(false); tokenBaseLocalVars.Add(apiKeyTokenLocalVar1); - apiKeyTokenLocalVar1.UseInHeader(httpRequestMessageLocalVar, "api_key"); + apiKeyTokenLocalVar1.UseInHeader(httpRequestMessageLocalVar); - ApiKeyToken apiKeyTokenLocalVar2 = (ApiKeyToken) await ApiKeyProvider.GetAsync(cancellationToken).ConfigureAwait(false); + ApiKeyToken apiKeyTokenLocalVar2 = (ApiKeyToken) await ApiKeyProvider.GetAsync("api_key_query", cancellationToken).ConfigureAwait(false); tokenBaseLocalVars.Add(apiKeyTokenLocalVar2); - apiKeyTokenLocalVar2.UseInQuery(httpRequestMessageLocalVar, uriBuilderLocalVar, parseQueryStringLocalVar, "api_key_query"); + apiKeyTokenLocalVar2.UseInQuery(httpRequestMessageLocalVar, uriBuilderLocalVar, parseQueryStringLocalVar); uriBuilderLocalVar.Query = parseQueryStringLocalVar.ToString(); - httpRequestMessageLocalVar.RequestUri = uriBuilderLocalVar.Uri; string[] acceptLocalVars = new string[] { @@ -1918,16 +1913,15 @@ namespace Org.OpenAPITools.Api : httpRequestMessageLocalVar.Content = new StringContent(JsonSerializer.Serialize(pet, _jsonSerializerOptions)); List tokenBaseLocalVars = new List(); - httpRequestMessageLocalVar.RequestUri = uriBuilderLocalVar.Uri; - OAuthToken oauthTokenLocalVar1 = (OAuthToken) await OauthTokenProvider.GetAsync(cancellationToken).ConfigureAwait(false); + OAuthToken oauthTokenLocalVar1 = (OAuthToken) await OauthTokenProvider.GetAsync(cancellation: cancellationToken).ConfigureAwait(false); tokenBaseLocalVars.Add(oauthTokenLocalVar1); oauthTokenLocalVar1.UseInHeader(httpRequestMessageLocalVar, ""); - HttpSignatureToken httpSignatureTokenLocalVar2 = (HttpSignatureToken) await HttpSignatureTokenProvider.GetAsync(cancellationToken).ConfigureAwait(false); + HttpSignatureToken httpSignatureTokenLocalVar2 = (HttpSignatureToken) await HttpSignatureTokenProvider.GetAsync(cancellation: cancellationToken).ConfigureAwait(false); tokenBaseLocalVars.Add(httpSignatureTokenLocalVar2); @@ -2167,10 +2161,9 @@ namespace Org.OpenAPITools.Api formParameterLocalVars.Add(new KeyValuePair("status", ClientUtils.ParameterToString(status.Value))); List tokenBaseLocalVars = new List(); - httpRequestMessageLocalVar.RequestUri = uriBuilderLocalVar.Uri; - OAuthToken oauthTokenLocalVar1 = (OAuthToken) await OauthTokenProvider.GetAsync(cancellationToken).ConfigureAwait(false); + OAuthToken oauthTokenLocalVar1 = (OAuthToken) await OauthTokenProvider.GetAsync(cancellation: cancellationToken).ConfigureAwait(false); tokenBaseLocalVars.Add(oauthTokenLocalVar1); @@ -2393,10 +2386,9 @@ namespace Org.OpenAPITools.Api formParameterLocalVars.Add(new KeyValuePair("additionalMetadata", ClientUtils.ParameterToString(additionalMetadata.Value))); List tokenBaseLocalVars = new List(); - httpRequestMessageLocalVar.RequestUri = uriBuilderLocalVar.Uri; - OAuthToken oauthTokenLocalVar1 = (OAuthToken) await OauthTokenProvider.GetAsync(cancellationToken).ConfigureAwait(false); + OAuthToken oauthTokenLocalVar1 = (OAuthToken) await OauthTokenProvider.GetAsync(cancellation: cancellationToken).ConfigureAwait(false); tokenBaseLocalVars.Add(oauthTokenLocalVar1); @@ -2659,10 +2651,9 @@ namespace Org.OpenAPITools.Api formParameterLocalVars.Add(new KeyValuePair("additionalMetadata", ClientUtils.ParameterToString(additionalMetadata.Value))); List tokenBaseLocalVars = new List(); - httpRequestMessageLocalVar.RequestUri = uriBuilderLocalVar.Uri; - OAuthToken oauthTokenLocalVar1 = (OAuthToken) await OauthTokenProvider.GetAsync(cancellationToken).ConfigureAwait(false); + OAuthToken oauthTokenLocalVar1 = (OAuthToken) await OauthTokenProvider.GetAsync(cancellation: cancellationToken).ConfigureAwait(false); tokenBaseLocalVars.Add(oauthTokenLocalVar1); diff --git a/samples/client/petstore/csharp/OpenAPIClient-generichost-net6.0-nrt/src/Org.OpenAPITools/Api/StoreApi.cs b/samples/client/petstore/csharp/OpenAPIClient-generichost-net6.0-nrt/src/Org.OpenAPITools/Api/StoreApi.cs index fb919d7e47..2757e12618 100644 --- a/samples/client/petstore/csharp/OpenAPIClient-generichost-net6.0-nrt/src/Org.OpenAPITools/Api/StoreApi.cs +++ b/samples/client/petstore/csharp/OpenAPIClient-generichost-net6.0-nrt/src/Org.OpenAPITools/Api/StoreApi.cs @@ -621,9 +621,9 @@ namespace Org.OpenAPITools.Api uriBuilderLocalVar.Path = ClientUtils.CONTEXT_PATH + "/store/inventory"; List tokenBaseLocalVars = new List(); - ApiKeyToken apiKeyTokenLocalVar1 = (ApiKeyToken) await ApiKeyProvider.GetAsync(cancellationToken).ConfigureAwait(false); + ApiKeyToken apiKeyTokenLocalVar1 = (ApiKeyToken) await ApiKeyProvider.GetAsync("api_key", cancellationToken).ConfigureAwait(false); tokenBaseLocalVars.Add(apiKeyTokenLocalVar1); - apiKeyTokenLocalVar1.UseInHeader(httpRequestMessageLocalVar, "api_key"); + apiKeyTokenLocalVar1.UseInHeader(httpRequestMessageLocalVar); httpRequestMessageLocalVar.RequestUri = uriBuilderLocalVar.Uri; diff --git a/samples/client/petstore/csharp/OpenAPIClient-generichost-net6.0-nrt/src/Org.OpenAPITools/Client/ApiKeyToken.cs b/samples/client/petstore/csharp/OpenAPIClient-generichost-net6.0-nrt/src/Org.OpenAPITools/Client/ApiKeyToken.cs index a421f4e53e..08d59f6738 100644 --- a/samples/client/petstore/csharp/OpenAPIClient-generichost-net6.0-nrt/src/Org.OpenAPITools/Client/ApiKeyToken.cs +++ b/samples/client/petstore/csharp/OpenAPIClient-generichost-net6.0-nrt/src/Org.OpenAPITools/Client/ApiKeyToken.cs @@ -13,14 +13,21 @@ namespace Org.OpenAPITools.Client { private string _raw; + /// + /// The header that this token will be used with. + /// + public string Header { get; } + /// /// Constructs an ApiKeyToken object. /// /// + /// /// /// - public ApiKeyToken(string value, string prefix = "Bearer ", TimeSpan? timeout = null) : base(timeout) + public ApiKeyToken(string value, string header, string prefix = "Bearer ", TimeSpan? timeout = null) : base(timeout) { + Header = header; _raw = $"{ prefix }{ value }"; } @@ -28,22 +35,20 @@ namespace Org.OpenAPITools.Client /// Places the token in the header. /// /// - /// - public virtual void UseInHeader(System.Net.Http.HttpRequestMessage request, string headerName) + public virtual void UseInHeader(System.Net.Http.HttpRequestMessage request) { - request.Headers.Add(headerName, _raw); + request.Headers.Add(Header, _raw); } - + /// /// Places the token in the query. /// /// /// /// - /// - public virtual void UseInQuery(System.Net.Http.HttpRequestMessage request, UriBuilder uriBuilder, System.Collections.Specialized.NameValueCollection parseQueryString, string parameterName) + public virtual void UseInQuery(System.Net.Http.HttpRequestMessage request, UriBuilder uriBuilder, System.Collections.Specialized.NameValueCollection parseQueryString) { - parseQueryString[parameterName] = Uri.EscapeDataString(_raw).ToString()!; + parseQueryString[Header] = Uri.EscapeDataString(_raw).ToString()!; } } } \ No newline at end of file diff --git a/samples/client/petstore/csharp/OpenAPIClient-generichost-net6.0-nrt/src/Org.OpenAPITools/Client/RateLimitProvider`1.cs b/samples/client/petstore/csharp/OpenAPIClient-generichost-net6.0-nrt/src/Org.OpenAPITools/Client/RateLimitProvider`1.cs index 49269df7f9..1abb5da410 100644 --- a/samples/client/petstore/csharp/OpenAPIClient-generichost-net6.0-nrt/src/Org.OpenAPITools/Client/RateLimitProvider`1.cs +++ b/samples/client/petstore/csharp/OpenAPIClient-generichost-net6.0-nrt/src/Org.OpenAPITools/Client/RateLimitProvider`1.cs @@ -11,9 +11,11 @@ #nullable enable using System; +using System.Collections.Generic; +using System.Linq; using System.Threading.Channels; -namespace Org.OpenAPITools.Client +namespace Org.OpenAPITools.Client { /// /// Provides a token to the api clients. Tokens will be rate limited based on the provided TimeSpan. @@ -21,7 +23,7 @@ namespace Org.OpenAPITools.Client /// public class RateLimitProvider : TokenProvider where TTokenBase : TokenBase { - internal Channel AvailableTokens { get; } + internal Dictionary> AvailableTokens { get; } = new(); /// /// Instantiates a ThrottledTokenProvider. Your tokens will be rate limited based on the token's timeout. @@ -32,17 +34,41 @@ namespace Org.OpenAPITools.Client foreach(TTokenBase token in _tokens) token.StartTimer(token.Timeout ?? TimeSpan.FromMilliseconds(40)); - BoundedChannelOptions options = new BoundedChannelOptions(_tokens.Length) - { - FullMode = BoundedChannelFullMode.DropWrite - }; + if (container is TokenContainer apiKeyTokenContainer) + { + string[] headers = apiKeyTokenContainer.Tokens.Select(t => t.Header).Distinct().ToArray(); - AvailableTokens = Channel.CreateBounded(options); + foreach (string header in headers) + { + BoundedChannelOptions options = new BoundedChannelOptions(apiKeyTokenContainer.Tokens.Count(t => t.Header.Equals(header))) + { + FullMode = BoundedChannelFullMode.DropWrite + }; - for (int i = 0; i < _tokens.Length; i++) - _tokens[i].TokenBecameAvailable += ((sender) => AvailableTokens.Writer.TryWrite((TTokenBase) sender)); + AvailableTokens.Add(header, Channel.CreateBounded(options)); + } + } + else + { + BoundedChannelOptions options = new BoundedChannelOptions(_tokens.Length) + { + FullMode = BoundedChannelFullMode.DropWrite + }; + + AvailableTokens.Add(string.Empty, Channel.CreateBounded(options)); + } + + foreach(Channel tokens in AvailableTokens.Values) + for (int i = 0; i < _tokens.Length; i++) + _tokens[i].TokenBecameAvailable += ((sender) => tokens.Writer.TryWrite((TTokenBase) sender)); + } + + internal override async System.Threading.Tasks.ValueTask GetAsync(string header = "", System.Threading.CancellationToken cancellation = default) + { + if (!AvailableTokens.TryGetValue(header, out Channel? tokens)) + throw new KeyNotFoundException($"Could not locate a token for header '{header}'."); + + return await tokens.Reader.ReadAsync(cancellation).ConfigureAwait(false); } - internal override async System.Threading.Tasks.ValueTask GetAsync(System.Threading.CancellationToken cancellation = default) - => await AvailableTokens.Reader.ReadAsync(cancellation).ConfigureAwait(false); } -} +} diff --git a/samples/client/petstore/csharp/OpenAPIClient-generichost-net6.0-nrt/src/Org.OpenAPITools/Client/TokenProvider`1.cs b/samples/client/petstore/csharp/OpenAPIClient-generichost-net6.0-nrt/src/Org.OpenAPITools/Client/TokenProvider`1.cs index a629474b6a..b873d5323e 100644 --- a/samples/client/petstore/csharp/OpenAPIClient-generichost-net6.0-nrt/src/Org.OpenAPITools/Client/TokenProvider`1.cs +++ b/samples/client/petstore/csharp/OpenAPIClient-generichost-net6.0-nrt/src/Org.OpenAPITools/Client/TokenProvider`1.cs @@ -27,7 +27,7 @@ namespace Org.OpenAPITools /// protected TTokenBase[] _tokens; - internal abstract System.Threading.Tasks.ValueTask GetAsync(System.Threading.CancellationToken cancellation = default); + internal abstract System.Threading.Tasks.ValueTask GetAsync(string header = "", System.Threading.CancellationToken cancellation = default); /// /// Instantiates a TokenProvider. diff --git a/samples/client/petstore/csharp/OpenAPIClient-generichost-net6.0/src/Org.OpenAPITools/Api/FakeApi.cs b/samples/client/petstore/csharp/OpenAPIClient-generichost-net6.0/src/Org.OpenAPITools/Api/FakeApi.cs index 35e71d5dd2..26f352c525 100644 --- a/samples/client/petstore/csharp/OpenAPIClient-generichost-net6.0/src/Org.OpenAPITools/Api/FakeApi.cs +++ b/samples/client/petstore/csharp/OpenAPIClient-generichost-net6.0/src/Org.OpenAPITools/Api/FakeApi.cs @@ -3243,10 +3243,9 @@ namespace Org.OpenAPITools.Api formParameterLocalVars.Add(new KeyValuePair("dateTime", ClientUtils.ParameterToString(dateTime.Value))); List tokenBaseLocalVars = new List(); - httpRequestMessageLocalVar.RequestUri = uriBuilderLocalVar.Uri; - BasicToken basicTokenLocalVar1 = (BasicToken) await BasicTokenProvider.GetAsync(cancellationToken).ConfigureAwait(false); + BasicToken basicTokenLocalVar1 = (BasicToken) await BasicTokenProvider.GetAsync(cancellation: cancellationToken).ConfigureAwait(false); tokenBaseLocalVars.Add(basicTokenLocalVar1); @@ -3768,10 +3767,9 @@ namespace Org.OpenAPITools.Api httpRequestMessageLocalVar.Headers.Add("boolean_group", ClientUtils.ParameterToString(booleanGroup.Value)); List tokenBaseLocalVars = new List(); - httpRequestMessageLocalVar.RequestUri = uriBuilderLocalVar.Uri; - BearerToken bearerTokenLocalVar1 = (BearerToken) await BearerTokenProvider.GetAsync(cancellationToken).ConfigureAwait(false); + BearerToken bearerTokenLocalVar1 = (BearerToken) await BearerTokenProvider.GetAsync(cancellation: cancellationToken).ConfigureAwait(false); tokenBaseLocalVars.Add(bearerTokenLocalVar1); diff --git a/samples/client/petstore/csharp/OpenAPIClient-generichost-net6.0/src/Org.OpenAPITools/Api/FakeClassnameTags123Api.cs b/samples/client/petstore/csharp/OpenAPIClient-generichost-net6.0/src/Org.OpenAPITools/Api/FakeClassnameTags123Api.cs index e38e144de6..77506dc379 100644 --- a/samples/client/petstore/csharp/OpenAPIClient-generichost-net6.0/src/Org.OpenAPITools/Api/FakeClassnameTags123Api.cs +++ b/samples/client/petstore/csharp/OpenAPIClient-generichost-net6.0/src/Org.OpenAPITools/Api/FakeClassnameTags123Api.cs @@ -278,13 +278,12 @@ namespace Org.OpenAPITools.Api : httpRequestMessageLocalVar.Content = new StringContent(JsonSerializer.Serialize(modelClient, _jsonSerializerOptions)); List tokenBaseLocalVars = new List(); - ApiKeyToken apiKeyTokenLocalVar1 = (ApiKeyToken) await ApiKeyProvider.GetAsync(cancellationToken).ConfigureAwait(false); + ApiKeyToken apiKeyTokenLocalVar1 = (ApiKeyToken) await ApiKeyProvider.GetAsync("api_key_query", cancellationToken).ConfigureAwait(false); tokenBaseLocalVars.Add(apiKeyTokenLocalVar1); - apiKeyTokenLocalVar1.UseInQuery(httpRequestMessageLocalVar, uriBuilderLocalVar, parseQueryStringLocalVar, "api_key_query"); + apiKeyTokenLocalVar1.UseInQuery(httpRequestMessageLocalVar, uriBuilderLocalVar, parseQueryStringLocalVar); uriBuilderLocalVar.Query = parseQueryStringLocalVar.ToString(); - httpRequestMessageLocalVar.RequestUri = uriBuilderLocalVar.Uri; string[] contentTypes = new string[] { diff --git a/samples/client/petstore/csharp/OpenAPIClient-generichost-net6.0/src/Org.OpenAPITools/Api/PetApi.cs b/samples/client/petstore/csharp/OpenAPIClient-generichost-net6.0/src/Org.OpenAPITools/Api/PetApi.cs index 4d540b4c2a..e784d42075 100644 --- a/samples/client/petstore/csharp/OpenAPIClient-generichost-net6.0/src/Org.OpenAPITools/Api/PetApi.cs +++ b/samples/client/petstore/csharp/OpenAPIClient-generichost-net6.0/src/Org.OpenAPITools/Api/PetApi.cs @@ -766,16 +766,15 @@ namespace Org.OpenAPITools.Api : httpRequestMessageLocalVar.Content = new StringContent(JsonSerializer.Serialize(pet, _jsonSerializerOptions)); List tokenBaseLocalVars = new List(); - httpRequestMessageLocalVar.RequestUri = uriBuilderLocalVar.Uri; - OAuthToken oauthTokenLocalVar1 = (OAuthToken) await OauthTokenProvider.GetAsync(cancellationToken).ConfigureAwait(false); + OAuthToken oauthTokenLocalVar1 = (OAuthToken) await OauthTokenProvider.GetAsync(cancellation: cancellationToken).ConfigureAwait(false); tokenBaseLocalVars.Add(oauthTokenLocalVar1); oauthTokenLocalVar1.UseInHeader(httpRequestMessageLocalVar, ""); - HttpSignatureToken httpSignatureTokenLocalVar2 = (HttpSignatureToken) await HttpSignatureTokenProvider.GetAsync(cancellationToken).ConfigureAwait(false); + HttpSignatureToken httpSignatureTokenLocalVar2 = (HttpSignatureToken) await HttpSignatureTokenProvider.GetAsync(cancellation: cancellationToken).ConfigureAwait(false); tokenBaseLocalVars.Add(httpSignatureTokenLocalVar2); @@ -984,10 +983,9 @@ namespace Org.OpenAPITools.Api httpRequestMessageLocalVar.Headers.Add("api_key", ClientUtils.ParameterToString(apiKey.Value)); List tokenBaseLocalVars = new List(); - httpRequestMessageLocalVar.RequestUri = uriBuilderLocalVar.Uri; - OAuthToken oauthTokenLocalVar1 = (OAuthToken) await OauthTokenProvider.GetAsync(cancellationToken).ConfigureAwait(false); + OAuthToken oauthTokenLocalVar1 = (OAuthToken) await OauthTokenProvider.GetAsync(cancellation: cancellationToken).ConfigureAwait(false); tokenBaseLocalVars.Add(oauthTokenLocalVar1); @@ -1178,16 +1176,15 @@ namespace Org.OpenAPITools.Api uriBuilderLocalVar.Query = parseQueryStringLocalVar.ToString(); List tokenBaseLocalVars = new List(); - httpRequestMessageLocalVar.RequestUri = uriBuilderLocalVar.Uri; - OAuthToken oauthTokenLocalVar1 = (OAuthToken) await OauthTokenProvider.GetAsync(cancellationToken).ConfigureAwait(false); + OAuthToken oauthTokenLocalVar1 = (OAuthToken) await OauthTokenProvider.GetAsync(cancellation: cancellationToken).ConfigureAwait(false); tokenBaseLocalVars.Add(oauthTokenLocalVar1); oauthTokenLocalVar1.UseInHeader(httpRequestMessageLocalVar, ""); - HttpSignatureToken httpSignatureTokenLocalVar2 = (HttpSignatureToken) await HttpSignatureTokenProvider.GetAsync(cancellationToken).ConfigureAwait(false); + HttpSignatureToken httpSignatureTokenLocalVar2 = (HttpSignatureToken) await HttpSignatureTokenProvider.GetAsync(cancellation: cancellationToken).ConfigureAwait(false); tokenBaseLocalVars.Add(httpSignatureTokenLocalVar2); @@ -1430,16 +1427,15 @@ namespace Org.OpenAPITools.Api uriBuilderLocalVar.Query = parseQueryStringLocalVar.ToString(); List tokenBaseLocalVars = new List(); - httpRequestMessageLocalVar.RequestUri = uriBuilderLocalVar.Uri; - OAuthToken oauthTokenLocalVar1 = (OAuthToken) await OauthTokenProvider.GetAsync(cancellationToken).ConfigureAwait(false); + OAuthToken oauthTokenLocalVar1 = (OAuthToken) await OauthTokenProvider.GetAsync(cancellation: cancellationToken).ConfigureAwait(false); tokenBaseLocalVars.Add(oauthTokenLocalVar1); oauthTokenLocalVar1.UseInHeader(httpRequestMessageLocalVar, ""); - HttpSignatureToken httpSignatureTokenLocalVar2 = (HttpSignatureToken) await HttpSignatureTokenProvider.GetAsync(cancellationToken).ConfigureAwait(false); + HttpSignatureToken httpSignatureTokenLocalVar2 = (HttpSignatureToken) await HttpSignatureTokenProvider.GetAsync(cancellation: cancellationToken).ConfigureAwait(false); tokenBaseLocalVars.Add(httpSignatureTokenLocalVar2); @@ -1666,17 +1662,16 @@ namespace Org.OpenAPITools.Api System.Collections.Specialized.NameValueCollection parseQueryStringLocalVar = System.Web.HttpUtility.ParseQueryString(string.Empty); List tokenBaseLocalVars = new List(); - ApiKeyToken apiKeyTokenLocalVar1 = (ApiKeyToken) await ApiKeyProvider.GetAsync(cancellationToken).ConfigureAwait(false); + ApiKeyToken apiKeyTokenLocalVar1 = (ApiKeyToken) await ApiKeyProvider.GetAsync("api_key", cancellationToken).ConfigureAwait(false); tokenBaseLocalVars.Add(apiKeyTokenLocalVar1); - apiKeyTokenLocalVar1.UseInHeader(httpRequestMessageLocalVar, "api_key"); + apiKeyTokenLocalVar1.UseInHeader(httpRequestMessageLocalVar); - ApiKeyToken apiKeyTokenLocalVar2 = (ApiKeyToken) await ApiKeyProvider.GetAsync(cancellationToken).ConfigureAwait(false); + ApiKeyToken apiKeyTokenLocalVar2 = (ApiKeyToken) await ApiKeyProvider.GetAsync("api_key_query", cancellationToken).ConfigureAwait(false); tokenBaseLocalVars.Add(apiKeyTokenLocalVar2); - apiKeyTokenLocalVar2.UseInQuery(httpRequestMessageLocalVar, uriBuilderLocalVar, parseQueryStringLocalVar, "api_key_query"); + apiKeyTokenLocalVar2.UseInQuery(httpRequestMessageLocalVar, uriBuilderLocalVar, parseQueryStringLocalVar); uriBuilderLocalVar.Query = parseQueryStringLocalVar.ToString(); - httpRequestMessageLocalVar.RequestUri = uriBuilderLocalVar.Uri; string[] acceptLocalVars = new string[] { @@ -1916,16 +1911,15 @@ namespace Org.OpenAPITools.Api : httpRequestMessageLocalVar.Content = new StringContent(JsonSerializer.Serialize(pet, _jsonSerializerOptions)); List tokenBaseLocalVars = new List(); - httpRequestMessageLocalVar.RequestUri = uriBuilderLocalVar.Uri; - OAuthToken oauthTokenLocalVar1 = (OAuthToken) await OauthTokenProvider.GetAsync(cancellationToken).ConfigureAwait(false); + OAuthToken oauthTokenLocalVar1 = (OAuthToken) await OauthTokenProvider.GetAsync(cancellation: cancellationToken).ConfigureAwait(false); tokenBaseLocalVars.Add(oauthTokenLocalVar1); oauthTokenLocalVar1.UseInHeader(httpRequestMessageLocalVar, ""); - HttpSignatureToken httpSignatureTokenLocalVar2 = (HttpSignatureToken) await HttpSignatureTokenProvider.GetAsync(cancellationToken).ConfigureAwait(false); + HttpSignatureToken httpSignatureTokenLocalVar2 = (HttpSignatureToken) await HttpSignatureTokenProvider.GetAsync(cancellation: cancellationToken).ConfigureAwait(false); tokenBaseLocalVars.Add(httpSignatureTokenLocalVar2); @@ -2165,10 +2159,9 @@ namespace Org.OpenAPITools.Api formParameterLocalVars.Add(new KeyValuePair("status", ClientUtils.ParameterToString(status.Value))); List tokenBaseLocalVars = new List(); - httpRequestMessageLocalVar.RequestUri = uriBuilderLocalVar.Uri; - OAuthToken oauthTokenLocalVar1 = (OAuthToken) await OauthTokenProvider.GetAsync(cancellationToken).ConfigureAwait(false); + OAuthToken oauthTokenLocalVar1 = (OAuthToken) await OauthTokenProvider.GetAsync(cancellation: cancellationToken).ConfigureAwait(false); tokenBaseLocalVars.Add(oauthTokenLocalVar1); @@ -2391,10 +2384,9 @@ namespace Org.OpenAPITools.Api formParameterLocalVars.Add(new KeyValuePair("additionalMetadata", ClientUtils.ParameterToString(additionalMetadata.Value))); List tokenBaseLocalVars = new List(); - httpRequestMessageLocalVar.RequestUri = uriBuilderLocalVar.Uri; - OAuthToken oauthTokenLocalVar1 = (OAuthToken) await OauthTokenProvider.GetAsync(cancellationToken).ConfigureAwait(false); + OAuthToken oauthTokenLocalVar1 = (OAuthToken) await OauthTokenProvider.GetAsync(cancellation: cancellationToken).ConfigureAwait(false); tokenBaseLocalVars.Add(oauthTokenLocalVar1); @@ -2657,10 +2649,9 @@ namespace Org.OpenAPITools.Api formParameterLocalVars.Add(new KeyValuePair("additionalMetadata", ClientUtils.ParameterToString(additionalMetadata.Value))); List tokenBaseLocalVars = new List(); - httpRequestMessageLocalVar.RequestUri = uriBuilderLocalVar.Uri; - OAuthToken oauthTokenLocalVar1 = (OAuthToken) await OauthTokenProvider.GetAsync(cancellationToken).ConfigureAwait(false); + OAuthToken oauthTokenLocalVar1 = (OAuthToken) await OauthTokenProvider.GetAsync(cancellation: cancellationToken).ConfigureAwait(false); tokenBaseLocalVars.Add(oauthTokenLocalVar1); diff --git a/samples/client/petstore/csharp/OpenAPIClient-generichost-net6.0/src/Org.OpenAPITools/Api/StoreApi.cs b/samples/client/petstore/csharp/OpenAPIClient-generichost-net6.0/src/Org.OpenAPITools/Api/StoreApi.cs index 8be6accc14..2117e516e1 100644 --- a/samples/client/petstore/csharp/OpenAPIClient-generichost-net6.0/src/Org.OpenAPITools/Api/StoreApi.cs +++ b/samples/client/petstore/csharp/OpenAPIClient-generichost-net6.0/src/Org.OpenAPITools/Api/StoreApi.cs @@ -619,9 +619,9 @@ namespace Org.OpenAPITools.Api uriBuilderLocalVar.Path = ClientUtils.CONTEXT_PATH + "/store/inventory"; List tokenBaseLocalVars = new List(); - ApiKeyToken apiKeyTokenLocalVar1 = (ApiKeyToken) await ApiKeyProvider.GetAsync(cancellationToken).ConfigureAwait(false); + ApiKeyToken apiKeyTokenLocalVar1 = (ApiKeyToken) await ApiKeyProvider.GetAsync("api_key", cancellationToken).ConfigureAwait(false); tokenBaseLocalVars.Add(apiKeyTokenLocalVar1); - apiKeyTokenLocalVar1.UseInHeader(httpRequestMessageLocalVar, "api_key"); + apiKeyTokenLocalVar1.UseInHeader(httpRequestMessageLocalVar); httpRequestMessageLocalVar.RequestUri = uriBuilderLocalVar.Uri; diff --git a/samples/client/petstore/csharp/OpenAPIClient-generichost-net6.0/src/Org.OpenAPITools/Client/ApiKeyToken.cs b/samples/client/petstore/csharp/OpenAPIClient-generichost-net6.0/src/Org.OpenAPITools/Client/ApiKeyToken.cs index 5cc9c25492..541961ff98 100644 --- a/samples/client/petstore/csharp/OpenAPIClient-generichost-net6.0/src/Org.OpenAPITools/Client/ApiKeyToken.cs +++ b/samples/client/petstore/csharp/OpenAPIClient-generichost-net6.0/src/Org.OpenAPITools/Client/ApiKeyToken.cs @@ -11,14 +11,21 @@ namespace Org.OpenAPITools.Client { private string _raw; + /// + /// The header that this token will be used with. + /// + public string Header { get; } + /// /// Constructs an ApiKeyToken object. /// /// + /// /// /// - public ApiKeyToken(string value, string prefix = "Bearer ", TimeSpan? timeout = null) : base(timeout) + public ApiKeyToken(string value, string header, string prefix = "Bearer ", TimeSpan? timeout = null) : base(timeout) { + Header = header; _raw = $"{ prefix }{ value }"; } @@ -26,22 +33,20 @@ namespace Org.OpenAPITools.Client /// Places the token in the header. /// /// - /// - public virtual void UseInHeader(System.Net.Http.HttpRequestMessage request, string headerName) + public virtual void UseInHeader(System.Net.Http.HttpRequestMessage request) { - request.Headers.Add(headerName, _raw); + request.Headers.Add(Header, _raw); } - + /// /// Places the token in the query. /// /// /// /// - /// - public virtual void UseInQuery(System.Net.Http.HttpRequestMessage request, UriBuilder uriBuilder, System.Collections.Specialized.NameValueCollection parseQueryString, string parameterName) + public virtual void UseInQuery(System.Net.Http.HttpRequestMessage request, UriBuilder uriBuilder, System.Collections.Specialized.NameValueCollection parseQueryString) { - parseQueryString[parameterName] = Uri.EscapeDataString(_raw).ToString(); + parseQueryString[Header] = Uri.EscapeDataString(_raw).ToString(); } } } \ No newline at end of file diff --git a/samples/client/petstore/csharp/OpenAPIClient-generichost-net6.0/src/Org.OpenAPITools/Client/RateLimitProvider`1.cs b/samples/client/petstore/csharp/OpenAPIClient-generichost-net6.0/src/Org.OpenAPITools/Client/RateLimitProvider`1.cs index 79a3bd75bf..5f297efa7b 100644 --- a/samples/client/petstore/csharp/OpenAPIClient-generichost-net6.0/src/Org.OpenAPITools/Client/RateLimitProvider`1.cs +++ b/samples/client/petstore/csharp/OpenAPIClient-generichost-net6.0/src/Org.OpenAPITools/Client/RateLimitProvider`1.cs @@ -9,9 +9,11 @@ */ using System; +using System.Collections.Generic; +using System.Linq; using System.Threading.Channels; -namespace Org.OpenAPITools.Client +namespace Org.OpenAPITools.Client { /// /// Provides a token to the api clients. Tokens will be rate limited based on the provided TimeSpan. @@ -19,7 +21,7 @@ namespace Org.OpenAPITools.Client /// public class RateLimitProvider : TokenProvider where TTokenBase : TokenBase { - internal Channel AvailableTokens { get; } + internal Dictionary> AvailableTokens { get; } = new(); /// /// Instantiates a ThrottledTokenProvider. Your tokens will be rate limited based on the token's timeout. @@ -30,17 +32,41 @@ namespace Org.OpenAPITools.Client foreach(TTokenBase token in _tokens) token.StartTimer(token.Timeout ?? TimeSpan.FromMilliseconds(40)); - BoundedChannelOptions options = new BoundedChannelOptions(_tokens.Length) - { - FullMode = BoundedChannelFullMode.DropWrite - }; + if (container is TokenContainer apiKeyTokenContainer) + { + string[] headers = apiKeyTokenContainer.Tokens.Select(t => t.Header).Distinct().ToArray(); - AvailableTokens = Channel.CreateBounded(options); + foreach (string header in headers) + { + BoundedChannelOptions options = new BoundedChannelOptions(apiKeyTokenContainer.Tokens.Count(t => t.Header.Equals(header))) + { + FullMode = BoundedChannelFullMode.DropWrite + }; - for (int i = 0; i < _tokens.Length; i++) - _tokens[i].TokenBecameAvailable += ((sender) => AvailableTokens.Writer.TryWrite((TTokenBase) sender)); + AvailableTokens.Add(header, Channel.CreateBounded(options)); + } + } + else + { + BoundedChannelOptions options = new BoundedChannelOptions(_tokens.Length) + { + FullMode = BoundedChannelFullMode.DropWrite + }; + + AvailableTokens.Add(string.Empty, Channel.CreateBounded(options)); + } + + foreach(Channel tokens in AvailableTokens.Values) + for (int i = 0; i < _tokens.Length; i++) + _tokens[i].TokenBecameAvailable += ((sender) => tokens.Writer.TryWrite((TTokenBase) sender)); + } + + internal override async System.Threading.Tasks.ValueTask GetAsync(string header = "", System.Threading.CancellationToken cancellation = default) + { + if (!AvailableTokens.TryGetValue(header, out Channel tokens)) + throw new KeyNotFoundException($"Could not locate a token for header '{header}'."); + + return await tokens.Reader.ReadAsync(cancellation).ConfigureAwait(false); } - internal override async System.Threading.Tasks.ValueTask GetAsync(System.Threading.CancellationToken cancellation = default) - => await AvailableTokens.Reader.ReadAsync(cancellation).ConfigureAwait(false); } -} +} diff --git a/samples/client/petstore/csharp/OpenAPIClient-generichost-net6.0/src/Org.OpenAPITools/Client/TokenProvider`1.cs b/samples/client/petstore/csharp/OpenAPIClient-generichost-net6.0/src/Org.OpenAPITools/Client/TokenProvider`1.cs index c49f723f8c..36d7dad03e 100644 --- a/samples/client/petstore/csharp/OpenAPIClient-generichost-net6.0/src/Org.OpenAPITools/Client/TokenProvider`1.cs +++ b/samples/client/petstore/csharp/OpenAPIClient-generichost-net6.0/src/Org.OpenAPITools/Client/TokenProvider`1.cs @@ -25,7 +25,7 @@ namespace Org.OpenAPITools /// protected TTokenBase[] _tokens; - internal abstract System.Threading.Tasks.ValueTask GetAsync(System.Threading.CancellationToken cancellation = default); + internal abstract System.Threading.Tasks.ValueTask GetAsync(string header = "", System.Threading.CancellationToken cancellation = default); /// /// Instantiates a TokenProvider. diff --git a/samples/client/petstore/csharp/OpenAPIClient-generichost-netcore-latest-allOf/src/Org.OpenAPITools/Client/RateLimitProvider`1.cs b/samples/client/petstore/csharp/OpenAPIClient-generichost-netcore-latest-allOf/src/Org.OpenAPITools/Client/RateLimitProvider`1.cs index d77743bb7c..d50991f16e 100644 --- a/samples/client/petstore/csharp/OpenAPIClient-generichost-netcore-latest-allOf/src/Org.OpenAPITools/Client/RateLimitProvider`1.cs +++ b/samples/client/petstore/csharp/OpenAPIClient-generichost-netcore-latest-allOf/src/Org.OpenAPITools/Client/RateLimitProvider`1.cs @@ -11,9 +11,11 @@ #nullable enable using System; +using System.Collections.Generic; +using System.Linq; using System.Threading.Channels; -namespace Org.OpenAPITools.Client +namespace Org.OpenAPITools.Client { /// /// Provides a token to the api clients. Tokens will be rate limited based on the provided TimeSpan. @@ -21,7 +23,7 @@ namespace Org.OpenAPITools.Client /// public class RateLimitProvider : TokenProvider where TTokenBase : TokenBase { - internal Channel AvailableTokens { get; } + internal Dictionary> AvailableTokens { get; } = new(); /// /// Instantiates a ThrottledTokenProvider. Your tokens will be rate limited based on the token's timeout. @@ -32,17 +34,24 @@ namespace Org.OpenAPITools.Client foreach(TTokenBase token in _tokens) token.StartTimer(token.Timeout ?? TimeSpan.FromMilliseconds(40)); - BoundedChannelOptions options = new BoundedChannelOptions(_tokens.Length) - { - FullMode = BoundedChannelFullMode.DropWrite + BoundedChannelOptions options = new BoundedChannelOptions(_tokens.Length) + { + FullMode = BoundedChannelFullMode.DropWrite }; - AvailableTokens = Channel.CreateBounded(options); + AvailableTokens.Add(string.Empty, Channel.CreateBounded(options)); - for (int i = 0; i < _tokens.Length; i++) - _tokens[i].TokenBecameAvailable += ((sender) => AvailableTokens.Writer.TryWrite((TTokenBase) sender)); + foreach(Channel tokens in AvailableTokens.Values) + for (int i = 0; i < _tokens.Length; i++) + _tokens[i].TokenBecameAvailable += ((sender) => tokens.Writer.TryWrite((TTokenBase) sender)); + } + + internal override async System.Threading.Tasks.ValueTask GetAsync(string header = "", System.Threading.CancellationToken cancellation = default) + { + if (!AvailableTokens.TryGetValue(header, out Channel? tokens)) + throw new KeyNotFoundException($"Could not locate a token for header '{header}'."); + + return await tokens.Reader.ReadAsync(cancellation).ConfigureAwait(false); } - internal override async System.Threading.Tasks.ValueTask GetAsync(System.Threading.CancellationToken cancellation = default) - => await AvailableTokens.Reader.ReadAsync(cancellation).ConfigureAwait(false); } -} +} diff --git a/samples/client/petstore/csharp/OpenAPIClient-generichost-netcore-latest-allOf/src/Org.OpenAPITools/Client/TokenProvider`1.cs b/samples/client/petstore/csharp/OpenAPIClient-generichost-netcore-latest-allOf/src/Org.OpenAPITools/Client/TokenProvider`1.cs index b0949d3d08..25a52d3c71 100644 --- a/samples/client/petstore/csharp/OpenAPIClient-generichost-netcore-latest-allOf/src/Org.OpenAPITools/Client/TokenProvider`1.cs +++ b/samples/client/petstore/csharp/OpenAPIClient-generichost-netcore-latest-allOf/src/Org.OpenAPITools/Client/TokenProvider`1.cs @@ -27,7 +27,7 @@ namespace Org.OpenAPITools /// protected TTokenBase[] _tokens; - internal abstract System.Threading.Tasks.ValueTask GetAsync(System.Threading.CancellationToken cancellation = default); + internal abstract System.Threading.Tasks.ValueTask GetAsync(string header = "", System.Threading.CancellationToken cancellation = default); /// /// Instantiates a TokenProvider. diff --git a/samples/client/petstore/csharp/OpenAPIClient-generichost-netcore-latest-anyOf/src/Org.OpenAPITools/Client/RateLimitProvider`1.cs b/samples/client/petstore/csharp/OpenAPIClient-generichost-netcore-latest-anyOf/src/Org.OpenAPITools/Client/RateLimitProvider`1.cs index 89d8079d63..b10d5ac58f 100644 --- a/samples/client/petstore/csharp/OpenAPIClient-generichost-netcore-latest-anyOf/src/Org.OpenAPITools/Client/RateLimitProvider`1.cs +++ b/samples/client/petstore/csharp/OpenAPIClient-generichost-netcore-latest-anyOf/src/Org.OpenAPITools/Client/RateLimitProvider`1.cs @@ -11,9 +11,11 @@ #nullable enable using System; +using System.Collections.Generic; +using System.Linq; using System.Threading.Channels; -namespace Org.OpenAPITools.Client +namespace Org.OpenAPITools.Client { /// /// Provides a token to the api clients. Tokens will be rate limited based on the provided TimeSpan. @@ -21,7 +23,7 @@ namespace Org.OpenAPITools.Client /// public class RateLimitProvider : TokenProvider where TTokenBase : TokenBase { - internal Channel AvailableTokens { get; } + internal Dictionary> AvailableTokens { get; } = new(); /// /// Instantiates a ThrottledTokenProvider. Your tokens will be rate limited based on the token's timeout. @@ -32,17 +34,24 @@ namespace Org.OpenAPITools.Client foreach(TTokenBase token in _tokens) token.StartTimer(token.Timeout ?? TimeSpan.FromMilliseconds(40)); - BoundedChannelOptions options = new BoundedChannelOptions(_tokens.Length) - { - FullMode = BoundedChannelFullMode.DropWrite + BoundedChannelOptions options = new BoundedChannelOptions(_tokens.Length) + { + FullMode = BoundedChannelFullMode.DropWrite }; - AvailableTokens = Channel.CreateBounded(options); + AvailableTokens.Add(string.Empty, Channel.CreateBounded(options)); - for (int i = 0; i < _tokens.Length; i++) - _tokens[i].TokenBecameAvailable += ((sender) => AvailableTokens.Writer.TryWrite((TTokenBase) sender)); + foreach(Channel tokens in AvailableTokens.Values) + for (int i = 0; i < _tokens.Length; i++) + _tokens[i].TokenBecameAvailable += ((sender) => tokens.Writer.TryWrite((TTokenBase) sender)); + } + + internal override async System.Threading.Tasks.ValueTask GetAsync(string header = "", System.Threading.CancellationToken cancellation = default) + { + if (!AvailableTokens.TryGetValue(header, out Channel? tokens)) + throw new KeyNotFoundException($"Could not locate a token for header '{header}'."); + + return await tokens.Reader.ReadAsync(cancellation).ConfigureAwait(false); } - internal override async System.Threading.Tasks.ValueTask GetAsync(System.Threading.CancellationToken cancellation = default) - => await AvailableTokens.Reader.ReadAsync(cancellation).ConfigureAwait(false); } -} +} diff --git a/samples/client/petstore/csharp/OpenAPIClient-generichost-netcore-latest-anyOf/src/Org.OpenAPITools/Client/TokenProvider`1.cs b/samples/client/petstore/csharp/OpenAPIClient-generichost-netcore-latest-anyOf/src/Org.OpenAPITools/Client/TokenProvider`1.cs index 8628269573..db3ce767d4 100644 --- a/samples/client/petstore/csharp/OpenAPIClient-generichost-netcore-latest-anyOf/src/Org.OpenAPITools/Client/TokenProvider`1.cs +++ b/samples/client/petstore/csharp/OpenAPIClient-generichost-netcore-latest-anyOf/src/Org.OpenAPITools/Client/TokenProvider`1.cs @@ -27,7 +27,7 @@ namespace Org.OpenAPITools /// protected TTokenBase[] _tokens; - internal abstract System.Threading.Tasks.ValueTask GetAsync(System.Threading.CancellationToken cancellation = default); + internal abstract System.Threading.Tasks.ValueTask GetAsync(string header = "", System.Threading.CancellationToken cancellation = default); /// /// Instantiates a TokenProvider. diff --git a/samples/client/petstore/csharp/OpenAPIClient-generichost-netcore-latest-oneOf/src/Org.OpenAPITools/Client/RateLimitProvider`1.cs b/samples/client/petstore/csharp/OpenAPIClient-generichost-netcore-latest-oneOf/src/Org.OpenAPITools/Client/RateLimitProvider`1.cs index 89d8079d63..b10d5ac58f 100644 --- a/samples/client/petstore/csharp/OpenAPIClient-generichost-netcore-latest-oneOf/src/Org.OpenAPITools/Client/RateLimitProvider`1.cs +++ b/samples/client/petstore/csharp/OpenAPIClient-generichost-netcore-latest-oneOf/src/Org.OpenAPITools/Client/RateLimitProvider`1.cs @@ -11,9 +11,11 @@ #nullable enable using System; +using System.Collections.Generic; +using System.Linq; using System.Threading.Channels; -namespace Org.OpenAPITools.Client +namespace Org.OpenAPITools.Client { /// /// Provides a token to the api clients. Tokens will be rate limited based on the provided TimeSpan. @@ -21,7 +23,7 @@ namespace Org.OpenAPITools.Client /// public class RateLimitProvider : TokenProvider where TTokenBase : TokenBase { - internal Channel AvailableTokens { get; } + internal Dictionary> AvailableTokens { get; } = new(); /// /// Instantiates a ThrottledTokenProvider. Your tokens will be rate limited based on the token's timeout. @@ -32,17 +34,24 @@ namespace Org.OpenAPITools.Client foreach(TTokenBase token in _tokens) token.StartTimer(token.Timeout ?? TimeSpan.FromMilliseconds(40)); - BoundedChannelOptions options = new BoundedChannelOptions(_tokens.Length) - { - FullMode = BoundedChannelFullMode.DropWrite + BoundedChannelOptions options = new BoundedChannelOptions(_tokens.Length) + { + FullMode = BoundedChannelFullMode.DropWrite }; - AvailableTokens = Channel.CreateBounded(options); + AvailableTokens.Add(string.Empty, Channel.CreateBounded(options)); - for (int i = 0; i < _tokens.Length; i++) - _tokens[i].TokenBecameAvailable += ((sender) => AvailableTokens.Writer.TryWrite((TTokenBase) sender)); + foreach(Channel tokens in AvailableTokens.Values) + for (int i = 0; i < _tokens.Length; i++) + _tokens[i].TokenBecameAvailable += ((sender) => tokens.Writer.TryWrite((TTokenBase) sender)); + } + + internal override async System.Threading.Tasks.ValueTask GetAsync(string header = "", System.Threading.CancellationToken cancellation = default) + { + if (!AvailableTokens.TryGetValue(header, out Channel? tokens)) + throw new KeyNotFoundException($"Could not locate a token for header '{header}'."); + + return await tokens.Reader.ReadAsync(cancellation).ConfigureAwait(false); } - internal override async System.Threading.Tasks.ValueTask GetAsync(System.Threading.CancellationToken cancellation = default) - => await AvailableTokens.Reader.ReadAsync(cancellation).ConfigureAwait(false); } -} +} diff --git a/samples/client/petstore/csharp/OpenAPIClient-generichost-netcore-latest-oneOf/src/Org.OpenAPITools/Client/TokenProvider`1.cs b/samples/client/petstore/csharp/OpenAPIClient-generichost-netcore-latest-oneOf/src/Org.OpenAPITools/Client/TokenProvider`1.cs index 8628269573..db3ce767d4 100644 --- a/samples/client/petstore/csharp/OpenAPIClient-generichost-netcore-latest-oneOf/src/Org.OpenAPITools/Client/TokenProvider`1.cs +++ b/samples/client/petstore/csharp/OpenAPIClient-generichost-netcore-latest-oneOf/src/Org.OpenAPITools/Client/TokenProvider`1.cs @@ -27,7 +27,7 @@ namespace Org.OpenAPITools /// protected TTokenBase[] _tokens; - internal abstract System.Threading.Tasks.ValueTask GetAsync(System.Threading.CancellationToken cancellation = default); + internal abstract System.Threading.Tasks.ValueTask GetAsync(string header = "", System.Threading.CancellationToken cancellation = default); /// /// Instantiates a TokenProvider. diff --git a/samples/client/petstore/csharp/OpenAPIClient-generichost-netstandard2.0/src/Org.OpenAPITools/Api/FakeApi.cs b/samples/client/petstore/csharp/OpenAPIClient-generichost-netstandard2.0/src/Org.OpenAPITools/Api/FakeApi.cs index 06c50cca00..44ea52c57d 100644 --- a/samples/client/petstore/csharp/OpenAPIClient-generichost-netstandard2.0/src/Org.OpenAPITools/Api/FakeApi.cs +++ b/samples/client/petstore/csharp/OpenAPIClient-generichost-netstandard2.0/src/Org.OpenAPITools/Api/FakeApi.cs @@ -3235,10 +3235,9 @@ namespace Org.OpenAPITools.Api formParameterLocalVars.Add(new KeyValuePair("dateTime", ClientUtils.ParameterToString(dateTime.Value))); List tokenBaseLocalVars = new List(); - httpRequestMessageLocalVar.RequestUri = uriBuilderLocalVar.Uri; - BasicToken basicTokenLocalVar1 = (BasicToken) await BasicTokenProvider.GetAsync(cancellationToken).ConfigureAwait(false); + BasicToken basicTokenLocalVar1 = (BasicToken) await BasicTokenProvider.GetAsync(cancellation: cancellationToken).ConfigureAwait(false); tokenBaseLocalVars.Add(basicTokenLocalVar1); @@ -3760,10 +3759,9 @@ namespace Org.OpenAPITools.Api httpRequestMessageLocalVar.Headers.Add("boolean_group", ClientUtils.ParameterToString(booleanGroup.Value)); List tokenBaseLocalVars = new List(); - httpRequestMessageLocalVar.RequestUri = uriBuilderLocalVar.Uri; - BearerToken bearerTokenLocalVar1 = (BearerToken) await BearerTokenProvider.GetAsync(cancellationToken).ConfigureAwait(false); + BearerToken bearerTokenLocalVar1 = (BearerToken) await BearerTokenProvider.GetAsync(cancellation: cancellationToken).ConfigureAwait(false); tokenBaseLocalVars.Add(bearerTokenLocalVar1); diff --git a/samples/client/petstore/csharp/OpenAPIClient-generichost-netstandard2.0/src/Org.OpenAPITools/Api/FakeClassnameTags123Api.cs b/samples/client/petstore/csharp/OpenAPIClient-generichost-netstandard2.0/src/Org.OpenAPITools/Api/FakeClassnameTags123Api.cs index 970c301061..e9cbea054a 100644 --- a/samples/client/petstore/csharp/OpenAPIClient-generichost-netstandard2.0/src/Org.OpenAPITools/Api/FakeClassnameTags123Api.cs +++ b/samples/client/petstore/csharp/OpenAPIClient-generichost-netstandard2.0/src/Org.OpenAPITools/Api/FakeClassnameTags123Api.cs @@ -277,13 +277,12 @@ namespace Org.OpenAPITools.Api : httpRequestMessageLocalVar.Content = new StringContent(JsonSerializer.Serialize(modelClient, _jsonSerializerOptions)); List tokenBaseLocalVars = new List(); - ApiKeyToken apiKeyTokenLocalVar1 = (ApiKeyToken) await ApiKeyProvider.GetAsync(cancellationToken).ConfigureAwait(false); + ApiKeyToken apiKeyTokenLocalVar1 = (ApiKeyToken) await ApiKeyProvider.GetAsync("api_key_query", cancellationToken).ConfigureAwait(false); tokenBaseLocalVars.Add(apiKeyTokenLocalVar1); - apiKeyTokenLocalVar1.UseInQuery(httpRequestMessageLocalVar, uriBuilderLocalVar, parseQueryStringLocalVar, "api_key_query"); + apiKeyTokenLocalVar1.UseInQuery(httpRequestMessageLocalVar, uriBuilderLocalVar, parseQueryStringLocalVar); uriBuilderLocalVar.Query = parseQueryStringLocalVar.ToString(); - httpRequestMessageLocalVar.RequestUri = uriBuilderLocalVar.Uri; string[] contentTypes = new string[] { diff --git a/samples/client/petstore/csharp/OpenAPIClient-generichost-netstandard2.0/src/Org.OpenAPITools/Api/PetApi.cs b/samples/client/petstore/csharp/OpenAPIClient-generichost-netstandard2.0/src/Org.OpenAPITools/Api/PetApi.cs index 5b7c22813d..1913798bef 100644 --- a/samples/client/petstore/csharp/OpenAPIClient-generichost-netstandard2.0/src/Org.OpenAPITools/Api/PetApi.cs +++ b/samples/client/petstore/csharp/OpenAPIClient-generichost-netstandard2.0/src/Org.OpenAPITools/Api/PetApi.cs @@ -765,16 +765,15 @@ namespace Org.OpenAPITools.Api : httpRequestMessageLocalVar.Content = new StringContent(JsonSerializer.Serialize(pet, _jsonSerializerOptions)); List tokenBaseLocalVars = new List(); - httpRequestMessageLocalVar.RequestUri = uriBuilderLocalVar.Uri; - OAuthToken oauthTokenLocalVar1 = (OAuthToken) await OauthTokenProvider.GetAsync(cancellationToken).ConfigureAwait(false); + OAuthToken oauthTokenLocalVar1 = (OAuthToken) await OauthTokenProvider.GetAsync(cancellation: cancellationToken).ConfigureAwait(false); tokenBaseLocalVars.Add(oauthTokenLocalVar1); oauthTokenLocalVar1.UseInHeader(httpRequestMessageLocalVar, ""); - HttpSignatureToken httpSignatureTokenLocalVar2 = (HttpSignatureToken) await HttpSignatureTokenProvider.GetAsync(cancellationToken).ConfigureAwait(false); + HttpSignatureToken httpSignatureTokenLocalVar2 = (HttpSignatureToken) await HttpSignatureTokenProvider.GetAsync(cancellation: cancellationToken).ConfigureAwait(false); tokenBaseLocalVars.Add(httpSignatureTokenLocalVar2); @@ -983,10 +982,9 @@ namespace Org.OpenAPITools.Api httpRequestMessageLocalVar.Headers.Add("api_key", ClientUtils.ParameterToString(apiKey.Value)); List tokenBaseLocalVars = new List(); - httpRequestMessageLocalVar.RequestUri = uriBuilderLocalVar.Uri; - OAuthToken oauthTokenLocalVar1 = (OAuthToken) await OauthTokenProvider.GetAsync(cancellationToken).ConfigureAwait(false); + OAuthToken oauthTokenLocalVar1 = (OAuthToken) await OauthTokenProvider.GetAsync(cancellation: cancellationToken).ConfigureAwait(false); tokenBaseLocalVars.Add(oauthTokenLocalVar1); @@ -1176,16 +1174,15 @@ namespace Org.OpenAPITools.Api uriBuilderLocalVar.Query = parseQueryStringLocalVar.ToString(); List tokenBaseLocalVars = new List(); - httpRequestMessageLocalVar.RequestUri = uriBuilderLocalVar.Uri; - OAuthToken oauthTokenLocalVar1 = (OAuthToken) await OauthTokenProvider.GetAsync(cancellationToken).ConfigureAwait(false); + OAuthToken oauthTokenLocalVar1 = (OAuthToken) await OauthTokenProvider.GetAsync(cancellation: cancellationToken).ConfigureAwait(false); tokenBaseLocalVars.Add(oauthTokenLocalVar1); oauthTokenLocalVar1.UseInHeader(httpRequestMessageLocalVar, ""); - HttpSignatureToken httpSignatureTokenLocalVar2 = (HttpSignatureToken) await HttpSignatureTokenProvider.GetAsync(cancellationToken).ConfigureAwait(false); + HttpSignatureToken httpSignatureTokenLocalVar2 = (HttpSignatureToken) await HttpSignatureTokenProvider.GetAsync(cancellation: cancellationToken).ConfigureAwait(false); tokenBaseLocalVars.Add(httpSignatureTokenLocalVar2); @@ -1427,16 +1424,15 @@ namespace Org.OpenAPITools.Api uriBuilderLocalVar.Query = parseQueryStringLocalVar.ToString(); List tokenBaseLocalVars = new List(); - httpRequestMessageLocalVar.RequestUri = uriBuilderLocalVar.Uri; - OAuthToken oauthTokenLocalVar1 = (OAuthToken) await OauthTokenProvider.GetAsync(cancellationToken).ConfigureAwait(false); + OAuthToken oauthTokenLocalVar1 = (OAuthToken) await OauthTokenProvider.GetAsync(cancellation: cancellationToken).ConfigureAwait(false); tokenBaseLocalVars.Add(oauthTokenLocalVar1); oauthTokenLocalVar1.UseInHeader(httpRequestMessageLocalVar, ""); - HttpSignatureToken httpSignatureTokenLocalVar2 = (HttpSignatureToken) await HttpSignatureTokenProvider.GetAsync(cancellationToken).ConfigureAwait(false); + HttpSignatureToken httpSignatureTokenLocalVar2 = (HttpSignatureToken) await HttpSignatureTokenProvider.GetAsync(cancellation: cancellationToken).ConfigureAwait(false); tokenBaseLocalVars.Add(httpSignatureTokenLocalVar2); @@ -1662,17 +1658,16 @@ namespace Org.OpenAPITools.Api System.Collections.Specialized.NameValueCollection parseQueryStringLocalVar = System.Web.HttpUtility.ParseQueryString(string.Empty); List tokenBaseLocalVars = new List(); - ApiKeyToken apiKeyTokenLocalVar1 = (ApiKeyToken) await ApiKeyProvider.GetAsync(cancellationToken).ConfigureAwait(false); + ApiKeyToken apiKeyTokenLocalVar1 = (ApiKeyToken) await ApiKeyProvider.GetAsync("api_key", cancellationToken).ConfigureAwait(false); tokenBaseLocalVars.Add(apiKeyTokenLocalVar1); - apiKeyTokenLocalVar1.UseInHeader(httpRequestMessageLocalVar, "api_key"); + apiKeyTokenLocalVar1.UseInHeader(httpRequestMessageLocalVar); - ApiKeyToken apiKeyTokenLocalVar2 = (ApiKeyToken) await ApiKeyProvider.GetAsync(cancellationToken).ConfigureAwait(false); + ApiKeyToken apiKeyTokenLocalVar2 = (ApiKeyToken) await ApiKeyProvider.GetAsync("api_key_query", cancellationToken).ConfigureAwait(false); tokenBaseLocalVars.Add(apiKeyTokenLocalVar2); - apiKeyTokenLocalVar2.UseInQuery(httpRequestMessageLocalVar, uriBuilderLocalVar, parseQueryStringLocalVar, "api_key_query"); + apiKeyTokenLocalVar2.UseInQuery(httpRequestMessageLocalVar, uriBuilderLocalVar, parseQueryStringLocalVar); uriBuilderLocalVar.Query = parseQueryStringLocalVar.ToString(); - httpRequestMessageLocalVar.RequestUri = uriBuilderLocalVar.Uri; string[] acceptLocalVars = new string[] { @@ -1911,16 +1906,15 @@ namespace Org.OpenAPITools.Api : httpRequestMessageLocalVar.Content = new StringContent(JsonSerializer.Serialize(pet, _jsonSerializerOptions)); List tokenBaseLocalVars = new List(); - httpRequestMessageLocalVar.RequestUri = uriBuilderLocalVar.Uri; - OAuthToken oauthTokenLocalVar1 = (OAuthToken) await OauthTokenProvider.GetAsync(cancellationToken).ConfigureAwait(false); + OAuthToken oauthTokenLocalVar1 = (OAuthToken) await OauthTokenProvider.GetAsync(cancellation: cancellationToken).ConfigureAwait(false); tokenBaseLocalVars.Add(oauthTokenLocalVar1); oauthTokenLocalVar1.UseInHeader(httpRequestMessageLocalVar, ""); - HttpSignatureToken httpSignatureTokenLocalVar2 = (HttpSignatureToken) await HttpSignatureTokenProvider.GetAsync(cancellationToken).ConfigureAwait(false); + HttpSignatureToken httpSignatureTokenLocalVar2 = (HttpSignatureToken) await HttpSignatureTokenProvider.GetAsync(cancellation: cancellationToken).ConfigureAwait(false); tokenBaseLocalVars.Add(httpSignatureTokenLocalVar2); @@ -2160,10 +2154,9 @@ namespace Org.OpenAPITools.Api formParameterLocalVars.Add(new KeyValuePair("status", ClientUtils.ParameterToString(status.Value))); List tokenBaseLocalVars = new List(); - httpRequestMessageLocalVar.RequestUri = uriBuilderLocalVar.Uri; - OAuthToken oauthTokenLocalVar1 = (OAuthToken) await OauthTokenProvider.GetAsync(cancellationToken).ConfigureAwait(false); + OAuthToken oauthTokenLocalVar1 = (OAuthToken) await OauthTokenProvider.GetAsync(cancellation: cancellationToken).ConfigureAwait(false); tokenBaseLocalVars.Add(oauthTokenLocalVar1); @@ -2386,10 +2379,9 @@ namespace Org.OpenAPITools.Api formParameterLocalVars.Add(new KeyValuePair("additionalMetadata", ClientUtils.ParameterToString(additionalMetadata.Value))); List tokenBaseLocalVars = new List(); - httpRequestMessageLocalVar.RequestUri = uriBuilderLocalVar.Uri; - OAuthToken oauthTokenLocalVar1 = (OAuthToken) await OauthTokenProvider.GetAsync(cancellationToken).ConfigureAwait(false); + OAuthToken oauthTokenLocalVar1 = (OAuthToken) await OauthTokenProvider.GetAsync(cancellation: cancellationToken).ConfigureAwait(false); tokenBaseLocalVars.Add(oauthTokenLocalVar1); @@ -2651,10 +2643,9 @@ namespace Org.OpenAPITools.Api formParameterLocalVars.Add(new KeyValuePair("additionalMetadata", ClientUtils.ParameterToString(additionalMetadata.Value))); List tokenBaseLocalVars = new List(); - httpRequestMessageLocalVar.RequestUri = uriBuilderLocalVar.Uri; - OAuthToken oauthTokenLocalVar1 = (OAuthToken) await OauthTokenProvider.GetAsync(cancellationToken).ConfigureAwait(false); + OAuthToken oauthTokenLocalVar1 = (OAuthToken) await OauthTokenProvider.GetAsync(cancellation: cancellationToken).ConfigureAwait(false); tokenBaseLocalVars.Add(oauthTokenLocalVar1); diff --git a/samples/client/petstore/csharp/OpenAPIClient-generichost-netstandard2.0/src/Org.OpenAPITools/Api/StoreApi.cs b/samples/client/petstore/csharp/OpenAPIClient-generichost-netstandard2.0/src/Org.OpenAPITools/Api/StoreApi.cs index ad4eec35cd..6615881a36 100644 --- a/samples/client/petstore/csharp/OpenAPIClient-generichost-netstandard2.0/src/Org.OpenAPITools/Api/StoreApi.cs +++ b/samples/client/petstore/csharp/OpenAPIClient-generichost-netstandard2.0/src/Org.OpenAPITools/Api/StoreApi.cs @@ -617,9 +617,9 @@ namespace Org.OpenAPITools.Api uriBuilderLocalVar.Path = ClientUtils.CONTEXT_PATH + "/store/inventory"; List tokenBaseLocalVars = new List(); - ApiKeyToken apiKeyTokenLocalVar1 = (ApiKeyToken) await ApiKeyProvider.GetAsync(cancellationToken).ConfigureAwait(false); + ApiKeyToken apiKeyTokenLocalVar1 = (ApiKeyToken) await ApiKeyProvider.GetAsync("api_key", cancellationToken).ConfigureAwait(false); tokenBaseLocalVars.Add(apiKeyTokenLocalVar1); - apiKeyTokenLocalVar1.UseInHeader(httpRequestMessageLocalVar, "api_key"); + apiKeyTokenLocalVar1.UseInHeader(httpRequestMessageLocalVar); httpRequestMessageLocalVar.RequestUri = uriBuilderLocalVar.Uri; diff --git a/samples/client/petstore/csharp/OpenAPIClient-generichost-netstandard2.0/src/Org.OpenAPITools/Client/ApiKeyToken.cs b/samples/client/petstore/csharp/OpenAPIClient-generichost-netstandard2.0/src/Org.OpenAPITools/Client/ApiKeyToken.cs index 5cc9c25492..541961ff98 100644 --- a/samples/client/petstore/csharp/OpenAPIClient-generichost-netstandard2.0/src/Org.OpenAPITools/Client/ApiKeyToken.cs +++ b/samples/client/petstore/csharp/OpenAPIClient-generichost-netstandard2.0/src/Org.OpenAPITools/Client/ApiKeyToken.cs @@ -11,14 +11,21 @@ namespace Org.OpenAPITools.Client { private string _raw; + /// + /// The header that this token will be used with. + /// + public string Header { get; } + /// /// Constructs an ApiKeyToken object. /// /// + /// /// /// - public ApiKeyToken(string value, string prefix = "Bearer ", TimeSpan? timeout = null) : base(timeout) + public ApiKeyToken(string value, string header, string prefix = "Bearer ", TimeSpan? timeout = null) : base(timeout) { + Header = header; _raw = $"{ prefix }{ value }"; } @@ -26,22 +33,20 @@ namespace Org.OpenAPITools.Client /// Places the token in the header. /// /// - /// - public virtual void UseInHeader(System.Net.Http.HttpRequestMessage request, string headerName) + public virtual void UseInHeader(System.Net.Http.HttpRequestMessage request) { - request.Headers.Add(headerName, _raw); + request.Headers.Add(Header, _raw); } - + /// /// Places the token in the query. /// /// /// /// - /// - public virtual void UseInQuery(System.Net.Http.HttpRequestMessage request, UriBuilder uriBuilder, System.Collections.Specialized.NameValueCollection parseQueryString, string parameterName) + public virtual void UseInQuery(System.Net.Http.HttpRequestMessage request, UriBuilder uriBuilder, System.Collections.Specialized.NameValueCollection parseQueryString) { - parseQueryString[parameterName] = Uri.EscapeDataString(_raw).ToString(); + parseQueryString[Header] = Uri.EscapeDataString(_raw).ToString(); } } } \ No newline at end of file diff --git a/samples/client/petstore/csharp/OpenAPIClient-generichost-netstandard2.0/src/Org.OpenAPITools/Client/RateLimitProvider`1.cs b/samples/client/petstore/csharp/OpenAPIClient-generichost-netstandard2.0/src/Org.OpenAPITools/Client/RateLimitProvider`1.cs index 9e28ab28ee..a61dd1027f 100644 --- a/samples/client/petstore/csharp/OpenAPIClient-generichost-netstandard2.0/src/Org.OpenAPITools/Client/RateLimitProvider`1.cs +++ b/samples/client/petstore/csharp/OpenAPIClient-generichost-netstandard2.0/src/Org.OpenAPITools/Client/RateLimitProvider`1.cs @@ -9,12 +9,11 @@ */ using System; -using System.Collections.Concurrent; +using System.Collections.Generic; using System.Linq; -using System.Threading; -using System.Threading.Tasks; +using System.Threading.Channels; -namespace Org.OpenAPITools.Client +namespace Org.OpenAPITools.Client { /// /// Provides a token to the api clients. Tokens will be rate limited based on the provided TimeSpan. @@ -22,8 +21,7 @@ namespace Org.OpenAPITools.Client /// public class RateLimitProvider : TokenProvider where TTokenBase : TokenBase { - internal ConcurrentDictionary AvailableTokens = new ConcurrentDictionary(); - private SemaphoreSlim _semaphore; + internal Dictionary> AvailableTokens { get; } = new Dictionary>(); /// /// Instantiates a ThrottledTokenProvider. Your tokens will be rate limited based on the token's timeout. @@ -31,48 +29,44 @@ namespace Org.OpenAPITools.Client /// public RateLimitProvider(TokenContainer container) : base(container.Tokens) { - _semaphore = new SemaphoreSlim(1, 1); - foreach(TTokenBase token in _tokens) token.StartTimer(token.Timeout ?? TimeSpan.FromMilliseconds(40)); - for (int i = 0; i < _tokens.Length; i++) + if (container is TokenContainer apiKeyTokenContainer) { - _tokens[i].TokenBecameAvailable += ((sender) => - { - TTokenBase token = (TTokenBase)sender; + string[] headers = apiKeyTokenContainer.Tokens.Select(t => t.Header).Distinct().ToArray(); - AvailableTokens.TryAdd(token, token); - }); + foreach (string header in headers) + { + BoundedChannelOptions options = new BoundedChannelOptions(apiKeyTokenContainer.Tokens.Count(t => t.Header.Equals(header))) + { + FullMode = BoundedChannelFullMode.DropWrite + }; + + AvailableTokens.Add(header, Channel.CreateBounded(options)); + } } + else + { + BoundedChannelOptions options = new BoundedChannelOptions(_tokens.Length) + { + FullMode = BoundedChannelFullMode.DropWrite + }; + + AvailableTokens.Add(string.Empty, Channel.CreateBounded(options)); + } + + foreach(Channel tokens in AvailableTokens.Values) + for (int i = 0; i < _tokens.Length; i++) + _tokens[i].TokenBecameAvailable += ((sender) => tokens.Writer.TryWrite((TTokenBase) sender)); } - internal override async System.Threading.Tasks.ValueTask GetAsync(System.Threading.CancellationToken cancellation = default) + internal override async System.Threading.Tasks.ValueTask GetAsync(string header = "", System.Threading.CancellationToken cancellation = default) { - await _semaphore.WaitAsync().ConfigureAwait(false); + if (!AvailableTokens.TryGetValue(header, out Channel tokens)) + throw new KeyNotFoundException($"Could not locate a token for header '{header}'."); - try - { - TTokenBase result = null; - - while (result == null) - { - TTokenBase tokenToRemove = AvailableTokens.FirstOrDefault().Value; - - if (tokenToRemove != null && AvailableTokens.TryRemove(tokenToRemove, out result)) - return result; - - await Task.Delay(40).ConfigureAwait(false); - - tokenToRemove = AvailableTokens.FirstOrDefault().Value; - } - - return result; - } - finally - { - _semaphore.Release(); - } + return await tokens.Reader.ReadAsync(cancellation).ConfigureAwait(false); } } } diff --git a/samples/client/petstore/csharp/OpenAPIClient-generichost-netstandard2.0/src/Org.OpenAPITools/Client/TokenProvider`1.cs b/samples/client/petstore/csharp/OpenAPIClient-generichost-netstandard2.0/src/Org.OpenAPITools/Client/TokenProvider`1.cs index c49f723f8c..36d7dad03e 100644 --- a/samples/client/petstore/csharp/OpenAPIClient-generichost-netstandard2.0/src/Org.OpenAPITools/Client/TokenProvider`1.cs +++ b/samples/client/petstore/csharp/OpenAPIClient-generichost-netstandard2.0/src/Org.OpenAPITools/Client/TokenProvider`1.cs @@ -25,7 +25,7 @@ namespace Org.OpenAPITools /// protected TTokenBase[] _tokens; - internal abstract System.Threading.Tasks.ValueTask GetAsync(System.Threading.CancellationToken cancellation = default); + internal abstract System.Threading.Tasks.ValueTask GetAsync(string header = "", System.Threading.CancellationToken cancellation = default); /// /// Instantiates a TokenProvider. diff --git a/samples/client/petstore/csharp/OpenAPIClient-generichost-netstandard2.0/src/Org.OpenAPITools/Org - Backup.OpenAPITools.csproj b/samples/client/petstore/csharp/OpenAPIClient-generichost-netstandard2.0/src/Org.OpenAPITools/Org - Backup.OpenAPITools.csproj new file mode 100644 index 0000000000..ab2ce35e26 --- /dev/null +++ b/samples/client/petstore/csharp/OpenAPIClient-generichost-netstandard2.0/src/Org.OpenAPITools/Org - Backup.OpenAPITools.csproj @@ -0,0 +1,31 @@ + + + + true + netstandard2.0 + Org.OpenAPITools + Org.OpenAPITools + Library + OpenAPI + OpenAPI + OpenAPI Library + A library generated from a OpenAPI doc + No Copyright + Org.OpenAPITools + 1.0.0 + bin\$(Configuration)\$(TargetFramework)\Org.OpenAPITools.xml + https://github.com/GIT_USER_ID/GIT_REPO_ID.git + git + Minor update + + + + + + + + + + + + diff --git a/samples/client/petstore/csharp/OpenAPIClient-generichost-netstandard2.0/src/Org.OpenAPITools/Org.OpenAPITools.csproj b/samples/client/petstore/csharp/OpenAPIClient-generichost-netstandard2.0/src/Org.OpenAPITools/Org.OpenAPITools.csproj index aae556ae25..ab2ce35e26 100644 --- a/samples/client/petstore/csharp/OpenAPIClient-generichost-netstandard2.0/src/Org.OpenAPITools/Org.OpenAPITools.csproj +++ b/samples/client/petstore/csharp/OpenAPIClient-generichost-netstandard2.0/src/Org.OpenAPITools/Org.OpenAPITools.csproj @@ -24,6 +24,7 @@ +