Skip to content

Commit

Permalink
Added WebAPI help page package
Browse files Browse the repository at this point in the history
  • Loading branch information
davidebbo committed Oct 14, 2012
1 parent 92a0a81 commit 4759fcd
Show file tree
Hide file tree
Showing 31 changed files with 2,121 additions and 0 deletions.
39 changes: 39 additions & 0 deletions Kudu.Services.Web/Areas/HelpPage/ApiDescriptionExtensions.cs
@@ -0,0 +1,39 @@
using System;
using System.Text;
using System.Web;
using System.Web.Http.Description;

namespace Kudu.Services.Web.Areas.HelpPage
{
public static class ApiDescriptionExtensions
{
/// <summary>
/// Generates an URI-friendly ID for the <see cref="ApiDescription"/>. E.g. "Get-Values-id_name" instead of "GetValues/{id}?name={name}"
/// </summary>
/// <param name="description">The <see cref="ApiDescription"/>.</param>
/// <returns>The ID as a string.</returns>
public static string GetFriendlyId(this ApiDescription description)
{
string path = description.RelativePath;
string[] urlParts = path.Split('?');
string localPath = urlParts[0];
string queryKeyString = null;
if (urlParts.Length > 1)
{
string query = urlParts[1];
string[] queryKeys = HttpUtility.ParseQueryString(query).AllKeys;
queryKeyString = String.Join("_", queryKeys);
}

StringBuilder friendlyPath = new StringBuilder();
friendlyPath.AppendFormat("{0}-{1}",
description.HttpMethod.Method,
localPath.Replace("/", "-").Replace("{", "").Replace("}", ""));
if (queryKeyString != null)
{
friendlyPath.AppendFormat("_{0}", queryKeyString);
}
return friendlyPath.ToString();
}
}
}
51 changes: 51 additions & 0 deletions Kudu.Services.Web/Areas/HelpPage/App_Start/HelpPageConfig.cs
@@ -0,0 +1,51 @@
using System;
using System.Collections.Generic;
using System.Net.Http.Headers;
using System.Web;
using System.Web.Http;

namespace Kudu.Services.Web.Areas.HelpPage
{
/// <summary>
/// Use this class to customize the Help Page.
/// For example you can set a custom <see cref="System.Web.Http.Description.IDocumentationProvider"/> to supply the documentation
/// or you can provide the samples for the requests/responses.
/// </summary>
public static class HelpPageConfig
{
public static void Register(HttpConfiguration config)
{
//// Uncomment the following to use the documentation from XML documentation file.
//config.SetDocumentationProvider(new XmlDocumentationProvider(HttpContext.Current.Server.MapPath("~/App_Data/XmlDocument.xml")));

//// Uncomment the following to use "sample string" as the sample for all actions that have string as the body parameter or return type.
//// Also, the string arrays will be used for IEnumerable<string>. The sample objects will be serialized into different media type
//// formats by the available formatters.
//config.SetSampleObjects(new Dictionary<Type, object>
//{
// {typeof(string), "sample string"},
// {typeof(IEnumerable<string>), new string[]{"sample 1", "sample 2"}}
//});

//// Uncomment the following to use "[0]=foo&[1]=bar" directly as the sample for all actions that support form URL encoded format
//// and have IEnumerable<string> as the body parameter or return type.
//config.SetSampleForType("[0]=foo&[1]=bar", new MediaTypeHeaderValue("application/x-www-form-urlencoded"), typeof(IEnumerable<string>));

//// Uncomment the following to use "1234" directly as the request sample for media type "text/plain" on the controller named "Values"
//// and action named "Put".
//config.SetSampleRequest("1234", new MediaTypeHeaderValue("text/plain"), "Values", "Put");

//// Uncomment the following to use the image on "../images/aspNetHome.png" directly as the response sample for media type "image/png"
//// on the controller named "Values" and action named "Get" with parameter "id".
//config.SetSampleResponse(new ImageSample("../images/aspNetHome.png"), new MediaTypeHeaderValue("image/png"), "Values", "Get", "id");

//// Uncomment the following to correct the sample request when the action expects an HttpRequestMessage with ObjectContent<string>.
//// The sample will be generated as if the controller named "Values" and action named "Get" were having string as the body parameter.
//config.SetActualRequestType(typeof(string), "Values", "Get");

//// Uncomment the following to correct the sample response when the action returns an HttpResponseMessage with ObjectContent<string>.
//// The sample will be generated as if the controller named "Values" and action named "Post" were returning a string.
//config.SetActualResponseType(typeof(string), "Values", "Post");
}
}
}
44 changes: 44 additions & 0 deletions Kudu.Services.Web/Areas/HelpPage/Controllers/HelpController.cs
@@ -0,0 +1,44 @@
using System;
using System.Web.Http;
using System.Web.Mvc;
using Kudu.Services.Web.Areas.HelpPage.Models;

namespace Kudu.Services.Web.Areas.HelpPage.Controllers
{
/// <summary>
/// The controller that will handle requests for the help page.
/// </summary>
public class HelpController : Controller
{
public HelpController()
: this(GlobalConfiguration.Configuration)
{
}

public HelpController(HttpConfiguration config)
{
Configuration = config;
}

public HttpConfiguration Configuration { get; private set; }

public ActionResult Index()
{
return View(Configuration.Services.GetApiExplorer().ApiDescriptions);
}

public ActionResult Api(string apiId)
{
if (!String.IsNullOrEmpty(apiId))
{
HelpPageApiModel apiModel = Configuration.GetHelpPageApiModel(apiId);
if (apiModel != null)
{
return View(apiModel);
}
}

return View("Error");
}
}
}
19 changes: 19 additions & 0 deletions Kudu.Services.Web/Areas/HelpPage/HelpPage.css
@@ -0,0 +1,19 @@
pre.wrapped
{
white-space: -moz-pre-wrap;
white-space: -pre-wrap;
white-space: -o-pre-wrap;
white-space: pre-wrap
}

.warning-message-container
{
margin-top: 20px;
padding: 0 .7em;
}

.warning-message-icon
{
float: left;
margin-right: .3em;
}

0 comments on commit 4759fcd

Please sign in to comment.