Example #1
0
        internal override HttpContent Bindbody(RequestCreContext requestCreContext)
        {
            List <KeyValuePair <String, String> > keyValues = new List <KeyValuePair <String, String> >();

            if (this.parameterWraps.Count == 0)
            {
            }
            this.parameterWraps.ForEach(x =>
            {
                string valueStr;
                if (TypeReflector.IsComplextClass(x.Parameter.ParameterType))
                {
                    List <KeyValuePair <string, object> > valuePairs =
                        DeconstructUtil.Deconstruct(requestCreContext.ParameterValues.Value[x.Parameter.Position]);
                    valuePairs.ForEach(kp =>
                    {
                        valueStr = x.Serial(kp.Value);
                        keyValues.Add(new KeyValuePair <string, string>(kp.Key, valueStr));
                    });
                }
                else
                {
                    valueStr = x.Serial(requestCreContext);
                    valueStr = valueStr ?? "";
                    keyValues.Add(new KeyValuePair <string, string>(x.DataName, valueStr));
                }
            });

            FormUrlEncodedContent formContent = new FormUrlEncodedContent(keyValues);

            return(formContent);
        }
Example #2
0
        private void AddParaValueHeader(RequestCreContext requestCreContext, ParameterWrapContext parameterWrap, object paraValue)
        {
            if (false == Enable)
            {
                return;
            }
            if (paraValue == null)
            {
                base.AddHeader(requestCreContext, this.Name, "");
                return;
            }

            object pValue;
            string pValueStr = Serial(paraValue);

            if (this.Field != null)
            {
                pValue = this.Field.GetValue(paraValue);
                AddHeader(requestCreContext, this.Name, pValueStr);
                return;
            }
            if (this.Property != null)
            {
                pValue = this.Property.GetValue(paraValue);
                AddHeader(requestCreContext, this.Name, pValueStr);
                return;
            }
            AddHeader(requestCreContext, this.Name, pValueStr);
        }
Example #3
0
        internal void FillPath(RequestCreContext requestCreContext, ParameterWrapContext parameterWrapContext)
        {
            HttpContent httpContext = requestCreContext.httpRequestMessage.Content;
            object      value       = requestCreContext.ParameterValues.Value[parameterWrapContext.Parameter.Position];
            string      valueStr    = parameterWrapContext.Serial(value);

            requestCreContext.FillPath(this.Name, valueStr);
        }
Example #4
0
 internal virtual void AddHeader(RequestCreContext requestCreContext)
 {
     if (false == Enable)
     {
         return;
     }
     AddHeader(requestCreContext, this.Name, this.Value);
 }
Example #5
0
        internal override HttpContent Bindbody(RequestCreContext requestCreContext)
        {
            StringBuilder stringBuilder = new StringBuilder();

            Newtonsoft.Json.JsonTextWriter jsonWriter = new JsonTextWriter(new StringWriter(stringBuilder));
            jsonWriter.Formatting = Formatting.Indented;
            if (this.parameterWraps.Count == 1)
            {
                ParameterWrapContext parameterWrapContext = this.parameterWraps[0];
                string valueStr = parameterWrapContext.Serial(requestCreContext);
                if (valueStr == null)
                {
                    throw new LarkException("parameter value can not be null!");
                }
                if (string.IsNullOrEmpty(parameterWrapContext.Name) && TypeReflector.IsComplextClass(parameterWrapContext.Parameter.ParameterType))
                {
                    jsonWriter.WriteRaw(valueStr);
                }
                else
                {
                    jsonWriter.WriteStartObject();
                    jsonWriter.WritePropertyName(parameterWrapContext.DataName);
                    if (valueStr != null)
                    {
                        jsonWriter.WriteRawValue(valueStr);
                    }

                    jsonWriter.WriteEndObject();
                }
            }
            else if (this.parameterWraps.Count > 1)
            {
                jsonWriter.WriteStartObject();
                this.parameterWraps.ForEach(x =>
                {
                    jsonWriter.WritePropertyName(x.DataName);
                    object value    = requestCreContext.ParameterValues.Value[x.Parameter.Position];
                    string valueStr = x.Serial(value);
                    if (valueStr != null)
                    {
                        jsonWriter.WriteRawValue(valueStr);
                    }
                });
                jsonWriter.WriteEndObject();
            }
            else
            {
                // output :{}
                jsonWriter.WriteRaw("{}");
            }



            jsonWriter.Flush();
            StringContent stringContent = new StringContent(stringBuilder.ToString(), Encoding.UTF8, "application/json");

            return(stringContent);
        }
