Exemplo n.º 1
0
        public RPCServer(NexusAPI api, string endPoint, int port, LoggerCallback logger = null)
        {
            if (string.IsNullOrEmpty(endPoint))
            {
                endPoint = "/";
            }

            Port     = port;
            EndPoint = endPoint;
            API      = api;

            var settings = new ServerSettings()
            {
                Environment = ServerEnvironment.Prod, Port = port, MaxPostSizeInBytes = 1024 * 128, Compression = false
            };

            _server = new HTTPServer(settings, logger);

            var rpc = new RPCPlugin(_server, endPoint);

            foreach (var entry in api.Methods)
            {
                var methodName = char.ToLower(entry.Name[0]) + entry.Name.Substring(1);
                var apiMethod  = entry;
                rpc.RegisterHandler(methodName, (paramNode) =>
                {
                    var args = new object[apiMethod.Parameters.Count];
                    for (int i = 0; i < args.Length; i++)
                    {
                        if (i < paramNode.ChildCount)
                        {
                            args[i] = paramNode.GetNodeByIndex(i).Value;
                        }
                        else
                        if (apiMethod.Parameters[i].HasDefaultValue)
                        {
                            args[i] = apiMethod.Parameters[i].DefaultValue;
                        }
                        else
                        {
                            throw new RPCException("missing argument: " + apiMethod.Parameters[i].Name);
                        }
                    }

                    IAPIResult result;
                    try
                    {
                        result = api.Execute(apiMethod.Name, args);
                    }
                    catch (APIException e)
                    {
                        throw new RPCException(e.Message);
                    }

                    CheckForError(result);
                    return(APIUtils.FromAPIResult(result));
                });
            }
        }
Exemplo n.º 2
0
        public APIEntry(NexusAPI api, MethodInfo info)
        {
            _api  = api;
            _info = info;
            Name  = info.Name;

            var parameters = info.GetParameters();

            Parameters = new List <APIValue>();
            foreach (var entry in parameters)
            {
                string description;
                string exampleValue;

                var descAttr = entry.GetCustomAttribute <APIParameterAttribute>();
                if (descAttr != null)
                {
                    description  = descAttr.Description;
                    exampleValue = descAttr.Value;
                }
                else
                {
                    description  = "TODO document me";
                    exampleValue = "TODO document me";
                }

                object defaultValue;

                if (entry.HasDefaultValue)
                {
                    defaultValue = entry.DefaultValue;
                }
                else
                {
                    defaultValue = null;
                }

                Parameters.Add(new APIValue(entry.ParameterType, entry.Name, description, exampleValue, defaultValue, entry.HasDefaultValue));
            }

            try
            {
                FailCases = info.GetCustomAttributes <APIFailCaseAttribute>().ToArray();
            }
            catch
            {
                FailCases = new APIFailCaseAttribute[0];
            }

            try
            {
                var attr = info.GetCustomAttribute <APIInfoAttribute>();
                ReturnType  = attr.ReturnType;
                Description = attr.Description;
                IsPaginated = attr.Paginated;
            }
            catch
            {
                ReturnType  = null;
                Description = "TODO document me";
                IsPaginated = false;
            }
        }
Exemplo n.º 3
0
        public RESTServer(NexusAPI api, string endPoint, int port, LoggerCallback logger = null)
        {
            if (string.IsNullOrEmpty(endPoint))
            {
                endPoint = "/";
            }

            if (!endPoint.EndsWith("/"))
            {
                endPoint += "/";
            }

            Port     = port;
            EndPoint = endPoint;
            API      = api;

            var settings = new ServerSettings()
            {
                Environment = ServerEnvironment.Prod, Port = port, MaxPostSizeInBytes = 1024 * 128
            };

            _server = new HTTPServer(settings, logger);

            var apiMap = DataNode.CreateObject();

            foreach (var entry in api.Methods)
            {
                var methodName = char.ToLower(entry.Name[0]) + entry.Name.Substring(1);
                var apiMethod  = entry;

                var path  = endPoint + methodName;
                var paths = new List <string>();

                var mapEntry = DataNode.CreateObject(methodName);
                apiMap.AddNode(mapEntry);

                var argMap = DataNode.CreateArray("args");
                mapEntry.AddNode(argMap);

                paths.Add(path);
                foreach (var arg in apiMethod.Parameters)
                {
                    var argEntry = DataNode.CreateObject();
                    argEntry.AddField("name", arg.Name);
                    argEntry.AddField("type", arg.Type.Name);
                    argEntry.AddField("required", arg.DefaultValue == null);
                    argEntry.AddField("description", arg.Description);
                    argMap.AddNode(argEntry);

                    path += "/{" + arg.Name + "}";
                    paths.Add(path);
                }

                foreach (var url in paths)
                {
                    _server.Get(url, (request) =>
                    {
                        var args = new object[apiMethod.Parameters.Count];

                        IAPIResult result;
                        try
                        {
                            for (int i = 0; i < args.Length; i++)
                            {
                                var name = apiMethod.Parameters[i].Name;
                                if (request.args.ContainsKey(name))
                                {
                                    args[i] = request.args[name];
                                }
                                else
                                if (apiMethod.Parameters[i].DefaultValue != null)
                                {
                                    args[i] = apiMethod.Parameters[i].DefaultValue;
                                }
                                else
                                {
                                    throw new APIException("missing argument: " + apiMethod.Parameters[i].Name);
                                }
                            }

                            result = api.Execute(apiMethod.Name, args);
                        }
                        catch (APIException e)
                        {
                            result = new ErrorResult()
                            {
                                error = e.Message
                            };
                        }

                        if (result is ErrorResult)
                        {
                            var temp  = (ErrorResult)result;
                            var error = DataNode.CreateObject();
                            error.AddField("error", temp.error);
                            return(error);
                        }

                        return(APIUtils.FromAPIResult(result));
                    });
                }

                _server.Get(endPoint, (request) =>
                {
                    return(apiMap);
                });
            }
        }