Delen via


Outputting WADL for API Management from a Web API Service

I created a new set of services in Azure Mobile Services (this is specific to AMS) and tried to consume them with API Management. In order to import the API however, you have to have a WADL or Swagger document that describes the API. Being new to AMS applications I wasn’t sure how to do it. Along came some help from the API Management product team. They provided me a very simple file that output WADL automatically. Hopefully this will help make life easier for everyone (note that for Web API or Azure Websites you can simply add one of the available WADL or Swagger packages from NuGet that are available to you).

 using System;
using System.Collections.Generic;

using System.Linq;

using System.Net;

using System.Net.Http;

using System.Web.Http;

using Microsoft.WindowsAzure.Mobile.Service;

using System.Xml;

using System.Web.UI.WebControls;

using System.IO;

using System.Text;

using System.Web.Http.Description;
namespace DataService.Controllers
{
    public static class XmlTypeExtensions
    {
        private static readonly Dictionary<Type, string> TypeMappings = new Dictionary<Type, string>()     
     {                 
        {typeof(string), "string" },
        {typeof(bool), "boolean" },
        {typeof(decimal), "decimal" },
        {typeof(float), "float" },
        {typeof(double), "double" },
        {typeof(int), "int" },
        {typeof(DateTime), "datetime" },
        {typeof(Guid), "guid" }
     };
        public static string ToXmlTypeString(this Type type)
        {
            string typeString;
            if (TypeMappings.TryGetValue(type, out typeString))
            {
                return typeString;
            }
            return null;
        }
    }
    [AuthorizeLevel(Microsoft.WindowsAzure.Mobile.Service.Security.AuthorizationLevel.Anonymous)]
    public class WadlController : ApiController
    {
        public ApiServices Services { get; set; }
        // GET api/Wadl
        public HttpResponseMessage Get()
        {
            StringBuilder sb = new StringBuilder();
            using (XmlWriter xw = XmlWriter.Create(new StringWriter(sb)))
            {
                xw.WriteStartElement("application", "https://wadl.dev.java.net/2009/02");
                xw.WriteStartElement("resources", "https://wadl.dev.java.net/2009/02");
                xw.WriteAttributeString("base", "https://" + Services.Settings.Name + ".azure-mobile.net");
                var apiDescriptions = Configuration.Services.GetApiExplorer().ApiDescriptions;
                foreach (System.Web.Http.Description.ApiDescription api in apiDescriptions)
                {
                    xw.WriteStartElement("resource", "https://wadl.dev.java.net/2009/02");
                    xw.WriteAttributeString("path", api.RelativePath);
                    xw.WriteStartElement("doc", "https://wadl.dev.java.net/2009/02");
                    xw.WriteAttributeString("title", api.Documentation);
                    
                    xw.WriteEndElement();
                    xw.WriteStartElement("method", "https://wadl.dev.java.net/2009/02");
                    xw.WriteAttributeString("name", api.HttpMethod.Method);
                    xw.WriteStartElement("request", "https://wadl.dev.java.net/2009/02");
                    foreach (var param in api.ParameterDescriptions)
                    {
                        xw.WriteStartElement("param", "https://wadl.dev.java.net/2009/02");
                        xw.WriteAttributeString("name", param.Name);
                        xw.WriteAttributeString("style", "template");
                        xw.WriteStartAttribute("type");
                        xw.WriteQualifiedName(param.ParameterDescriptor.ParameterType.ToXmlTypeString() ?? "string", "https://www.w3.org/2001/XMLSchema");
                        xw.WriteEndAttribute();
                        xw.WriteAttributeString("required", "true");
                        xw.WriteEndElement();
                    }
                    var model = Configuration.GetHelpPageApiModel(api.GetFriendlyId());
                    
                    xw.WriteEndElement(); // request
                    xw.WriteEndElement(); // method
                    xw.WriteEndElement(); //resource
                }
                xw.WriteEndElement(); //resources
                xw.WriteEndElement(); //application
            }
            HttpResponseMessage response = Request.CreateResponse();
            response.Content = new StringContent(sb.ToString(), UTF8Encoding.Unicode, "text/xml");
            return response;
        }
    }
}

Add this controller to your Web API application and your done. At this point you can import your API into API Management.