Exemplo n.º 1
0
        public static async Task <IDictionary <string, object> > RpcAsync(this TMClient @this, string methodName, params object[] args)
        {
            var rpcRequest = new TMJsonRequest();

            var encodedArgs = new List <object>();

            foreach (var a in args)
            {
                var ea = TMEncoding.Instance.Encode(a);
                encodedArgs.Add(ea);
            }

            Dictionary <string, object> body = new Dictionary <string, object>()
            {
                { "args", encodedArgs },
            };

            rpcRequest.Url  = string.Format("/rpc/{0}", methodName);
            rpcRequest.Body = body;
            var json = await @this.SendAsync(rpcRequest);

            var result = json.Results;

            return(result);
        }
Exemplo n.º 2
0
        public bool Match(TMJsonRequest request)
        {
            if (Url.StartsWith("/rpc", StringComparison.Ordinal) && request.Url.StartsWith("/rpc", StringComparison.Ordinal))
            {
                return(true);
            }

            if (string.IsNullOrEmpty(Url))
            {
                return(false);
            }

            var result = Url.Equals(request.Url);

            if (!string.IsNullOrEmpty(request.Method))
            {
                result &= Method.ToLower().Equals(request.Method.ToLower());
            }
            return(result);
        }
Exemplo n.º 3
0
        public static async Task <T> RpcWithChoiceAsync <T>(this TMClient @this, string methodName, IEnumerable <T> choices, bool canBeSkip = false, int timeLimitForDefaultResult = 15000)
        {
            var rpcRequest = new TMJsonRequest();

            var encodedArgs = new List <object>();

            object[] args = new object[] { choices, canBeSkip, timeLimitForDefaultResult };
            foreach (var a in args)
            {
                var ea = TMEncoding.Instance.Encode(a);
                encodedArgs.Add(ea);
            }

            Dictionary <string, object> body = new Dictionary <string, object>()
            {
                { "args", encodedArgs },
            };

            rpcRequest.Url  = string.Format("/rpc/{0}", methodName);
            rpcRequest.Body = body;
            if (timeLimitForDefaultResult == 0)
            {
                timeLimitForDefaultResult = 15000;
            }
            var json = await @this.SendAsync(rpcRequest, true, timeLimitForDefaultResult);

            var result = json.Results;

            if (result == null)
            {
                if (!canBeSkip)
                {
                    return(choices.PickRandom());
                }
            }
            var deocdedObj = AVDecoder.Instance.Decode(result);
            T   rtn        = Conversion.To <T>(deocdedObj);

            return(rtn);
        }