Moved classes out of Rest.cs into their own files.

This commit is contained in:
high 2011-09-05 14:15:46 -04:00
parent b7fd7f1953
commit 4b694c0c31
5 changed files with 152 additions and 106 deletions

View file

@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
@ -136,108 +135,4 @@ namespace Rests
#endregion
}
public class RestVerbs : Dictionary<string, string>
{
}
public class RestObject : Dictionary<string, string>
{
public string Status
{
get { return this["status"]; }
set { this["status"] = value; }
}
public string Error
{
get { return this["error"]; }
set { this["error"] = value; }
}
public string Response
{
get { return this["response"]; }
set { this["response"] = value; }
}
public RestObject(string status)
{
Status = status;
}
/// <summary>
/// Gets value safely, if it does not exist, return null. Sets/Adds value safely, if null it will remove.
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
/// <returns>Returns null if key does not exist.</returns>
public new string this[string key]
{
get
{
string ret;
if (TryGetValue(key, out ret))
return ret;
return null;
}
set
{
if (!ContainsKey(key))
{
if (value == null)
return;
Add(key, value);
}
else
{
if (value != null)
base[key] = value;
else
Remove(key);
}
}
}
}
public class RestCommand
{
public string Name { get; protected set; }
public string UriTemplate { get; protected set; }
public string UriVerbMatch { get; protected set; }
public string[] UriVerbs { get; protected set; }
public RestCommandD Callback { get; protected set; }
public bool RequiesToken { get; set; }
/// <summary>
///
/// </summary>
/// <param name="name">Used for identification</param>
/// <param name="uritemplate">Url template</param>
/// <param name="callback">Rest Command callback</param>
public RestCommand(string name, string uritemplate, RestCommandD callback)
{
Name = name;
UriTemplate = uritemplate;
UriVerbMatch = string.Join("([^/]*)", Regex.Split(uritemplate, "\\{[^\\{\\}]*\\}"));
var matches = Regex.Matches(uritemplate, "\\{([^\\{\\}]*)\\}");
UriVerbs = (from Match match in matches select match.Groups[1].Value).ToArray();
Callback = callback;
RequiesToken = true;
}
/// <summary>
///
/// </summary>
/// <param name="uritemplate">Url template</param>
/// <param name="callback">Rest Command callback</param>
public RestCommand(string uritemplate, RestCommandD callback)
: this(string.Empty, uritemplate, callback)
{
}
public bool HasVerbs
{
get { return UriVerbs.Length > 0; }
}
}
}

View file

@ -0,0 +1,47 @@
using System.Linq;
using System.Text.RegularExpressions;
namespace Rests
{
public class RestCommand
{
public string Name { get; protected set; }
public string UriTemplate { get; protected set; }
public string UriVerbMatch { get; protected set; }
public string[] UriVerbs { get; protected set; }
public RestCommandD Callback { get; protected set; }
public bool RequiesToken { get; set; }
/// <summary>
///
/// </summary>
/// <param name="name">Used for identification</param>
/// <param name="uritemplate">Url template</param>
/// <param name="callback">Rest Command callback</param>
public RestCommand(string name, string uritemplate, RestCommandD callback)
{
Name = name;
UriTemplate = uritemplate;
UriVerbMatch = string.Join("([^/]*)", Regex.Split(uritemplate, "\\{[^\\{\\}]*\\}"));
var matches = Regex.Matches(uritemplate, "\\{([^\\{\\}]*)\\}");
UriVerbs = (from Match match in matches select match.Groups[1].Value).ToArray();
Callback = callback;
RequiesToken = true;
}
/// <summary>
///
/// </summary>
/// <param name="uritemplate">Url template</param>
/// <param name="callback">Rest Command callback</param>
public RestCommand(string uritemplate, RestCommandD callback)
: this(string.Empty, uritemplate, callback)
{
}
public bool HasVerbs
{
get { return UriVerbs.Length > 0; }
}
}
}

View file

@ -0,0 +1,61 @@
using System.Collections.Generic;
namespace Rests
{
public class RestObject : Dictionary<string, string>
{
public string Status
{
get { return this["status"]; }
set { this["status"] = value; }
}
public string Error
{
get { return this["error"]; }
set { this["error"] = value; }
}
public string Response
{
get { return this["response"]; }
set { this["response"] = value; }
}
public RestObject(string status)
{
Status = status;
}
/// <summary>
/// Gets value safely, if it does not exist, return null. Sets/Adds value safely, if null it will remove.
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
/// <returns>Returns null if key does not exist.</returns>
public new string this[string key]
{
get
{
string ret;
if (TryGetValue(key, out ret))
return ret;
return null;
}
set
{
if (!ContainsKey(key))
{
if (value == null)
return;
Add(key, value);
}
else
{
if (value != null)
base[key] = value;
else
Remove(key);
}
}
}
}
}

View file

@ -0,0 +1,40 @@
using System.Collections.Generic;
namespace Rests
{
public class RestVerbs : Dictionary<string, string>
{
/// <summary>
/// Gets value safely, if it does not exist, return null. Sets/Adds value safely, if null it will remove.
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
/// <returns>Returns null if key does not exist.</returns>
public new string this[string key]
{
get
{
string ret;
if (TryGetValue(key, out ret))
return ret;
return null;
}
set
{
if (!ContainsKey(key))
{
if (value == null)
return;
Add(key, value);
}
else
{
if (value != null)
base[key] = value;
else
Remove(key);
}
}
}
}
}

View file

@ -125,7 +125,10 @@
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
<Compile Include="Rest\Rest.cs" />
<Compile Include="Rest\RestCommand.cs" />
<Compile Include="Rest\RestManager.cs" />
<Compile Include="Rest\RestObject.cs" />
<Compile Include="Rest\RestVerbs.cs" />
<Compile Include="Rest\SecureRest.cs" />
<Compile Include="Tools.cs" />
<Compile Include="TShock.cs" />
@ -183,7 +186,7 @@
</PropertyGroup>
<ProjectExtensions>
<VisualStudio>
<UserProperties BuildVersion_IncrementBeforeBuild="False" BuildVersion_StartDate="2011/6/17" BuildVersion_BuildVersioningStyle="None.None.None.MonthAndDayStamp" BuildVersion_BuildAction="Both" BuildVersion_UpdateFileVersion="True" BuildVersion_UpdateAssemblyVersion="True" />
<UserProperties BuildVersion_UpdateAssemblyVersion="True" BuildVersion_UpdateFileVersion="True" BuildVersion_BuildAction="Both" BuildVersion_BuildVersioningStyle="None.None.None.MonthAndDayStamp" BuildVersion_StartDate="2011/6/17" BuildVersion_IncrementBeforeBuild="False" />
</VisualStudio>
</ProjectExtensions>
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.