private static IEnumerable <CitrinaObjectProperty> GetObjectProperties(ApiObject obj)
        {
            var properties = new List <ApiObjectProperty>();

            if (obj.AllOf != null)
            {
                if (obj.AllOf.Any(o => o.OneOf != null))
                {
                    var oneOfList = obj.AllOf.FirstOrDefault(o => o.OneOf != null)?.OneOf;

                    if (oneOfList != null)
                    {
                        foreach (var o in oneOfList)
                        {
                            var props = o.Reference.Properties;

                            if (props != null)
                            {
                                properties.AddRange(props);
                            }
                        }
                    }
                }
                else
                {
                    var props = obj.AllOf.FirstOrDefault(o => o.Reference == null && o.OneOf == null)?.Properties;

                    if (props != null)
                    {
                        properties.AddRange(props);
                    }
                }
            }
            else
            {
                if (obj.Properties != null)
                {
                    properties.AddRange(obj.Properties);
                }
            }

            return(properties.Distinct(new ObjectPropertyEqualityComparer()).Select(p =>
            {
                var name = p.Name;
                var needJsonAttr = false;

                if (char.IsDigit(name.First()))
                {
                    name = '_' + name;
                    needJsonAttr = true;
                }

                return new CitrinaObjectProperty
                {
                    Name = name,
                    OriginalName = p.OriginalName,
                    Type = TypesResolver.ResolveType(p),
                    NeedJsonAttribute = needJsonAttr,
                    Summary = p.Description
                };
            }).ToArray());
        }
Example #2
0
        public static IDictionary <string, IList <CitrinaMethod> > Process()
        {
            var result = new Dictionary <string, IList <CitrinaMethod> >();

            var apiSchema = new VKApiSchema();
            var methods   = apiSchema.GetMethodsAsync().Result.Methods;

            foreach (var method in methods)
            {
                if (method.AccessTokenTypes == null)
                {
                    method.AccessTokenTypes = new[] { ApiAccessTokenType.Open };
                }

                foreach (var accessTokenType in method.AccessTokenTypes)
                {
                    var excludedParameters = method.Responses.Select(r => r.Name.Replace("Response", "")).ToArray();

                    foreach (var response in method.Responses)
                    {
                        if (response.Reference == null)
                        {
                            // Waiting for fixes to come...
                            continue;
                        }

                        var methodNamePostfix = response.Name.Replace("Response", "");

                        var methodParameters  = new List <string>();
                        var mappingParameters = new List <string>();
                        var needAccessToken   = true;

                        switch (accessTokenType)
                        {
                        case ApiAccessTokenType.User:
                            methodParameters.Add("UserAccessToken accessToken");
                            mappingParameters.Add("[\"access_token\"] = accessToken?.Value");
                            break;

                        case ApiAccessTokenType.Group:
                            methodParameters.Add("GroupAccessToken accessToken");
                            mappingParameters.Add("[\"access_token\"] = accessToken?.Value");
                            break;

                        case ApiAccessTokenType.Service:
                            methodParameters.Add("ServiceAccessToken accessToken");
                            mappingParameters.Add("[\"access_token\"] = accessToken?.Value");
                            break;

                        default:
                            needAccessToken = false;
                            break;
                        }

                        if (method.Parameters != null)
                        {
                            foreach (var parameter in method.Parameters)
                            {
                                var parameterName         = parameter.Name;
                                var originalParameterName = parameter.OriginalName;
                                var parameterType         = TypesResolver.ResolveType(parameter);

                                if (!parameterName.Equals(methodNamePostfix) && excludedParameters.Contains(parameterName))
                                {
                                    continue;
                                }

                                parameterName = parameterName[0].ToString().ToLower() + string.Join("", parameterName.Skip(1));

                                if (InternalNames.Contains(parameterName))
                                {
                                    parameterName         = "@" + parameterName;
                                    originalParameterName = "@" + originalParameterName;
                                }

                                methodParameters.Add($"{parameterType} {parameterName} = null");

                                mappingParameters.Add(GetDictionaryParameter(parameterType, originalParameterName, parameterName));
                            }
                        }

                        var citrinaMethod = new CitrinaMethod
                        {
                            Name              = $"{method.Name}{methodNamePostfix}",
                            OriginalName      = method.OriginalName,
                            Description       = method.Description,
                            InlineParameters  = string.Join(", ", methodParameters),
                            MappingParameters = mappingParameters,
                            ReturnType        = TypesResolver.ResolveType(response.Reference),
                            NeedAccessToken   = needAccessToken
                        };

                        if (result.ContainsKey(method.MethodGroup))
                        {
                            result[method.MethodGroup].Add(citrinaMethod);
                        }
                        else
                        {
                            result.Add(method.MethodGroup, new List <CitrinaMethod> {
                                citrinaMethod
                            });
                        }
                    }
                }
            }

            return(result);
        }