public void Descriptions_RecognizesIgnoreApiForDirectRoutes_Controller()
        {
            var config = new HttpConfiguration();
            var routeTemplate = "api/values";
            var controllerDescriptor = new HttpControllerDescriptor(config, "IgnoreApiValues", typeof(IgnoreApiValuesController));
            var actions = new ReflectedHttpActionDescriptor[] 
            {
                new ReflectedHttpActionDescriptor(controllerDescriptor, typeof(IgnoreApiValuesController).GetMethod("Get")),
                new ReflectedHttpActionDescriptor(controllerDescriptor, typeof(IgnoreApiValuesController).GetMethod("Post")),
            };
            config.Routes.Add("Route", new HttpDirectRoute(routeTemplate, 0, actions));

            var descriptions = new ApiExplorer(config).ApiDescriptions;

            Assert.Empty(descriptions);
        }
        public void Descriptions_RecognizesDirectRoutes()
        {
            var config = new HttpConfiguration();
            var routeTemplate = "api/values";
            var controllerDescriptor = new HttpControllerDescriptor(config, "ApiExplorerValues", typeof(ApiExplorerValuesController));
            var action = new ReflectedHttpActionDescriptor(controllerDescriptor, typeof(ApiExplorerValuesController).GetMethod("Get"));
            var actions = new ReflectedHttpActionDescriptor[] { action };
            config.Routes.Add("Route", new HttpDirectRoute(routeTemplate, 0, actions));

            var descriptions = new ApiExplorer(config).ApiDescriptions;

            ApiDescription description = Assert.Single(descriptions);
            Assert.Equal(HttpMethod.Get, description.HttpMethod);
            Assert.Equal(routeTemplate, description.RelativePath);
            Assert.Equal(action, description.ActionDescriptor);
        }
        public static List<ApiEndPoint> Get(HttpConfiguration config)
        {
            var apiDescriptions = new ApiExplorer(config).ApiDescriptions;
            var controllers = apiDescriptions
                .GroupBy(x => x.ActionDescriptor.ControllerDescriptor.ControllerName)
                .Select(x => x.First().ActionDescriptor.ControllerDescriptor.ControllerName)
                .ToList();

            var apiEndPoints = new List<ApiEndPoint>();

            foreach (var controller in controllers)
            {
                apiEndPoints.Add(getApiEndPoint(controller, config));
            }

            return apiEndPoints;
        }
        static ApiEndPoint getApiEndPoint(string controller, HttpConfiguration config)
        {
            string ctrl = controller;
            var apis = new ApiExplorer(config)
             .ApiDescriptions
             .Where(x => x.ActionDescriptor.ControllerDescriptor.ControllerName == ctrl).ToList();

            List<ApiEndPointDetail> apiEndPointDetails = null;

            if (apis.ToList().Count > 0)
            {

                apiEndPointDetails = new List<ApiEndPointDetail>();
                foreach (var api in apis)
                {
                    apiEndPointDetails.Add(getApiEndPointDetail(api));
                }
            }
            else
            {
                controller = string.Format("The {0} api does not exist.", controller);
            }
            return new ApiEndPoint(controller, apiEndPointDetails);
        }
        public IEnumerable<Endpoint> Endpoints()
        {
            var config = ControllerContext.Configuration;// new HttpConfiguration();//
            IApiExplorer apiExplorer = new ApiExplorer(config);//config.Services.GetApiExplorer();

            List<Endpoint> data = new List<Endpoint>();

            foreach (ApiDescription api in apiExplorer.ApiDescriptions)
            {
                Endpoint e = new Endpoint { Method = api.HttpMethod, URI = api.RelativePath, Parameters = new List<EndpointParam>() };

                foreach (ApiParameterDescription parameter in api.ParameterDescriptions)
                {
                    if (parameter != null)
                    {
                        e.Parameters.Add(new EndpointParam { Name = parameter.Name, Source = parameter.Source });
                    }
                }

                data.Add(e);
            }

            return data;
        }
        public string GenerateScript(HttpConfiguration config)
        {
            if (String.IsNullOrEmpty(script))
            {
                lock (lockObject)
                {
                    if (String.IsNullOrEmpty(script))
                    {
                        ApiExplorer apiExplorer = new ApiExplorer(config);

                        IEnumerable<ApiResource> resources = apiExplorer.ApiDescriptions
                                                                .Select(d => new ApiResource(d))
                                                                .GroupBy(r => r.Name)
                                                                .Select(g => ApiResource.Join(g.ToArray()));

                        ScriptProxyTemplate proxyTemplate = new ScriptProxyTemplate(resources);

                        script = proxyTemplate.TransformText();
                    }
                }
            }

            return script;
        }
