Skip to content

Add HttpResponseWrapper #116

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
@@ -1,5 +1,6 @@
using System.Dynamic;
using Microsoft.AspNetCore.Http;
using System.Net.Mime;
using System.Text;

namespace Codehard.Functional.AspNetCore;

Expand Down Expand Up @@ -65,9 +66,14 @@ await this.Error.Data
ar => ar.ExecuteResultAsync(context),
None: async () =>
{
context.HttpContext.Response.StatusCode = (int)this.Error.StatusCode;
context.HttpContext.Response.ContentType = "text/plain";
await context.HttpContext.Response.WriteAsync(context.HttpContext.TraceIdentifier);
var responseWrapper = new HttpResponseWrapper(context.HttpContext.Response)
{
StatusCode = (int)this.Error.StatusCode,
ContentType = MediaTypeNames.Text.Plain
};

await responseWrapper.WriteAsync(
context.HttpContext.TraceIdentifier, Encoding.UTF8, CancellationToken.None);
});

return;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System.Text;
using Microsoft.AspNetCore.Http;

namespace Codehard.Functional.AspNetCore;

public interface IHttpResponseWrapper
{
Task WriteAsync(string text, Encoding encoding, CancellationToken cancellationToken);
IHeaderDictionary Headers { get; }
int StatusCode { get; set; }

string ContentType { get; set; }
}

public class HttpResponseWrapper : IHttpResponseWrapper
{
private readonly HttpResponse response;

public HttpResponseWrapper(HttpResponse response)
{
this.response = response;
}

public Task WriteAsync(string text, Encoding encoding, CancellationToken cancellationToken)
{
return response.WriteAsync(text, encoding, cancellationToken);
}

public IHeaderDictionary Headers => response.Headers;

public int StatusCode
{
get => response.StatusCode;
set => response.StatusCode = value;
}

public string ContentType
{
get => response.ContentType;
set => response.ContentType = value;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// ReSharper disable once CheckNamespace
namespace LanguageExt;
namespace System.Linq;

/// <summary>
/// Extension methods for working with IEnumerable collections.
Expand Down
Loading