Exemplo n.º 1
0
        /// <summary>
        /// Get response schema
        /// </summary>
        /// <param name="operationDescription"></param>
        /// <returns></returns>
        public Message GetResponseXmlSchemaAsMessage(OperationDescription operationDescription)
        {
            bool isXmlSerializerType;
            Type body = OperationDescriptionUtilites.GetResponseBodyType(operationDescription, out isXmlSerializerType);

            if (IsBodySpecial(body))
            {
                return(GetSpecialBodyDocumentationAsMessage(body, "response"));
            }

            try
            {
                return(GetXmlSchemaAsMessage(body, isXmlSerializerType));
            }
            catch (Exception e)
            {
                return(GetTextMessage(String.Format("Could not generate schema for request. Failed with error: {0}", e.Message)));
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Get the response example
        /// </summary>
        /// <param name="operationDescription"></param>
        /// <returns></returns>
        public Message GetResponseExampleAsMessage(OperationDescription operationDescription)
        {
            bool isXmlSerializerType;
            Type body = OperationDescriptionUtilites.GetResponseBodyType(operationDescription, out isXmlSerializerType);

            if (IsBodySpecial(body))
            {
                return(GetSpecialBodyDocumentationAsMessage(body, "response"));
            }

            try
            {
                object instance = Activator.CreateInstance(body);
                return(OperationDescriptionUtilites.GetResponseFormat(operationDescription) == "Json" ?
                       GetJsonExampleAsMessage(instance, body) :
                       GetXmlExampleAsMessage(instance, body, isXmlSerializerType));
            }
            catch (Exception e)
            {
                return(GetTextMessage(String.Format("Could not generate example for response. Failed with error: {0}", e.Message)));
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Get Documentation for a particular operation
        /// </summary>
        /// <param name="operationDescription"></param>
        /// <returns></returns>
        private string GetOperationDocumentation(OperationDescription operationDescription)
        {
            StringBuilder bodyXml = new StringBuilder();

            bodyXml.AppendFormat("<p class=\"heading2\"><strong>{0}</strong></p><br/>", operationDescription.Name);

            WebOperationDocAttribute docAttribute = operationDescription.Behaviors.Find <WebOperationDocAttribute>();

            if (docAttribute != null)
            {
                if (!String.IsNullOrEmpty(docAttribute.Description))
                {
                    bodyXml.AppendFormat("<p class=\"intro\">{0}", HttpUtility.HtmlEncode(docAttribute.Description));
                    if (!String.IsNullOrEmpty(docAttribute.SupportLink))
                    {
                        bodyXml.AppendFormat("  <a href=\"{0}\">Report Bug</a>", HttpUtility.HtmlEncode(docAttribute.SupportLink));
                    }
                    else
                    {
                        WebServiceDocAttribute serviceDocAttribute = ContractDescription.Behaviors.Find <WebServiceDocAttribute>();

                        if (!String.IsNullOrEmpty(serviceDocAttribute.SupportLink))
                        {
                            bodyXml.AppendFormat("  <a href=\"{0}\">Report Bug</a>", HttpUtility.HtmlEncode(serviceDocAttribute.SupportLink));
                        }
                    }
                    bodyXml.Append("</p>");
                }
            }

            bodyXml.AppendFormat("<p class=\"intro\"><strong>Method:</strong> {0}</p>", OperationDescriptionUtilites.GetMethod(operationDescription));

            string uriTemplate = OperationDescriptionUtilites.GetUriTemplate(operationDescription);

            uriTemplate = String.Format("{0}/{1}", BaseUri.AbsoluteUri, uriTemplate);
            uriTemplate = HttpUtility.HtmlEncode(uriTemplate);
            bodyXml.AppendFormat("<p class=\"intro\"><strong>URI Template:</strong> {0}</p>", uriTemplate);

            string requestFormat = OperationDescriptionUtilites.GetRequestFormat(operationDescription);

            if (!String.IsNullOrEmpty(requestFormat))
            {
                bodyXml.AppendFormat("<p class=\"intro\"><strong>Request Format:</strong> {0}</p>", requestFormat);
            }

            string requestExample = GetRequestExample(operationDescription);

            FormatCodeBlock(ref requestExample);
            if (OperationDescriptionUtilites.GetResponseFormat(operationDescription) != "Json")
            {
                bodyXml.AppendFormat("<p class=\"intro\"><strong>Request Example (<a href=\"./docs/request/schema\">schema</a>):</strong> <br/>");
            }
            else
            {
                bodyXml.AppendFormat("<p class=\"intro\"><strong>Request Example:</strong> <br/>");
            }
            bodyXml.AppendLine(requestExample);
            bodyXml.Append("</p>");

            bodyXml.AppendFormat("<p class=\"intro\"><strong>Response Format:</strong> {0}</p>", OperationDescriptionUtilites.GetResponseFormat(operationDescription));

            string responseExample = GetResponseExample(operationDescription);

            FormatCodeBlock(ref responseExample);
            if (OperationDescriptionUtilites.GetResponseFormat(operationDescription) != "Json")
            {
                bodyXml.AppendFormat("<p class=\"intro\"><strong>Response Example (<a href=\"./docs/response/schema\">schema</a>):</strong><br/>");
            }
            else
            {
                bodyXml.AppendFormat("<p class=\"intro\"><strong>Response Example:</strong> <br/>");
            }
            bodyXml.AppendLine(responseExample);
            bodyXml.AppendLine("</p>");

            return(bodyXml.ToString());
        }
Exemplo n.º 4
0
        /// <summary>
        /// Get ServiceDocumentation
        /// </summary>
        /// <returns></returns>
        private Message GetServiceDocumentation()
        {
            XmlDocument doc = new XmlDocument();

            XmlDeclaration declaration = doc.CreateXmlDeclaration("1.0", "UTF-8", null);

            doc.AppendChild(declaration);

            WebServiceDocAttribute docAttribute = ContractDescription.Behaviors.Find <WebServiceDocAttribute>();

            string pageTitle = docAttribute == null || String.IsNullOrEmpty(docAttribute.Title)
                                   ? String.Format("{0} REST API Documentation", ContractDescription.Name)
                                   : docAttribute.Title;

            XmlElement   root  = doc.CreateElement("html");
            XmlElement   title = doc.CreateElement("title");
            XmlElement   head  = doc.CreateElement("head");
            XmlElement   style = doc.CreateElement("style");
            XmlAttribute type  = doc.CreateAttribute("type", "text/css");

            style.Attributes.Append(type);
            style.InnerText = CssTemplate;
            title.InnerXml  = pageTitle;

            XmlElement    body    = doc.CreateElement("body");
            StringBuilder bodyXml = new StringBuilder();

            bodyXml.Append("<div id=\"content\">");
            bodyXml.AppendFormat("<p class=\"heading1\">{0}</p><br/>", HttpUtility.HtmlEncode(pageTitle));
            if (docAttribute != null)
            {
                if (!String.IsNullOrEmpty(docAttribute.Description))
                {
                    bodyXml.AppendFormat("<p class=\"intro\">{0}", HttpUtility.HtmlEncode(docAttribute.Description));
                    if (!String.IsNullOrEmpty(docAttribute.SupportLink))
                    {
                        bodyXml.AppendFormat("   <a href=\"{0}\">Report Bug</a>", HttpUtility.HtmlEncode(docAttribute.SupportLink));
                    }
                    bodyXml.Append("</p>");
                }
            }

            foreach (OperationDescription operationDescription in ContractDescription.Operations)
            {
                bodyXml.AppendFormat("<p class=\"intro\"><PRE>{0} : <a href=\"{1}/docs\">{2}</a>",
                                     HttpUtility.HtmlEncode(operationDescription.Name),
                                     HttpUtility.HtmlEncode(operationDescription.Name.ToLowerInvariant()),
                                     HttpUtility.HtmlEncode(OperationDescriptionUtilites.GetUriTemplate(operationDescription).ToLowerInvariant()));
                bodyXml.AppendFormat("</PRE></p>");
            }
            bodyXml.AppendFormat("</div>");
            body.InnerXml = bodyXml.ToString();

            head.AppendChild(style);
            head.AppendChild(title);
            root.AppendChild(head);
            root.AppendChild(body);
            doc.AppendChild(root);

            Message message = Message.CreateMessage(MessageVersion.None, null, new XmlNodeReader(doc));

            return(message);
        }