Ejemplo n.º 1
0
 public static void LogError(this ILogger self, EventId eventId, string message, IToDictionary args) => self.LogError(eventId, message, args.ToDictionary());
Ejemplo n.º 2
0
 public static IDisposable BeginStructuredScope <T>(this ILogger self, IToDictionary <T> state) => self.BeginScope(state.ToDictionary());
Ejemplo n.º 3
0
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="TResult"></typeparam>
        /// <param name="createErrorResult"></param>
        /// <param name="requestUri"></param>
        /// <param name="args0"></param>
        /// <param name="args1"></param>
        /// <returns></returns>
        protected async Task <TResult> SendAsync <TResult>(
            Func <string, TResult> createErrorResult,
            string requestUri,
            IToDictionary args0 = null,
            IEnumerable <KeyValuePair <string, object> > args1 = null) where TResult : new()
        {
            var is_iOS_msg = false;
            IDictionary <string, object> args = null;

            #region args0

            var args0_ = args0.ToDictionary();
            if (args0_ != null)
            {
                args = args0_;
            }
            else
            {
                if (args0 is MessageBase.IPlatform message)
                {
                    if (!IsValidMessageType(message))
                    {
                        return(createErrorResult(Constants.ErrorMessageType));
                    }
                    is_iOS_msg = message.Platform == Platform.iOS;
                }

                if (args0 is IsValid @is && [email protected]())
                {
                    return(createErrorResult(Constants.ErrorMessageInvalid));
                }
            }

            #endregion

            #region args1

            if (args1 != null)
            {
                if (args == null)
                {
                    if (args1 is IDictionary <string, object> args1_)
                    {
                        args = args1_;
                    }
                    else
                    {
                        args = args1.ToDictionary(k => k.Key, v => v.Value);
                    }
                }
                else
                {
                    foreach (var arg in args1)
                    {
                        args.Add(arg.Key, arg.Value);
                    }
                }
            }

            #endregion

            args = TryInitParams(args);

            if (is_iOS_msg && !args.ContainsKey(Constants.environment))
            {
                args.Add(Constants.environment, iOSEnvironment);
            }

            IDictionary <string, string> postArgs = null;
            StringBuilder requestUriStringBuilder = null;
            StringBuilder signStringBuilder;

            if (HttpMethod == HttpMethod.Get)
            {
                requestUriStringBuilder = new StringBuilder();
                signStringBuilder       = new StringBuilder(Constants.GET);
            }
            else if (HttpMethod == HttpMethod.Post)
            {
                postArgs          = new Dictionary <string, string>();
                signStringBuilder = new StringBuilder(Constants.POST);
            }
            else
            {
                throw new ArgumentOutOfRangeException(nameof(HttpMethod));
            }

            if (requestUri.IsHttpUrl())
            {
                var u = new Uri(requestUri);
                requestUriStringBuilder?.Append(requestUri);
                signStringBuilder.Append(u.Host);
                signStringBuilder.Append(u.AbsolutePath);
            }
            else
            {
                var baseAddress = client.BaseAddress;
                if (baseAddress == null)
                {
                    requestUri = Constants.BaseAddress_HTTPS + requestUri;
                    requestUriStringBuilder.Append(Constants.BaseAddress_HTTPS);
                    signStringBuilder.Append(Constants.Host);
                }
                else
                {
                    requestUri = baseAddress.ToString() + requestUri;
                    requestUriStringBuilder.Append(requestUri);
                    signStringBuilder.Append(baseAddress.Host);
                }
                signStringBuilder.Append(requestUri);
            }

            foreach (var arg in args.OrderBy(arg => arg.Key))
            {
                signStringBuilder.Append(arg.Key);
                signStringBuilder.Append(Constants.equal);
                var value = arg.Value == null ? string.Empty :
                            (arg.Value is JArray jArray ?
                             jArray.ToString(Formatting.None) : arg.Value.ToString());
                signStringBuilder.Append(value);

                if (postArgs != null)
                {
                    postArgs.Add(arg.Key, value);
                }

                if (requestUriStringBuilder != null)
                {
                    requestUriStringBuilder.Append(arg.Key);
                    requestUriStringBuilder.Append(Constants.equal);
                    value = TFMs_Compat.UrlEncode(value);
                    requestUriStringBuilder.Append(value);
                }
            }

            signStringBuilder.Append(m_secret_key);

            var signString = signStringBuilder.ToString();
            signString = ComputeMD5(signString);

            if (requestUriStringBuilder != null)
            {
                requestUriStringBuilder.Append(Constants.sign);
                requestUriStringBuilder.Append(Constants.equal);
                requestUriStringBuilder.Append(signString);

                requestUri = requestUriStringBuilder.ToString();
            }

            if (HttpMethod == HttpMethod.Post)
            {
                args.Add(Constants.sign, signString);
            }

            string @string;

            if (HttpMethod == HttpMethod.Get)
            {
                @string = await GetStringAsync(requestUri);
            }
            else if (HttpMethod == HttpMethod.Post)
            {
                @string = await PostStringAsync(requestUri, postArgs);
            }
            else
            {
                throw new ArgumentOutOfRangeException(nameof(HttpMethod));
            }

            return(@string.Deserialize <TResult>());
        }
Ejemplo n.º 4
0
 public static void LogInformation <T>(this ILogger self, string message, IToDictionary <T> args) => self.LogInformation(message, args.ToDictionary());
Ejemplo n.º 5
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="class"></param>
 /// <returns></returns>
 public static Dictionary <string, object> toJson(this IToDictionary @class)
 {
     return(@class.ToDictionary());
 }