示例#1
0
        private static void initializeParams()
        {
            string  swag  = Proxy.swagger;
            JObject json  = JObject.Parse(swag);
            JObject json1 = JObject.Parse(json["paths"].ToString());


            foreach (var x in json1.Children())
            {
                string Base = "/";
                if (json["basePath"] != null)
                {
                    Base = json["basePath"].ToString();
                }
                json = JObject.Parse(json1[x.Path.ToString()].ToString());
                List <SwaggerOperation> ops = new List <SwaggerOperation>();
                operations.Add(x.Path.ToString(), ops);
                foreach (var y in json.Children())
                {
                    JObject t = JObject.Parse(json[y.Path.ToString()].ToString());
                    string  p = "";
                    if (!Base.Equals("/"))
                    {
                        p += Base;
                    }
                    p += x.Path.ToString();
                    SwaggerOperation temp = new SwaggerOperation(p, y.Path.ToUpper(), t["operationId"].ToString());
                    if (t["responses"] != null)
                    {
                        JObject res = JObject.Parse(t["responses"].ToString());
                        res = JObject.Parse(res[res.First.Path].ToString());
                        if (res["schema"] != null)
                        {
                            if (res["schema"]["type"] != null)
                            {
                                temp.resDataType = res["schema"]["type"].ToString();
                                if (temp.resDataType.Equals("array"))
                                {
                                    temp.resArrayType = res["schema"]["items"].First.ToString();
                                }
                            }
                            else
                            {
                                temp.resDataType = res["schema"]["$ref"].ToString();
                            }
                        }
                    }
                    JArray a;
                    if (t["parameters"] != null)
                    {
                        a = JArray.Parse(t["parameters"].ToString());
                        foreach (var param in a.Children())
                        {
                            //Parameters(string name,string In,string type,string itype,string cf,bool required)
                            string name = param["name"].ToString();
                            string In   = param["in"].ToString();
                            string type;
                            if (!In.Equals("body"))
                            {
                                type = param["type"].ToString();
                            }
                            else
                            {
                                type = param["schema"].First.ToString();
                            }
                            string item = null;
                            if (param["items"] != null)
                            {
                                item = param["items"]["type"].ToString();
                            }
                            string collectionFormat = "csv";
                            if (param["collectionFormat"] != null)
                            {
                                collectionFormat = param["collectionFormat"].ToString();
                            }
                            bool req = true;
                            if (param["required"] != null)
                            {
                                req = (bool)param["required"];
                            }
                            temp.addParam(new Parameters(name, In, type, item, collectionFormat, req));
                        }
                    }
                    //add operation to the dictionary
                    ops.Add(temp);
                }
            }
        }
示例#2
0
        private static string getCode(SwaggerOperation op, Record r)
        {
            string        code = "";
            List <string> arrays = new List <string>();
            int           count = 0, k = op.parameters.Count;

            //check if parameters is empty, if yes  return only the method name
            if (op.parameters.Count == 0)
            {
                if (op.method.Equals("GET"))
                {
                    string func = getFunc(op, r, arrays, code);
                    return(func);
                }
                return("\\api." + op.operationId + "();");
            }

            foreach (Parameters p in op.parameters)
            {
                string   value;
                string[] values;
                if (!p.type.Equals("array"))
                {
                    value = getVal(p, r, op);
                    if (p.type.Equals("integer"))
                    {
                        int  n;
                        bool isNumeric = int.TryParse(value, out n);
                        if (!isNumeric)
                        {
                            value = "0";
                        }
                    }
                    if (p.type.Equals("string"))
                    {
                        value = "\"" + value + "\"";
                    }
                    if (count != k - 1)
                    {
                        code += value + ",";
                    }
                    else
                    {
                        code += value;
                    }
                }
                else
                {
                    values = getVal(p, r, op).Split(',');
                    string temp = getStringArr(values, arrays.Count, p.itype);
                    arrays.Add(temp);
                    if (count != k - 1)
                    {
                        code += "arr" + (arrays.Count - 1) + ",";
                    }
                    else
                    {
                        code += "arr" + (arrays.Count - 1);
                    }
                }
                count++;
            }

            if (r.req.method.Equals("GET"))
            {
                return(getFunc(op, r, arrays, code));
            }

            return("//api." + op.operationId + "(" + code + ");");
        }