Ejemplo n.º 1
0
        private static bool TryGetResourceParameter(ApiDescription apiDescription, HttpConfiguration config, out ApiParameterDescription parameterDescription, out Type resourceType)
        {
            parameterDescription = apiDescription.ParameterDescriptions.FirstOrDefault(
                p => p.Source == ApiParameterSource.FromBody ||
                (p.ParameterDescriptor != null && p.ParameterDescriptor.ParameterType == typeof(HttpRequestMessage)));

            if (parameterDescription == null)
            {
                resourceType = null;
                return(false);
            }

            resourceType = parameterDescription.ParameterDescriptor.ParameterType;

            if (resourceType == typeof(HttpRequestMessage))
            {
                AppPageSampleGenerator sampleGenerator = config.GetAppPageSampleGenerator();
                resourceType = sampleGenerator.ResolveHttpRequestMessageType(apiDescription);
            }

            if (resourceType == null)
            {
                parameterDescription = null;
                return(false);
            }

            return(true);
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Sets the help page sample generator.
 /// </summary>
 /// <param name="config">The <see cref="HttpConfiguration"/>.</param>
 /// <param name="sampleGenerator">The help page sample generator.</param>
 public static void SetHelpPageSampleGenerator(this HttpConfiguration config, AppPageSampleGenerator sampleGenerator)
 {
     config.Properties.AddOrUpdate(
         typeof(AppPageSampleGenerator),
         k => sampleGenerator,
         (k, o) => sampleGenerator);
 }
Ejemplo n.º 3
0
        private static AppPageApiModel GenerateApiModel(ApiDescription apiDescription, HttpConfiguration config)
        {
            AppPageApiModel apiModel = new AppPageApiModel()
            {
                ApiDescription = apiDescription,
            };

            ModelDescriptionGenerator modelGenerator  = config.GetModelDescriptionGenerator();
            AppPageSampleGenerator    sampleGenerator = config.GetAppPageSampleGenerator();

            GenerateUriParameters(apiModel, modelGenerator);
            GenerateRequestModelDescription(apiModel, modelGenerator, sampleGenerator);
            GenerateResourceDescription(apiModel, modelGenerator);
            GenerateSamples(apiModel, sampleGenerator);

            return(apiModel);
        }
Ejemplo n.º 4
0
        private static void GenerateSamples(AppPageApiModel apiModel, AppPageSampleGenerator sampleGenerator)
        {
            try
            {
                foreach (var item in sampleGenerator.GetSampleRequests(apiModel.ApiDescription))
                {
                    apiModel.SampleRequests.Add(item.Key, item.Value);
                    LogInvalidSampleAsError(apiModel, item.Value);
                }

                foreach (var item in sampleGenerator.GetSampleResponses(apiModel.ApiDescription))
                {
                    apiModel.SampleResponses.Add(item.Key, item.Value);
                    LogInvalidSampleAsError(apiModel, item.Value);
                }
            }
            catch (Exception e)
            {
                apiModel.ErrorMessages.Add(String.Format(CultureInfo.CurrentCulture,
                                                         "An exception has occurred while generating the sample. Exception message: {0}",
                                                         AppPageSampleGenerator.UnwrapException(e).Message));
            }
        }
Ejemplo n.º 5
0
 // Default factory for sample objects
 private static object DefaultSampleObjectFactory(AppPageSampleGenerator sampleGenerator, Type type)
 {
     // Try to create a default sample object
     ObjectGenerator objectGenerator = new ObjectGenerator();
     return objectGenerator.GenerateObject(type);
 }
Ejemplo n.º 6
0
        private static void GenerateRequestModelDescription(AppPageApiModel apiModel, ModelDescriptionGenerator modelGenerator, AppPageSampleGenerator sampleGenerator)
        {
            ApiDescription apiDescription = apiModel.ApiDescription;

            foreach (ApiParameterDescription apiParameter in apiDescription.ParameterDescriptions)
            {
                if (apiParameter.Source == ApiParameterSource.FromBody)
                {
                    Type parameterType = apiParameter.ParameterDescriptor.ParameterType;
                    apiModel.RequestModelDescription = modelGenerator.GetOrCreateModelDescription(parameterType);
                    apiModel.RequestDocumentation    = apiParameter.Documentation;
                }
                else if (apiParameter.ParameterDescriptor != null &&
                         apiParameter.ParameterDescriptor.ParameterType == typeof(HttpRequestMessage))
                {
                    Type parameterType = sampleGenerator.ResolveHttpRequestMessageType(apiDescription);

                    if (parameterType != null)
                    {
                        apiModel.RequestModelDescription = modelGenerator.GetOrCreateModelDescription(parameterType);
                    }
                }
            }
        }