Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -33,6 +33,11 @@ namespace {{packageName}}.{{clientPackage}}
/// The raw content of this response.
/// </summary>
string RawContent { get; }

/// <summary>
/// The raw binary stream (only set for binary responses)
/// </summary>
public System.IO.Stream? ContentStream { get; }

/// <summary>
/// The DateTime when the request was retrieved.
Expand Down Expand Up @@ -81,6 +86,11 @@ namespace {{packageName}}.{{clientPackage}}
/// </summary>
public string RawContent { get; protected set; }

/// <summary>
/// The raw binary stream (only set for binary responses)
/// </summary>
public System.IO.Stream? ContentStream { get; protected set; }

/// <summary>
/// The IsSuccessStatusCode from the api response
/// </summary>
Expand Down Expand Up @@ -144,6 +154,30 @@ namespace {{packageName}}.{{clientPackage}}
OnCreated(httpRequestMessage, httpResponseMessage);
}

/// <summary>
/// Construct the response using an HttpResponseMessage
/// </summary>
/// <param name="httpRequestMessage"></param>
/// <param name="httpResponseMessage"></param>
/// <param name="contentStream"></param>
/// <param name="path"></param>
/// <param name="requestedAt"></param>
/// <param name="jsonSerializerOptions"></param>
public ApiResponse(global::System.Net.Http.HttpRequestMessage httpRequestMessage, System.Net.Http.HttpResponseMessage httpResponseMessage, System.IO.Stream contentStream, string path, DateTime requestedAt, System.Text.Json.JsonSerializerOptions jsonSerializerOptions)
{
StatusCode = httpResponseMessage.StatusCode;
Headers = httpResponseMessage.Headers;
IsSuccessStatusCode = httpResponseMessage.IsSuccessStatusCode;
ReasonPhrase = httpResponseMessage.ReasonPhrase;
ContentStream = contentStream;
RawContent = string.Empty;
Path = path;
RequestUri = httpRequestMessage.RequestUri;
RequestedAt = requestedAt;
_jsonSerializerOptions = jsonSerializerOptions;
OnCreated(httpRequestMessage, httpResponseMessage);
}

