Esempio n. 1
0
        private void AddRpcModulesData(List <Type> rpcTypes)
        {
            foreach (Type rpcType in rpcTypes)
            {
                string rpcModuleName = rpcType.Name.Substring(1).Replace("RpcModule", "").ToLower();

                MethodInfo[] moduleMethods = rpcType.GetMethods();
                Dictionary <string, MethodData> methods =
                    new Dictionary <string, MethodData>();

                foreach (MethodInfo moduleMethod in moduleMethods)
                {
                    string methodName = moduleMethod.Name.Substring(moduleMethod.Name.IndexOf('_') + 1);
                    JsonRpcMethodAttribute methodAttribute = moduleMethod.GetCustomAttribute <JsonRpcMethodAttribute>();

                    MethodData methodData = new MethodData()
                    {
                        IsImplemented       = methodAttribute?.IsImplemented,
                        ReturnType          = moduleMethod.ReturnType,
                        Parameters          = moduleMethod.GetParameters(),
                        Description         = methodAttribute?.Description,
                        EdgeCaseHint        = methodAttribute?.EdgeCaseHint,
                        ResponseDescription = methodAttribute?.ResponseDescription,
                        ExampleResponse     = methodAttribute?.ExampleResponse,
                        InvocationType      = InvocationType.JsonRpc
                    };

                    methods.Add(methodName, methodData);
                }

                _modulesData.Add(rpcModuleName, methods);
            }
        }
Esempio n. 2
0
 private static void CheckDescribed(MethodInfo method)
 {
     JsonRpcMethodAttribute attribute = method.GetCustomAttribute<JsonRpcMethodAttribute>();
     // this should really check if the string is not empty
     if (attribute?.Description is null)
     {
         throw new AssertionException(
             $"JSON RPC method {method.DeclaringType}.{method.Name} has no description.");
     }
 }
