Skip to content

Commit

Permalink
Generate response headers dictionary only when used.
Browse files Browse the repository at this point in the history
  • Loading branch information
paulomorgado committed Nov 22, 2023
1 parent 37f7c42 commit c67a59c
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 103 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
public bool ReadResponseAsString { get; set; }

protected virtual async System.Threading.Tasks.Task<ObjectResponseResult<T>> ReadObjectResponseAsync<T>(System.Net.Http.HttpResponseMessage response, System.Collections.Generic.IReadOnlyDictionary<string, System.Collections.Generic.IEnumerable<string>> headers, System.Threading.CancellationToken cancellationToken)
protected virtual async System.Threading.Tasks.Task<ObjectResponseResult<T>> ReadObjectResponseAsync<T>(System.Net.Http.HttpResponseMessage response, System.Collections.Generic.IReadOnlyDictionary<string, System.Threading.CancellationToken cancellationToken)
{
if (response == null || response.Content == null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,31 +92,23 @@ namespace MyNamespace
var disposeResponse_ = true;
try
{
var headers_ = new System.Collections.Generic.Dictionary<string, System.Collections.Generic.IEnumerable<string>>();
foreach (var item_ in response_.Headers)
headers_[item_.Key] = item_.Value;
if (response_.Content != null && response_.Content.Headers != null)
{
foreach (var item_ in response_.Content.Headers)
headers_[item_.Key] = item_.Value;
}


ProcessResponse(client_, response_);

var status_ = (int)response_.StatusCode;
if (status_ == 200)
{
var objectResponse_ = await ReadObjectResponseAsync<string>(response_, headers_, cancellationToken).ConfigureAwait(false);
var objectResponse_ = await ReadObjectResponseAsync<string>(response_, cancellationToken).ConfigureAwait(false);
if (objectResponse_.Object == null)
{
throw new ApiException("Response was null which was not expected.", status_, objectResponse_.Text, headers_, null);
throw new ApiException("Response was null which was not expected.", status_, objectResponse_.Text, GetResponseHeaders(response_), null);
}
return objectResponse_.Object;
}
else
{
var responseData_ = response_.Content == null ? null : await response_.Content.ReadAsStringAsync().ConfigureAwait(false);
throw new ApiException("The HTTP status code of the response was not expected (" + status_ + ").", status_, responseData_, headers_, null);
throw new ApiException("The HTTP status code of the response was not expected (" + status_ + ").", status_, responseData_, GetResponseHeaders(response_), null);
}
}
finally
Expand Down Expand Up @@ -177,31 +169,23 @@ namespace MyNamespace
var disposeResponse_ = true;
try
{
var headers_ = new System.Collections.Generic.Dictionary<string, System.Collections.Generic.IEnumerable<string>>();
foreach (var item_ in response_.Headers)
headers_[item_.Key] = item_.Value;
if (response_.Content != null && response_.Content.Headers != null)
{
foreach (var item_ in response_.Content.Headers)
headers_[item_.Key] = item_.Value;
}


ProcessResponse(client_, response_);

var status_ = (int)response_.StatusCode;
if (status_ == 200)
{
var objectResponse_ = await ReadObjectResponseAsync<int>(response_, headers_, cancellationToken).ConfigureAwait(false);
var objectResponse_ = await ReadObjectResponseAsync<int>(response_, cancellationToken).ConfigureAwait(false);
if (objectResponse_.Object == null)
{
throw new ApiException("Response was null which was not expected.", status_, objectResponse_.Text, headers_, null);
throw new ApiException("Response was null which was not expected.", status_, objectResponse_.Text, GetResponseHeaders(response_), null);
}
return objectResponse_.Object;
}
else
{
var responseData_ = response_.Content == null ? null : await response_.Content.ReadAsStringAsync().ConfigureAwait(false);
throw new ApiException("The HTTP status code of the response was not expected (" + status_ + ").", status_, responseData_, headers_, null);
throw new ApiException("The HTTP status code of the response was not expected (" + status_ + ").", status_, responseData_, GetResponseHeaders(response_), null);
}
}
finally
Expand Down Expand Up @@ -258,29 +242,23 @@ namespace MyNamespace
var disposeResponse_ = true;
try
{
var headers_ = System.Linq.Enumerable.ToDictionary(response_.Headers, h_ => h_.Key, h_ => h_.Value);
if (response_.Content != null && response_.Content.Headers != null)
{
foreach (var item_ in response_.Content.Headers)
headers_[item_.Key] = item_.Value;
}


ProcessResponse(client_, response_);

var status_ = (int)response_.StatusCode;
if (status_ == 200)
{
var objectResponse_ = await ReadObjectResponseAsync<int>(response_, headers_, cancellationToken).ConfigureAwait(false);
var objectResponse_ = await ReadObjectResponseAsync<int>(response_, cancellationToken).ConfigureAwait(false);
if (objectResponse_.Object == null)
{
throw new ApiException("Response was null which was not expected.", status_, objectResponse_.Text, headers_, null);
throw new ApiException("Response was null which was not expected.", status_, objectResponse_.Text, GetResponseHeaders(response_), null);
}
return objectResponse_.Object;
}
else
{
var responseData_ = response_.Content == null ? null : await response_.Content.ReadAsStringAsync().ConfigureAwait(false);
throw new ApiException("The HTTP status code of the response was not expected (" + status_ + ").", status_, responseData_, headers_, null);
throw new ApiException("The HTTP status code of the response was not expected (" + status_ + ").", status_, responseData_, GetResponseHeaders(response_), null);
}
}
finally
Expand Down Expand Up @@ -310,9 +288,22 @@ namespace MyNamespace
public string Text { get; }
}

private static System.Collections.Generic.Dictionary<string, System.Collections.Generic.IEnumerable<string>> GetResponseHeaders(System.Net.Http.HttpResponseMessage response)
{
var headers = new System.Collections.Generic.Dictionary<string, System.Collections.Generic.IEnumerable<string>>();
foreach (var item in response.Headers)
headers[item.Key] = item.Value;
if (response.Content != null && response.Content.Headers != null)
{
foreach (var item in response.Content.Headers)
headers[item.Key] = item.Value;
}
return headers;
}

public bool ReadResponseAsString { get; set; }

protected virtual async System.Threading.Tasks.Task<ObjectResponseResult<T>> ReadObjectResponseAsync<T>(System.Net.Http.HttpResponseMessage response, System.Collections.Generic.IReadOnlyDictionary<string, System.Collections.Generic.IEnumerable<string>> headers, System.Threading.CancellationToken cancellationToken)
protected virtual async System.Threading.Tasks.Task<ObjectResponseResult<T>> ReadObjectResponseAsync<T>(System.Net.Http.HttpResponseMessage response, System.Collections.Generic.IReadOnlyDictionary<string, System.Threading.CancellationToken cancellationToken)
{
if (response == null || response.Content == null)
{
Expand All @@ -330,7 +321,7 @@ namespace MyNamespace
catch (Newtonsoft.Json.JsonException exception)
{
var message = "Could not deserialize the response body string as " + typeof(T).FullName + ".";
throw new ApiException(message, (int)response.StatusCode, responseText, headers, exception);
throw new ApiException(message, (int)response.StatusCode, responseText, GetResponseHeaders(response), exception);
}
}
else
Expand All @@ -349,7 +340,7 @@ namespace MyNamespace
catch (Newtonsoft.Json.JsonException exception)
{
var message = "Could not deserialize the response body stream as " + typeof(T).FullName + ".";
throw new ApiException(message, (int)response.StatusCode, string.Empty, headers, exception);
throw new ApiException(message, (int)response.StatusCode, string.Empty, GetResponseHeaders(response), exception);
}
}
}
Expand Down Expand Up @@ -483,29 +474,21 @@ namespace MyNamespace
var disposeResponse_ = true;
try
{
var headers_ = new System.Collections.Generic.Dictionary<string, System.Collections.Generic.IEnumerable<string>>();
foreach (var item_ in response_.Headers)
headers_[item_.Key] = item_.Value;
if (response_.Content != null && response_.Content.Headers != null)
{
foreach (var item_ in response_.Content.Headers)
headers_[item_.Key] = item_.Value;
}


ProcessResponse(client_, response_);

var status_ = (int)response_.StatusCode;
if (status_ == 200 || status_ == 206)
{
var responseStream_ = response_.Content == null ? System.IO.Stream.Null : await response_.Content.ReadAsStreamAsync().ConfigureAwait(false);
var fileResponse_ = new FileResponse(status_, headers_, responseStream_, null, response_);
var fileResponse_ = new FileResponse(status_, GetResponseHeaders(response_), responseStream_, null, response_);
disposeClient_ = false; disposeResponse_ = false; // response and client are disposed by FileResponse
return fileResponse_;
}
else
{
var responseData_ = response_.Content == null ? null : await response_.Content.ReadAsStringAsync().ConfigureAwait(false);
throw new ApiException("The HTTP status code of the response was not expected (" + status_ + ").", status_, responseData_, headers_, null);
throw new ApiException("The HTTP status code of the response was not expected (" + status_ + ").", status_, responseData_, GetResponseHeaders(response_), null);
}
}
finally
Expand Down Expand Up @@ -535,9 +518,22 @@ namespace MyNamespace
public string Text { get; }
}