Example #6
0
        internal static LarkResult Create(RequestCreContext requestCreContext)
        {
            WrapBase wrapBase = requestCreContext.WrapInstance;

            MethodWrapContext methodWrap = requestCreContext.MethodWrap;

            if (methodWrap == null)
            {
                throw new ArgumentNullException(nameof(methodWrap));
            }

            InterfaceWrapContext interfaceWrap = requestCreContext.InfaceContext;


            HttpRequestMessage httpRequestMessage = requestCreContext.PreparaRequestMessage();


            //todo is it ok?
            httpRequestMessage.RequestUri = new Uri(requestCreContext.GetRequestUrl());

            System.Net.Http.HttpClient httpClient = new System.Net.Http.HttpClient();

            //TODO  it's need to  deal with the http status code
            Task <HttpResponseMessage> task;

            switch (requestCreContext.HttpMethod.Method)
            {
            case "GET":
            case "POST":
                httpRequestMessage.Method = new HttpMethod(requestCreContext.HttpMethod.Method);
                break;

            default:
                throw new NotSupportedException("Not supported Http Method!");
            }
            if (InternalConfig.SaveRequest)
            {
                wrapBase.MyClient = httpClient;
                wrapBase.MyHttpRequestMessagea = httpRequestMessage;
                wrapBase.MyRequestCreContext   = requestCreContext;
            }
            if (InternalConfig.NotRequest)
            {
                return(LarkResult.GetResult(null, requestCreContext.MethodWrap.ReturnContext));;
            }

            task = httpClient.SendAsync(httpRequestMessage);
            //todo need to try-catch ?
            task.Wait();

            if (InternalConfig.SaveResponse)
            {
                requestCreContext.WrapInstance.OriginalResponseMessage = task.Result;
            }

            return(LarkResult.GetResult(task.Result, requestCreContext.MethodWrap.ReturnContext));
        }
Example #7
0
        internal override void AddHeader(RequestCreContext requestCreContext)
        {
            if (false == Enable)
            {
                return;
            }
            object value = requestCreContext.ParameterValues.Value[this.ParameterWrapContext.Parameter.Position];

            AddParaValueHeader(requestCreContext, this.ParameterWrapContext, value);
        }
Example #8
0
        internal override HttpContent Bindbody(RequestCreContext requestCreContext)
        {
            System.Xml.XmlDocument xmlDocument = new System.Xml.XmlDocument();
            StringBuilder          stringBuild = new StringBuilder();
            XmlWriter xmlWriter = new XmlTextWriter(new StringWriter(stringBuild));

            xmlDocument.WriteContentTo(xmlWriter);
            StringContent stringContent = new StringContent(stringBuild.ToString());

            return(stringContent);
        }
Example #9
0
        internal void AddHeader(RequestCreContext requestCreContext, string name, string value)
        {
            if (false == Enable)
            {
                return;
            }
            HttpContent httpContext = requestCreContext.httpRequestMessage.Content;

            if (this.Unique)
            {
                httpContext.Headers.Remove(this.Name);
                httpContext.Headers.Add(this.Name, value);
            }
            else
            {
                httpContext.Headers.Add(this.Name, value);
            }
        }
Example #10
0
        internal void AddParameterQueryString(RequestCreContext requestCreContext, ParameterWrapContext parameterWrap)
        {
            HttpContent httpContext = requestCreContext.httpRequestMessage.Content;
            object      paraValue   = requestCreContext.ParameterValues.Value[parameterWrap.Parameter.Position];

            if (TypeReflector.IsPrivateValue(parameterWrap.Parameter.ParameterType))
            {
                //todo  valueStr :performance problem ?
                string valueStr = parameterWrap.Serial(paraValue);
                parameterWrap.QueryString = this.Name + "=" + valueStr;
                requestCreContext.QueryString.Add(this.Name, valueStr);
            }
            else
            {
                if (Field == null && Property == null)
                {
                    string valueStr = parameterWrap.Serial(paraValue);
                    parameterWrap.QueryString = this.Name + "=" + valueStr;
                    requestCreContext.QueryString.Add(this.Name, valueStr);
                }
                else
                {
                    object value;
                    string queryName = null;
                    if (Field != null)
                    {
                        queryName = Field.Name;
                        value     = Field.GetValue(paraValue);
                    }
                    else if (Property != null)
                    {
                        queryName = Property.Name;
                        value     = Property.GetValue(paraValue);
                    }
                    else
                    {
                        throw new SystemException(nameof(this.Name));
                    }
                    string valueStr = parameterWrap.Serial(value);
                    parameterWrap.QueryString = queryName + "=" + valueStr;
                    requestCreContext.QueryString.Add(queryName, valueStr);
                }
            }
        }
Example #11
0
        public static object Invoke(Type interfacetype, WrapBase wrapBase, MethodInfo methodInfo, List <Object> args)
        {
            // if (InternalConfig.EmitTestCode)
            // {
            //     Console.WriteLine("InvokeProxy args:");
            //     args.ForEach(x=>{
            //         Console.WriteLine((x??new object()).ToString());
            //     });
            // }

            try
            {
                if (false == Lark.InterfaceWrapCache.ContainsKey(interfacetype))
                {
                    throw new LarkException("RuntimeException:wrapcache is not exists!");
                }
                InterfaceItem interfaceItem = Lark.InterfaceWrapCache[interfacetype];

                if (false == interfaceItem.WrapContext.MethodCache.ContainsKey(methodInfo))
                {
                    throw new LarkException("RuntimeException:MethodCache is not exists!");
                }

                MethodItem methodItem = interfaceItem.WrapContext.MethodCache[methodInfo];

                //todo need a pool of RequestCreContext
                RequestCreContext requestCreContext = RequestCreContext.Create(interfaceItem.WrapContext, methodItem.WrapContext, wrapBase);
                requestCreContext.ParameterValues.Value = args;

                return(HttpCreater.Create(requestCreContext).DealResponse(methodInfo.ReturnType));
            }
            catch (System.Exception ex)
            {
                System.Console.WriteLine(ex.ToString());
                throw ex;
            }
        }
Example #12
0
        internal override HttpContent Bindbody(RequestCreContext requestCreContext)
        {
            StringContent stringContent = new StringContent("");

            throw new System.NotImplementedException();
        }
Example #13
0
 internal abstract HttpContent Bindbody(RequestCreContext requestCreContext);