Пример #1
0
        private ClientGeneratorMethod BuildClassMethod(string url, Operation operation, RAML.Parser.Model.EndPoint resource, string modelsNamespace)
        {
            var parentUrl = UrlGeneratorHelper.GetParentUri(url, resource.Path);

            //TODO: check
            var responseContentTypes = operation.Responses != null?
                                       operation.Responses.Where(r => r.Payloads != null).SelectMany(r => r.Payloads).Select(p => p.MediaType).ToArray()
                                           : new string[0];

            var generatedMethod = new ClientGeneratorMethod
            {
                ModelsNamespace      = modelsNamespace,
                Name                 = NetNamingMapper.GetMethodName(operation.Method ?? "Get" + resource.Path),
                ReturnType           = GetReturnType(operation, resource, url),
                Parameter            = GetParameter(GeneratorServiceHelper.GetKeyForResource(operation, resource), operation, resource, url),
                Comment              = GetComment(resource, operation, url),
                Url                  = url,
                Verb                 = NetNamingMapper.Capitalize(operation.Method),
                Parent               = null,
                UseSecurity          = resource.Operations.Any(m => m.Method == operation.Method && m.Security != null && m.Security.Any()),
                RequestContentTypes  = operation.ContentType,
                ResponseContentTypes = responseContentTypes
            };

            // look in traits

            // look in resource types

            return(generatedMethod);
        }
        private IEnumerable <Property> BuildResponseProperties(ClientGeneratorMethod method)
        {
            var properties = new Collection <Property>();

            if (method.ReturnType != "string" && method.ReturnType != "HttpContent")
            {
                properties.Add(new Property
                {
                    Name        = "Content",
                    Description = "Typed Response content",
                    Type        = method.ReturnType
                });
            }

            if (method.ResponseHeaders != null && method.ResponseHeaders.Any())
            {
                properties.Add(new Property
                {
                    Name        = "Headers",
                    Description = "Typed Response headers (defined in RAML)",
                    Type        = method.ResponseHeaderType
                });
            }

            return(properties);
        }
Пример #3
0
 private void GetHeaders(string objectName, Operation method, ClientGeneratorMethod generatedMethod)
 {
     if (method.Request != null && method.Request.Headers != null && method.Request.Headers.Any())
     {
         var headerObject = HeadersParser.GetHeadersObject(generatedMethod, method, objectName);
         generatedMethod.Header = headerObject;
         headerObjects.Add(headerObject.Name, headerObject);
     }
 }
Пример #4
0
        private IEnumerable <Property> BuildRequestProperties(ClientGeneratorMethod method)
        {
            var properties = new Collection <Property>();

            if (method.Header != null)
            {
                properties.Add(new Property
                {
                    Name        = "Headers",
                    Description = "Typed Request headers",
                    Type        = method.Header.Name
                });
            }

            if (method.Verb.ToLowerInvariant() != "get" && method.Verb.ToLowerInvariant() != "delete")
            {
                properties.Add(new Property
                {
                    Name        = "Content",
                    Description = "Request content",
                    Type        = method.Parameter != null && method.Parameter.Type != "string" ? method.Parameter.Type : "HttpContent"
                });

                properties.Add(new Property
                {
                    Name        = "Formatter",
                    Description = "Request formatter",
                    Type        = "MediaTypeFormatter"
                });
            }

            if (method.Query != null)
            {
                properties.Add(new Property
                {
                    Name        = "Query",
                    Description = "Request query string properties",
                    Type        = method.Query.Name
                });
            }

            if (method.UriParameters.Any())
            {
                properties.Add(new Property
                {
                    Name        = "UriParameters",
                    Description = "Request Uri Parameters",
                    Type        = method.UriParametersType
                });
            }

            return(properties);
        }
Пример #5
0
 private void GetQueryParameters(string objectName, Operation method, ClientGeneratorMethod generatedMethod)
 {
     if (method.Request != null && method.Request.QueryParameters != null && method.Request.QueryParameters.Any())
     {
         var queryObject = queryParametersParser.GetQueryObject(generatedMethod, method, objectName);
         generatedMethod.Query = queryObject;
         if (!queryObjects.ContainsKey(queryObject.Name))
         {
             queryObjects.Add(queryObject.Name, queryObject);
         }
     }
 }
