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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,9 @@ namespace {{packageName}}.Client
/// </remarks>
{{>visibility}} partial class ApiClient : IDisposable, ISynchronousClient{{#supportsAsync}}, IAsynchronousClient{{/supportsAsync}}
{
{{#net60OrLater}}
private static readonly HttpRequestOptionsKey<List<Cookie>> _httpOptionsCookieContainerKey = new("CookieContainer");
{{/net60OrLater}}
private readonly string _baseUrl;

private readonly HttpClientHandler _httpClientHandler;
Expand Down Expand Up @@ -382,12 +385,15 @@ namespace {{packageName}}.Client
}
}



// TODO provide an alternative that allows cookies per request instead of per API client
if (options.Cookies != null && options.Cookies.Count > 0)
{
{{#net60OrLater}}
request.Options.Set(_httpOptionsCookieContainerKey, options.Cookies);
{{/net60OrLater}}
{{^net60OrLater}}
request.Properties["CookieContainer"] = options.Cookies;
{{/net60OrLater}}
}

return request;
Expand Down Expand Up @@ -474,9 +480,16 @@ namespace {{packageName}}.Client
_httpClientHandler.ClientCertificates.AddRange(configuration.ClientCertificates);
}

{{^net60OrLater}}
var cookieContainer = req.Properties.ContainsKey("CookieContainer") ? req.Properties["CookieContainer"] as List<Cookie> : null;
{{/net60OrLater}}

{{#net60OrLater}}
if (req.Options.TryGetValue(_httpOptionsCookieContainerKey, out var cookieContainer))
{{/net60OrLater}}
{{^net60OrLater}}
if (cookieContainer != null)
{{/net60OrLater}}
{
if(_httpClientHandler == null) throw new InvalidOperationException("Request property `CookieContainer` not supported when the client is explicitly created without an HttpClientHandler, use the proper constructor.");
foreach (var cookie in cookieContainer)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ namespace {{packageName}}.Client

public async Task<T> Deserialize<T>(HttpResponseMessage response)
{
var result = (T) await Deserialize(response, typeof(T)).ConfigureAwait(false);
var result = (T)await Deserialize(response, typeof(T)).ConfigureAwait(false);
return result;
}

Expand All @@ -95,13 +95,13 @@ namespace {{packageName}}.Client
// process response headers, e.g. Access-Control-Allow-Methods
foreach (var responseHeader in response.Headers)
{
headers.Add(responseHeader.Key + "=" + ClientUtils.ParameterToString(responseHeader.Value));
headers.Add(responseHeader.Key + "=" + ClientUtils.ParameterToString(responseHeader.Value));
}

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

// RFC 2183 & RFC 2616
Expand All @@ -112,7 +112,8 @@ namespace {{packageName}}.Client
}
else if (type == typeof(FileParameter))
{
if (headers != null) {
if (headers != null)
{
foreach (var header in headers)
{
var match = fileNameRegex.Match(header.ToString());
Expand Down Expand Up @@ -191,6 +192,9 @@ namespace {{packageName}}.Client
/// </remarks>
{{>visibility}} partial class ApiClient : IDisposable, ISynchronousClient{{#supportsAsync}}, IAsynchronousClient{{/supportsAsync}}
{
{{#net60OrLater}}
private static readonly HttpRequestOptionsKey<List<Cookie>> _httpOptionsCookieContainerKey = new("CookieContainer");
{{/net60OrLater}}
private readonly string _baseUrl;

private readonly HttpClientHandler _httpClientHandler;
Expand Down Expand Up @@ -283,7 +287,8 @@ namespace {{packageName}}.Client
/// </summary>
public void Dispose()
{
if(_disposeClient) {
if(_disposeClient)
{
_httpClient.Dispose();
}
}
Expand Down Expand Up @@ -413,7 +418,12 @@ namespace {{packageName}}.Client
// TODO provide an alternative that allows cookies per request instead of per API client
if (options.Cookies != null && options.Cookies.Count > 0)
{
{{#net60OrLater}}
request.Options.Set(_httpOptionsCookieContainerKey, options.Cookies);
{{/net60OrLater}}
{{^net60OrLater}}
request.Properties["CookieContainer"] = options.Cookies;
{{/net60OrLater}}
}

return request;
Expand All @@ -424,7 +434,7 @@ namespace {{packageName}}.Client

private async Task<ApiResponse<T>> ToApiResponse<T>(HttpResponseMessage response, object responseData, Uri uri)
{
T result = (T) responseData;
T result = (T)responseData;
string rawContent = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

var transformed = new ApiResponse<T>(response.StatusCode, new Multimap<string, string>({{#caseInsensitiveResponseHeaders}}StringComparer.OrdinalIgnoreCase{{/caseInsensitiveResponseHeaders}}), result, rawContent)
Expand Down Expand Up @@ -459,7 +469,7 @@ namespace {{packageName}}.Client
transformed.Cookies.Add(cookie);
}
}
catch (PlatformNotSupportedException) {}
catch (PlatformNotSupportedException) { }
}

return transformed;
Expand Down Expand Up @@ -496,15 +506,22 @@ namespace {{packageName}}.Client

if (configuration.ClientCertificates != null)
{
if(_httpClientHandler == null) throw new InvalidOperationException("Configuration `ClientCertificates` not supported when the client is explicitly created without an HttpClientHandler, use the proper constructor.");
if (_httpClientHandler == null) throw new InvalidOperationException("Configuration `ClientCertificates` not supported when the client is explicitly created without an HttpClientHandler, use the proper constructor.");
_httpClientHandler.ClientCertificates.AddRange(configuration.ClientCertificates);
}

{{^net60OrLater}}
var cookieContainer = req.Properties.ContainsKey("CookieContainer") ? req.Properties["CookieContainer"] as List<Cookie> : null;
{{/net60OrLater}}

{{#net60OrLater}}
if (req.Options.TryGetValue(_httpOptionsCookieContainerKey, out var cookieContainer))
{{/net60OrLater}}
{{^net60OrLater}}
if (cookieContainer != null)
{{/net60OrLater}}
{
if(_httpClientHandler == null) throw new InvalidOperationException("Request property `CookieContainer` not supported when the client is explicitly created without an HttpClientHandler, use the proper constructor.");
if (_httpClientHandler == null) throw new InvalidOperationException("Request property `CookieContainer` not supported when the client is explicitly created without an HttpClientHandler, use the proper constructor.");
foreach (var cookie in cookieContainer)
{
_httpClientHandler.CookieContainer.Add(cookie);
Expand Down Expand Up @@ -546,11 +563,11 @@ namespace {{packageName}}.Client
// if the response type is oneOf/anyOf, call FromJSON to deserialize the data
if (typeof({{{packageName}}}.{{modelPackage}}.AbstractOpenAPISchema).IsAssignableFrom(typeof(T)))
{
responseData = (T) typeof(T).GetMethod("FromJson").Invoke(null, new object[] { response.Content });
responseData = (T)typeof(T).GetMethod("FromJson").Invoke(null, new object[] { response.Content });
}
else if (typeof(T).Name == "Stream") // for binary response
{
responseData = (T) (object) await response.Content.ReadAsStreamAsync().ConfigureAwait(false);
responseData = (T)(object) await response.Content.ReadAsStreamAsync().ConfigureAwait(false);
}

InterceptResponse(req, response);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public string Serialize(object obj)

public async Task<T> Deserialize<T>(HttpResponseMessage response)
{
var result = (T) await Deserialize(response, typeof(T)).ConfigureAwait(false);
var result = (T)await Deserialize(response, typeof(T)).ConfigureAwait(false);
return result;
}

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

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

// RFC 2183 & RFC 2616
Expand All @@ -116,7 +116,8 @@ internal async Task<object> Deserialize(HttpResponseMessage response, Type type)
}
else if (type == typeof(FileParameter))
{
if (headers != null) {
if (headers != null)
{
foreach (var header in headers)
{
var match = fileNameRegex.Match(header.ToString());
Expand Down Expand Up @@ -195,6 +196,7 @@ public string ContentType
/// </remarks>
public partial class ApiClient : IDisposable, ISynchronousClient, IAsynchronousClient
{
private static readonly HttpRequestOptionsKey<List<Cookie>> _httpOptionsCookieContainerKey = new("CookieContainer");
private readonly string _baseUrl;

private readonly HttpClientHandler _httpClientHandler;
Expand Down Expand Up @@ -287,7 +289,8 @@ public ApiClient(HttpClient client, string basePath, HttpClientHandler handler =
/// </summary>
public void Dispose()
{
if(_disposeClient) {
if(_disposeClient)
{
_httpClient.Dispose();
}
}
Expand Down Expand Up @@ -415,7 +418,7 @@ private HttpRequestMessage NewRequest(
// TODO provide an alternative that allows cookies per request instead of per API client
if (options.Cookies != null && options.Cookies.Count > 0)
{
request.Properties["CookieContainer"] = options.Cookies;
request.Options.Set(_httpOptionsCookieContainerKey, options.Cookies);
}

return request;
Expand All @@ -426,7 +429,7 @@ private HttpRequestMessage NewRequest(

private async Task<ApiResponse<T>> ToApiResponse<T>(HttpResponseMessage response, object responseData, Uri uri)
{
T result = (T) responseData;
T result = (T)responseData;
string rawContent = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

var transformed = new ApiResponse<T>(response.StatusCode, new Multimap<string, string>(), result, rawContent)
Expand Down Expand Up @@ -461,7 +464,7 @@ private async Task<ApiResponse<T>> ToApiResponse<T>(HttpResponseMessage response
transformed.Cookies.Add(cookie);
}
}
catch (PlatformNotSupportedException) {}
catch (PlatformNotSupportedException) { }
}

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

if (configuration.ClientCertificates != null)
{
if(_httpClientHandler == null) throw new InvalidOperationException("Configuration `ClientCertificates` not supported when the client is explicitly created without an HttpClientHandler, use the proper constructor.");
if (_httpClientHandler == null) throw new InvalidOperationException("Configuration `ClientCertificates` not supported when the client is explicitly created without an HttpClientHandler, use the proper constructor.");
_httpClientHandler.ClientCertificates.AddRange(configuration.ClientCertificates);
}

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

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

InterceptResponse(req, response);
Expand Down
Loading
Loading