Пример #1
0
        protected TOutput ParseResponse <TOutput>(TTransportData response, IVkMethod <TOutput> method, ExecuteEnvironment environment)
        {
            var methodVisitor = this.CreateMethodVisitor(response, environment);
            var error         = methodVisitor.GetError();

            if (error != null)
            {
                throw new Exception($"An error with code {error.ErrorCode} was occured during method {method.Name} execution. Error text: {error.ErrorMsg}");
            }

            return(method.Accept(methodVisitor, methodVisitor.Data));
        }
Пример #2
0
        public TOutput Execute <TOutput>(IVkMethod <TOutput> method)
        {
            var env = this.CreateEnvironment();

            TOutput result;

            do
            {
                env.NeedResend = false;

                var parameters = this.PrepareRequestParameters(method, env);
                var request    = this.CreateRequest(parameters, method, env);
                var response   = this.TransmitRequest(request, env);
                result = this.ParseResponse(response, method, env);
            } while (env.NeedResend);

            return(result);
        }
Пример #3
0
        protected override Dictionary <String, String> PrepareRequestParameters(IVkMethod method, ExecuteEnvironment environment)
        {
            var properties = method.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public);
            var parameters = new Dictionary <String, String>();

            foreach (var propertyInfo in properties)
            {
                var attr = propertyInfo.GetCustomAttributes(typeof(VkParamAttribute), true);
                if (attr.Length <= 0)
                {
                    continue;
                }

                var mpa = (VkParamAttribute)attr[0];

                if (propertyInfo.PropertyType == typeof(Boolean?))
                {
                    var boolValue = (bool?)propertyInfo.GetValue(method, null);
                    if (boolValue.HasValue)
                    {
                        parameters.Add(mpa.Name, GetBoolValue(boolValue.Value));
                    }
                }
                if (propertyInfo.PropertyType == typeof(String))
                {
                    var stringValue = (String)propertyInfo.GetValue(method, null);
                    if (stringValue != null)
                    {
                        parameters.Add(mpa.Name, stringValue);
                    }
                }
                if (propertyInfo.PropertyType == typeof(List <String>))
                {
                    var listValue = (List <String>)propertyInfo.GetValue(method, null);
                    if (listValue != null && listValue.Count > 0)
                    {
                        parameters.Add(mpa.Name, GetStringList(listValue));
                    }
                }
                if (propertyInfo.PropertyType == typeof(Int32?))
                {
                    var intValue = (Int32?)propertyInfo.GetValue(method, null);
                    if (intValue.HasValue)
                    {
                        parameters.Add(mpa.Name, GetInt32(intValue.Value, environment.Culture));
                    }
                }
                if (propertyInfo.PropertyType == typeof(UInt32?))
                {
                    var intValue = (UInt32?)propertyInfo.GetValue(method, null);
                    if (intValue.HasValue)
                    {
                        parameters.Add(mpa.Name, GetUInt32(intValue.Value, environment.Culture));
                    }
                }
                if (propertyInfo.PropertyType == typeof(DateTime?))
                {
                    var dateTimeValue = (DateTime?)propertyInfo.GetValue(method, null);
                    if (dateTimeValue.HasValue)
                    {
                        parameters.Add(mpa.Name, GetDateTime(dateTimeValue.Value, environment.Culture));
                    }
                }
                if (propertyInfo.PropertyType == typeof(Guid?))
                {
                    var guidValue = (Guid?)propertyInfo.GetValue(method, null);
                    if (guidValue.HasValue)
                    {
                        parameters.Add(mpa.Name, GetGuid(guidValue.Value, environment.Culture));
                    }
                }
                if (propertyInfo.PropertyType == typeof(Double?))
                {
                    var doubleValue = (Double?)propertyInfo.GetValue(method, null);
                    if (doubleValue.HasValue)
                    {
                        parameters.Add(mpa.Name, GetDouble(doubleValue.Value, environment.Culture));
                    }
                }
            }

            if (environment.PrepareParametersExtension != null)
            {
                environment.PrepareParametersExtension(parameters);
                environment.PrepareParametersExtension = null;
            }

            return(parameters);
        }
Пример #4
0
        protected override String CreateRequest(Dictionary <String, String> requestParameters, IVkMethod method, ExecuteEnvironment ee)
        {
            if (requestParameters.Count == 0)
            {
                throw new Exception($"При формировании запроса для метода {method.Name} не были заданы входные параметры");
            }

            var sb = new StringBuilder();
            int i  = 1;

            foreach (var pair in requestParameters)
            {
                sb.Append(pair.Key).Append("=").Append(pair.Value);

                if (i < requestParameters.Count)
                {
                    sb.Append("&");
                }

                i++;
            }

            var queryString = sb.ToString();

            if (this.AuthenticationProvider.IsAuthenticated)
            {
                if (method.NeedAuthentication)
                {
                    var r = String.Format(WithAccessTokenTemlate, method.Name, queryString, this.AuthenticationProvider.AccessToken);
                    //this.Log.WriteInfo(String.Format("Id: {0}; Запрос: {1}", ee.Id, r));
                    return(r);
                }

                var r2 = String.Format(WithoutAccessTokenTemlate, method.Name, queryString);
                //this.Log.WriteInfo(String.Format("Id: {0}; Запрос: {1}", ee.Id, r2));
                return(r2);
            }

            if (method.NeedAuthentication)
            {
                throw new Exception($"Метод {method.Name} нельзя вызывать без проведенной аутентификации");
            }

            var r3 = String.Format(WithoutAccessTokenTemlate, method.Name, queryString);

            //this.Log.WriteInfo(String.Format("Id: {0}; Запрос: {1}", ee.Id, r3));
            return(r3);
        }
Пример #5
0
 protected abstract TTransportData CreateRequest(Dictionary <String, String> requestParameters, IVkMethod method, ExecuteEnvironment ee);
Пример #6
0
 protected abstract Dictionary <String, String> PrepareRequestParameters(IVkMethod method, ExecuteEnvironment environment);