SimpleREST.NET 0.1
A simple and minimal api framework for .net base on Express.js
Loading...
Searching...
No Matches
SimpleRestResponse.cs
Go to the documentation of this file.
1using System.Net;
2using System.Text;
3using System.Text.Json;
4using Dumpify;
5using Newtonsoft.Json;
7using Spectre.Console;
8
9namespace SimpleRest.Api;
10
12{
13 public delegate void Result(string result = "");
14 public HttpListenerResponse Response { get; private set; }
16 public string? ContentType { get; private set; }
17 public event Result? OnSend;
18 public bool HasCompleted { get; private set; }
19 StatusCode m_StatusCode = StatusCode.Success;
20 JsonSerializerOptions m_SerializerOptions;
22 {
23 get => m_StatusCode;
24 set
25 {
26 if (value.Code.ToString().Length == 3)
27 {
28 m_StatusCode = value;
29 }
30 }
31 }
32
33 public SimpleRestBody Body { get; private set; }
34
35 public WebHeaderCollection Headers { get; set; } = new WebHeaderCollection();
36
37 public long ContentLength { get; private set; }
38
39 public string? UserAgent { get; private set; }
40
41 public SimpleRestResponse(HttpListenerResponse response, ISimpleRestContentTypeParser parser, JsonSerializerOptions jsonSerializerOptions)
42 {
43 Response = response;
44 m_TypeParser = parser;
45 Body = new SimpleRestBody("", jsonSerializerOptions);
46 m_SerializerOptions = jsonSerializerOptions;
47 }
48
49 public void Return()
50 {
51
52
53 ContentLength = 0;
54 ContentType = null;
55 StatusCode = new StatusCode(204, "No Resource");
56 SetHeaders();
57 WriteResponse();
58 }
59
60 public void Send<T>(T result)
61 {
62
63 Response.StatusCode = StatusCode.InternalServerError.Code;
64
65 string finalOutput = JsonConvert.SerializeObject(result);
66 ContentType = m_TypeParser.GetType<T>();
67 Body = new SimpleRestBody(finalOutput, m_SerializerOptions);
68 SetHeaders();
69 WriteResponse();
70 }
71
72 public void View(string content, string contentType = "text/html; charset=utf-8")
73 {
74 ContentType = contentType;
75 Body = new SimpleRestBody(content, m_SerializerOptions);
76 SetHeaders();
77 WriteResponse();
78 }
79
80 public void View(ISimpleRestView view, string contentType = "text/html; charset=urf-8")
81 {
82 ContentType = contentType;
83 Body = new SimpleRestBody(view.GetView(), m_SerializerOptions);
84 SetHeaders();
85 WriteResponse();
86 }
87
88 public void Redirect(string location, RedirectCode? redirectCode = null)
89 {
90 redirectCode = redirectCode ?? RedirectCode.TemporaryRedirect;
91 Response.StatusCode = redirectCode.Code;
92 Response.StatusDescription = redirectCode.Name;
93 Body = new SimpleRestBody(redirectCode.ToString(), m_SerializerOptions);
94 SetHeaders();
95 Response.Headers["Location"] = location;
96
97 }
98
99 void SetHeaders()
100 {
101 ContentLength = Body.Bytes.Length;
102 Response.Headers = Headers;
103 Response.ContentType = ContentType;
104 Response.ContentLength64 = ContentLength;
105 Response.StatusCode = StatusCode.Code;
106 }
107
108 void WriteResponse()
109 {
110
111 byte[] buffer = Body.Bytes;
112 Response.OutputStream.Write(buffer, 0, buffer.Length);
113
114 Response.Close();
115 HasCompleted = true;
116 // OnSend?.Invoke(Body.Content);
117
118 }
119
120 public void Error(StatusCode? statusCode = null)
121 {
122 statusCode ??= m_StatusCode;
123 m_StatusCode = statusCode;
124 Body = new SimpleRestBody(m_StatusCode.Message, m_SerializerOptions);
125 SetHeaders();
126 WriteResponse();
127 }
128}
static RedirectCode TemporaryRedirect
The body of a SimpleRest.Api.SimpleRestRequest. This class encapsulates the content of an HTTP reques...
byte[] Bytes
Gets the raw content of the request body as a byte array.
SimpleRestResponse(HttpListenerResponse response, ISimpleRestContentTypeParser parser, JsonSerializerOptions jsonSerializerOptions)
void Error(StatusCode? statusCode=null)
void Redirect(string location, RedirectCode? redirectCode=null)
delegate void Result(string result="")
void View(ISimpleRestView view, string contentType="text/html; charset=urf-8")
void View(string content, string contentType="text/html; charset=utf-8")
static readonly StatusCode InternalServerError
Definition StatusCode.cs:44
static readonly StatusCode Success
Definition StatusCode.cs:20