Cleaning up code and added more documentation.
This commit is contained in:
@@ -49,27 +49,6 @@ namespace MontoyaTech.Rest.Net
|
|||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Sets the response content type to text encoded as utf16 and writes the given text to it.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="response"></param>
|
|
||||||
/// <param name="text"></param>
|
|
||||||
/// <returns>This response.</returns>
|
|
||||||
public static HttpListenerResponse WithText16(this HttpListenerResponse response, string text)
|
|
||||||
{
|
|
||||||
response.ContentType = "text/plain; charset=utf-16";
|
|
||||||
|
|
||||||
var bytes = Encoding.Unicode.GetBytes(text);
|
|
||||||
|
|
||||||
response.ContentLength64 = bytes.Length;
|
|
||||||
|
|
||||||
response.OutputStream.Write(bytes, 0, bytes.Length);
|
|
||||||
|
|
||||||
response.OutputStream.Dispose();
|
|
||||||
|
|
||||||
return response;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Sets the response content type to text and writes the given text compressed to it.
|
/// Sets the response content type to text and writes the given text compressed to it.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -122,27 +101,6 @@ namespace MontoyaTech.Rest.Net
|
|||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Sets the response content type to json encoded as utf16 and serializes the object as json and writes it.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="response"></param>
|
|
||||||
/// <param name="obj"></param>
|
|
||||||
/// <returns>This response.</returns>
|
|
||||||
public static HttpListenerResponse WithJson16(this HttpListenerResponse response, object obj)
|
|
||||||
{
|
|
||||||
response.ContentType = "application/json; charset=utf-16";
|
|
||||||
|
|
||||||
var bytes = Encoding.Unicode.GetBytes(JsonConvert.SerializeObject(obj));
|
|
||||||
|
|
||||||
response.ContentLength64 = bytes.Length;
|
|
||||||
|
|
||||||
response.OutputStream.Write(bytes, 0, bytes.Length);
|
|
||||||
|
|
||||||
response.OutputStream.Dispose();
|
|
||||||
|
|
||||||
return response;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Sets the response content type to json and writes the given json compressed to it.
|
/// Sets the response content type to json and writes the given json compressed to it.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -355,25 +313,6 @@ namespace MontoyaTech.Rest.Net
|
|||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Sets the response content type to html encoded in utf 16 and writes the given html to it.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="response"></param>
|
|
||||||
/// <param name="html"></param>
|
|
||||||
/// <returns>This response.</returns>
|
|
||||||
public static HttpListenerResponse WithHtml16(this HttpListenerResponse response, string html)
|
|
||||||
{
|
|
||||||
response.ContentType = "text/html; charset=utf-16";
|
|
||||||
|
|
||||||
var bytes = Encoding.Unicode.GetBytes(html);
|
|
||||||
|
|
||||||
response.ContentLength64 = bytes.Length;
|
|
||||||
|
|
||||||
response.OutputStream.Write(bytes, 0, bytes.Length);
|
|
||||||
|
|
||||||
return response;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Sets the response to include the given stream and sets the length and content type if possible.
|
/// Sets the response to include the given stream and sets the length and content type if possible.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -695,7 +634,7 @@ namespace MontoyaTech.Rest.Net
|
|||||||
if (requestComponents.Count == 0)
|
if (requestComponents.Count == 0)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
var absolutePath = Path.Combine(basePath, requestComponents.Separate(Path.DirectorySeparatorChar));
|
var absolutePath = Path.Combine(basePath, requestComponents.Concat(Path.DirectorySeparatorChar));
|
||||||
|
|
||||||
if (File.Exists(absolutePath))
|
if (File.Exists(absolutePath))
|
||||||
{
|
{
|
||||||
@@ -842,7 +781,7 @@ namespace MontoyaTech.Rest.Net
|
|||||||
requestComponents.Add(indexFile);
|
requestComponents.Add(indexFile);
|
||||||
|
|
||||||
//Combine the path into an absolute path
|
//Combine the path into an absolute path
|
||||||
var absolutePath = Path.Combine(basePath, requestComponents.Separate(Path.DirectorySeparatorChar));
|
var absolutePath = Path.Combine(basePath, requestComponents.Concat(Path.DirectorySeparatorChar));
|
||||||
|
|
||||||
//If a file exists, return true
|
//If a file exists, return true
|
||||||
if (File.Exists(absolutePath))
|
if (File.Exists(absolutePath))
|
||||||
|
@@ -14,13 +14,21 @@ namespace MontoyaTech.Rest.Net
|
|||||||
public enum HttpRequestMethod
|
public enum HttpRequestMethod
|
||||||
{
|
{
|
||||||
Get,
|
Get,
|
||||||
|
|
||||||
Head,
|
Head,
|
||||||
|
|
||||||
Post,
|
Post,
|
||||||
|
|
||||||
Put,
|
Put,
|
||||||
|
|
||||||
Delete,
|
Delete,
|
||||||
|
|
||||||
Connect,
|
Connect,
|
||||||
|
|
||||||
Options,
|
Options,
|
||||||
|
|
||||||
Trace,
|
Trace,
|
||||||
|
|
||||||
Patch
|
Patch
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -8,15 +8,23 @@ namespace MontoyaTech.Rest.Net
|
|||||||
{
|
{
|
||||||
internal static class MimeTypeExtensions
|
internal static class MimeTypeExtensions
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Returns the mime type if possible for known extensions.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="extension">The extension to get the mime type for. Example valid values: ".txt", "txt"</param>
|
||||||
|
/// <returns></returns>
|
||||||
public static string GetMimeType(this string extension)
|
public static string GetMimeType(this string extension)
|
||||||
{
|
{
|
||||||
|
//If the extension is null or empty use the default mime type.
|
||||||
if (string.IsNullOrWhiteSpace(extension))
|
if (string.IsNullOrWhiteSpace(extension))
|
||||||
return "application/octet-stream";
|
return "application/octet-stream";
|
||||||
|
|
||||||
|
//Remove the leading . if needed
|
||||||
if (extension.StartsWith("."))
|
if (extension.StartsWith("."))
|
||||||
extension = extension.Substring(1);
|
extension = extension.Substring(1);
|
||||||
|
|
||||||
switch (extension.ToLower())
|
//Check the extension against known types.
|
||||||
|
switch (extension.ToLower().Trim())
|
||||||
{
|
{
|
||||||
case "323": return "text/h323";
|
case "323": return "text/h323";
|
||||||
case "3g2": return "video/3gpp2";
|
case "3g2": return "video/3gpp2";
|
||||||
@@ -584,7 +592,6 @@ namespace MontoyaTech.Rest.Net
|
|||||||
case "xwd": return "image/x-xwindowdump";
|
case "xwd": return "image/x-xwindowdump";
|
||||||
case "z": return "application/x-compress";
|
case "z": return "application/x-compress";
|
||||||
case "zip": return "application/x-zip-compressed";
|
case "zip": return "application/x-zip-compressed";
|
||||||
|
|
||||||
default: return "application/octet-stream";
|
default: return "application/octet-stream";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -162,11 +162,14 @@ namespace MontoyaTech.Rest.Net
|
|||||||
{
|
{
|
||||||
foreach (var include in routeIncludes)
|
foreach (var include in routeIncludes)
|
||||||
{
|
{
|
||||||
var types = this.FindTypeDependencies(include.Type);
|
if (include.Type != null)
|
||||||
|
{
|
||||||
|
var types = this.FindTypeDependencies(include.Type);
|
||||||
|
|
||||||
foreach (var type in types)
|
foreach (var type in types)
|
||||||
if (!dependencies.Contains(type))
|
if (!dependencies.Contains(type))
|
||||||
dependencies.Add(type);
|
dependencies.Add(type);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -28,6 +28,9 @@ namespace MontoyaTech.Rest.Net
|
|||||||
/// <param name="type"></param>
|
/// <param name="type"></param>
|
||||||
public RouteInclude(Type type)
|
public RouteInclude(Type type)
|
||||||
{
|
{
|
||||||
|
if (type == null)
|
||||||
|
throw new ArgumentNullException($"{nameof(type)} cannot be null.");
|
||||||
|
|
||||||
this.Type = type;
|
this.Type = type;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -38,6 +38,9 @@ namespace MontoyaTech.Rest.Net
|
|||||||
/// <param name="requestType"></param>
|
/// <param name="requestType"></param>
|
||||||
public RouteRequest(Type requestType)
|
public RouteRequest(Type requestType)
|
||||||
{
|
{
|
||||||
|
if (requestType == null)
|
||||||
|
throw new ArgumentNullException($"{nameof(requestType)} cannot be null.");
|
||||||
|
|
||||||
this.RequestType = requestType;
|
this.RequestType = requestType;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -43,6 +43,9 @@ namespace MontoyaTech.Rest.Net
|
|||||||
/// <param name="responseType"></param>
|
/// <param name="responseType"></param>
|
||||||
public RouteResponse(Type responseType)
|
public RouteResponse(Type responseType)
|
||||||
{
|
{
|
||||||
|
if (responseType == null)
|
||||||
|
throw new ArgumentNullException($"{nameof(responseType)} cannot be null.");
|
||||||
|
|
||||||
this.ResponseType = responseType;
|
this.ResponseType = responseType;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -17,8 +17,15 @@ namespace MontoyaTech.Rest.Net
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public string Name;
|
public string Name;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Creates a new RouteTypeName with the new name to use.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="name"></param>
|
||||||
public RouteTypeName(string name)
|
public RouteTypeName(string name)
|
||||||
{
|
{
|
||||||
|
if (string.IsNullOrWhiteSpace(name))
|
||||||
|
throw new ArgumentException($"{nameof(name)} cannot be null or empty.");
|
||||||
|
|
||||||
this.Name = name;
|
this.Name = name;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -6,8 +6,17 @@ using System.Threading.Tasks;
|
|||||||
|
|
||||||
namespace MontoyaTech.Rest.Net
|
namespace MontoyaTech.Rest.Net
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// A list of helper extensions for when working with strings.
|
||||||
|
/// </summary>
|
||||||
internal static class StringExtensions
|
internal static class StringExtensions
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Counts the occurance of a character in a string and returns it.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="input"></param>
|
||||||
|
/// <param name="c"></param>
|
||||||
|
/// <returns></returns>
|
||||||
public static int Count(this string input, char c)
|
public static int Count(this string input, char c)
|
||||||
{
|
{
|
||||||
int count = 0;
|
int count = 0;
|
||||||
@@ -19,7 +28,13 @@ namespace MontoyaTech.Rest.Net
|
|||||||
return count;
|
return count;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static string Separate(this IList<string> input, char separator)
|
/// <summary>
|
||||||
|
/// Concats a list of strings and separates them by a given separator character.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="input"></param>
|
||||||
|
/// <param name="separator"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static string Concat(this IList<string> input, char separator)
|
||||||
{
|
{
|
||||||
if (input == null || input.Count == 0)
|
if (input == null || input.Count == 0)
|
||||||
return "";
|
return "";
|
||||||
@@ -36,7 +51,13 @@ namespace MontoyaTech.Rest.Net
|
|||||||
return builder.ToString();
|
return builder.ToString();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static string Separate(this IList<string> input, string separator)
|
/// <summary>
|
||||||
|
/// Concats a list of strings and separates them by a given separator string.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="input"></param>
|
||||||
|
/// <param name="separator"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static string Concat(this IList<string> input, string separator)
|
||||||
{
|
{
|
||||||
if (input == null || input.Count == 0)
|
if (input == null || input.Count == 0)
|
||||||
return "";
|
return "";
|
||||||
@@ -48,7 +69,10 @@ namespace MontoyaTech.Rest.Net
|
|||||||
builder.Append(input[0]);
|
builder.Append(input[0]);
|
||||||
|
|
||||||
for (int i = 1; i < input.Count; i++)
|
for (int i = 1; i < input.Count; i++)
|
||||||
builder.Append(separator).Append(input[i]);
|
if (separator != null)
|
||||||
|
builder.Append(separator).Append(input[i]);
|
||||||
|
else
|
||||||
|
builder.Append(input[i]);
|
||||||
|
|
||||||
return builder.ToString();
|
return builder.ToString();
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user