Exemplo n.º 1
0
        public RAMLModel WebApiToRamlModel(Uri baseUri, string title, string version, string defaultMediaTypes, string description)
        {
            var model = new RAMLModel
            {
                BaseUri = baseUri, //new Uri("http://www.google.com"),
                Title = title, //"Super web api",
                Version = version, //"1"
                DefaultMediaType = defaultMediaTypes,
                Description = description,
                Routes = new List<RouteModel>()
            };

            foreach (var api in _apiDescriptions)
            {
                IEnumerable<RequestHeadersAttribute> headers = null;
                IEnumerable<ResponseBodyAttribute> responseBody = null;

                if(api.ActionDescriptor != null)
                {
                    headers = api.ActionDescriptor.GetCustomAttributes<RequestHeadersAttribute>();
                    responseBody = api.ActionDescriptor.GetCustomAttributes<ResponseBodyAttribute>();
                }

                var routeModel = new RouteModel
                {
                    Verb = api.HttpMethod.Method.ToLower(),
                    UrlTemplate = api.RelativePath,
                    Headers = GetHeaders(headers),
                    //QueryParameters = GetQueryParameters(api),
                    BodyParameters = GetBodyParameters(api),
                    UriParameters = GetUriParameters(api),
                    Responses = GetResponseBodies(responseBody),
                    Description = api.Documentation
                };

                if (routeModel.Verb == "put" || routeModel.Verb == "post")
                {
                    routeModel.RequestContentType = "application/x-www-form-urlencoded:";
                }

                model.Routes.Add(routeModel);
            }

            return model;
        }
Exemplo n.º 2
0
 public void TestInitialize()
 {
     expectedModel = new RAMLModel
     {
         BaseUri = new Uri("http://www.test.com"),
         DefaultMediaType = "application/json",
         Description = "test",
         Title = "test",
         Version = "1",
         Routes = new List<RouteModel>()
     };
 }
        public void TestInitialize()
        {
            mockActionDescriptor = new Mock<HttpActionDescriptor>();

            expectedModel = new RAMLModel
            {
                BaseUri = new Uri("http://www.test.com"),
                DefaultMediaType = "application/json",
                Description = "test",
                Title = "test",
                Version = "1",
                Routes = new List<RouteModel>
                {
                    new RouteModel
                    {
                        UrlTemplate="api/test",
                        Verb="get",
                        Responses= null
                    }
                }
            };

            descriptions = new List<ApiDescription>()
            {
                new ApiDescription
                {
                     HttpMethod = new System.Net.Http.HttpMethod("get"),
                     RelativePath = "api/test",
                     ActionDescriptor = mockActionDescriptor.Object
                }
            };

            expectedResponseBody = new List<ResponseModel>
            {
                new ResponseModel {
                      ContentType="application/json",
                      Description="This is a json response.",
                      Example="{ 'value':'Hello World' }",
                      StatusCode= System.Net.HttpStatusCode.OK
                },
                new ResponseModel {
                      ContentType="application/xml",
                      Description="This is an error response in xml.",
                      Example="<error><message>opps</message></error>",
                      StatusCode= System.Net.HttpStatusCode.BadRequest
                }
            };

            expectedResponseBodyAttributes = new Collection<ResponseBodyAttribute>(
                    expectedResponseBody.Select(
                    p => new ResponseBodyAttribute
                    {
                        ContentType = p.ContentType,
                        Description = p.Description,
                        Example = p.Example,
                        StatusCode = p.StatusCode
                    }
                ).ToList());
        }