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 @@ -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>
System.IO.Stream{{nrt?}} 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{{nrt?}} 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 @@ -291,7 +291,6 @@ namespace {{packageName}}.{{apiPackage}}
bool suppressDefaultLog = false;
After{{operationId}}({{#lambda.joinWithComma}}ref suppressDefaultLog apiResponseLocalVar {{#allParams}}{{paramName}} {{/allParams}}{{/lambda.joinWithComma}});
{{>AfterOperationDefaultImplementation}}

}

/// <summary>
Expand Down Expand Up @@ -624,11 +623,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 +740,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
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,7 @@ private void AfterHelloWorldPostDefaultImplementation(IHelloWorldPostApiResponse
bool suppressDefaultLog = false;
AfterHelloWorldPost(ref suppressDefaultLog, apiResponseLocalVar, helloWorldPostRequest);
if (!suppressDefaultLog)
Logger.LogInformation("{0,-9} | {1} | {3}", (apiResponseLocalVar.DownloadedAt - apiResponseLocalVar.RequestedAt).TotalSeconds, apiResponseLocalVar.StatusCode, apiResponseLocalVar.Path);
}
Logger.LogInformation("{0,-9} | {1} | {3}", (apiResponseLocalVar.DownloadedAt - apiResponseLocalVar.RequestedAt).TotalSeconds, apiResponseLocalVar.StatusCode, apiResponseLocalVar.Path); }

/// <summary>
/// Processes the server response
Expand Down Expand Up @@ -268,11 +267,17 @@ public async Task<IHelloWorldPostApiResponse> HelloWorldPostAsync(Option<HelloWo

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

ILogger<HelloWorldPostApiResponse> apiResponseLoggerLocalVar = LoggerFactory.CreateLogger<HelloWorldPostApiResponse>();
HelloWorldPostApiResponse apiResponseLocalVar;

switch ((int)httpResponseMessageLocalVar.StatusCode) {
default: {
string responseContentLocalVar = await httpResponseMessageLocalVar.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false);
apiResponseLocalVar = new(apiResponseLoggerLocalVar, httpRequestMessageLocalVar, httpResponseMessageLocalVar, responseContentLocalVar, "/helloWorld", requestedAtLocalVar, _jsonSerializerOptions);

HelloWorldPostApiResponse apiResponseLocalVar = new(apiResponseLoggerLocalVar, httpRequestMessageLocalVar, httpResponseMessageLocalVar, responseContentLocalVar, "/helloWorld", requestedAtLocalVar, _jsonSerializerOptions);
break;
}
}

AfterHelloWorldPostDefaultImplementation(apiResponseLocalVar, helloWorldPostRequest);

Expand Down Expand Up @@ -316,6 +321,22 @@ public HelloWorldPostApiResponse(ILogger<HelloWorldPostApiResponse> logger, Syst
OnCreated(httpRequestMessage, httpResponseMessage);
}

/// <summary>
/// The <see cref="HelloWorldPostApiResponse"/>
/// </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 HelloWorldPostApiResponse(ILogger<HelloWorldPostApiResponse> 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);

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ public partial interface IApiResponse
/// The raw content of this response.
/// </summary>
string RawContent { get; }

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

/// <summary>
/// The DateTime when the request was retrieved.
Expand Down Expand Up @@ -84,6 +89,11 @@ public partial class ApiResponse : IApiResponse
/// </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 @@ -147,6 +157,30 @@ public ApiResponse(global::System.Net.Http.HttpRequestMessage httpRequestMessage
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,7 @@ private void AfterOneOfArrayDefaultImplementation(IOneOfArrayApiResponse apiResp
bool suppressDefaultLog = false;
AfterOneOfArray(ref suppressDefaultLog, apiResponseLocalVar, oneOfArrayRequest);
if (!suppressDefaultLog)
Logger.LogInformation("{0,-9} | {1} | {3}", (apiResponseLocalVar.DownloadedAt - apiResponseLocalVar.RequestedAt).TotalSeconds, apiResponseLocalVar.StatusCode, apiResponseLocalVar.Path);
}
Logger.LogInformation("{0,-9} | {1} | {3}", (apiResponseLocalVar.DownloadedAt - apiResponseLocalVar.RequestedAt).TotalSeconds, apiResponseLocalVar.StatusCode, apiResponseLocalVar.Path); }

/// <summary>
/// Processes the server response
Expand Down Expand Up @@ -249,11 +248,17 @@ public async Task<IOneOfArrayApiResponse> OneOfArrayAsync(Option<OneOfArrayReque

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

ILogger<OneOfArrayApiResponse> apiResponseLoggerLocalVar = LoggerFactory.CreateLogger<OneOfArrayApiResponse>();
OneOfArrayApiResponse apiResponseLocalVar;

switch ((int)httpResponseMessageLocalVar.StatusCode) {
default: {
string responseContentLocalVar = await httpResponseMessageLocalVar.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false);
apiResponseLocalVar = new(apiResponseLoggerLocalVar, httpRequestMessageLocalVar, httpResponseMessageLocalVar, responseContentLocalVar, "/one-of-array", requestedAtLocalVar, _jsonSerializerOptions);

OneOfArrayApiResponse apiResponseLocalVar = new(apiResponseLoggerLocalVar, httpRequestMessageLocalVar, httpResponseMessageLocalVar, responseContentLocalVar, "/one-of-array", requestedAtLocalVar, _jsonSerializerOptions);
break;
}
}

AfterOneOfArrayDefaultImplementation(apiResponseLocalVar, oneOfArrayRequest);

Expand Down Expand Up @@ -297,6 +302,22 @@ public OneOfArrayApiResponse(ILogger<OneOfArrayApiResponse> logger, System.Net.H
OnCreated(httpRequestMessage, httpResponseMessage);
}

/// <summary>
/// The <see cref="OneOfArrayApiResponse"/>
/// </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 OneOfArrayApiResponse(ILogger<OneOfArrayApiResponse> 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);

/// <summary>
Expand Down
Loading
Loading