Skip to content

Commit 3d00fd2

Browse files
committed
update C# samples
1 parent 00d1649 commit 3d00fd2

File tree

1 file changed

+16
-14
lines changed
  • samples/client/petstore/csharp/httpclient/net9/Petstore-nonPublicApi/src/Org.OpenAPITools/Client

1 file changed

+16
-14
lines changed

samples/client/petstore/csharp/httpclient/net9/Petstore-nonPublicApi/src/Org.OpenAPITools/Client/ApiClient.cs

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public string Serialize(object obj)
8383

8484
public async Task<T> Deserialize<T>(HttpResponseMessage response)
8585
{
86-
var result = (T) await Deserialize(response, typeof(T)).ConfigureAwait(false);
86+
var result = (T)await Deserialize(response, typeof(T)).ConfigureAwait(false);
8787
return result;
8888
}
8989

@@ -99,13 +99,13 @@ internal async Task<object> Deserialize(HttpResponseMessage response, Type type)
9999
// process response headers, e.g. Access-Control-Allow-Methods
100100
foreach (var responseHeader in response.Headers)
101101
{
102-
headers.Add(responseHeader.Key + "=" + ClientUtils.ParameterToString(responseHeader.Value));
102+
headers.Add(responseHeader.Key + "=" + ClientUtils.ParameterToString(responseHeader.Value));
103103
}
104104

105105
// process response content headers, e.g. Content-Type
106106
foreach (var responseHeader in response.Content.Headers)
107107
{
108-
headers.Add(responseHeader.Key + "=" + ClientUtils.ParameterToString(responseHeader.Value));
108+
headers.Add(responseHeader.Key + "=" + ClientUtils.ParameterToString(responseHeader.Value));
109109
}
110110

111111
// RFC 2183 & RFC 2616
@@ -116,7 +116,8 @@ internal async Task<object> Deserialize(HttpResponseMessage response, Type type)
116116
}
117117
else if (type == typeof(FileParameter))
118118
{
119-
if (headers != null) {
119+
if (headers != null)
120+
{
120121
foreach (var header in headers)
121122
{
122123
var match = fileNameRegex.Match(header.ToString());
@@ -195,6 +196,7 @@ public string ContentType
195196
/// </remarks>
196197
internal partial class ApiClient : IDisposable, ISynchronousClient, IAsynchronousClient
197198
{
199+
private static readonly HttpRequestOptionsKey<List<Cookie>> _httpOptionsCookieContainerKey = new("CookieContainer");
198200
private readonly string _baseUrl;
199201

200202
private readonly HttpClientHandler _httpClientHandler;
@@ -287,7 +289,8 @@ public ApiClient(HttpClient client, string basePath, HttpClientHandler handler =
287289
/// </summary>
288290
public void Dispose()
289291
{
290-
if(_disposeClient) {
292+
if(_disposeClient)
293+
{
291294
_httpClient.Dispose();
292295
}
293296
}
@@ -415,7 +418,7 @@ private HttpRequestMessage NewRequest(
415418
// TODO provide an alternative that allows cookies per request instead of per API client
416419
if (options.Cookies != null && options.Cookies.Count > 0)
417420
{
418-
request.Properties["CookieContainer"] = options.Cookies;
421+
request.Options.Set(_httpOptionsCookieContainerKey, options.Cookies);
419422
}
420423

421424
return request;
@@ -426,7 +429,7 @@ private HttpRequestMessage NewRequest(
426429

427430
private async Task<ApiResponse<T>> ToApiResponse<T>(HttpResponseMessage response, object responseData, Uri uri)
428431
{
429-
T result = (T) responseData;
432+
T result = (T)responseData;
430433
string rawContent = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
431434

432435
var transformed = new ApiResponse<T>(response.StatusCode, new Multimap<string, string>(), result, rawContent)
@@ -461,7 +464,7 @@ private async Task<ApiResponse<T>> ToApiResponse<T>(HttpResponseMessage response
461464
transformed.Cookies.Add(cookie);
462465
}
463466
}
464-
catch (PlatformNotSupportedException) {}
467+
catch (PlatformNotSupportedException) { }
465468
}
466469

467470
return transformed;
@@ -498,15 +501,14 @@ private async Task<ApiResponse<T>> ExecAsync<T>(HttpRequestMessage req,
498501

499502
if (configuration.ClientCertificates != null)
500503
{
501-
if(_httpClientHandler == null) throw new InvalidOperationException("Configuration `ClientCertificates` not supported when the client is explicitly created without an HttpClientHandler, use the proper constructor.");
504+
if (_httpClientHandler == null) throw new InvalidOperationException("Configuration `ClientCertificates` not supported when the client is explicitly created without an HttpClientHandler, use the proper constructor.");
502505
_httpClientHandler.ClientCertificates.AddRange(configuration.ClientCertificates);
503506
}
504507

505-
var cookieContainer = req.Properties.ContainsKey("CookieContainer") ? req.Properties["CookieContainer"] as List<Cookie> : null;
506508

507-
if (cookieContainer != null)
509+
if (req.Options.TryGetValue(_httpOptionsCookieContainerKey, out var cookieContainer))
508510
{
509-
if(_httpClientHandler == null) throw new InvalidOperationException("Request property `CookieContainer` not supported when the client is explicitly created without an HttpClientHandler, use the proper constructor.");
511+
if (_httpClientHandler == null) throw new InvalidOperationException("Request property `CookieContainer` not supported when the client is explicitly created without an HttpClientHandler, use the proper constructor.");
510512
foreach (var cookie in cookieContainer)
511513
{
512514
_httpClientHandler.CookieContainer.Add(cookie);
@@ -544,11 +546,11 @@ private async Task<ApiResponse<T>> ExecAsync<T>(HttpRequestMessage req,
544546
// if the response type is oneOf/anyOf, call FromJSON to deserialize the data
545547
if (typeof(Org.OpenAPITools.Model.AbstractOpenAPISchema).IsAssignableFrom(typeof(T)))
546548
{
547-
responseData = (T) typeof(T).GetMethod("FromJson").Invoke(null, new object[] { response.Content });
549+
responseData = (T)typeof(T).GetMethod("FromJson").Invoke(null, new object[] { response.Content });
548550
}
549551
else if (typeof(T).Name == "Stream") // for binary response
550552
{
551-
responseData = (T) (object) await response.Content.ReadAsStreamAsync().ConfigureAwait(false);
553+
responseData = (T)(object) await response.Content.ReadAsStreamAsync().ConfigureAwait(false);
552554
}
553555

554556
InterceptResponse(req, response);

0 commit comments

Comments
 (0)