11using System . Diagnostics . CodeAnalysis ;
22using System . Net ;
3- using FluentValidation . Results ;
43using Microsoft . AspNetCore . Http ;
54using Microsoft . AspNetCore . Mvc ;
6- using Microsoft . AspNetCore . Mvc . ModelBinding ;
75
86namespace Initium . Response ;
97
@@ -79,18 +77,37 @@ public ApiResponseBuilder WithErrors(params ApiError[] errors)
7977 }
8078
8179 /// <summary>
82- /// Adds a list of validation errors to the API response.
80+ /// Adds a custom header to the API response.
8381 /// </summary>
84- /// <param name="validationResultErrors">The list of validation failures to include in the response.</param>
85- /// <returns>The current instance of the <see cref="ApiResponseBuilder"/>.</returns>
86- public ApiResponseBuilder WithErrors ( List < ValidationFailure > validationResultErrors )
82+ /// <param name="headerName">The name of the header to be added.</param>
83+ /// <param name="headerValue">The value for the specified header.</param>
84+ /// <returns>The current instance of <see cref="ApiResponseBuilder"/> with the custom header added.</returns>
85+ /// <exception cref="ArgumentException">Thrown when <paramref name="headerName"/> is null, empty, or consists only of white-space characters.</exception>
86+ public ApiResponseBuilder WithCustomHeader ( string headerName , string headerValue )
8787 {
88- _apiResponse . Errors = validationResultErrors
89- . Select ( validationFailure => new ApiError ( validationFailure . ErrorCode , validationFailure . ErrorMessage ) )
90- . ToList ( ) ;
88+ if ( string . IsNullOrWhiteSpace ( headerName ) )
89+ throw new ArgumentException ( "Header name cannot be null or empty." , nameof ( headerName ) ) ;
90+
91+ _apiResponse . CustomHeaders ??= new Dictionary < string , string > ( ) ;
92+ _apiResponse . CustomHeaders [ headerName ] = headerValue ;
9193 return this ;
9294 }
9395
96+ /// <summary>
97+ /// Adds custom headers to the API response.
98+ /// </summary>
99+ /// <param name="headers">A dictionary containing header names as keys and their respective values.</param>
100+ /// <returns>The current instance of <see cref="ApiResponseBuilder"/> with the added custom headers.</returns>
101+ /// <exception cref="ArgumentException">Thrown when the <paramref name="headers"/> dictionary is null or empty.</exception>
102+ public ApiResponseBuilder WithCustomHeaders ( Dictionary < string , string > headers )
103+ {
104+ _apiResponse . CustomHeaders ??= new Dictionary < string , string > ( ) ;
105+ foreach ( var header in headers )
106+ _apiResponse . CustomHeaders [ header . Key ] = header . Value ;
107+
108+ return this ;
109+ }
110+
94111 /// <summary>
95112 /// Builds and returns the constructed <see cref="ApiResponse"/>.
96113 /// </summary>
@@ -105,14 +122,4 @@ public ApiResponseBuilder WithErrors(List<ValidationFailure> validationResultErr
105122 {
106123 StatusCode = _apiResponse . StatusCode
107124 } ;
108-
109- public ApiResponseBuilder WithCustomHeader ( string headerName , string headerValue )
110- {
111- if ( string . IsNullOrWhiteSpace ( headerName ) )
112- throw new ArgumentException ( "Header name cannot be null or empty." , nameof ( headerName ) ) ;
113-
114- _apiResponse . CustomHeaders ??= new Dictionary < string , string > ( ) ;
115- _apiResponse . CustomHeaders [ headerName ] = headerValue ;
116- return this ;
117- }
118125}
0 commit comments