[csharp][generichost] Added enum for api key names (#17295)

* added enum

* added enum
This commit is contained in:
devhl-labs
2023-12-03 21:44:12 -05:00
committed by GitHub
parent 95340d5264
commit 2967b33ab6
33 changed files with 294 additions and 153 deletions

View File

@@ -18,7 +18,7 @@ namespace {{packageName}}.{{clientPackage}}
/// <summary>
/// The header that this token will be used with.
/// </summary>
public string Header { get; }
public ClientUtils.ApiKeyHeader Header { get; }
/// <summary>
/// Constructs an ApiKeyToken object.
@@ -27,7 +27,7 @@ namespace {{packageName}}.{{clientPackage}}
/// <param name="header"></param>
/// <param name="prefix"></param>
/// <param name="timeout"></param>
public ApiKeyToken(string value, string header, string prefix = "Bearer ", TimeSpan? timeout = null) : base(timeout)
public ApiKeyToken(string value, ClientUtils.ApiKeyHeader header, string prefix = "Bearer ", TimeSpan? timeout = null) : base(timeout)
{
Header = header;
_raw = $"{ prefix }{ value }";
@@ -39,7 +39,7 @@ namespace {{packageName}}.{{clientPackage}}
/// <param name="request"></param>
public virtual void UseInHeader(System.Net.Http.HttpRequestMessage request)
{
request.Headers.Add(Header, _raw);
request.Headers.Add(ClientUtils.ApiKeyHeaderToString(Header), _raw);
}
/// <summary>
@@ -50,7 +50,7 @@ namespace {{packageName}}.{{clientPackage}}
/// <param name="parseQueryString"></param>
public virtual void UseInQuery(System.Net.Http.HttpRequestMessage request, UriBuilder uriBuilder, System.Collections.Specialized.NameValueCollection parseQueryString)
{
parseQueryString[Header] = Uri.EscapeDataString(_raw).ToString(){{nrt!}};
parseQueryString[ClientUtils.ApiKeyHeaderToString(Header)] = Uri.EscapeDataString(_raw).ToString(){{nrt!}};
}
}
}

View File

