Exemplo n.º 1
0
        /// <summary>
        /// Call json-rpc method
        /// </summary>
        /// <param name="returnType"></param>
        /// <param name="request"></param>
        /// <returns>null or object of requested type</returns>
        public object Invoke(Type returnType, IJsonRequest request)
        {
            if (!IsActive)
            {
                return(null);
            }

            if (request == null)
            {
                return(null);
            }

            lock (mSyncInvoke)
            {
                if (mRemoteMethods.Contains(request.Method))
                {
                    try
                    {
                        return(JsonRpcCaller.Invoke(mTcpConnection.GetStream(),
                                                    returnType,
                                                    request));
                    }
                    catch (Exception)
                    {
                        Dispose();
                        return(null);
                    }
                }

                return(null);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Call json-rpc method
        /// </summary>
        /// <param name="request"></param>
        /// <returns>raw json response</returns>
        public string JsonInvoke(IJsonRequest request)
        {
            if (!IsActive)
            {
                return("{\"id\":" + "-" + ",\"error\":{\"name\":\"JSONRPCError\",\"message\":\"Connection disactive\",\"errors\":\"\"}}");
            }

            if (request == null)
            {
                return("{\"id\":" + "-" + ",\"error\":{\"name\":\"JSONRPCError\",\"message\":\"JsonRequest cannot be null\",\"errors\":\"\"}}");
            }

            lock (mSyncInvoke)
            {
                if (mRemoteMethods.Contains(request.Method))
                {
                    try
                    {
                        return((string)JsonRpcCaller.Invoke(mTcpConnection.GetStream(),
                                                            typeof(JsonBuffer),
                                                            request));
                    }
                    catch (Exception)
                    {
                        Dispose();
                        return("{\"id\":" + request.Id +
                               ",\"error\":{\"name\":\"JSONRPCError\",\"message\":\"Timeout method call\",\"errors\":\"\"}}");
                    }
                }

                return("{\"id\":" + request.Id +
                       ",\"error\":{\"name\":\"JSONRPCError\",\"message\":\"Method unavailable\",\"errors\":\"\"}}");
            }
        }
Exemplo n.º 3
0
        public ConstantsResponse Constants()
        {
            Console.WriteLine("Getting constants...");
            ConstantsResponse response = JsonRpcCaller.Call <ConstantsResponse>("idoit.constants", new { apikey = Config.GetInstance().ApiKey });

            return(response);
        }
Exemplo n.º 4
0
        public ObjectTypesResponse ObjectTypes()
        {
            Console.WriteLine("Getting object types...");
            ObjectTypesResponse result = JsonRpcCaller.Call <ObjectTypesResponse>("cmdb.object_types", new { apikey = Config.GetInstance().ApiKey });

            return(result);
        }
Exemplo n.º 5
0
        public CmdbObject GetCmdbObject(int cmdb_id)
        {
            Console.WriteLine("Fetching CMDB Object {0}...", cmdb_id);
            CmdbObject response = JsonRpcCaller.Call <CmdbObject>("cmdb.object.read", new
            {
                apikey = Config.GetInstance().ApiKey,
                id     = cmdb_id
            });

            return(response);
        }
Exemplo n.º 6
0
        public Dictionary <string, DialogFieldInfo> CategoryInfo(CategoryInfo ci)
        {
            Console.WriteLine("Getting category info {0}...", ci.constant);
            Dictionary <string, DialogFieldInfo> result = JsonRpcCaller.Call <Dictionary <string, DialogFieldInfo> >("cmdb.category_info.read", new
            {
                apikey   = Config.GetInstance().ApiKey,
                category = ci.constant
            });

            return(result);
        }
Exemplo n.º 7
0
        public ObjectTypeCategoriesResponse ObjectTypeCategories(ObjectType ot)
        {
            Console.WriteLine("Getting object type categories...");
            ObjectTypeCategoriesResponse result = JsonRpcCaller.Call <ObjectTypeCategoriesResponse>("cmdb.object_type_categories", new
            {
                apikey = Config.GetInstance().ApiKey,
                type   = ot.constant
            });

            return(result);
        }
Exemplo n.º 8
0
        public LogoutResponse Logout()
        {
            Console.WriteLine("Logging out...");
            LogoutResponse result = JsonRpcCaller.Call <LogoutResponse>("idoit.logout", new { apikey = Config.GetInstance().ApiKey });

            if (result.result == 1)
            {
                JsonRpcCaller.XRpcAuthSession = null;
            }
            return(result);
        }
Exemplo n.º 9
0
        public List <T> CategoryRead <T>(long id, int catg)
        {
            List <T> temp = JsonRpcCaller.Call <List <T> >("cmdb.category.read", new
            {
                apikey = Config.GetInstance().ApiKey,
                objID  = id,
                catgID = 43
            });

            return(temp);
        }
Exemplo n.º 10
0
        string RequestId()
        {
            if (!IsActive)
            {
                return("");
            }


            return((string)JsonRpcCaller.Invoke(mTcpConnection.GetStream(),
                                                typeof(string),
                                                new JsonRequest(0, "smart.id", null)));
        }
Exemplo n.º 11
0
        IEnumerable <string> RequestMethodsList()
        {
            if (!IsActive)
            {
                return(null);
            }


            return((string[])JsonRpcCaller.Invoke(mTcpConnection.GetStream(),
                                                  typeof(string[]),
                                                  new JsonRequest(0, "proxy.methods", null)));
        }
Exemplo n.º 12
0
        public bool DialogEditSpecific(Category c, string propertyName, string valueToAdd)
        {
            Console.WriteLine("Adding {0} to {1} {2}s...", valueToAdd, c.DisplayName, propertyName);

            DialogEditResponse response = JsonRpcCaller.Call <DialogEditResponse>("cmdb.dialog.create", new
            {
                apikey   = Config.GetInstance().ApiKey,
                catsID   = c.Constant,
                property = propertyName,
                value    = valueToAdd
            });

            return(response.success);
        }
Exemplo n.º 13
0
        public bool DialogEdit(Category c, string propertyName, string valueToAdd)
        {
            Console.WriteLine("Adding {0} to {1} {2}s...", valueToAdd, c.DisplayName, propertyName);

            DialogEditResponse response = JsonRpcCaller.Call <DialogEditResponse>("cmdb.dialog.create", new
            {
                apikey   = Config.GetInstance().ApiKey,
                catgID   = c.Constant,      //nicht dokumentierter Aufruf... Quelle: http://forum.i-doit.org/index.php/topic,4570.msg13880.html#msg13880
                property = propertyName,
                value    = valueToAdd
            });

            return(response.success);
        }
Exemplo n.º 14
0
        public CreateObjectResponse CreateCmdbObject(ObjectType objectType, string name)
        {
            Console.WriteLine("Creating a {0} named {1}...", objectType.title, name);
            CreateObjectResponse response = JsonRpcCaller.Call <CreateObjectResponse>("cmdb.object.create", new
            {
                apikey = Config.GetInstance().ApiKey,
                type   = objectType.constant,
                title  = name
            });

            Console.WriteLine("-> {0}", response.message);

            return(response);
        }
Exemplo n.º 15
0
        public List <CmdbObject> FindObjects(ObjectType objectType)
        {
            Console.WriteLine("Looking up {1}s...", null, objectType.title);
            List <CmdbObject> o = JsonRpcCaller.Call <List <CmdbObject> >("cmdb.objects", new
            {
                apikey = Config.GetInstance().ApiKey,
                filter = new
                {
                    type = objectType.id
                }
            });

            return(o);
        }
Exemplo n.º 16
0
        public IdoitEnumerator Dialog(string categoryConstant, string property)
        {
            Console.WriteLine("Getting Dialog {0} of {1}...", property, categoryConstant);
            IdoitEnumerator result = JsonRpcCaller.Call <IdoitEnumerator>("cmdb.dialog.read", new
            {
                apikey   = Config.GetInstance().ApiKey,
                category = categoryConstant,
                property = property
            });

            result.categoryConstant = categoryConstant;
            result.property         = property;

            return(result);
        }
Exemplo n.º 17
0
        public LoginResponse Login(string username, string password)
        {
            Console.WriteLine("Logging in...");
            JsonRpcCaller.XRpcAuthUsername = username;
            JsonRpcCaller.XRpcAuthPassword = password;

            LoginResponse result = JsonRpcCaller.Call <LoginResponse>("idoit.login",
                                                                      new { apikey = Config.GetInstance().ApiKey });

            //Daten wieder vergessen, nachdem das einloggen erfolgreich war!
            JsonRpcCaller.XRpcAuthUsername = null;
            JsonRpcCaller.XRpcAuthPassword = null;

            JsonRpcCaller.XRpcAuthSession = result.session_id;
            return(result);
        }
Exemplo n.º 18
0
        public List <T> CategoryRead <T>(long id, Category c)
        {
            if (c.Constant.Equals("C__CMDB__SUBCAT__STORAGE__DEVICE"))
            {
                return(CategoryRead <T>(id, 43)); //Workaround für einen Bug in der i-doit API.
            }

            List <T> temp = JsonRpcCaller.Call <List <T> >("cmdb.category.read", new
            {
                apikey   = Config.GetInstance().ApiKey,
                objID    = id,
                category = c.Constant
            });

            return(temp);
        }
Exemplo n.º 19
0
        public void CategoryUpdate <T>(CmdbObject obj, T theObject)
            where T : Category
        {
            if (theObject.Constant.Equals("C__CMDB__SUBCAT__STORAGE__DEVICE"))
            {
                CategoryUpdate(obj, theObject, 43);     //Workaround für einen Bug in der i-doit API
                return;
            }
            Dictionary <string, object> updateData = new Dictionary <string, object>();

            updateData.Add("category_id", theObject.id);
            foreach (var fi in theObject.GetType().GetFields())
            {
                //Falls ein Feld mit JsonIgnore gekennzeichnet ist, es ausschließen.
                object[] attribs  = fi.GetCustomAttributes(true);
                bool     ignoreMe = false;
                foreach (object o in attribs)
                {
                    if (o is JsonIgnoreAttribute)
                    {
                        ignoreMe = true;
                        break;
                    }
                }
                if (ignoreMe)
                {
                    continue;
                }

                //Feld in die Update Warteschlage einreihen:
                updateData.Add(fi.Name, fi.GetValue(theObject));
            }

            Console.WriteLine("Updating {0} #{1} in {2}", theObject.DisplayName, theObject.id, obj.title);
            CategoryDeleteResponse r = JsonRpcCaller.Call <CategoryDeleteResponse>("cmdb.category.update", new
            {
                apikey   = Config.GetInstance().ApiKey,
                category = theObject.Constant,
                objID    = obj.id,
                data     = updateData
            });

            if (r.success != 1)
            {
                throw new Exception(r.message);
            }
        }
Exemplo n.º 20
0
        public void CategoryDelete <T>(CmdbObject obj, string catConstant, int categoryNumber)
            where T : Category
        {
            Console.WriteLine("Deleting {1} #{2} from {0}", obj.title, catConstant, categoryNumber);
            CategoryDeleteResponse cdr = JsonRpcCaller.Call <CategoryDeleteResponse>("cmdb.category.delete", new
            {
                catgID = catConstant,
                cateID = categoryNumber,
                objID  = obj.id,
                apikey = Config.GetInstance().ApiKey
            });

            if (cdr.success != 1)
            {
                throw new Exception(cdr.message);
            }
        }
Exemplo n.º 21
0
        public void CategoryCreate <T>(CmdbObject obj, T theObject)
            where T : Category
        {
            Console.WriteLine("Adding a {0} to {1}", theObject.DisplayName, obj.title);

            CategoryCreateResponse response;

            if (theObject.Constant.Equals("C__CATG__GLOBAL"))
            {
                response = JsonRpcCaller.Call <CategoryCreateResponse>("cmdb.category.create", new
                {
                    apikey = Config.GetInstance().ApiKey,
                    catgID = 1,
                    objID  = obj.id,
                    data   = theObject,
                });
            }

            if (theObject.Constant.Contains("CATS__"))
            {
                response = JsonRpcCaller.Call <CategoryCreateResponse>("cmdb.category.create", new
                {
                    apikey = Config.GetInstance().ApiKey,
                    catsID = theObject.Constant,
                    objID  = obj.id,
                    data   = theObject,
                });
            }
            else
            {
                response = JsonRpcCaller.Call <CategoryCreateResponse>("cmdb.category.create", new
                {
                    apikey = Config.GetInstance().ApiKey,
                    catgID = theObject.Constant,
                    objID  = obj.id,
                    data   = theObject,
                });
            }

            if (response.success != 1)
            {
                throw new Exception(response.message);
            }
        }
Exemplo n.º 22
0
        public object Invoke(Type returnType, string method, IDictionary args)
        {
            try
            {
                JsonRpcDispatcher internalDispatcher;
                if (mRpcMethods.TryGetValue(method, out internalDispatcher))
                {
                    // internal method
                    return((new ResponseParser(internalDispatcher.Process((new JsonRequest(0, method, args)).ToString()), returnType)).Result());
                }

                // Heardbeat test
                // try to get updated state of connection. It's hack
                try
                {
                    var timeout = GetJsonRpc().WriteTimeout;
                    GetJsonRpc().WriteTimeout = 2;

                    GetJsonRpc().WriteByte(0);
                    GetJsonRpc().WriteByte(0);
                    GetJsonRpc().WriteByte(0);
                    GetJsonRpc().WriteByte(0);
                    GetJsonRpc().WriteByte(0);

                    Thread.Sleep(5);
                    GetJsonRpc().WriteTimeout = timeout;
                }
                catch (IOException)
                {
                    //Debug.WriteLine("remote server connection test");
                }


                // external method
                return(JsonRpcCaller.Invoke(GetJsonRpc(),
                                            returnType,
                                            (++mIdRequest), method, args));
            }
            catch (Exception)
            {
                return(null);
            }
        }
Exemplo n.º 23
0
        /// <summary>
        /// Call json-rpc method
        /// </summary>
        /// <param name="returnType"></param>
        /// <param name="request"></param>
        /// <returns>null or object of requested type</returns>
        public object Invoke(Type returnType, IJsonRequest request)
        {
            if (!IsActive)
            {
                return(null);
            }

            try
            {
                return(JsonRpcCaller.Invoke(mTcpConnection.GetStream(),
                                            returnType,
                                            request));
            }
            catch (Exception)
            {
                Dispose();
                return(null);
            }
        }
Exemplo n.º 24
0
        /// <summary>
        /// Call json-rpc method
        /// </summary>
        /// <param name="request"></param>
        /// <returns>raw json response</returns>
        public string JsonInvoke(IJsonRequest request)
        {
            if (!IsActive)
            {
                return(null);
            }

            try
            {
                return((string)JsonRpcCaller.Invoke(mTcpConnection.GetStream(),
                                                    typeof(JsonBuffer),
                                                    request));
            }
            catch (Exception)
            {
                Dispose();
                return("{\"id\":" + request.Id + ",\"error\":{\"name\":\"JSONRPCError\",\"message\":\"Timeout method call\",\"errors\":\"\"}}");
            }
        }
Exemplo n.º 25
0
        public Dictionary <string, DialogFieldInfo> CategoryInfo(string ci)
        {
            switch (ci) //Workaround für einen Bug in der i-doit API.
            {
            case "C__CMDB__SUBCAT__STORAGE__DEVICE":
                return(CategoryInfo(43));

            default:
                break;
            }
            Console.WriteLine("Getting category info {0}...", ci);
            Dictionary <string, DialogFieldInfo> result = JsonRpcCaller.Call <Dictionary <string, DialogFieldInfo> >("cmdb.category_info.read", new
            {
                apikey   = Config.GetInstance().ApiKey,
                category = ci
            });

            return(result);
        }
Exemplo n.º 26
0
 public VersionResponse Version()
 {
     Console.WriteLine("Checking i-doit Version...");
     return(JsonRpcCaller.Call <VersionResponse>("idoit.version", new { apikey = Config.GetInstance().ApiKey }));
 }