Esempio n. 3
0
        public void Generate()
        {
            StringBuilder descriptionsBuilder = new StringBuilder(@"JSON RPC
********

JSON RPC is available via HTTP and WS (needs to be explicitly switched on in the InitConfig).
Some of the methods listed below are not implemented by Nethermind (they are marked).

");

            List <Type> jsonRpcModules = new List <Type>();

            foreach (string assemblyName in _assemblyNames)
            {
                Assembly assembly = Assembly.Load(new AssemblyName(assemblyName));
                foreach (Type type in assembly.GetTypes().Where(t => typeof(IModule).IsAssignableFrom(t)).Where(t => t.IsInterface && t != typeof(IModule)))
                {
                    jsonRpcModules.Add(type);
                }
            }

            foreach (Type jsonRpcModule in jsonRpcModules.OrderBy(t => t.Name))
            {
                string moduleName = jsonRpcModule.Name.Substring(1).Replace("Module", "").ToLowerInvariant();
                descriptionsBuilder.Append($@"{moduleName}
{string.Empty.PadLeft(moduleName.Length, '^')}

");

                var properties = jsonRpcModule.GetMethods(BindingFlags.Public | BindingFlags.Instance);
                foreach (MethodInfo methodInfo in properties.OrderBy(p => p.Name))
                {
                    JsonRpcMethodAttribute attribute = methodInfo.GetCustomAttribute <JsonRpcMethodAttribute>();
                    string notImplementedString      = attribute == null || attribute.IsImplemented ? string.Empty : "[NOT IMPLEMENTED] ";
                    descriptionsBuilder.AppendLine($" {methodInfo.Name}({string.Join(", ", methodInfo.GetParameters().Select(p => p.Name))})")
                    .AppendLine($"  {notImplementedString}{attribute?.Description ?? "<description missing>"}").AppendLine();
                }
            }

            string result = descriptionsBuilder.ToString();

            Console.WriteLine(result);
            File.WriteAllText("jsonrpc.rst", result);
            string sourceDir = DocsDirFinder.FindDocsDir();

            File.WriteAllText(Path.Combine(sourceDir, "jsonrpc.rst"), result);
        }
Esempio n. 4
0
 /// <summary>
 /// JsonRpc请求内容
 /// </summary>
 /// <param name="context">请求上下文</param>
 /// <param name="jsonRpcMethod">请求方法特性</param>
 public JsonRpcContent(ApiActionContext context, JsonRpcMethodAttribute jsonRpcMethod)
 {
     this.context             = context;
     this.jsonRpcMethod       = jsonRpcMethod;
     this.Headers.ContentType = new MediaTypeHeaderValue(jsonRpcMethod.ContentType ?? JsonRpc.ContentType);
 }
Esempio n. 5
0
        private void GenerateDocFileContent(Type rpcType, string docsDir)
        {
            StringBuilder docBuilder = new StringBuilder();

            string moduleName = rpcType.Name.Substring(1).Replace("Module", "");

            MethodInfo[] moduleMethods = rpcType.GetMethods().OrderBy(m => m.Name).ToArray();
            List <Type>  rpcTypesToDescribe;

            docBuilder.AppendLine(@$ "# {moduleName}");
            docBuilder.AppendLine();
            moduleName = moduleName.ToLower();

            foreach (MethodInfo method in moduleMethods)
            {
                JsonRpcMethodAttribute attribute = method.GetCustomAttribute <JsonRpcMethodAttribute>();
                bool isImplemented = attribute == null || attribute.IsImplemented;

                if (!isImplemented)
                {
                    continue;
                }

                string methodName = method.Name.Substring(method.Name.IndexOf('_'));
                docBuilder.AppendLine(@$ "## {moduleName}{methodName}");
                docBuilder.AppendLine();
                docBuilder.AppendLine(@$ "{attribute?.Description ?? " "} ");
                docBuilder.AppendLine();
                _markdownGenerator.OpenTabs(docBuilder);
                _markdownGenerator.CreateTab(docBuilder, "Request");
                docBuilder.AppendLine(@"### **Parameters**");
                docBuilder.AppendLine();

                ParameterInfo[] parameters = method.GetParameters();
                rpcTypesToDescribe = new List <Type>();

                if (parameters.Length == 0)
                {
                    docBuilder.AppendLine("_None_");
                }
                else
                {
                    docBuilder.AppendLine("| Parameter name | Type |");
                    docBuilder.AppendLine("| :--- | :--- |");
                    string rpcParameterType;
                    foreach (ParameterInfo parameter in method.GetParameters())
                    {
                        rpcParameterType = _sharedContent.GetTypeToWrite(parameter.ParameterType, rpcTypesToDescribe);
                        docBuilder.AppendLine($"| {parameter.Name} | `{rpcParameterType}` |");
                    }
                }

                _markdownGenerator.CloseTab(docBuilder);
                docBuilder.AppendLine();
                _markdownGenerator.CreateTab(docBuilder, "Response");
                docBuilder.AppendLine(@$ "### Return type");
                docBuilder.AppendLine();

                Type   returnType    = GetTypeFromWrapper(method.ReturnType);
                string returnRpcType = _sharedContent.GetTypeToWrite(returnType, rpcTypesToDescribe);

                docBuilder.AppendLine(@$ "`{returnRpcType}`");

                _markdownGenerator.CloseTab(docBuilder);

                if (rpcTypesToDescribe.Count != 0)
                {
                    docBuilder.AppendLine();
                    _markdownGenerator.CreateTab(docBuilder, "Object definitions");
                    _sharedContent.AddObjectsDescription(docBuilder, rpcTypesToDescribe);
                    _markdownGenerator.CloseTab(docBuilder);
                }

                _markdownGenerator.CloseTabs(docBuilder);
                docBuilder.AppendLine();
            }

            _sharedContent.Save(moduleName, docsDir + "/json-rpc-modules", docBuilder);
        }