Пример #6
0
        private static string GetOkReturnType(ClientGeneratorMethod generatedMethod)
        {
            if (!generatedMethod.ReturnTypeObject.IsMultiple)
            {
                return(generatedMethod.ReturnType);
            }

            if (generatedMethod.ReturnTypeObject.Properties.Any(p => p.StatusCode == HttpStatusCode.OK))
            {
                return(generatedMethod.ReturnTypeObject.Properties.First(p => p.StatusCode == HttpStatusCode.OK).Type);
            }

            return(generatedMethod.ReturnTypeObject.Properties.First().Type);
        }
Пример #7
0
        private static List <Property> BuildProperties(ClientGeneratorMethod generatedMethod)
        {
            var properties = generatedMethod.ResponseHeaders
                             .Select(kv => new Property
            {
                Name        = kv.Value.Name,
                Description = kv.Value.Description,
                Example     = kv.Value.Example,
                StatusCode  = kv.Key,
                Type        = kv.Value.Name
            })
                             .ToList();

            return(properties);
        }
Пример #8
0
        private void CreateMultipleType(ClientGeneratorMethod generatedMethod)
        {
            var properties = BuildProperties(generatedMethod);

            var name = NetNamingMapper.GetObjectName("Multiple" + generatedMethod.Url + generatedMethod.Name + "Header");

            var apiObject = new ApiObject
            {
                Name        = name,
                Description = "Multiple Header Types " + string.Join(", ", properties.Select(p => p.Name)),
                Properties  = properties,
                IsMultiple  = true
            };

            responseHeadersObjects.Add(new KeyValuePair <string, ApiObject>(name, apiObject));

            generatedMethod.ResponseHeaderType = generatedMethod.ModelsNamespace + "." + name;
        }
Пример #9
0
        private void BuildRequest(ClientGeneratorMethod method, ClassObject classObject, ICollection <ApiObject> objects)
        {
            var requestProperties = BuildRequestProperties(method).ToList();

            if (requestProperties.Any())
            {
                var reqObject = new ApiObject
                {
                    Name        = classObject.Name + method.Name + "Request",
                    Description = "Request object for method " + method.Name + " of class " + classObject.Name,
                    Properties  = requestProperties
                };
                objects.Add(reqObject);
                method.RequestType = method.ModelsNamespace + "." + reqObject.Name;
            }
            else
            {
                method.RequestType = "ApiRequest";
            }
        }
        private void BuildResponse(ClientGeneratorMethod method, ClassObject classObject, ICollection <ApiObject> objects)
        {
            var responseProperties = BuildResponseProperties(method).ToList();

            if (responseProperties.Any())
            {
                var respObject = new ApiObject
                {
                    Name        = classObject.Name + method.Name + "Response",
                    Description = "Response object for method " + method.Name + " of class " + classObject.Name,
                    Properties  = responseProperties,
                    IsUnionType = method.ReturnTypeObject != null && method.ReturnTypeObject.IsUnionType
                };
                objects.Add(respObject);
                method.ResponseType = method.ModelsNamespace + "." + respObject.Name;
            }
            else
            {
                method.ResponseType = "ApiResponse";
            }
        }
Пример #11
0
        private void GetResponseHeaders(string objectName, ClientGeneratorMethod generatedMethod, Operation method)
        {
            generatedMethod.ResponseHeaders = new Dictionary <HttpStatusCode, ApiObject>();
            foreach (var resp in method.Responses.Where(r => r.Headers != null && r.Headers.Any()))
            {
                var headerObject = HeadersParser.GetHeadersObject(generatedMethod, resp, objectName);
                generatedMethod.ResponseHeaders.Add(ParserHelpers.GetHttpStatusCode(resp.StatusCode), headerObject);
                responseHeadersObjects.Add(headerObject.Name, headerObject);
            }

            if (!generatedMethod.ResponseHeaders.Any())
            {
                generatedMethod.ResponseHeaderType = defaultHeaderType;
            }
            else if (generatedMethod.ResponseHeaders.Count == 1)
            {
                generatedMethod.ResponseHeaderType = generatedMethod.ModelsNamespace + "." + generatedMethod.ResponseHeaders.First().Value.Name;
            }
            else
            {
                CreateMultipleType(generatedMethod);
            }
        }