Esempio n. 7
0
        private string HelpContent()
        {
            var sb = new StringBuilder();
            var apiExExplorer = new ApiExplorer(ControllerContext.Configuration);
            foreach (var api in apiExExplorer.ApiDescriptions)
            {
                sb.AppendFormat("<li><a href='{0}'>/{1}</a> - {2} - {3}", ToLink(api.RelativePath), api.RelativePath.ToLower(), api.HttpMethod, api.Documentation);
                //                if (api.ParameterDescriptions.Count > 0)
                //                {
                //                    sb.AppendFormat("<ul>");
                //                    foreach (var parameter in api.ParameterDescriptions)
                //                    {
                //                        sb.AppendFormat("<li>{0}: {1} ({2})</li>", parameter.Name, parameter.Documentation, parameter.Source);
                //                    }
                //                    sb.AppendFormat("</ul></li>");
                //                }
            }

            var content = string.Format(@"
                            <!DOCTYPE html>
                            <html>
                                <head>
                                    <title>httpapi - Request Response Service</title>
                                    <style>
                                        body {{font-family: monospace;font-size:14px;line-height:1.5em;}}
                                        a:visited {{color: blue}}
                                        ul {{list-style: none}}
                                    </style>
                                </head>
                                <body>
            <a href='http://github.com/chandmk/httpapi'><img style='position: absolute; top: 0; right: 0; border: 0;' src='https://s3.amazonaws.com/github/ribbons/forkme_right_darkblue_121621.png' alt='Fork me on GitHub'></a>
                                    <h1>httpapi - Request Response Service</h1>
                                    <section>
                                    <h3>ENDPOINTS</h3>
                                    <ul>
                                    {0}
                                    </ul>
                                    </section>
                                    <section>
                                        <h3>DESCRIPTION</h3>
                                        <ul><li>
                                        Inspiration for this service is from httpbin.org.</li>
                                        <li>This service provides api for http scenarios. </li>
                                        <li>All endpoints, where it makes sense by default return JSON-encoded content.</li>
                                        <li>You can also request for xml/json by adding respective extension.</li>
                                        <li>
                                        &nbsp;&nbsp;<a href='/get.xml' > /get.xml </a> returns xml content. </li><li>&nbsp;&nbsp;<a href='/get.json' > /get.json </a> returns json content.</li></ul>
                                    </section>
                                </body>
                            </html>", sb);
            return content;
        }
        public void Setup()
        {
            // Get ApiExplorer for TestApp
            var httpConfiguration = new HttpConfiguration();
            TestApp.WebApiConfig.Register(httpConfiguration);
            _apiExplorer = new ApiExplorer(httpConfiguration);

            var xmlCommentsDoc = new XPathDocument(String.Format(@"{0}\XmlComments.xml", AppDomain.CurrentDomain.BaseDirectory));
            _filter = new ApplyActionXmlComments(xmlCommentsDoc);
        }
        public void Descriptions_RecognizesCompositeRoutes()
        {
            var config = new HttpConfiguration();
            var routeTemplate = "api/values";
            var controllerDescriptor = new HttpControllerDescriptor(config, "AttributeApiExplorerValues", typeof(AttributeApiExplorerValuesController));
            var action = new ReflectedHttpActionDescriptor(controllerDescriptor, typeof(AttributeApiExplorerValuesController).GetMethod("Action"));
            var actions = new ReflectedHttpActionDescriptor[] { action };

            var routeCollection = new HttpSubRouteCollection();
            routeCollection.Add("testroute", new HttpDirectRoute(routeTemplate, 0, actions));

            RouteCollectionRoute route = new RouteCollectionRoute();
            route.EnsureInitialized(() => routeCollection);

            config.Routes.Add("Route", route);

            var descriptions = new ApiExplorer(config).ApiDescriptions;

            ApiDescription description = Assert.Single(descriptions);
            Assert.Equal(HttpMethod.Get, description.HttpMethod);
            Assert.Equal(routeTemplate, description.RelativePath);
            Assert.Equal(action, description.ActionDescriptor);
        }
Esempio n. 10
0
        public void Setup()
        {
            // Basic config
            _config = new SwaggerSpecConfig()
                .ResolveBasePath(() => "http://tempuri.org");

            // Get ApiExplorer for TestApp
            var httpConfiguration = new HttpConfiguration();
            WebApiConfig.Register(httpConfiguration);
            _apiExplorer = new ApiExplorer(httpConfiguration);
        }
        public void Setup()
        {
            SwaggerSpecConfig.Customize(c =>
                {
                    c.ResolveBasePath(() => "http://tempuri.org");
                    c.PostFilter<AddStandardErrorCodes>();
                    c.PostFilter<AddAuthorizationErrorCodes>();
                });

            // Get ApiExplorer for TestApp
            var httpConfiguration = new HttpConfiguration();
            WebApiConfig.Register(httpConfiguration);
            var apiExplorer = new ApiExplorer(httpConfiguration);

            _swaggerSpec = SwaggerSpec.CreateFrom(apiExplorer);
        }
 public void Setup()
 {
     // Get ApiExplorer for TestApp
     var httpConfiguration = new HttpConfiguration();
     TestApp.WebApiConfig.Register(httpConfiguration);
     _apiExplorer = new ApiExplorer(httpConfiguration);
 }
 /// <summary>
 ///     Initializes a new instance of the <see cref="T:System.Object" /> class.
 /// </summary>
 public VersionedApiExplorer(HttpConfiguration configuration) {
     this._configuration = configuration;
     this._defaultApiExplorer = new ApiExplorer(this._configuration);
     this._apiDescriptions = new Lazy<Collection<ApiDescription>>(() => this._defaultApiExplorer.ApiDescriptions);
 }
        public void Setup()
        {
            // Get dummy ApiExplorer
            var config = new HttpConfiguration();
            Dummy.WebApiConfig.Register(config);
            _apiExplorer = new ApiExplorer(config);
            config.EnsureInitialized();

            var xmlCommentsDoc = new XPathDocument(String.Format(@"{0}\XmlComments.xml", AppDomain.CurrentDomain.BaseDirectory));
            _filter = new ApplyActionXmlComments(xmlCommentsDoc);
        }
 public VersionedApiExplorer(HttpConfiguration configuration)
 {
     this.configuration = configuration;
     this.apiDescription = new Lazy<Collection<ApiDescription>>(InitializeApiDescriptions);
     this.DefaultExplorer = new ApiExplorer(configuration);
 }
 public void Setup()
 {
     // Get dummy ApiExplorer
     var config = new HttpConfiguration();
     Dummy.WebApiConfig.Register(config);
     _apiExplorer = new ApiExplorer(config);
     config.EnsureInitialized();
 }
Esempio n. 17
0
        public void Descriptions_RecognizesMixedCaseParameters()
        {
            // Ensure that two "Id"s, one from "api/values/{id}" and another "Id" from ClassWithId,
            // would not cause any exception and only one of them is added.
            var config = new HttpConfiguration();
            var routeTemplate = "api/values/{id}";
            var controllerDescriptor = new HttpControllerDescriptor(config, "ApiExplorerValues", typeof(DuplicatedIdController));
            var action = new ReflectedHttpActionDescriptor(controllerDescriptor, typeof(DuplicatedIdController).GetMethod("Get"));
            var actions = new ReflectedHttpActionDescriptor[] { action };
            config.Routes.Add("Route", CreateDirectRoute(routeTemplate, actions));

            var descriptions = new ApiExplorer(config).ApiDescriptions;

            ApiDescription description = Assert.Single(descriptions);
            Assert.Equal(HttpMethod.Get, description.HttpMethod);
            Assert.Equal(routeTemplate, description.RelativePath, StringComparer.OrdinalIgnoreCase);
            Assert.Equal(action, description.ActionDescriptor);
        }