@@ -88,6 +88,11 @@ internal sealed class RestApiOperationRunner
88
88
/// </summary>
89
89
private readonly HttpResponseContentReader ? _httpResponseContentReader ;
90
90
91
+ /// <summary>
92
+ /// The external response factory for creating <see cref="RestApiOperationResponse"/>.
93
+ /// </summary>
94
+ private readonly RestApiOperationResponseFactory ? _responseFactory ;
95
+
91
96
/// <summary>
92
97
/// The external URL factory to use if provided, instead of the default one.
93
98
/// </summary>
@@ -115,6 +120,7 @@ internal sealed class RestApiOperationRunner
115
120
/// <param name="enablePayloadNamespacing">Determines whether payload parameters are resolved from the arguments by
116
121
/// full name (parameter name prefixed with the parent property name).</param>
117
122
/// <param name="httpResponseContentReader">Custom HTTP response content reader.</param>
123
+ /// <param name="responseFactory">The external response factory for creating <see cref="RestApiOperationResponse"/>.</param>
118
124
/// <param name="urlFactory">The external URL factory to use if provided if provided instead of the default one.</param>
119
125
/// <param name="headersFactory">The external headers factory to use if provided instead of the default one.</param>
120
126
/// <param name="payloadFactory">The external payload factory to use if provided instead of the default one.</param>
@@ -125,6 +131,7 @@ public RestApiOperationRunner(
125
131
bool enableDynamicPayload = false ,
126
132
bool enablePayloadNamespacing = false ,
127
133
HttpResponseContentReader ? httpResponseContentReader = null ,
134
+ RestApiOperationResponseFactory ? responseFactory = null ,
128
135
RestApiOperationUrlFactory ? urlFactory = null ,
129
136
RestApiOperationHeadersFactory ? headersFactory = null ,
130
137
RestApiOperationPayloadFactory ? payloadFactory = null )
@@ -134,6 +141,7 @@ public RestApiOperationRunner(
134
141
this . _enableDynamicPayload = enableDynamicPayload ;
135
142
this . _enablePayloadNamespacing = enablePayloadNamespacing ;
136
143
this . _httpResponseContentReader = httpResponseContentReader ;
144
+ this . _responseFactory = responseFactory ;
137
145
this . _urlFactory = urlFactory ;
138
146
this . _headersFactory = headersFactory ;
139
147
this . _payloadFactory = payloadFactory ;
@@ -577,11 +585,31 @@ private Uri BuildsOperationUrl(RestApiOperation operation, IDictionary<string, o
577
585
/// <returns>The operation response.</returns>
578
586
private async Task < RestApiOperationResponse > BuildResponseAsync ( RestApiOperation operation , HttpRequestMessage requestMessage , HttpResponseMessage responseMessage , object ? payload , CancellationToken cancellationToken )
579
587
{
580
- var response = await this . ReadContentAndCreateOperationResponseAsync ( requestMessage , responseMessage , payload , cancellationToken ) . ConfigureAwait ( false ) ;
588
+ async Task < RestApiOperationResponse > Build ( RestApiOperationResponseFactoryContext context , CancellationToken ct )
589
+ {
590
+ var response = await this . ReadContentAndCreateOperationResponseAsync ( context . Request , context . Response , payload , ct ) . ConfigureAwait ( false ) ;
591
+
592
+ response . ExpectedSchema ??= GetExpectedSchema ( context . Operation . Responses . ToDictionary ( item => item . Key , item => item . Value . Schema ) , context . Response . StatusCode ) ;
593
+
594
+ return response ;
595
+ }
581
596
582
- response . ExpectedSchema ??= GetExpectedSchema ( operation . Responses . ToDictionary ( item => item . Key , item => item . Value . Schema ) , responseMessage . StatusCode ) ;
597
+ // Delegate the response building to the custom response factory if provided.
598
+ if ( this . _responseFactory is not null )
599
+ {
600
+ var response = await this . _responseFactory ( new ( operation : operation , request : requestMessage , response : responseMessage , internalFactory : Build ) , cancellationToken ) . ConfigureAwait ( false ) ;
601
+
602
+ // Handling the case when the content is a stream
603
+ if ( response . Content is Stream stream and not HttpResponseStream )
604
+ {
605
+ // Wrap the stream content to capture the HTTP response message, delegating its disposal to the caller.
606
+ response . Content = new HttpResponseStream ( stream , responseMessage ) ;
607
+ }
608
+
609
+ return response ;
610
+ }
583
611
584
- return response ;
612
+ return await Build ( new ( operation : operation , request : requestMessage , response : responseMessage , internalFactory : null ! ) , cancellationToken ) . ConfigureAwait ( false ) ;
585
613
}
586
614
587
615
#endregion
0 commit comments