private static System.Collections.Generic.Dictionary<string, System.Collections.Generic.IEnumerable<string>> GetResponseHeaders(System.Net.Http.HttpResponseMessage response)
{
var headers = new System.Collections.Generic.Dictionary<string, System.Collections.Generic.IEnumerable<string>>();
foreach (var item in response.Headers)
headers[item.Key] = item.Value;
if (response.Content != null && response.Content.Headers != null)
{
foreach (var item in response.Content.Headers)
headers[item.Key] = item.Value;
}
return headers;
}

public bool ReadResponseAsString { get; set; }

protected virtual async System.Threading.Tasks.Task<ObjectResponseResult<T>> ReadObjectResponseAsync<T>(System.Net.Http.HttpResponseMessage response, System.Collections.Generic.IReadOnlyDictionary<string, System.Collections.Generic.IEnumerable<string>> headers, System.Threading.CancellationToken cancellationToken)
protected virtual async System.Threading.Tasks.Task<ObjectResponseResult<T>> ReadObjectResponseAsync<T>(System.Net.Http.HttpResponseMessage response, System.Collections.Generic.IReadOnlyDictionary<string, System.Threading.CancellationToken cancellationToken)
{
if (response == null || response.Content == null)
{
Expand All @@ -555,7 +551,7 @@ namespace MyNamespace
catch (Newtonsoft.Json.JsonException exception)
{
var message = "Could not deserialize the response body string as " + typeof(T).FullName + ".";
throw new ApiException(message, (int)response.StatusCode, responseText, headers, exception);
throw new ApiException(message, (int)response.StatusCode, responseText, GetResponseHeaders(response), exception);
}
}
else
Expand All @@ -574,7 +570,7 @@ namespace MyNamespace
catch (Newtonsoft.Json.JsonException exception)
{
var message = "Could not deserialize the response body stream as " + typeof(T).FullName + ".";
throw new ApiException(message, (int)response.StatusCode, string.Empty, headers, exception);
throw new ApiException(message, (int)response.StatusCode, string.Empty, GetResponseHeaders(response), exception);
}
}
}
Expand Down

0 comments on commit c67a59c

Please sign in to comment.