refactor(ProxyController): Updated to add default headers

This commit is contained in:
Developer 02 2025-01-30 12:10:30 +01:00
parent d8ee61d107
commit 61bb5acdff
3 changed files with 11 additions and 6 deletions

View File

@ -4,6 +4,6 @@
{ {
public required string Url { get; init; } public required string Url { get; init; }
public Dictionary<string, string>? DefaultHeaders { get; init; } public Dictionary<string, string> DefaultHeaders { get; init; } = new();
} }
} }

View File

@ -11,12 +11,12 @@ using System.Threading.Tasks;
[ApiController] [ApiController]
public class ProxyController(IOptions<OriginServerParams> originServerParamsOptions) : ControllerBase public class ProxyController(IOptions<OriginServerParams> originServerParamsOptions) : ControllerBase
{ {
private readonly OriginServerParams origin = originServerParamsOptions.Value; private readonly OriginServerParams _origin = originServerParamsOptions.Value;
[HttpPost("{servicetierName}/ODataV4/{webserviceName}_CreateInvoice")] [HttpPost("{servicetierName}/ODataV4/{webserviceName}_CreateInvoice")]
public async Task<IActionResult> Redir([FromRoute] string servicetierName, [FromRoute] string webserviceName, [FromQuery] string company) public async Task<IActionResult> Redir([FromRoute] string servicetierName, [FromRoute] string webserviceName, [FromQuery] string company)
{ {
var res = await origin.Url var res = await _origin.Url
.AppendPathSegment(servicetierName) .AppendPathSegment(servicetierName)
.AppendPathSegment("ODataV4") .AppendPathSegment("ODataV4")
.AppendPathSegment(webserviceName + "_CreateInvoice") .AppendPathSegment(webserviceName + "_CreateInvoice")
@ -24,9 +24,14 @@ public class ProxyController(IOptions<OriginServerParams> originServerParamsOpti
//.WithOAuthBearerToken("my_oauth_token") //.WithOAuthBearerToken("my_oauth_token")
.PostAsync(); .PostAsync();
//merge with default headers
var mergedDictionary = _origin.DefaultHeaders
.Concat(res.Headers.ToDictionary(kv => kv.Name, kv => kv.Value))
.ToDictionary(kv => kv.Key, kv => kv.Value);
// set headers // set headers
foreach (var (Name, Value) in res.Headers) foreach (var header in mergedDictionary)
HttpContext.Response.Headers[Name] = Value; HttpContext.Response.Headers[header.Key] = header.Value;
// set cookies // set cookies
foreach (var (Name, Value) in res.Cookies.ToKeyValuePairs()) foreach (var (Name, Value) in res.Cookies.ToKeyValuePairs())

View File

@ -9,7 +9,7 @@
"OriginServer": { "OriginServer": {
"Url": "https://localhost:7248", "Url": "https://localhost:7248",
"DefaultHeaders": { "DefaultHeaders": {
"foo": "bar"
} }
} }
} }