@@ -31,7 +31,7 @@ namespace {{packageName}}.Test.{{apiPackage}}
{{#lambda.trimTrailingWithNewLine}}
{{#apiKeyMethods}}
string apiKeyTokenValue{{-index}} = context.Configuration["<token>"] ?? throw new Exception("Token not found.");
ApiKeyToken apiKeyToken{{-index}} = new{{^net70OrLater}} ApiKeyToken{{/net70OrLater}}(apiKeyTokenValue{{-index}}, "{{name}}", timeout: TimeSpan.FromSeconds(1));
ApiKeyToken apiKeyToken{{-index}} = new{{^net70OrLater}} ApiKeyToken{{/net70OrLater}}(apiKeyTokenValue{{-index}}, ClientUtils.ApiKeyHeader.{{#lambda.titlecase}}{{name}}{{/lambda.titlecase}}, timeout: TimeSpan.FromSeconds(1));
options.AddTokens(apiKeyToken{{-index}});
{{/apiKeyMethods}}

View File

@@ -50,6 +50,51 @@ using System.Runtime.CompilerServices;
/// <returns></returns>
public delegate void EventHandler<T>(object sender, T e) where T : EventArgs;
{{#hasApiKeyMethods}}
/// <summary>
/// An enum of headers
/// </summary>
public enum ApiKeyHeader
{
{{#apiKeyMethods}}
/// <summary>
/// The {{keyParamName}} header
/// </summary>
{{#lambda.titlecase}}{{keyParamName}}{{/lambda.titlecase}}{{^-last}},{{/-last}}
{{/apiKeyMethods}}
}
/// <summary>
/// Converte an ApiKeyHeader to a string
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
/// <exception cref="System.ComponentModel.InvalidEnumArgumentException"></exception>
{{>visibility}} static string ApiKeyHeaderToString(ApiKeyHeader value)
{
{{#net80OrLater}}
return value switch
{
{{#apiKeyMethods}}
ApiKeyHeader.{{#lambda.titlecase}}{{keyParamName}}{{/lambda.titlecase}} => "{{keyParamName}}",
{{/apiKeyMethods}}
_ => throw new System.ComponentModel.InvalidEnumArgumentException(nameof(value), (int)value, typeof(ApiKeyHeader)),
};
{{/net80OrLater}}
{{^net80OrLater}}
switch(value)
{
{{#apiKeyMethods}}
case ApiKeyHeader.{{#lambda.titlecase}}{{keyParamName}}{{/lambda.titlecase}}:
return "{{keyParamName}}";
{{/apiKeyMethods}}
default:
throw new System.ComponentModel.InvalidEnumArgumentException(nameof(value), (int)value, typeof(ApiKeyHeader));
}
{{/net80OrLater}}
}
{{/hasApiKeyMethods}}
/// <summary>
/// Returns true when deserialization succeeds.
/// </summary>

View File

@@ -21,12 +21,12 @@ namespace {{packageName}}.Test.{{apiPackage}}
{
{{#lambda.trimTrailingWithNewLine}}
{{#apiKeyMethods}}
ApiKeyToken apiKeyToken{{-index}} = new{{^net70OrLater}} ApiKeyToken{{/net70OrLater}}($"<token>", "{{name}}", timeout: TimeSpan.FromSeconds(1));
ApiKeyToken apiKeyToken{{-index}} = new{{^net70OrLater}} ApiKeyToken{{/net70OrLater}}("<token>", ClientUtils.ApiKeyHeader.{{#lambda.titlecase}}{{name}}{{/lambda.titlecase}}, timeout: TimeSpan.FromSeconds(1));
options.AddTokens(apiKeyToken{{-index}});
{{/apiKeyMethods}}
{{#httpBearerMethods}}
BearerToken bearerToken{{-index}} = new{{^net70OrLater}} BearerToken{{/net70OrLater}}($"<token>", timeout: TimeSpan.FromSeconds(1));
BearerToken bearerToken{{-index}} = new{{^net70OrLater}} BearerToken{{/net70OrLater}}("<token>", timeout: TimeSpan.FromSeconds(1));
options.AddTokens(bearerToken{{-index}});
{{/httpBearerMethods}}
@@ -55,12 +55,12 @@ namespace {{packageName}}.Test.{{apiPackage}}
{
{{#lambda.trimTrailingWithNewLine}}
{{#apiKeyMethods}}
ApiKeyToken apiKeyToken{{-index}} = new{{^net70OrLater}} ApiKeyToken{{/net70OrLater}}($"<token>", "{{name}}", timeout: TimeSpan.FromSeconds(1));
ApiKeyToken apiKeyToken{{-index}} = new{{^net70OrLater}} ApiKeyToken{{/net70OrLater}}("<token>", ClientUtils.ApiKeyHeader.{{#lambda.titlecase}}{{name}}{{/lambda.titlecase}}, timeout: TimeSpan.FromSeconds(1));
options.AddTokens(apiKeyToken{{-index}});
{{/apiKeyMethods}}
{{#httpBearerMethods}}
BearerToken bearerToken{{-index}} = new{{^net70OrLater}} BearerToken{{/net70OrLater}}($"<token>", timeout: TimeSpan.FromSeconds(1));
BearerToken bearerToken{{-index}} = new{{^net70OrLater}} BearerToken{{/net70OrLater}}("<token>", timeout: TimeSpan.FromSeconds(1));
options.AddTokens(bearerToken{{-index}});
{{/httpBearerMethods}}
@@ -92,12 +92,12 @@ namespace {{packageName}}.Test.{{apiPackage}}
{
{{#lambda.trimTrailingWithNewLine}}
{{#apiKeyMethods}}
ApiKeyToken apiKeyToken{{-index}} = new{{^net70OrLater}} ApiKeyToken{{/net70OrLater}}($"<token>", "{{name}}", timeout: TimeSpan.FromSeconds(1));
ApiKeyToken apiKeyToken{{-index}} = new{{^net70OrLater}} ApiKeyToken{{/net70OrLater}}("<token>", ClientUtils.ApiKeyHeader.{{#lambda.titlecase}}{{name}}{{/lambda.titlecase}}, timeout: TimeSpan.FromSeconds(1));
options.AddTokens(apiKeyToken{{-index}});
{{/apiKeyMethods}}
{{#httpBearerMethods}}
BearerToken bearerToken{{-index}} = new{{^net70OrLater}} BearerToken{{/net70OrLater}}($"<token>", timeout: TimeSpan.FromSeconds(1));
BearerToken bearerToken{{-index}} = new{{^net70OrLater}} BearerToken{{/net70OrLater}}("<token>", timeout: TimeSpan.FromSeconds(1));
options.AddTokens(bearerToken{{-index}});
{{/httpBearerMethods}}
@@ -129,12 +129,12 @@ namespace {{packageName}}.Test.{{apiPackage}}
{
{{#lambda.trimTrailingWithNewLine}}
{{#apiKeyMethods}}
ApiKeyToken apiKeyToken{{-index}} = new{{^net70OrLater}} ApiKeyToken{{/net70OrLater}}($"<token>", "{{name}}", timeout: TimeSpan.FromSeconds(1));
ApiKeyToken apiKeyToken{{-index}} = new{{^net70OrLater}} ApiKeyToken{{/net70OrLater}}("<token>", ClientUtils.ApiKeyHeader.{{#lambda.titlecase}}{{name}}{{/lambda.titlecase}}, timeout: TimeSpan.FromSeconds(1));
options.AddTokens(apiKeyToken{{-index}});
{{/apiKeyMethods}}
{{#httpBearerMethods}}
BearerToken bearerToken{{-index}} = new{{^net70OrLater}} BearerToken{{/net70OrLater}}($"<token>", timeout: TimeSpan.FromSeconds(1));
BearerToken bearerToken{{-index}} = new{{^net70OrLater}} BearerToken{{/net70OrLater}}("<token>", timeout: TimeSpan.FromSeconds(1));
options.AddTokens(bearerToken{{-index}});
{{/httpBearerMethods}}

View File

@@ -81,7 +81,7 @@ namespace YourProject
.Configure{{apiName}}((context, options) =>
{
{{#authMethods}}// the type of token here depends on the api security specifications
ApiKeyToken token = new("<your token>");
ApiKeyToken token = new("<your token>", ClientUtils.ApiKeyHeader.{{#lambda.titlecase}}{{name}}{{/lambda.titlecase}});
options.AddTokens(token);
// optionally choose the method the tokens will be provided with, default is RateLimitProvider

View File

@@ -44,11 +44,11 @@ namespace {{packageName}}.{{clientPackage}}
{{#hasApiKeyMethods}}
if (container is TokenContainer<ApiKeyToken> apiKeyTokenContainer)
{
string[] headers = apiKeyTokenContainer.Tokens.Select(t => t.Header).Distinct().ToArray();
string[] headers = apiKeyTokenContainer.Tokens.Select(t => ClientUtils.ApiKeyHeaderToString(t.Header)).Distinct().ToArray();
foreach (string header in headers)
{
BoundedChannelOptions options = new BoundedChannelOptions(apiKeyTokenContainer.Tokens.Count(t => t.Header.Equals(header)))
BoundedChannelOptions options = new BoundedChannelOptions(apiKeyTokenContainer.Tokens.Count(t => ClientUtils.ApiKeyHeaderToString(t.Header).Equals(header)))
{
FullMode = BoundedChannelFullMode.DropWrite
};

View File

@@ -22,7 +22,7 @@ public class UnitTest1
IHostBuilder hostBuild = Host.CreateDefaultBuilder(Array.Empty<string>()).ConfigureApi((context, services, options) =>
{
string apiKeyTokenValue = context.Configuration["<token>"] ?? "Token not found.";
ApiKeyToken apiKeyToken = new(apiKeyTokenValue, "session", timeout: TimeSpan.FromSeconds(1));
ApiKeyToken apiKeyToken = new(apiKeyTokenValue, ClientUtils.ApiKeyHeader.Api_key, timeout: TimeSpan.FromSeconds(1));
options.AddTokens(apiKeyToken);
string bearerTokenValue = context.Configuration["<token>"] ?? "Token not found.";

View File

@@ -18,7 +18,7 @@ namespace OpenAPIClient_generichost_manual_tests
IHostBuilder hostBuild = Host.CreateDefaultBuilder(Array.Empty<string>()).ConfigureApi((context, services, options) =>
{
string apiKeyTokenValue = context.Configuration["<token>"] ?? "Token not found.";
ApiKeyToken apiKeyToken = new(apiKeyTokenValue, "session", timeout: TimeSpan.FromSeconds(1));
ApiKeyToken apiKeyToken = new(apiKeyTokenValue, ClientUtils.ApiKeyHeader.Api_key, timeout: TimeSpan.FromSeconds(1));
options.AddTokens(apiKeyToken);
string bearerTokenValue = context.Configuration["<token>"] ?? "Token not found.";

View File

@@ -53,11 +53,11 @@ namespace UseSourceGeneration.Test.Api
.ConfigureApi((context, services, options) =>
{
string apiKeyTokenValue1 = context.Configuration["<token>"] ?? throw new Exception("Token not found.");
ApiKeyToken apiKeyToken1 = new(apiKeyTokenValue1, "api_key", timeout: TimeSpan.FromSeconds(1));
ApiKeyToken apiKeyToken1 = new(apiKeyTokenValue1, ClientUtils.ApiKeyHeader.Api_key, timeout: TimeSpan.FromSeconds(1));
options.AddTokens(apiKeyToken1);
string apiKeyTokenValue2 = context.Configuration["<token>"] ?? throw new Exception("Token not found.");
ApiKeyToken apiKeyToken2 = new(apiKeyTokenValue2, "api_key_query", timeout: TimeSpan.FromSeconds(1));
ApiKeyToken apiKeyToken2 = new(apiKeyTokenValue2, ClientUtils.ApiKeyHeader.Api_key_query, timeout: TimeSpan.FromSeconds(1));
options.AddTokens(apiKeyToken2);
string bearerTokenValue1 = context.Configuration["<token>"] ?? throw new Exception("Token not found.");

View File

@@ -27,13 +27,13 @@ namespace UseSourceGeneration.Test.Api
private readonly IHost _hostUsingConfigureWithoutAClient =
Host.CreateDefaultBuilder([]).ConfigureApi((context, services, options) =>
{
ApiKeyToken apiKeyToken1 = new($"<token>", "api_key", timeout: TimeSpan.FromSeconds(1));
ApiKeyToken apiKeyToken1 = new("<token>", ClientUtils.ApiKeyHeader.Api_key, timeout: TimeSpan.FromSeconds(1));
options.AddTokens(apiKeyToken1);
ApiKeyToken apiKeyToken2 = new($"<token>", "api_key_query", timeout: TimeSpan.FromSeconds(1));
ApiKeyToken apiKeyToken2 = new("<token>", ClientUtils.ApiKeyHeader.Api_key_query, timeout: TimeSpan.FromSeconds(1));
options.AddTokens(apiKeyToken2);
BearerToken bearerToken1 = new($"<token>", timeout: TimeSpan.FromSeconds(1));
BearerToken bearerToken1 = new("<token>", timeout: TimeSpan.FromSeconds(1));
options.AddTokens(bearerToken1);
BasicToken basicToken1 = new("<username>", "<password>", timeout: TimeSpan.FromSeconds(1));
@@ -51,13 +51,13 @@ namespace UseSourceGeneration.Test.Api
private readonly IHost _hostUsingConfigureWithAClient =
Host.CreateDefaultBuilder([]).ConfigureApi((context, services, options) =>
{
ApiKeyToken apiKeyToken1 = new($"<token>", "api_key", timeout: TimeSpan.FromSeconds(1));
ApiKeyToken apiKeyToken1 = new("<token>", ClientUtils.ApiKeyHeader.Api_key, timeout: TimeSpan.FromSeconds(1));
options.AddTokens(apiKeyToken1);
ApiKeyToken apiKeyToken2 = new($"<token>", "api_key_query", timeout: TimeSpan.FromSeconds(1));
ApiKeyToken apiKeyToken2 = new("<token>", ClientUtils.ApiKeyHeader.Api_key_query, timeout: TimeSpan.FromSeconds(1));
options.AddTokens(apiKeyToken2);
BearerToken bearerToken1 = new($"<token>", timeout: TimeSpan.FromSeconds(1));
BearerToken bearerToken1 = new("<token>", timeout: TimeSpan.FromSeconds(1));
options.AddTokens(bearerToken1);
BasicToken basicToken1 = new("<username>", "<password>", timeout: TimeSpan.FromSeconds(1));
@@ -78,13 +78,13 @@ namespace UseSourceGeneration.Test.Api
{
services.AddApi(options =>
{
ApiKeyToken apiKeyToken1 = new($"<token>", "api_key", timeout: TimeSpan.FromSeconds(1));
ApiKeyToken apiKeyToken1 = new("<token>", ClientUtils.ApiKeyHeader.Api_key, timeout: TimeSpan.FromSeconds(1));
options.AddTokens(apiKeyToken1);
ApiKeyToken apiKeyToken2 = new($"<token>", "api_key_query", timeout: TimeSpan.FromSeconds(1));
ApiKeyToken apiKeyToken2 = new("<token>", ClientUtils.ApiKeyHeader.Api_key_query, timeout: TimeSpan.FromSeconds(1));
options.AddTokens(apiKeyToken2);
BearerToken bearerToken1 = new($"<token>", timeout: TimeSpan.FromSeconds(1));
BearerToken bearerToken1 = new("<token>", timeout: TimeSpan.FromSeconds(1));
options.AddTokens(bearerToken1);
BasicToken basicToken1 = new("<username>", "<password>", timeout: TimeSpan.FromSeconds(1));
@@ -105,13 +105,13 @@ namespace UseSourceGeneration.Test.Api
{
services.AddApi(options =>
{
ApiKeyToken apiKeyToken1 = new($"<token>", "api_key", timeout: TimeSpan.FromSeconds(1));
ApiKeyToken apiKeyToken1 = new("<token>", ClientUtils.ApiKeyHeader.Api_key, timeout: TimeSpan.FromSeconds(1));
options.AddTokens(apiKeyToken1);
ApiKeyToken apiKeyToken2 = new($"<token>", "api_key_query", timeout: TimeSpan.FromSeconds(1));
ApiKeyToken apiKeyToken2 = new("<token>", ClientUtils.ApiKeyHeader.Api_key_query, timeout: TimeSpan.FromSeconds(1));
options.AddTokens(apiKeyToken2);
BearerToken bearerToken1 = new($"<token>", timeout: TimeSpan.FromSeconds(1));
BearerToken bearerToken1 = new("<token>", timeout: TimeSpan.FromSeconds(1));
options.AddTokens(bearerToken1);
BasicToken basicToken1 = new("<username>", "<password>", timeout: TimeSpan.FromSeconds(1));

View File

@@ -16,7 +16,7 @@ namespace UseSourceGeneration.Client
/// <summary>
/// The header that this token will be used with.
/// </summary>
public string Header { get; }
public ClientUtils.ApiKeyHeader Header { get; }
/// <summary>
/// Constructs an ApiKeyToken object.
@@ -25,7 +25,7 @@ namespace UseSourceGeneration.Client
/// <param name="header"></param>
/// <param name="prefix"></param>
/// <param name="timeout"></param>
public ApiKeyToken(string value, string header, string prefix = "Bearer ", TimeSpan? timeout = null) : base(timeout)
public ApiKeyToken(string value, ClientUtils.ApiKeyHeader header, string prefix = "Bearer ", TimeSpan? timeout = null) : base(timeout)
{
Header = header;
_raw = $"{ prefix }{ value }";
@@ -37,7 +37,7 @@ namespace UseSourceGeneration.Client
/// <param name="request"></param>
public virtual void UseInHeader(System.Net.Http.HttpRequestMessage request)
{
request.Headers.Add(Header, _raw);
request.Headers.Add(ClientUtils.ApiKeyHeaderToString(Header), _raw);
}
/// <summary>
@@ -48,7 +48,7 @@ namespace UseSourceGeneration.Client
/// <param name="parseQueryString"></param>
public virtual void UseInQuery(System.Net.Http.HttpRequestMessage request, UriBuilder uriBuilder, System.Collections.Specialized.NameValueCollection parseQueryString)
{
parseQueryString[Header] = Uri.EscapeDataString(_raw).ToString()!;
parseQueryString[ClientUtils.ApiKeyHeaderToString(Header)] = Uri.EscapeDataString(_raw).ToString()!;
}
}
}

View File

@@ -54,6 +54,37 @@ namespace UseSourceGeneration.Client
/// <returns></returns>
public delegate void EventHandler<T>(object sender, T e) where T : EventArgs;
/// <summary>
/// An enum of headers
/// </summary>
public enum ApiKeyHeader
{
/// <summary>
/// The api_key header
/// </summary>
Api_key,
/// <summary>
/// The api_key_query header
/// </summary>
Api_key_query
}
/// <summary>
/// Converte an ApiKeyHeader to a string
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
/// <exception cref="System.ComponentModel.InvalidEnumArgumentException"></exception>
public static string ApiKeyHeaderToString(ApiKeyHeader value)
{
return value switch
{
ApiKeyHeader.Api_key => "api_key",
ApiKeyHeader.Api_key_query => "api_key_query",
_ => throw new System.ComponentModel.InvalidEnumArgumentException(nameof(value), (int)value, typeof(ApiKeyHeader)),
};
}
/// <summary>
/// Returns true when deserialization succeeds.
/// </summary>

View File

@@ -36,11 +36,11 @@ namespace UseSourceGeneration.Client
if (container is TokenContainer<ApiKeyToken> apiKeyTokenContainer)
{
string[] headers = apiKeyTokenContainer.Tokens.Select(t => t.Header).Distinct().ToArray();
string[] headers = apiKeyTokenContainer.Tokens.Select(t => ClientUtils.ApiKeyHeaderToString(t.Header)).Distinct().ToArray();
foreach (string header in headers)
{
BoundedChannelOptions options = new BoundedChannelOptions(apiKeyTokenContainer.Tokens.Count(t => t.Header.Equals(header)))
BoundedChannelOptions options = new BoundedChannelOptions(apiKeyTokenContainer.Tokens.Count(t => ClientUtils.ApiKeyHeaderToString(t.Header).Equals(header)))
{
FullMode = BoundedChannelFullMode.DropWrite
};

View File

@@ -69,42 +69,42 @@ namespace YourProject
.ConfigureApi((context, options) =>
{
// the type of token here depends on the api security specifications
ApiKeyToken token = new("<your token>");
ApiKeyToken token = new("<your token>", ClientUtils.ApiKeyHeader.Petstore_auth);
options.AddTokens(token);
// optionally choose the method the tokens will be provided with, default is RateLimitProvider
options.UseProvider<RateLimitProvider<ApiKeyToken>, ApiKeyToken>();
// the type of token here depends on the api security specifications
ApiKeyToken token = new("<your token>");
ApiKeyToken token = new("<your token>", ClientUtils.ApiKeyHeader.Api_key);
options.AddTokens(token);
// optionally choose the method the tokens will be provided with, default is RateLimitProvider
options.UseProvider<RateLimitProvider<ApiKeyToken>, ApiKeyToken>();
// the type of token here depends on the api security specifications
ApiKeyToken token = new("<your token>");
ApiKeyToken token = new("<your token>", ClientUtils.ApiKeyHeader.Api_key_query);
options.AddTokens(token);
// optionally choose the method the tokens will be provided with, default is RateLimitProvider
options.UseProvider<RateLimitProvider<ApiKeyToken>, ApiKeyToken>();
// the type of token here depends on the api security specifications
ApiKeyToken token = new("<your token>");
ApiKeyToken token = new("<your token>", ClientUtils.ApiKeyHeader.Http_basic_test);
options.AddTokens(token);
// optionally choose the method the tokens will be provided with, default is RateLimitProvider
options.UseProvider<RateLimitProvider<ApiKeyToken>, ApiKeyToken>();
// the type of token here depends on the api security specifications
ApiKeyToken token = new("<your token>");
ApiKeyToken token = new("<your token>", ClientUtils.ApiKeyHeader.Bearer_test);
options.AddTokens(token);
// optionally choose the method the tokens will be provided with, default is RateLimitProvider
options.UseProvider<RateLimitProvider<ApiKeyToken>, ApiKeyToken>();
// the type of token here depends on the api security specifications
ApiKeyToken token = new("<your token>");
ApiKeyToken token = new("<your token>", ClientUtils.ApiKeyHeader.Http_signature_test);
options.AddTokens(token);
// optionally choose the method the tokens will be provided with, default is RateLimitProvider

View File

@@ -53,11 +53,11 @@ namespace Org.OpenAPITools.Test.Api
.ConfigureApi((context, services, options) =>
{
string apiKeyTokenValue1 = context.Configuration["<token>"] ?? throw new Exception("Token not found.");
ApiKeyToken apiKeyToken1 = new(apiKeyTokenValue1, "api_key", timeout: TimeSpan.FromSeconds(1));
ApiKeyToken apiKeyToken1 = new(apiKeyTokenValue1, ClientUtils.ApiKeyHeader.Api_key, timeout: TimeSpan.FromSeconds(1));
options.AddTokens(apiKeyToken1);
string apiKeyTokenValue2 = context.Configuration["<token>"] ?? throw new Exception("Token not found.");
ApiKeyToken apiKeyToken2 = new(apiKeyTokenValue2, "api_key_query", timeout: TimeSpan.FromSeconds(1));
ApiKeyToken apiKeyToken2 = new(apiKeyTokenValue2, ClientUtils.ApiKeyHeader.Api_key_query, timeout: TimeSpan.FromSeconds(1));
options.AddTokens(apiKeyToken2);
string bearerTokenValue1 = context.Configuration["<token>"] ?? throw new Exception("Token not found.");

View File

@@ -27,13 +27,13 @@ namespace Org.OpenAPITools.Test.Api
private readonly IHost _hostUsingConfigureWithoutAClient =
Host.CreateDefaultBuilder([]).ConfigureApi((context, services, options) =>
{
ApiKeyToken apiKeyToken1 = new($"<token>", "api_key", timeout: TimeSpan.FromSeconds(1));
ApiKeyToken apiKeyToken1 = new("<token>", ClientUtils.ApiKeyHeader.Api_key, timeout: TimeSpan.FromSeconds(1));
options.AddTokens(apiKeyToken1);
ApiKeyToken apiKeyToken2 = new($"<token>", "api_key_query", timeout: TimeSpan.FromSeconds(1));
ApiKeyToken apiKeyToken2 = new("<token>", ClientUtils.ApiKeyHeader.Api_key_query, timeout: TimeSpan.FromSeconds(1));
options.AddTokens(apiKeyToken2);
BearerToken bearerToken1 = new($"<token>", timeout: TimeSpan.FromSeconds(1));
BearerToken bearerToken1 = new("<token>", timeout: TimeSpan.FromSeconds(1));
options.AddTokens(bearerToken1);
BasicToken basicToken1 = new("<username>", "<password>", timeout: TimeSpan.FromSeconds(1));
@@ -51,13 +51,13 @@ namespace Org.OpenAPITools.Test.Api
private readonly IHost _hostUsingConfigureWithAClient =
Host.CreateDefaultBuilder([]).ConfigureApi((context, services, options) =>
{
ApiKeyToken apiKeyToken1 = new($"<token>", "api_key", timeout: TimeSpan.FromSeconds(1));
ApiKeyToken apiKeyToken1 = new("<token>", ClientUtils.ApiKeyHeader.Api_key, timeout: TimeSpan.FromSeconds(1));
options.AddTokens(apiKeyToken1);
ApiKeyToken apiKeyToken2 = new($"<token>", "api_key_query", timeout: TimeSpan.FromSeconds(1));
ApiKeyToken apiKeyToken2 = new("<token>", ClientUtils.ApiKeyHeader.Api_key_query, timeout: TimeSpan.FromSeconds(1));
options.AddTokens(apiKeyToken2);
BearerToken bearerToken1 = new($"<token>", timeout: TimeSpan.FromSeconds(1));
BearerToken bearerToken1 = new("<token>", timeout: TimeSpan.FromSeconds(1));
options.AddTokens(bearerToken1);
BasicToken basicToken1 = new("<username>", "<password>", timeout: TimeSpan.FromSeconds(1));
@@ -78,13 +78,13 @@ namespace Org.OpenAPITools.Test.Api
{
services.AddApi(options =>
{
ApiKeyToken apiKeyToken1 = new($"<token>", "api_key", timeout: TimeSpan.FromSeconds(1));
ApiKeyToken apiKeyToken1 = new("<token>", ClientUtils.ApiKeyHeader.Api_key, timeout: TimeSpan.FromSeconds(1));
options.AddTokens(apiKeyToken1);
ApiKeyToken apiKeyToken2 = new($"<token>", "api_key_query", timeout: TimeSpan.FromSeconds(1));
ApiKeyToken apiKeyToken2 = new("<token>", ClientUtils.ApiKeyHeader.Api_key_query, timeout: TimeSpan.FromSeconds(1));
options.AddTokens(apiKeyToken2);
BearerToken bearerToken1 = new($"<token>", timeout: TimeSpan.FromSeconds(1));
BearerToken bearerToken1 = new("<token>", timeout: TimeSpan.FromSeconds(1));
options.AddTokens(bearerToken1);
BasicToken basicToken1 = new("<username>", "<password>", timeout: TimeSpan.FromSeconds(1));
@@ -105,13 +105,13 @@ namespace Org.OpenAPITools.Test.Api
{
services.AddApi(options =>
{
ApiKeyToken apiKeyToken1 = new($"<token>", "api_key", timeout: TimeSpan.FromSeconds(1));
ApiKeyToken apiKeyToken1 = new("<token>", ClientUtils.ApiKeyHeader.Api_key, timeout: TimeSpan.FromSeconds(1));
options.AddTokens(apiKeyToken1);
ApiKeyToken apiKeyToken2 = new($"<token>", "api_key_query", timeout: TimeSpan.FromSeconds(1));
ApiKeyToken apiKeyToken2 = new("<token>", ClientUtils.ApiKeyHeader.Api_key_query, timeout: TimeSpan.FromSeconds(1));
options.AddTokens(apiKeyToken2);
BearerToken bearerToken1 = new($"<token>", timeout: TimeSpan.FromSeconds(1));
BearerToken bearerToken1 = new("<token>", timeout: TimeSpan.FromSeconds(1));
options.AddTokens(bearerToken1);
BasicToken basicToken1 = new("<username>", "<password>", timeout: TimeSpan.FromSeconds(1));

View File

@@ -16,7 +16,7 @@ namespace Org.OpenAPITools.Client
/// <summary>
/// The header that this token will be used with.
/// </summary>
public string Header { get; }
public ClientUtils.ApiKeyHeader Header { get; }
/// <summary>
/// Constructs an ApiKeyToken object.
@@ -25,7 +25,7 @@ namespace Org.OpenAPITools.Client
/// <param name="header"></param>
/// <param name="prefix"></param>
/// <param name="timeout"></param>
public ApiKeyToken(string value, string header, string prefix = "Bearer ", TimeSpan? timeout = null) : base(timeout)
public ApiKeyToken(string value, ClientUtils.ApiKeyHeader header, string prefix = "Bearer ", TimeSpan? timeout = null) : base(timeout)
{
Header = header;
_raw = $"{ prefix }{ value }";
@@ -37,7 +37,7 @@ namespace Org.OpenAPITools.Client
/// <param name="request"></param>
public virtual void UseInHeader(System.Net.Http.HttpRequestMessage request)
{
request.Headers.Add(Header, _raw);
request.Headers.Add(ClientUtils.ApiKeyHeaderToString(Header), _raw);
}
/// <summary>
@@ -48,7 +48,7 @@ namespace Org.OpenAPITools.Client
/// <param name="parseQueryString"></param>
public virtual void UseInQuery(System.Net.Http.HttpRequestMessage request, UriBuilder uriBuilder, System.Collections.Specialized.NameValueCollection parseQueryString)
{
parseQueryString[Header] = Uri.EscapeDataString(_raw).ToString()!;
parseQueryString[ClientUtils.ApiKeyHeaderToString(Header)] = Uri.EscapeDataString(_raw).ToString()!;
}
}
}

View File

@@ -54,6 +54,37 @@ namespace Org.OpenAPITools.Client
/// <returns></returns>
public delegate void EventHandler<T>(object sender, T e) where T : EventArgs;
/// <summary>
/// An enum of headers
/// </summary>
public enum ApiKeyHeader
{
/// <summary>
/// The api_key header
/// </summary>
Api_key,
/// <summary>
/// The api_key_query header
/// </summary>
Api_key_query
}
/// <summary>
/// Converte an ApiKeyHeader to a string
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
/// <exception cref="System.ComponentModel.InvalidEnumArgumentException"></exception>
public static string ApiKeyHeaderToString(ApiKeyHeader value)
{
return value switch
{
ApiKeyHeader.Api_key => "api_key",
ApiKeyHeader.Api_key_query => "api_key_query",
_ => throw new System.ComponentModel.InvalidEnumArgumentException(nameof(value), (int)value, typeof(ApiKeyHeader)),
};
}
/// <summary>
/// Returns true when deserialization succeeds.
/// </summary>

View File

@@ -36,11 +36,11 @@ namespace Org.OpenAPITools.Client
if (container is TokenContainer<ApiKeyToken> apiKeyTokenContainer)
{
string[] headers = apiKeyTokenContainer.Tokens.Select(t => t.Header).Distinct().ToArray();
string[] headers = apiKeyTokenContainer.Tokens.Select(t => ClientUtils.ApiKeyHeaderToString(t.Header)).Distinct().ToArray();
foreach (string header in headers)
{
BoundedChannelOptions options = new BoundedChannelOptions(apiKeyTokenContainer.Tokens.Count(t => t.Header.Equals(header)))
BoundedChannelOptions options = new BoundedChannelOptions(apiKeyTokenContainer.Tokens.Count(t => ClientUtils.ApiKeyHeaderToString(t.Header).Equals(header)))
{
FullMode = BoundedChannelFullMode.DropWrite
};

View File

@@ -69,42 +69,42 @@ namespace YourProject
.ConfigureApi((context, options) =>
{
// the type of token here depends on the api security specifications
ApiKeyToken token = new("<your token>");
ApiKeyToken token = new("<your token>", ClientUtils.ApiKeyHeader.Petstore_auth);
options.AddTokens(token);
// optionally choose the method the tokens will be provided with, default is RateLimitProvider
options.UseProvider<RateLimitProvider<ApiKeyToken>, ApiKeyToken>();
// the type of token here depends on the api security specifications
ApiKeyToken token = new("<your token>");
ApiKeyToken token = new("<your token>", ClientUtils.ApiKeyHeader.Api_key);
options.AddTokens(token);
// optionally choose the method the tokens will be provided with, default is RateLimitProvider
options.UseProvider<RateLimitProvider<ApiKeyToken>, ApiKeyToken>();
// the type of token here depends on the api security specifications
ApiKeyToken token = new("<your token>");
ApiKeyToken token = new("<your token>", ClientUtils.ApiKeyHeader.Api_key_query);
options.AddTokens(token);
// optionally choose the method the tokens will be provided with, default is RateLimitProvider
options.UseProvider<RateLimitProvider<ApiKeyToken>, ApiKeyToken>();
// the type of token here depends on the api security specifications
ApiKeyToken token = new("<your token>");
ApiKeyToken token = new("<your token>", ClientUtils.ApiKeyHeader.Http_basic_test);
options.AddTokens(token);
// optionally choose the method the tokens will be provided with, default is RateLimitProvider
options.UseProvider<RateLimitProvider<ApiKeyToken>, ApiKeyToken>();
// the type of token here depends on the api security specifications
ApiKeyToken token = new("<your token>");
ApiKeyToken token = new("<your token>", ClientUtils.ApiKeyHeader.Bearer_test);
options.AddTokens(token);
// optionally choose the method the tokens will be provided with, default is RateLimitProvider
options.UseProvider<RateLimitProvider<ApiKeyToken>, ApiKeyToken>();
// the type of token here depends on the api security specifications
ApiKeyToken token = new("<your token>");
ApiKeyToken token = new("<your token>", ClientUtils.ApiKeyHeader.Http_signature_test);
options.AddTokens(token);
// optionally choose the method the tokens will be provided with, default is RateLimitProvider

View File

@@ -53,11 +53,11 @@ namespace Org.OpenAPITools.Test.Api
.ConfigureApi((context, services, options) =>
{
string apiKeyTokenValue1 = context.Configuration["<token>"] ?? throw new Exception("Token not found.");
ApiKeyToken apiKeyToken1 = new(apiKeyTokenValue1, "api_key", timeout: TimeSpan.FromSeconds(1));
ApiKeyToken apiKeyToken1 = new(apiKeyTokenValue1, ClientUtils.ApiKeyHeader.Api_key, timeout: TimeSpan.FromSeconds(1));
options.AddTokens(apiKeyToken1);
string apiKeyTokenValue2 = context.Configuration["<token>"] ?? throw new Exception("Token not found.");
ApiKeyToken apiKeyToken2 = new(apiKeyTokenValue2, "api_key_query", timeout: TimeSpan.FromSeconds(1));
ApiKeyToken apiKeyToken2 = new(apiKeyTokenValue2, ClientUtils.ApiKeyHeader.Api_key_query, timeout: TimeSpan.FromSeconds(1));
options.AddTokens(apiKeyToken2);
string bearerTokenValue1 = context.Configuration["<token>"] ?? throw new Exception("Token not found.");

View File

@@ -27,13 +27,13 @@ namespace Org.OpenAPITools.Test.Api
private readonly IHost _hostUsingConfigureWithoutAClient =
Host.CreateDefaultBuilder([]).ConfigureApi((context, services, options) =>
{
ApiKeyToken apiKeyToken1 = new($"<token>", "api_key", timeout: TimeSpan.FromSeconds(1));
ApiKeyToken apiKeyToken1 = new("<token>", ClientUtils.ApiKeyHeader.Api_key, timeout: TimeSpan.FromSeconds(1));
options.AddTokens(apiKeyToken1);
ApiKeyToken apiKeyToken2 = new($"<token>", "api_key_query", timeout: TimeSpan.FromSeconds(1));
ApiKeyToken apiKeyToken2 = new("<token>", ClientUtils.ApiKeyHeader.Api_key_query, timeout: TimeSpan.FromSeconds(1));
options.AddTokens(apiKeyToken2);
BearerToken bearerToken1 = new($"<token>", timeout: TimeSpan.FromSeconds(1));
BearerToken bearerToken1 = new("<token>", timeout: TimeSpan.FromSeconds(1));
options.AddTokens(bearerToken1);
BasicToken basicToken1 = new("<username>", "<password>", timeout: TimeSpan.FromSeconds(1));
@@ -51,13 +51,13 @@ namespace Org.OpenAPITools.Test.Api
private readonly IHost _hostUsingConfigureWithAClient =
Host.CreateDefaultBuilder([]).ConfigureApi((context, services, options) =>
{
ApiKeyToken apiKeyToken1 = new($"<token>", "api_key", timeout: TimeSpan.FromSeconds(1));
ApiKeyToken apiKeyToken1 = new("<token>", ClientUtils.ApiKeyHeader.Api_key, timeout: TimeSpan.FromSeconds(1));
options.AddTokens(apiKeyToken1);
ApiKeyToken apiKeyToken2 = new($"<token>", "api_key_query", timeout: TimeSpan.FromSeconds(1));
ApiKeyToken apiKeyToken2 = new("<token>", ClientUtils.ApiKeyHeader.Api_key_query, timeout: TimeSpan.FromSeconds(1));
options.AddTokens(apiKeyToken2);
BearerToken bearerToken1 = new($"<token>", timeout: TimeSpan.FromSeconds(1));
BearerToken bearerToken1 = new("<token>", timeout: TimeSpan.FromSeconds(1));
options.AddTokens(bearerToken1);
BasicToken basicToken1 = new("<username>", "<password>", timeout: TimeSpan.FromSeconds(1));
@@ -78,13 +78,13 @@ namespace Org.OpenAPITools.Test.Api
{
services.AddApi(options =>
{
ApiKeyToken apiKeyToken1 = new($"<token>", "api_key", timeout: TimeSpan.FromSeconds(1));
ApiKeyToken apiKeyToken1 = new("<token>", ClientUtils.ApiKeyHeader.Api_key, timeout: TimeSpan.FromSeconds(1));
options.AddTokens(apiKeyToken1);
ApiKeyToken apiKeyToken2 = new($"<token>", "api_key_query", timeout: TimeSpan.FromSeconds(1));
ApiKeyToken apiKeyToken2 = new("<token>", ClientUtils.ApiKeyHeader.Api_key_query, timeout: TimeSpan.FromSeconds(1));
options.AddTokens(apiKeyToken2);
BearerToken bearerToken1 = new($"<token>", timeout: TimeSpan.FromSeconds(1));
BearerToken bearerToken1 = new("<token>", timeout: TimeSpan.FromSeconds(1));
options.AddTokens(bearerToken1);
BasicToken basicToken1 = new("<username>", "<password>", timeout: TimeSpan.FromSeconds(1));
@@ -105,13 +105,13 @@ namespace Org.OpenAPITools.Test.Api
{
services.AddApi(options =>
{
ApiKeyToken apiKeyToken1 = new($"<token>", "api_key", timeout: TimeSpan.FromSeconds(1));
ApiKeyToken apiKeyToken1 = new("<token>", ClientUtils.ApiKeyHeader.Api_key, timeout: TimeSpan.FromSeconds(1));
options.AddTokens(apiKeyToken1);
ApiKeyToken apiKeyToken2 = new($"<token>", "api_key_query", timeout: TimeSpan.FromSeconds(1));
ApiKeyToken apiKeyToken2 = new("<token>", ClientUtils.ApiKeyHeader.Api_key_query, timeout: TimeSpan.FromSeconds(1));
options.AddTokens(apiKeyToken2);
BearerToken bearerToken1 = new($"<token>", timeout: TimeSpan.FromSeconds(1));
BearerToken bearerToken1 = new("<token>", timeout: TimeSpan.FromSeconds(1));
options.AddTokens(bearerToken1);
BasicToken basicToken1 = new("<username>", "<password>", timeout: TimeSpan.FromSeconds(1));

View File

@@ -14,7 +14,7 @@ namespace Org.OpenAPITools.Client
/// <summary>
/// The header that this token will be used with.
/// </summary>
public string Header { get; }
public ClientUtils.ApiKeyHeader Header { get; }
/// <summary>
/// Constructs an ApiKeyToken object.
@@ -23,7 +23,7 @@ namespace Org.OpenAPITools.Client
/// <param name="header"></param>
/// <param name="prefix"></param>
/// <param name="timeout"></param>
public ApiKeyToken(string value, string header, string prefix = "Bearer ", TimeSpan? timeout = null) : base(timeout)
public ApiKeyToken(string value, ClientUtils.ApiKeyHeader header, string prefix = "Bearer ", TimeSpan? timeout = null) : base(timeout)
{
Header = header;
_raw = $"{ prefix }{ value }";
@@ -35,7 +35,7 @@ namespace Org.OpenAPITools.Client
/// <param name="request"></param>
public virtual void UseInHeader(System.Net.Http.HttpRequestMessage request)
{
request.Headers.Add(Header, _raw);
request.Headers.Add(ClientUtils.ApiKeyHeaderToString(Header), _raw);
}
/// <summary>
@@ -46,7 +46,7 @@ namespace Org.OpenAPITools.Client
/// <param name="parseQueryString"></param>
public virtual void UseInQuery(System.Net.Http.HttpRequestMessage request, UriBuilder uriBuilder, System.Collections.Specialized.NameValueCollection parseQueryString)
{
parseQueryString[Header] = Uri.EscapeDataString(_raw).ToString();
parseQueryString[ClientUtils.ApiKeyHeaderToString(Header)] = Uri.EscapeDataString(_raw).ToString();
}
}
}

View File

@@ -52,6 +52,37 @@ namespace Org.OpenAPITools.Client
/// <returns></returns>
public delegate void EventHandler<T>(object sender, T e) where T : EventArgs;
/// <summary>
/// An enum of headers
/// </summary>
public enum ApiKeyHeader
{
/// <summary>
/// The api_key header
/// </summary>
Api_key,
/// <summary>
/// The api_key_query header
/// </summary>
Api_key_query
}
/// <summary>
/// Converte an ApiKeyHeader to a string
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
/// <exception cref="System.ComponentModel.InvalidEnumArgumentException"></exception>
public static string ApiKeyHeaderToString(ApiKeyHeader value)
{
return value switch
{
ApiKeyHeader.Api_key => "api_key",
ApiKeyHeader.Api_key_query => "api_key_query",
_ => throw new System.ComponentModel.InvalidEnumArgumentException(nameof(value), (int)value, typeof(ApiKeyHeader)),
};
}
/// <summary>
/// Returns true when deserialization succeeds.
/// </summary>

View File

@@ -34,11 +34,11 @@ namespace Org.OpenAPITools.Client
if (container is TokenContainer<ApiKeyToken> apiKeyTokenContainer)
{
string[] headers = apiKeyTokenContainer.Tokens.Select(t => t.Header).Distinct().ToArray();
string[] headers = apiKeyTokenContainer.Tokens.Select(t => ClientUtils.ApiKeyHeaderToString(t.Header)).Distinct().ToArray();
foreach (string header in headers)
{
BoundedChannelOptions options = new BoundedChannelOptions(apiKeyTokenContainer.Tokens.Count(t => t.Header.Equals(header)))
BoundedChannelOptions options = new BoundedChannelOptions(apiKeyTokenContainer.Tokens.Count(t => ClientUtils.ApiKeyHeaderToString(t.Header).Equals(header)))
{
FullMode = BoundedChannelFullMode.DropWrite
};

View File

@@ -69,42 +69,42 @@ namespace YourProject
.ConfigureApi((context, options) =>
{
// the type of token here depends on the api security specifications
ApiKeyToken token = new("<your token>");
ApiKeyToken token = new("<your token>", ClientUtils.ApiKeyHeader.Petstore_auth);
options.AddTokens(token);
// optionally choose the method the tokens will be provided with, default is RateLimitProvider
options.UseProvider<RateLimitProvider<ApiKeyToken>, ApiKeyToken>();
// the type of token here depends on the api security specifications
ApiKeyToken token = new("<your token>");
ApiKeyToken token = new("<your token>", ClientUtils.ApiKeyHeader.Api_key);
options.AddTokens(token);
// optionally choose the method the tokens will be provided with, default is RateLimitProvider
options.UseProvider<RateLimitProvider<ApiKeyToken>, ApiKeyToken>();
// the type of token here depends on the api security specifications
ApiKeyToken token = new("<your token>");
ApiKeyToken token = new("<your token>", ClientUtils.ApiKeyHeader.Api_key_query);
options.AddTokens(token);
// optionally choose the method the tokens will be provided with, default is RateLimitProvider
options.UseProvider<RateLimitProvider<ApiKeyToken>, ApiKeyToken>();
// the type of token here depends on the api security specifications
ApiKeyToken token = new("<your token>");
ApiKeyToken token = new("<your token>", ClientUtils.ApiKeyHeader.Http_basic_test);
options.AddTokens(token);
// optionally choose the method the tokens will be provided with, default is RateLimitProvider
options.UseProvider<RateLimitProvider<ApiKeyToken>, ApiKeyToken>();
// the type of token here depends on the api security specifications
ApiKeyToken token = new("<your token>");
ApiKeyToken token = new("<your token>", ClientUtils.ApiKeyHeader.Bearer_test);
options.AddTokens(token);
// optionally choose the method the tokens will be provided with, default is RateLimitProvider
options.UseProvider<RateLimitProvider<ApiKeyToken>, ApiKeyToken>();
// the type of token here depends on the api security specifications
ApiKeyToken token = new("<your token>");
ApiKeyToken token = new("<your token>", ClientUtils.ApiKeyHeader.Http_signature_test);
options.AddTokens(token);
// optionally choose the method the tokens will be provided with, default is RateLimitProvider

View File

@@ -53,11 +53,11 @@ namespace Org.OpenAPITools.Test.Api
.ConfigureApi((context, services, options) =>
{
string apiKeyTokenValue1 = context.Configuration["<token>"] ?? throw new Exception("Token not found.");
ApiKeyToken apiKeyToken1 = new ApiKeyToken(apiKeyTokenValue1, "api_key", timeout: TimeSpan.FromSeconds(1));
ApiKeyToken apiKeyToken1 = new ApiKeyToken(apiKeyTokenValue1, ClientUtils.ApiKeyHeader.Api_key, timeout: TimeSpan.FromSeconds(1));
options.AddTokens(apiKeyToken1);
string apiKeyTokenValue2 = context.Configuration["<token>"] ?? throw new Exception("Token not found.");
ApiKeyToken apiKeyToken2 = new ApiKeyToken(apiKeyTokenValue2, "api_key_query", timeout: TimeSpan.FromSeconds(1));
ApiKeyToken apiKeyToken2 = new ApiKeyToken(apiKeyTokenValue2, ClientUtils.ApiKeyHeader.Api_key_query, timeout: TimeSpan.FromSeconds(1));
options.AddTokens(apiKeyToken2);
string bearerTokenValue1 = context.Configuration["<token>"] ?? throw new Exception("Token not found.");

View File

@@ -27,13 +27,13 @@ namespace Org.OpenAPITools.Test.Api
private readonly IHost _hostUsingConfigureWithoutAClient =
Host.CreateDefaultBuilder(Array.Empty<string>()).ConfigureApi((context, services, options) =>
{
ApiKeyToken apiKeyToken1 = new ApiKeyToken($"<token>", "api_key", timeout: TimeSpan.FromSeconds(1));
ApiKeyToken apiKeyToken1 = new ApiKeyToken("<token>", ClientUtils.ApiKeyHeader.Api_key, timeout: TimeSpan.FromSeconds(1));
options.AddTokens(apiKeyToken1);
ApiKeyToken apiKeyToken2 = new ApiKeyToken($"<token>", "api_key_query", timeout: TimeSpan.FromSeconds(1));
ApiKeyToken apiKeyToken2 = new ApiKeyToken("<token>", ClientUtils.ApiKeyHeader.Api_key_query, timeout: TimeSpan.FromSeconds(1));
options.AddTokens(apiKeyToken2);
BearerToken bearerToken1 = new BearerToken($"<token>", timeout: TimeSpan.FromSeconds(1));
BearerToken bearerToken1 = new BearerToken("<token>", timeout: TimeSpan.FromSeconds(1));
options.AddTokens(bearerToken1);
BasicToken basicToken1 = new BasicToken("<username>", "<password>", timeout: TimeSpan.FromSeconds(1));
@@ -51,13 +51,13 @@ namespace Org.OpenAPITools.Test.Api
private readonly IHost _hostUsingConfigureWithAClient =
Host.CreateDefaultBuilder(Array.Empty<string>()).ConfigureApi((context, services, options) =>
{
ApiKeyToken apiKeyToken1 = new ApiKeyToken($"<token>", "api_key", timeout: TimeSpan.FromSeconds(1));
ApiKeyToken apiKeyToken1 = new ApiKeyToken("<token>", ClientUtils.ApiKeyHeader.Api_key, timeout: TimeSpan.FromSeconds(1));
options.AddTokens(apiKeyToken1);
ApiKeyToken apiKeyToken2 = new ApiKeyToken($"<token>", "api_key_query", timeout: TimeSpan.FromSeconds(1));
ApiKeyToken apiKeyToken2 = new ApiKeyToken("<token>", ClientUtils.ApiKeyHeader.Api_key_query, timeout: TimeSpan.FromSeconds(1));
options.AddTokens(apiKeyToken2);
BearerToken bearerToken1 = new BearerToken($"<token>", timeout: TimeSpan.FromSeconds(1));
BearerToken bearerToken1 = new BearerToken("<token>", timeout: TimeSpan.FromSeconds(1));
options.AddTokens(bearerToken1);
BasicToken basicToken1 = new BasicToken("<username>", "<password>", timeout: TimeSpan.FromSeconds(1));
@@ -78,13 +78,13 @@ namespace Org.OpenAPITools.Test.Api
{
services.AddApi(options =>
{
ApiKeyToken apiKeyToken1 = new ApiKeyToken($"<token>", "api_key", timeout: TimeSpan.FromSeconds(1));
ApiKeyToken apiKeyToken1 = new ApiKeyToken("<token>", ClientUtils.ApiKeyHeader.Api_key, timeout: TimeSpan.FromSeconds(1));
options.AddTokens(apiKeyToken1);
ApiKeyToken apiKeyToken2 = new ApiKeyToken($"<token>", "api_key_query", timeout: TimeSpan.FromSeconds(1));
ApiKeyToken apiKeyToken2 = new ApiKeyToken("<token>", ClientUtils.ApiKeyHeader.Api_key_query, timeout: TimeSpan.FromSeconds(1));
options.AddTokens(apiKeyToken2);
BearerToken bearerToken1 = new BearerToken($"<token>", timeout: TimeSpan.FromSeconds(1));
BearerToken bearerToken1 = new BearerToken("<token>", timeout: TimeSpan.FromSeconds(1));
options.AddTokens(bearerToken1);
BasicToken basicToken1 = new BasicToken("<username>", "<password>", timeout: TimeSpan.FromSeconds(1));
@@ -105,13 +105,13 @@ namespace Org.OpenAPITools.Test.Api
{
services.AddApi(options =>
{
ApiKeyToken apiKeyToken1 = new ApiKeyToken($"<token>", "api_key", timeout: TimeSpan.FromSeconds(1));
ApiKeyToken apiKeyToken1 = new ApiKeyToken("<token>", ClientUtils.ApiKeyHeader.Api_key, timeout: TimeSpan.FromSeconds(1));
options.AddTokens(apiKeyToken1);
ApiKeyToken apiKeyToken2 = new ApiKeyToken($"<token>", "api_key_query", timeout: TimeSpan.FromSeconds(1));
ApiKeyToken apiKeyToken2 = new ApiKeyToken("<token>", ClientUtils.ApiKeyHeader.Api_key_query, timeout: TimeSpan.FromSeconds(1));
options.AddTokens(apiKeyToken2);
BearerToken bearerToken1 = new BearerToken($"<token>", timeout: TimeSpan.FromSeconds(1));
BearerToken bearerToken1 = new BearerToken("<token>", timeout: TimeSpan.FromSeconds(1));
options.AddTokens(bearerToken1);
BasicToken basicToken1 = new BasicToken("<username>", "<password>", timeout: TimeSpan.FromSeconds(1));

View File

@@ -14,7 +14,7 @@ namespace Org.OpenAPITools.Client
/// <summary>
/// The header that this token will be used with.
/// </summary>
public string Header { get; }
public ClientUtils.ApiKeyHeader Header { get; }
/// <summary>
/// Constructs an ApiKeyToken object.
@@ -23,7 +23,7 @@ namespace Org.OpenAPITools.Client
/// <param name="header"></param>
/// <param name="prefix"></param>
/// <param name="timeout"></param>
public ApiKeyToken(string value, string header, string prefix = "Bearer ", TimeSpan? timeout = null) : base(timeout)
public ApiKeyToken(string value, ClientUtils.ApiKeyHeader header, string prefix = "Bearer ", TimeSpan? timeout = null) : base(timeout)
{
Header = header;
_raw = $"{ prefix }{ value }";
@@ -35,7 +35,7 @@ namespace Org.OpenAPITools.Client
/// <param name="request"></param>
public virtual void UseInHeader(System.Net.Http.HttpRequestMessage request)
{
request.Headers.Add(Header, _raw);
request.Headers.Add(ClientUtils.ApiKeyHeaderToString(Header), _raw);
}
/// <summary>
@@ -46,7 +46,7 @@ namespace Org.OpenAPITools.Client
/// <param name="parseQueryString"></param>
public virtual void UseInQuery(System.Net.Http.HttpRequestMessage request, UriBuilder uriBuilder, System.Collections.Specialized.NameValueCollection parseQueryString)
{
parseQueryString[Header] = Uri.EscapeDataString(_raw).ToString();
parseQueryString[ClientUtils.ApiKeyHeaderToString(Header)] = Uri.EscapeDataString(_raw).ToString();
}
}
}

View File

@@ -52,6 +52,40 @@ namespace Org.OpenAPITools.Client
/// <returns></returns>
public delegate void EventHandler<T>(object sender, T e) where T : EventArgs;
/// <summary>
/// An enum of headers
/// </summary>
public enum ApiKeyHeader
{
/// <summary>
/// The api_key header
/// </summary>
Api_key,
/// <summary>
/// The api_key_query header
/// </summary>
Api_key_query
}
/// <summary>
/// Converte an ApiKeyHeader to a string
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
/// <exception cref="System.ComponentModel.InvalidEnumArgumentException"></exception>
public static string ApiKeyHeaderToString(ApiKeyHeader value)
{
switch(value)
{
case ApiKeyHeader.Api_key:
return "api_key";
case ApiKeyHeader.Api_key_query:
return "api_key_query";
default:
throw new System.ComponentModel.InvalidEnumArgumentException(nameof(value), (int)value, typeof(ApiKeyHeader));
}
}
/// <summary>
/// Returns true when deserialization succeeds.
/// </summary>

View File

@@ -34,11 +34,11 @@ namespace Org.OpenAPITools.Client
if (container is TokenContainer<ApiKeyToken> apiKeyTokenContainer)
{
string[] headers = apiKeyTokenContainer.Tokens.Select(t => t.Header).Distinct().ToArray();
string[] headers = apiKeyTokenContainer.Tokens.Select(t => ClientUtils.ApiKeyHeaderToString(t.Header)).Distinct().ToArray();
foreach (string header in headers)
{
BoundedChannelOptions options = new BoundedChannelOptions(apiKeyTokenContainer.Tokens.Count(t => t.Header.Equals(header)))
BoundedChannelOptions options = new BoundedChannelOptions(apiKeyTokenContainer.Tokens.Count(t => ClientUtils.ApiKeyHeaderToString(t.Header).Equals(header)))
{
FullMode = BoundedChannelFullMode.DropWrite
};

View File

@@ -1,31 +0,0 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<GenerateAssemblyInfo>true</GenerateAssemblyInfo> <!-- setting GenerateAssemblyInfo to false causes this bug https://github.com/dotnet/project-system/issues/3934 -->
<TargetFramework>netstandard2.0</TargetFramework>
<AssemblyName>Org.OpenAPITools</AssemblyName>
<PackageId>Org.OpenAPITools</PackageId>
<OutputType>Library</OutputType>
<Authors>OpenAPI</Authors>
<Company>OpenAPI</Company>
<AssemblyTitle>OpenAPI Library</AssemblyTitle>
<Description>A library generated from a OpenAPI doc</Description>
<Copyright>No Copyright</Copyright>
<RootNamespace>Org.OpenAPITools</RootNamespace>
<Version>1.0.0</Version>
<DocumentationFile>bin\$(Configuration)\$(TargetFramework)\Org.OpenAPITools.xml</DocumentationFile>
<RepositoryUrl>https://github.com/GIT_USER_ID/GIT_REPO_ID.git</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<PackageReleaseNotes>Minor update</PackageReleaseNotes>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="CompareNETObjects" Version="4.82.0" />
<PackageReference Include="Microsoft.Extensions.Http" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Http.Polly" Version="5.0.1" />
<PackageReference Include="System.Threading.Channels" Version="8.0.0" />
<PackageReference Include="System.ComponentModel.Annotations" Version="5.0.0" />
</ItemGroup>
</Project>

View File

@@ -69,42 +69,42 @@ namespace YourProject
.ConfigureApi((context, options) =>
{
// the type of token here depends on the api security specifications
ApiKeyToken token = new("<your token>");
ApiKeyToken token = new("<your token>", ClientUtils.ApiKeyHeader.Petstore_auth);
options.AddTokens(token);
// optionally choose the method the tokens will be provided with, default is RateLimitProvider
options.UseProvider<RateLimitProvider<ApiKeyToken>, ApiKeyToken>();
// the type of token here depends on the api security specifications
ApiKeyToken token = new("<your token>");
ApiKeyToken token = new("<your token>", ClientUtils.ApiKeyHeader.Api_key);
options.AddTokens(token);
// optionally choose the method the tokens will be provided with, default is RateLimitProvider
options.UseProvider<RateLimitProvider<ApiKeyToken>, ApiKeyToken>();
// the type of token here depends on the api security specifications
ApiKeyToken token = new("<your token>");
ApiKeyToken token = new("<your token>", ClientUtils.ApiKeyHeader.Api_key_query);
options.AddTokens(token);
// optionally choose the method the tokens will be provided with, default is RateLimitProvider
options.UseProvider<RateLimitProvider<ApiKeyToken>, ApiKeyToken>();
// the type of token here depends on the api security specifications
ApiKeyToken token = new("<your token>");
ApiKeyToken token = new("<your token>", ClientUtils.ApiKeyHeader.Http_basic_test);
options.AddTokens(token);
// optionally choose the method the tokens will be provided with, default is RateLimitProvider
options.UseProvider<RateLimitProvider<ApiKeyToken>, ApiKeyToken>();
// the type of token here depends on the api security specifications
ApiKeyToken token = new("<your token>");
ApiKeyToken token = new("<your token>", ClientUtils.ApiKeyHeader.Bearer_test);
options.AddTokens(token);
// optionally choose the method the tokens will be provided with, default is RateLimitProvider
options.UseProvider<RateLimitProvider<ApiKeyToken>, ApiKeyToken>();
// the type of token here depends on the api security specifications
ApiKeyToken token = new("<your token>");
ApiKeyToken token = new("<your token>", ClientUtils.ApiKeyHeader.Http_signature_test);
options.AddTokens(token);
// optionally choose the method the tokens will be provided with, default is RateLimitProvider