Ejemplo n.º 1
0
        public static object flattenDataDict(String dict)
        {
            object json = DataDict.get(dict);

            if (json == null)
            {
                return("Error: cannot locate data dict object " + dict);
            }
            return(Utils.JsonToExcelObjWithoutTypeInfo(json));
        }
Ejemplo n.º 2
0
        public static object getFromDataDict(string dict, string path)
        {
            object json = DataDict.get(dict);

            if (json == null)
            {
                return("Error: cannot locate data dict object " + dict);
            }
            object o = getFromDataDictByPath(json, path);

            if (o is object[])
            {
                object[,] ret = new object[((object[])o).Length, 1];
                for (int i = 0; i < ((object[])o).Length; ++i)
                {
                    ret[i, 0] = ((object[])o)[i];
                }
                return(ret);
            }
            else
            {
                return(o);
            }
        }
Ejemplo n.º 3
0
        public static object ExecParallel(object[,] tasks)
        {
            List <string> entries = new List <string>();
            int           rows = tasks.GetLength(0), cols = tasks.GetLength(1);

            List <string> retTypes = new List <string>();

            for (int i = 0; i < rows; ++i)
            {
                for (int j = 0; j < cols; ++j)
                {
                    object task = tasks[i, j];
                    if (task is ExcelError || task is ExcelMissing || task is ExcelEmpty)
                    {
                        continue;
                    }
                    String id = (String)(tasks[i, j]);
                    Utils.QLDelayedCall delayed = (Utils.QLDelayedCall)DataDict.get(id);
                    entries.Add(delayed.body);
                    retTypes.Add(delayed.retType);
                }
            }
            String json    = "[" + String.Join(",", entries) + "]";
            var    request = new RestRequest("excel-service/parallel", Method.POST);

            request.RequestFormat = DataFormat.Json;
            request.AddParameter("Application/Json", json, ParameterType.RequestBody);
            request.AddHeader("Authorization", "Bearer " + settings.token);
            var response = settings.client.Execute <Utils.QLResult>(request);

            if (response.ErrorException != null)
            {
                return("Exception while executing the API: " + response.ErrorException.Message);
            }
            List <object> results = (List <object>)response.Data.result;

            if ((int)response.StatusCode != 200 || results.Count < 1)
            {
                return("Error: Parallel jobs failed.");
            }

            object[,] ret = new object[rows, cols];
            int count = 0;

            for (int i = 0; i < rows; ++i)
            {
                for (int j = 0; j < cols; ++j)
                {
                    object task = tasks[i, j];
                    if (task is ExcelError || task is ExcelMissing || task is ExcelEmpty)
                    {
                        continue;
                    }
                    Dictionary <string, object> r = (Dictionary <string, object>)results[count];
                    if (r.ContainsKey("error"))
                    {
                        ret[i, 0] = ((Dictionary <string, object>)r["error"])["message"];
                    }
                    else
                    {
                        object o = r["result"];
                        if (o is Dictionary <string, object> || o is List <object> )
                        {
                            ret[i, j] = DataDict.put(o, "DelayedJobResult");
                        }
                        else
                        {
                            ret[i, j] = Utils.JsonToExcelObj(retTypes[i], o);
                        }
                    }
                    count++;
                }
            }
            return(ret);
        }
Ejemplo n.º 4
0
        private static object CallAPI(Utils.QLAPIInfo info, List <object> inputs)
        {
            if (ExcelDnaUtil.IsInFunctionWizard())
            {
                if (settings.display.Equals("chinese"))
                {
                    return("等待输入...");
                }
                else
                {
                    return("Waiting for inputs ...");
                }
            }

            List <Utils.QLAPIParam> argInfo = info.args;
            string api = info.method;

            bool          delayed = false;
            List <string> entries = new List <string>();

            for (int i = 0; i < argInfo.Count; ++i)
            {
                if (inputs[i] is ExcelMissing)
                {
                    continue;
                }
                if (argInfo[i].name == "delayed")
                {
                    delayed = (Boolean)inputs[i];
                    continue;
                }
                try
                {
                    entries.Add("\"" + argInfo[i].name + "\":" + Utils.ExcelObjToString(argInfo[i].type, inputs[i]));
                }
                catch (Exception e)
                {
                    return("Error while converting " + (i + 1).ToString() + "th input to Excel object with message: " + e.Message);
                }
            }
            string args = "{" + String.Join(",", entries) + "}";
            string body = "{\"jsonrpc\":\"2.0\",\"id\":\"100\",\"method\":\"" + api + "\",\"params\":" + args + "}";

            var request = new RestRequest(info.provider + "/rpc", Method.POST);

            request.RequestFormat = DataFormat.Json;
            request.AddParameter("Application/Json", body, ParameterType.RequestBody);
            //add header(for trade-service)
            request.AddHeader("Authorization", "Bearer " + settings.token);
            if (delayed)
            {
                return(DataDict.put(new Utils.QLDelayedCall {
                    body = body, retType = info.retType
                }, "DelayedJob"));
            }
            var response = settings.client.Execute <Utils.QLResult>(request);

            if (response.ErrorException != null)
            {
                return("Exception while executing the API: " + response.ErrorException.Message);
            }
            if ((int)response.StatusCode != 200)
            {
                return("Error: Server returned error: " + response.Data.error.message);
            }
            else
            {
                if (response.Data.result == null)
                {
                    if (response.Data.error != null)
                    {
                        return("Error: Server returned error: " + response.Data.error.message);
                    }
                    else
                    {
                        return(ExcelError.ExcelErrorNull);
                    }
                }
                // special data type: a DataDict is just a json object
                // we put it into a cache and then the user can query it for values by providing keys or maybe query criterion
                if (info.retType.Equals("DataDict"))
                {
                    return(DataDict.put(response.Data.result, api));
                }
                if (info.retType.Equals("Table")) // handling of table is different from all other types
                {
                    return(Utils.JsonToExcelObj(info.retType, response.Data.result));
                }
                if (info.retType.Equals("Json") || response.Data.result is Dictionary <string, object> )
                {
                    if (response.Data.result is List <object> )
                    {
                        List <object> arr      = new List <object>();
                        bool          isNested = false;
                        foreach (object o in (List <object>)response.Data.result)
                        {
                            if (!(o is List <object> || o is Dictionary <string, object>))
                            {
                                arr.Add(Utils.ParseJsonScalar(o));
                            }
                            else
                            {
                                isNested = true;
                                break;
                            }
                        }
                        if (isNested)
                        {
                            return(Utils.JsonToExcelObjWithoutTypeInfo(response.Data.result));
                        }
                        else
                        {
                            if (arr.Count == 1)
                            {
                                return(arr[0]);
                            }
                            object[,] ret = new object[arr.Count, 1];
                            for (int i = 0; i < arr.Count; ++i)
                            {
                                ret[i, 0] = arr[i];
                            }
                            return(ret);
                        }
                    }
                    else if (response.Data.result is Dictionary <string, object> )
                    {
                        return(Utils.JsonToExcelObjWithoutTypeInfo(response.Data.result));
                    }
                    else
                    {
                        return(Utils.ParseJsonScalar(response.Data.result));
                    }
                }
                else
                {
                    return(Utils.JsonToExcelObj(info.retType, response.Data.result));
                }
            }
        }