Пример #1
0
        private IIriTemplate BuildTemplate(DescriptionContext context, OperationInfo <Verb> operation, IOperation operationDocumentation)
        {
            IEnumerable <ArgumentInfo> parameterMapping;
            var uriTemplate = _descriptionBuilder.GetOperationUrlTemplate(operation.UnderlyingMethod, out parameterMapping);

            if (String.IsNullOrEmpty(uriTemplate))
            {
                return(null);
            }

            IIriTemplate template         = null;
            var          templateUri      = operationDocumentation.Id.Uri.AddFragment("template");
            var          templateMappings = from mapping in parameterMapping where !(mapping.Source is FromBodyAttribute) select BuildTemplateMapping(context, templateUri, operation, mapping);

            foreach (var templateMapping in templateMappings)
            {
                if (template == null)
                {
                    template          = context.ApiDocumentation.Context.Create <IIriTemplate>(templateUri);
                    template.Template = uriTemplate;
                }

                template.Mappings.Add(templateMapping);
            }

            return(template);
        }
Пример #2
0
        /// <summary>Initializes a new instance of the <see cref="LinkedOperation"/> struct.</summary>
        /// <param name="operation">The operation.</param>
        /// <param name="iriTemplate">The IRI template.</param>
        public LinkedOperation(IOperation operation, IIriTemplate iriTemplate)
        {
            if (operation == null)
            {
                throw new ArgumentNullException("operation");
            }

            _operation = operation;
            _iriTemplate = iriTemplate;
        }
Пример #3
0
        /// <summary>Initializes a new instance of the <see cref="LinkedOperation"/> struct.</summary>
        /// <param name="operation">The operation.</param>
        /// <param name="iriTemplate">The IRI template.</param>
        public LinkedOperation(IOperation operation, IIriTemplate iriTemplate)
        {
            if (operation == null)
            {
                throw new ArgumentNullException("operation");
            }

            _operation   = operation;
            _iriTemplate = iriTemplate;
        }
Пример #4
0
        // TODO: Add support for header out parameters like totalItems.
        private void AnalyzeOperation(IClass supportedClass, IOperation operation, IIriTemplate template, StringBuilder operations, IDictionary<string, string> classes)
        {
            foreach (var method in operation.Method)
            {
                var bodyArguments = new StringBuilder(256);
                var uriArguments = new StringBuilder(256);
                var parameters = new StringBuilder(256);
                var accept = new StringBuilder(256);
                var contentType = new StringBuilder(256);
                var returns = "void";
                var operationName = CreateName(operation, method);
                var uri = operation.Id.Uri.ToRelativeUri().ToString();
                var returnedType = String.Empty;
                var isReturns = String.Empty;
                bool isContentRangeHeaderParameterAdded = false;
                if (template != null)
                {
                    uri = template.Template;
                    isContentRangeHeaderParameterAdded = AnalyzeTemplate(ref uri, template.Mappings, parameters, uriArguments);
                }

                AnalyzeBody(operation.Expects, parameters, bodyArguments, contentType, operation.MediaTypes);
                if (parameters.Length > 2)
                {
                    parameters.Remove(parameters.Length - 2, 2);
                }

                if (operation.Returns.Any())
                {
                    isReturns = "var result = ";
                    returns = AnalyzeResult(operationName, operation.Returns, classes, accept, operation.MediaTypes);
                    returnedType = String.Format("<{0}>", returns);
                }

                if (accept.Length == 0)
                {
                    accept.Append("            var accept = new string[0];");
                }

                if (contentType.Length == 0)
                {
                    contentType.Append("            var contentType = new string[0];");
                }

                var isStatic = ((operation.Expects.Any(expected => expected == supportedClass)) && (method == "POST") ? " static" : String.Empty);
                operations.AppendFormat(
                    OperationTemplate,
                    isStatic,
                    returns,
                    operationName,
                    parameters,
                    method,
                    uri,
                    uriArguments,
                    bodyArguments,
                    isReturns,
                    returnedType,
                    accept,
                    contentType,
                    (isContentRangeHeaderParameterAdded ? "            totalEntities = (int)uriArguments[\"totalEntities\"];" + Environment.NewLine : String.Empty),
                    (isReturns.Length > 0 ? "            return result;" + Environment.NewLine : String.Empty));
            }
        }
Пример #5
0
        // TODO: Add support for header out parameters like totalItems.
        private void AnalyzeOperation(IClass supportedClass, IOperation operation, IIriTemplate template, StringBuilder operations, IDictionary <string, string> classes)
        {
            foreach (var method in operation.Method)
            {
                var  bodyArguments = new StringBuilder(256);
                var  uriArguments  = new StringBuilder(256);
                var  parameters    = new StringBuilder(256);
                var  accept        = new StringBuilder(256);
                var  contentType   = new StringBuilder(256);
                var  returns       = "void";
                var  operationName = CreateName(operation, method);
                var  uri           = operation.Id.Uri.ToRelativeUri().ToString();
                var  returnedType  = String.Empty;
                var  isReturns     = String.Empty;
                bool isContentRangeHeaderParameterAdded = false;
                if (template != null)
                {
                    uri = template.Template;
                    isContentRangeHeaderParameterAdded = AnalyzeTemplate(ref uri, template.Mappings, parameters, uriArguments);
                }

                AnalyzeBody(operation.Expects, parameters, bodyArguments, contentType, operation.MediaTypes);
                if (parameters.Length > 2)
                {
                    parameters.Remove(parameters.Length - 2, 2);
                }

                if (operation.Returns.Any())
                {
                    isReturns    = "var result = ";
                    returns      = AnalyzeResult(operationName, operation.Returns, classes, accept, operation.MediaTypes);
                    returnedType = String.Format("<{0}>", returns);
                }

                if (accept.Length == 0)
                {
                    accept.Append("            var accept = new string[0];");
                }

                if (contentType.Length == 0)
                {
                    contentType.Append("            var contentType = new string[0];");
                }

                var isStatic = ((operation.Expects.Any(expected => expected == supportedClass)) && (method == "POST") ? " static" : String.Empty);
                operations.AppendFormat(
                    OperationTemplate,
                    isStatic,
                    returns,
                    operationName,
                    parameters,
                    method,
                    uri,
                    uriArguments,
                    bodyArguments,
                    isReturns,
                    returnedType,
                    accept,
                    contentType,
                    (isContentRangeHeaderParameterAdded ? "            totalEntities = (int)uriArguments[\"totalEntities\"];" + Environment.NewLine : String.Empty),
                    (isReturns.Length > 0 ? "            return result;" + Environment.NewLine : String.Empty));
            }
        }
