Skip to content

Commit 7a64774

Browse files
committed
fix: Removed usage of HttpClient.DefaultRequestHeaders.
1 parent 77e969e commit 7a64774

File tree

1,773 files changed

+12534
-484
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,773 files changed

+12534
-484
lines changed

src/libs/AutoSDK/Models/Authorization.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Collections.Immutable;
12
using AutoSDK.Extensions;
23
using Microsoft.OpenApi.Models;
34

@@ -7,11 +8,14 @@ public readonly record struct Authorization(
78
string FriendlyName,
89
SecuritySchemeType Type,
910
ParameterLocation In,
11+
EquatableArray<string> Parameters,
1012
string Name,
1113
string Scheme,
1214
Settings Settings
1315
)
1416
{
17+
public string MethodName => $"AuthorizeUsing{FriendlyName}";
18+
1519
public static Authorization FromOpenApiSecurityScheme(
1620
OpenApiSecurityScheme scheme,
1721
Settings settings)
@@ -27,15 +31,25 @@ public static Authorization FromOpenApiSecurityScheme(
2731
(SecuritySchemeType.OAuth2, _, _) => "OAuth2",
2832
_ => scheme.Scheme?.ToPropertyName() ?? string.Empty,
2933
};
34+
string[] parameters = (scheme.Type, scheme.Scheme, scheme.In) switch
35+
{
36+
(SecuritySchemeType.Http, "bearer", _) => ["apiKey"],
37+
(SecuritySchemeType.Http, "basic", _) => ["username", "password"],
38+
(SecuritySchemeType.ApiKey, _, ParameterLocation.Header) => ["apiKey"],
39+
(SecuritySchemeType.ApiKey, _, ParameterLocation.Query) => ["apiKey"],
40+
_ => [],
41+
};
3042

3143
return new Authorization(
3244
FriendlyName: friendlyName,
3345
Type: scheme.Type,
3446
In: (scheme.Type, scheme.Scheme) switch
3547
{
3648
(SecuritySchemeType.Http, "bearer") => ParameterLocation.Header,
49+
(SecuritySchemeType.OAuth2, _) => ParameterLocation.Header,
3750
_ => scheme.In,
3851
},
52+
Parameters: parameters.ToImmutableArray().AsEquatableArray(),
3953
Name: scheme.Name ?? string.Empty,
4054
Scheme: scheme.Scheme ?? string.Empty,
4155
Settings: settings);

src/libs/AutoSDK/Sources/Sources.Authorizations.cs

Lines changed: 48 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -9,70 +9,32 @@ public static partial class Sources
99
public static string GenerateAuthorization(
1010
Authorization authorization)
1111
{
12-
var body = (authorization.Type, authorization.Scheme, authorization.In) switch
12+
if (authorization.Type is SecuritySchemeType.OAuth2 or SecuritySchemeType.OpenIdConnect)
1313
{
14-
(SecuritySchemeType.Http, "bearer", _) => $@"
15-
/// <summary>
16-
/// Authorize using bearer authentication.
17-
/// </summary>
18-
/// <param name=""apiKey""></param>
19-
public void AuthorizeUsingBearer(
20-
string apiKey)
21-
{{
22-
apiKey = apiKey ?? throw new global::System.ArgumentNullException(nameof(apiKey));
23-
24-
_httpClient.DefaultRequestHeaders.Authorization = new global::System.Net.Http.Headers.AuthenticationHeaderValue(
25-
scheme: ""Bearer"",
26-
parameter: apiKey);
27-
}}",
28-
(SecuritySchemeType.Http, "basic", _) => $@"
29-
/// <summary>
30-
/// Authorize using basic authentication.
31-
/// </summary>
32-
/// <param name=""username""></param>
33-
/// <param name=""password""></param>
34-
public void AuthorizeUsingBasic(
35-
string username,
36-
string password)
37-
{{
38-
username = username ?? throw new global::System.ArgumentNullException(nameof(username));
39-
password = password ?? throw new global::System.ArgumentNullException(nameof(password));
40-
41-
_httpClient.DefaultRequestHeaders.Authorization = new global::System.Net.Http.Headers.AuthenticationHeaderValue(
42-
scheme: ""Basic"",
43-
parameter: global::System.Convert.ToBase64String(
44-
global::System.Text.Encoding.UTF8.GetBytes($""{{username}}:{{password}}"")));
45-
}}",
46-
(SecuritySchemeType.ApiKey, _, ParameterLocation.Header) => $@"
47-
/// <summary>
48-
/// Authorize using ApiKey authentication.
49-
/// </summary>
50-
/// <param name=""apiKey""></param>
51-
public void AuthorizeUsingApiKey(
52-
string apiKey)
53-
{{
54-
apiKey = apiKey ?? throw new global::System.ArgumentNullException(nameof(apiKey));
55-
56-
_httpClient.DefaultRequestHeaders.Add(""{authorization.Name}"", apiKey);
57-
}}",
58-
(SecuritySchemeType.ApiKey, _, ParameterLocation.Query) or
59-
(SecuritySchemeType.ApiKey, _, ParameterLocation.Path) => $@"
60-
/// <summary>
61-
/// Authorize using ApiKey authentication.
62-
/// </summary>
63-
/// <param name=""apiKey""></param>
64-
public void AuthorizeUsingApiKey(
65-
string apiKey)
66-
{{
67-
apiKey = apiKey ?? throw new global::System.ArgumentNullException(nameof(apiKey));
68-
69-
_authorization = new global::{authorization.Settings.Namespace}.EndPointAuthorization
70-
{{
71-
Name = ""{authorization.Name}"",
72-
Value = apiKey,
73-
}};
74-
}}",
75-
_ => " ",
14+
return string.Empty;
15+
}
16+
17+
var name = (authorization.Type, authorization.Scheme, authorization.In) switch
18+
{
19+
(SecuritySchemeType.Http, "bearer", _) => "Bearer",
20+
(SecuritySchemeType.Http, "basic", _) => "Basic",
21+
(SecuritySchemeType.ApiKey, _, _) => authorization.Name,
22+
_ => string.Empty,
23+
};
24+
var value = (authorization.Type, authorization.Scheme, authorization.In) switch
25+
{
26+
(SecuritySchemeType.Http, "bearer", _) => "apiKey",
27+
(SecuritySchemeType.Http, "basic", _) => @"global::System.Convert.ToBase64String(
28+
global::System.Text.Encoding.UTF8.GetBytes($""{username}:{password}""))",
29+
(SecuritySchemeType.ApiKey, _, _) => "apiKey",
30+
_ => string.Empty,
31+
};
32+
var xmlDocs = (authorization.Type, authorization.Scheme, authorization.In) switch
33+
{
34+
(SecuritySchemeType.Http, "bearer", _) => "Authorize using bearer authentication.",
35+
(SecuritySchemeType.Http, "basic", _) => "Authorize using basic authentication.",
36+
(SecuritySchemeType.ApiKey, _, _) => "Authorize using ApiKey authentication.",
37+
_ => string.Empty,
7638
};
7739

7840
return $@"
@@ -82,31 +44,32 @@ namespace {authorization.Settings.Namespace}
8244
{{
8345
public sealed partial class {authorization.Settings.ClassName}
8446
{{
85-
{body}
47+
/// <summary>
48+
/// {xmlDocs}
49+
/// </summary>
50+
{authorization.Parameters.Select(x => $@"
51+
/// <param name=""{x}""></param>").Inject()}
52+
public void {authorization.MethodName}(
53+
{authorization.Parameters.Select(x => $@"
54+
string {x},").Inject().TrimEnd(',')})
55+
{{
56+
{authorization.Parameters.Select(x => $@"
57+
{x} = {x} ?? throw new global::System.ArgumentNullException(nameof({x}));").Inject()}
58+
59+
_authorization = new global::{authorization.Settings.Namespace}.EndPointAuthorization
60+
{{
61+
Name = ""{name}"",
62+
Value = {value},
63+
}};
64+
}}
8665
}}
8766
}}".RemoveBlankLinesWhereOnlyWhitespaces();
8867
}
8968

9069
public static string GenerateMainAuthorizationConstructor(
9170
Authorization authorization)
9271
{
93-
var methodName = (authorization.Type, authorization.Scheme, authorization.In) switch
94-
{
95-
(SecuritySchemeType.Http, "bearer", _) => "AuthorizeUsingBearer",
96-
(SecuritySchemeType.Http, "basic", _) => "AuthorizeUsingBasic",
97-
(SecuritySchemeType.ApiKey, _, ParameterLocation.Header) => "AuthorizeUsingApiKey",
98-
(SecuritySchemeType.ApiKey, _, ParameterLocation.Query) => "AuthorizeUsingApiKey",
99-
_ => string.Empty,
100-
};
101-
string[] parameters = (authorization.Type, authorization.Scheme, authorization.In) switch
102-
{
103-
(SecuritySchemeType.Http, "bearer", _) => ["apiKey"],
104-
(SecuritySchemeType.Http, "basic", _) => ["username", "password"],
105-
(SecuritySchemeType.ApiKey, _, ParameterLocation.Header) => ["apiKey"],
106-
(SecuritySchemeType.ApiKey, _, ParameterLocation.Query) => ["apiKey"],
107-
_ => [],
108-
};
109-
if (parameters.Length == 0)
72+
if (authorization.Parameters.IsEmpty)
11073
{
11174
return string.Empty;
11275
}
@@ -120,21 +83,21 @@ public sealed partial class {authorization.Settings.ClassName}
12083
{{
12184
/// <inheritdoc cref=""{authorization.Settings.ClassName}(global::System.Net.Http.HttpClient?, global::System.Uri?)""/>
12285
public {authorization.Settings.ClassName}(
123-
{string.Join("\n", parameters.Select(x => $@"
86+
{string.Join("\n", authorization.Parameters.Select(x => $@"
12487
string {x},"))}
12588
global::System.Net.Http.HttpClient? httpClient = null,
12689
global::System.Uri? baseUri = null) : this(httpClient, baseUri)
12790
{{
128-
Authorizing(_httpClient, {string.Join(", ", parameters.Select(x => $"ref {x}"))});
91+
Authorizing(_httpClient, {string.Join(", ", authorization.Parameters.Select(x => $"ref {x}"))});
12992
130-
{methodName}({string.Join(", ", parameters.Select(x => $"{x}"))});
93+
{authorization.MethodName}({string.Join(", ", authorization.Parameters.Select(x => $"{x}"))});
13194
13295
Authorized(_httpClient);
13396
}}
13497
13598
partial void Authorizing(
13699
global::System.Net.Http.HttpClient client,
137-
{string.Join("\n", parameters.Select(x => $@"
100+
{string.Join("\n", authorization.Parameters.Select(x => $@"
138101
ref string {x},")).TrimEnd(',')});
139102
partial void Authorized(
140103
global::System.Net.Http.HttpClient client);

src/libs/AutoSDK/Sources/Sources.Methods.cs

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,16 @@ public static string GenerateMethod(
153153
using var httpRequest = new global::System.Net.Http.HttpRequestMessage(
154154
method: {GetHttpMethod(endPoint.Settings.TargetFramework, endPoint.HttpMethod)},
155155
requestUri: new global::System.Uri(__path, global::System.UriKind.RelativeOrAbsolute));
156+
{(endPoint.Authorizations.Any(x => x is
157+
{ Type: SecuritySchemeType.ApiKey, In: ParameterLocation.Header } or
158+
{ Type: SecuritySchemeType.Http } or
159+
{ Type: SecuritySchemeType.OAuth2 }) ? @"
160+
if (_authorization != null)
161+
{{
162+
httpRequest.Headers.Authorization = new global::System.Net.Http.Headers.AuthenticationHeaderValue(
163+
scheme: _authorization.Name,
164+
parameter: _authorization.Value);
165+
}}" : " ")}
156166
{(endPoint.Parameters.Any(x => x is { Location: ParameterLocation.Header }) ? "" : " ")}
157167
{endPoint.Parameters
158168
.Where(x => x is { Location: ParameterLocation.Header, IsRequired: true })
@@ -290,25 +300,27 @@ public static string GeneratePathAndQuery(
290300
var __pathBuilder = new PathBuilder(
291301
path: {endPoint.Path},
292302
baseUri: _httpClient.BaseAddress);";
303+
if (endPoint.Authorizations.Any(x => x is { Type: SecuritySchemeType.ApiKey, In: ParameterLocation.Query }))
304+
{
305+
code += @"
306+
if (_authorization != null)
307+
{
308+
__pathBuilder = __pathBuilder.AddRequiredParameter(_authorization.Name, _authorization.Value);
309+
}";
310+
}
293311

294312
var queryParameters = endPoint.QueryParameters
295313
.Where(x =>
296314
x.Type.IsEnum ||
297315
(!x.Type.IsArray || (x.Type.SubTypes[0].Properties.Length == 0 && !x.Type.SubTypes[0].IsArray)))
298316
.ToArray();
299317

300-
if (queryParameters.Length > 0 ||
301-
endPoint.Authorizations.Any(x => x is { Type: SecuritySchemeType.ApiKey, In: ParameterLocation.Query }))
318+
if (queryParameters.Length > 0)
302319
{
303320
code += @"
304321
__pathBuilder";
305322
}
306323

307-
if (endPoint.Authorizations.Any(x => x is { Type: SecuritySchemeType.ApiKey, In: ParameterLocation.Query }))
308-
{
309-
code += @"
310-
.AddRequiredParameter(_authorization!.Name, _authorization!.Value)";
311-
}
312324
foreach (var parameter in queryParameters)
313325
{
314326
var additionalArguments = parameter.Type.IsArray
@@ -330,8 +342,7 @@ public static string GeneratePathAndQuery(
330342
}
331343
}
332344

333-
if (queryParameters.Length > 0 ||
334-
endPoint.Authorizations.Any(x => x is { Type: SecuritySchemeType.ApiKey, In: ParameterLocation.Query }))
345+
if (queryParameters.Length > 0)
335346
{
336347
code += @"
337348
;";

src/tests/AutoSDK.SnapshotTests/Snapshots/Ai21/NewtonsoftJson/_#G.Api.Authorizations.Bearer.g.verified.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@ public void AuthorizeUsingBearer(
1515
{
1616
apiKey = apiKey ?? throw new global::System.ArgumentNullException(nameof(apiKey));
1717

18-
_httpClient.DefaultRequestHeaders.Authorization = new global::System.Net.Http.Headers.AuthenticationHeaderValue(
19-
scheme: "Bearer",
20-
parameter: apiKey);
18+
_authorization = new global::G.EndPointAuthorization
19+
{
20+
Name = "Bearer",
21+
Value = apiKey,
22+
};
2123
}
2224
}
2325
}

src/tests/AutoSDK.SnapshotTests/Snapshots/Ai21/NewtonsoftJson/_#G.Api.ConvertDocumentFileStudioV1ChatFilesConvertPost.g.verified.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,13 @@ partial void ProcessConvertDocumentFileStudioV1ChatFilesConvertPostResponseConte
4747
using var httpRequest = new global::System.Net.Http.HttpRequestMessage(
4848
method: global::System.Net.Http.HttpMethod.Post,
4949
requestUri: new global::System.Uri(__path, global::System.UriKind.RelativeOrAbsolute));
50+
51+
if (_authorization != null)
52+
{{
53+
httpRequest.Headers.Authorization = new global::System.Net.Http.Headers.AuthenticationHeaderValue(
54+
scheme: _authorization.Name,
55+
parameter: _authorization.Value);
56+
}}
5057
using var __httpRequestContent = new global::System.Net.Http.MultipartFormDataContent();
5158
__httpRequestContent.Add(
5259
content: new global::System.Net.Http.StringContent($"[{string.Join(",", global::System.Linq.Enumerable.Select(request.Files, x => x))}]"),

src/tests/AutoSDK.SnapshotTests/Snapshots/Ai21/NewtonsoftJson/_#G.Api.V1Answer.g.verified.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,13 @@ partial void ProcessV1AnswerResponseContent(
4747
using var httpRequest = new global::System.Net.Http.HttpRequestMessage(
4848
method: global::System.Net.Http.HttpMethod.Post,
4949
requestUri: new global::System.Uri(__path, global::System.UriKind.RelativeOrAbsolute));
50+
51+
if (_authorization != null)
52+
{{
53+
httpRequest.Headers.Authorization = new global::System.Net.Http.Headers.AuthenticationHeaderValue(
54+
scheme: _authorization.Name,
55+
parameter: _authorization.Value);
56+
}}
5057
var __httpRequestContentBody = global::Newtonsoft.Json.JsonConvert.SerializeObject(request, JsonSerializerOptions);
5158
var __httpRequestContent = new global::System.Net.Http.StringContent(
5259
content: __httpRequestContentBody,

src/tests/AutoSDK.SnapshotTests/Snapshots/Ai21/NewtonsoftJson/_#G.Api.V1ConversationalRag.g.verified.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,13 @@ partial void ProcessV1ConversationalRagResponseContent(
4747
using var httpRequest = new global::System.Net.Http.HttpRequestMessage(
4848
method: global::System.Net.Http.HttpMethod.Post,
4949
requestUri: new global::System.Uri(__path, global::System.UriKind.RelativeOrAbsolute));
50+
51+
if (_authorization != null)
52+
{{
53+
httpRequest.Headers.Authorization = new global::System.Net.Http.Headers.AuthenticationHeaderValue(
54+
scheme: _authorization.Name,
55+
parameter: _authorization.Value);
56+
}}
5057
var __httpRequestContentBody = global::Newtonsoft.Json.JsonConvert.SerializeObject(request, JsonSerializerOptions);
5158
var __httpRequestContent = new global::System.Net.Http.StringContent(
5259
content: __httpRequestContentBody,

src/tests/AutoSDK.SnapshotTests/Snapshots/Ai21/NewtonsoftJson/_#G.Api.V1Embed.g.verified.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,13 @@ partial void ProcessV1EmbedResponseContent(
4747
using var httpRequest = new global::System.Net.Http.HttpRequestMessage(
4848
method: global::System.Net.Http.HttpMethod.Post,
4949
requestUri: new global::System.Uri(__path, global::System.UriKind.RelativeOrAbsolute));
50+
51+
if (_authorization != null)
52+
{{
53+
httpRequest.Headers.Authorization = new global::System.Net.Http.Headers.AuthenticationHeaderValue(
54+
scheme: _authorization.Name,
55+
parameter: _authorization.Value);
56+
}}
5057
var __httpRequestContentBody = global::Newtonsoft.Json.JsonConvert.SerializeObject(request, JsonSerializerOptions);
5158
var __httpRequestContent = new global::System.Net.Http.StringContent(
5259
content: __httpRequestContentBody,

src/tests/AutoSDK.SnapshotTests/Snapshots/Ai21/NewtonsoftJson/_#G.ChatClient.V1J2UltraChat.g.verified.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,13 @@ partial void ProcessV1J2UltraChatResponseContent(
5757
using var httpRequest = new global::System.Net.Http.HttpRequestMessage(
5858
method: global::System.Net.Http.HttpMethod.Post,
5959
requestUri: new global::System.Uri(__path, global::System.UriKind.RelativeOrAbsolute));
60+
61+
if (_authorization != null)
62+
{{
63+
httpRequest.Headers.Authorization = new global::System.Net.Http.Headers.AuthenticationHeaderValue(
64+
scheme: _authorization.Name,
65+
parameter: _authorization.Value);
66+
}}
6067
var __httpRequestContentBody = global::Newtonsoft.Json.JsonConvert.SerializeObject(request, JsonSerializerOptions);
6168
var __httpRequestContent = new global::System.Net.Http.StringContent(
6269
content: __httpRequestContentBody,

src/tests/AutoSDK.SnapshotTests/Snapshots/Ai21/NewtonsoftJson/_#G.CompletionClient.V1J2GrandeComplete.g.verified.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,13 @@ partial void ProcessV1J2GrandeCompleteResponseContent(
5757
using var httpRequest = new global::System.Net.Http.HttpRequestMessage(
5858
method: global::System.Net.Http.HttpMethod.Post,
5959
requestUri: new global::System.Uri(__path, global::System.UriKind.RelativeOrAbsolute));
60+
61+
if (_authorization != null)
62+
{{
63+
httpRequest.Headers.Authorization = new global::System.Net.Http.Headers.AuthenticationHeaderValue(
64+
scheme: _authorization.Name,
65+
parameter: _authorization.Value);
66+
}}
6067
var __httpRequestContentBody = global::Newtonsoft.Json.JsonConvert.SerializeObject(request, JsonSerializerOptions);
6168
var __httpRequestContent = new global::System.Net.Http.StringContent(
6269
content: __httpRequestContentBody,

0 commit comments

Comments
 (0)