Ejemplo n.º 1
0
 private bool TryGetSpec(MethodInfo methodInfo, out HttpMethodSpec httpMethodSpec)
 {
     if (httpSpecFactory == null)
     {
         RegisterSwaggerDoc(swaggerDocEndpoint, false);
     }
     return(httpSpecFactory.TryGetValue(methodInfo, out httpMethodSpec));
 }
Ejemplo n.º 2
0
        public void RegisterSwaggerDoc(Uri endpoint)
        {
            swaggerSpecDic ??= new Dictionary <string, List <HttpMethodSpec> >();
            swaggerSpecDic.Clear();
            RestClient client  = new RestClient($"{endpoint.Scheme}://{endpoint.IdnHost}:{endpoint.Port}");
            var        req     = client.TakeRequest <JObject>(endpoint.LocalPath);
            var        content = client.Execute(req);
            var        paths   = content["paths"];

            foreach (JProperty prop in paths)
            {
                var methodSpec = new HttpMethodSpec()
                {
                    Path = prop.Name
                };
                var methodProp = prop.Value.First as JProperty;
                methodSpec.AssignHttpMethod(methodProp.Name);
                var tags = methodProp.Value["tags"] as JArray;
                if (tags != null && tags.Count > 0)
                {
                    methodSpec.Tag = tags.First.Value <string>();
                }
                var parameters         = methodProp.Value["parameters"];
                var parameterSpecArray = (parameters != null) ? parameters.Value <JArray>() : null;
                if (parameterSpecArray != null)
                {
                    methodSpec.ParameterSpecs.AddRange(parameterSpecArray.ToObject <List <HttpMethodParameterSpec> >());
                }
                var requestBody = methodProp.Value["requestBody"];
                if (requestBody != null)
                {
                    if (requestBody["content"]["application/json"] != null)
                    {
                        methodSpec.ParameterSpecs.Add(new HttpMethodParameterSpec()
                        {
                            _in = "body"
                        });
                    }
                    else
                    {
                        var formData = requestBody["content"]["multipart/form-data"];
                        if (formData != null)
                        {
                            var formParameters = formData["schema"]["properties"];
                            foreach (JProperty formParameter in formParameters)
                            {
                                methodSpec.ParameterSpecs.Add(new HttpMethodParameterSpec()
                                {
                                    name = formParameter.Name, _in = "form"
                                });
                            }
                        }
                    }
                }

                var key = GenKey(endpoint, methodSpec.Tag);
                lock (this)
                {
                    List <HttpMethodSpec> httpMethodSpecList;
                    if (!swaggerSpecDic.TryGetValue(key, out httpMethodSpecList))
                    {
                        httpMethodSpecList  = new List <HttpMethodSpec>();
                        swaggerSpecDic[key] = httpMethodSpecList;
                    }
                    httpMethodSpecList.Add(methodSpec);
                }
            }
        }
Ejemplo n.º 3
0
 public bool TryGetValue(MethodInfo methodInfo, out HttpMethodSpec sepc)
 {
     return(serviceInterfaceMethodSpecDic.TryGetValue(methodInfo.Name, out sepc));
 }