Пример #6
0
        private IOperation BuildOperation(DescriptionContext context, OperationInfo<Verb> operation, out IIriTemplate template)
        {
            IOperation result = operation.AsOperation(context.ApiDocumentation);
            result.Label = operation.UnderlyingMethod.Name;
            result.Description = _xmlDocProvider.GetDescription(operation.UnderlyingMethod);
            result.Method.Add(operation.ProtocolSpecificCommand.ToString());
            template = BuildTemplate(context, operation, result);
            bool requiresRdf = context.RequiresRdf(SpecializationType);
            bool isRdfRequired = requiresRdf;
            var arguments = operation.Arguments.Where(parameter => parameter.Source is FromBodyAttribute).Select(parameter => parameter.Parameter);
            var results = operation.Results.Where(output => output.Target is ToBodyAttribute).Select(parameter => parameter.Parameter);

            foreach (var value in arguments)
            {
                var expected = (context.ContainsType(value.ParameterType) ? context[value.ParameterType] : context.ForType(value.ParameterType).BuildTypeDescription(out isRdfRequired));
                expected = context.SubClass(expected, value.ParameterType);
                expected.Label = value.Name ?? "instance";
                expected.Description = _xmlDocProvider.GetDescription(operation.UnderlyingMethod, value);
                result.Expects.Add(expected);
                requiresRdf |= isRdfRequired;
            }

            foreach (var value in results)
            {
                var returned = (context.ContainsType(value.ParameterType) ? context[value.ParameterType] : context.ForType(value.ParameterType).BuildTypeDescription(out isRdfRequired));
                returned = context.SubClass(returned, value.ParameterType);
                returned.Description = _xmlDocProvider.GetDescription(operation.UnderlyingMethod, value);
                result.Returns.Add(returned);
                requiresRdf |= isRdfRequired;
            }

            BuildStatusCodes(result, operation);
            BuildOperationMediaType(result, result.Returns, operation, requiresRdf);
            BuildOperationMediaType(result, result.Expects, operation, requiresRdf);
            if (!result.MediaTypes.Any())
            {
                BuildOperationMediaType(result, requiresRdf);
            }

            return result;
        }
Пример #7
0
        private IOperation BuildOperation(DescriptionContext context, OperationInfo <Verb> operation, out IIriTemplate template)
        {
            IOperation result = operation.AsOperation(context.ApiDocumentation);

            result.Label       = operation.UnderlyingMethod.Name;
            result.Description = _xmlDocProvider.GetDescription(operation.UnderlyingMethod);
            result.Method.Add(operation.ProtocolSpecificCommand.ToString());
            template = BuildTemplate(context, operation, result);
            bool requiresRdf   = context.RequiresRdf(SpecializationType);
            bool isRdfRequired = requiresRdf;
            var  arguments     = operation.Arguments.Where(parameter => parameter.Source is FromBodyAttribute).Select(parameter => parameter.Parameter);
            var  results       = operation.Results.Where(output => output.Target is ToBodyAttribute).Select(parameter => parameter.Parameter);

            foreach (var value in arguments)
            {
                var expected = (context.ContainsType(value.ParameterType) ? context[value.ParameterType] : context.ForType(value.ParameterType).BuildTypeDescription(out isRdfRequired));
                expected             = context.SubClass(expected, value.ParameterType);
                expected.Label       = value.Name ?? "instance";
                expected.Description = _xmlDocProvider.GetDescription(operation.UnderlyingMethod, value);
                result.Expects.Add(expected);
                requiresRdf |= isRdfRequired;
            }

            foreach (var value in results)
            {
                var returned = (context.ContainsType(value.ParameterType) ? context[value.ParameterType] : context.ForType(value.ParameterType).BuildTypeDescription(out isRdfRequired));
                returned             = context.SubClass(returned, value.ParameterType);
                returned.Description = _xmlDocProvider.GetDescription(operation.UnderlyingMethod, value);
                result.Returns.Add(returned);
                requiresRdf |= isRdfRequired;
            }

            BuildStatusCodes(result, operation);
            BuildOperationMediaType(result, result.Returns, operation, requiresRdf);
            BuildOperationMediaType(result, result.Expects, operation, requiresRdf);
            if (!result.MediaTypes.Any())
            {
                BuildOperationMediaType(result, requiresRdf);
            }

            return(result);
        }