partial void OnCreated(global::System.Net.Http.HttpRequestMessage httpRequestMessage, System.Net.Http.HttpResponseMessage httpResponseMessage);
}
{{#x-http-statuses-with-return}}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// This logic may be modified with the AsModel.mustache template
return Is{{vendorExtensions.x-http-status}}
? System.Text.Json.JsonSerializer.Deserialize<{{#isModel}}{{^containerType}}{{packageName}}.{{modelPackage}}.{{/containerType}}{{/isModel}}{{{dataType}}}>(RawContent, _jsonSerializerOptions)
? {{#isBinary}}ContentStream{{/isBinary}}{{^isBinary}}System.Text.Json.JsonSerializer.Deserialize<{{#isModel}}{{^containerType}}{{packageName}}.{{modelPackage}}.{{/containerType}}{{/isModel}}{{{dataType}}}>(RawContent, _jsonSerializerOptions){{/isBinary}}
: {{#net60OrLater}}null{{/net60OrLater}}{{^net60OrLater}}default{{/net60OrLater}};
Original file line number Diff line number Diff line change
Expand Up @@ -624,11 +624,35 @@ namespace {{packageName}}.{{apiPackage}}

using (HttpResponseMessage httpResponseMessageLocalVar = await HttpClient.SendAsync(httpRequestMessageLocalVar, cancellationToken).ConfigureAwait(false))
{
string responseContentLocalVar = await httpResponseMessageLocalVar.Content.ReadAsStringAsync({{#net60OrLater}}cancellationToken{{/net60OrLater}}).ConfigureAwait(false);

ILogger<{{#vendorExtensions.x-duplicates}}{{.}}.{{/vendorExtensions.x-duplicates}}{{operationId}}ApiResponse> apiResponseLoggerLocalVar = LoggerFactory.CreateLogger<{{#vendorExtensions.x-duplicates}}{{.}}.{{/vendorExtensions.x-duplicates}}{{operationId}}ApiResponse>();
{{#vendorExtensions.x-duplicates}}{{.}}.{{/vendorExtensions.x-duplicates}}{{operationId}}ApiResponse apiResponseLocalVar;

switch ((int)httpResponseMessageLocalVar.StatusCode) {
{{#responses}}
{{#isBinary}}
case ({{code}}):
{{/isBinary}}
{{/responses}}
{{#responses}}
{{#isBinary}}
{{#-first}}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess you want the first response which is a binary, but i don't think this first will filter the way you want.

Copy link
Copy Markdown
Contributor Author

@alec-petersen alec-petersen Aug 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How do ya mean? And yeah the goal here is just to add the section if there is a binary response, there may be a better way of accomplishing that in a more mustachey way haha

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the first will get rendered every time. Pretty sure we can only use first like this on arrays.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a first lambda besides the one you used here but it requires a two space delimiter which is not ideal for a multiline string.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems to have the effect I intended, I'd need to understand the failure scenario maybe.

{
byte[] responseBytesArrayLocalVar = await httpResponseMessageLocalVar.Content.ReadAsByteArrayAsync({{#net60OrLater}}cancellationToken{{/net60OrLater}}).ConfigureAwait(false);
System.IO.Stream responseContentStreamLocalVar = new System.IO.MemoryStream(responseBytesArrayLocalVar);
apiResponseLocalVar = new{{^net60OrLater}} {{operationId}}ApiResponse{{/net60OrLater}}(apiResponseLoggerLocalVar, httpRequestMessageLocalVar, httpResponseMessageLocalVar, responseContentStreamLocalVar, "{{{path}}}", requestedAtLocalVar, _jsonSerializerOptions);

{{#vendorExtensions.x-duplicates}}{{.}}.{{/vendorExtensions.x-duplicates}}{{operationId}}ApiResponse apiResponseLocalVar = new{{^net60OrLater}} {{operationId}}ApiResponse{{/net60OrLater}}(apiResponseLoggerLocalVar, httpRequestMessageLocalVar, httpResponseMessageLocalVar, responseContentLocalVar, "{{{path}}}", requestedAtLocalVar, _jsonSerializerOptions);
break;
}
{{/-first}}
{{/isBinary}}
{{/responses}}
default: {
string responseContentLocalVar = await httpResponseMessageLocalVar.Content.ReadAsStringAsync({{#net60OrLater}}cancellationToken{{/net60OrLater}}).ConfigureAwait(false);
apiResponseLocalVar = new{{^net60OrLater}} {{operationId}}ApiResponse{{/net60OrLater}}(apiResponseLoggerLocalVar, httpRequestMessageLocalVar, httpResponseMessageLocalVar, responseContentLocalVar, "{{{path}}}", requestedAtLocalVar, _jsonSerializerOptions);

break;
}
}
Comment thread
alec-petersen marked this conversation as resolved.

After{{operationId}}DefaultImplementation({{#lambda.joinWithComma}}apiResponseLocalVar {{#allParams}}{{paramName}} {{/allParams}}{{/lambda.joinWithComma}});

Expand Down Expand Up @@ -717,6 +741,22 @@ namespace {{packageName}}.{{apiPackage}}
OnCreated(httpRequestMessage, httpResponseMessage);
}

/// <summary>
/// The <see cref="{{operationId}}ApiResponse"/>
/// </summary>
/// <param name="logger"></param>
/// <param name="httpRequestMessage"></param>
/// <param name="httpResponseMessage"></param>
/// <param name="contentStream"></param>
/// <param name="path"></param>
/// <param name="requestedAt"></param>
/// <param name="jsonSerializerOptions"></param>
public {{operationId}}ApiResponse(ILogger<{{operationId}}ApiResponse> logger, System.Net.Http.HttpRequestMessage httpRequestMessage, System.Net.Http.HttpResponseMessage httpResponseMessage, System.IO.Stream contentStream, string path, DateTime requestedAt, System.Text.Json.JsonSerializerOptions jsonSerializerOptions) : base(httpRequestMessage, httpResponseMessage, contentStream, path, requestedAt, jsonSerializerOptions)
{
Logger = logger;
OnCreated(httpRequestMessage, httpResponseMessage);
}

partial void OnCreated(global::System.Net.Http.HttpRequestMessage httpRequestMessage, System.Net.Http.HttpResponseMessage httpResponseMessage);
{{#responses}}

Expand Down
Loading