Exemplo n.º 1
0
        private bool Query(object[] args, out object result)
        {
            if (GetLastCall() == "Query")
            {
                if (args.Length == 0)
                {
                    throw new ArgumentException("Please provide a query");
                }

                if (args.Length != 1)
                {
                    throw new ArgumentException("Query has one argument");
                }

                if (args[0].GetType().Namespace != null)
                {
                    throw new ArgumentException("Query must be a anonymous type");
                }

                var pairs = args[0].GetType().GetProperties()
                            .Select(x => x.Name + "=" + HttpUtility.UrlEncode(x.GetValue(args[0], null).ToString())).ToArray();
                var queryString = "?" + string.Join("&", pairs);

                result = new MemberAccessWrapper(_httpClientWrapper, _baseUri, _callLog.Replace("/Query", string.Empty) + queryString);

                return(true);
            }

            result = null;
            return(false);
        }
Exemplo n.º 2
0
        private bool Resource(object[] args, out object result)
        {
            if (GetLastCall() == "Resource")
            {
                if (args.Length == 0)
                {
                    throw new ArgumentException("Please provide a resource");
                }

                if (args.Length != 1)
                {
                    throw new ArgumentException("Resource has one argument");
                }

                if (!args[0].GetType().IsPrimitive)
                {
                    throw new ArgumentException("Resource must be a Primitive type");
                }

                result = new MemberAccessWrapper(_httpClientWrapper, _baseUri, _callLog.Replace("/Resource", string.Empty) + "/" + args[0]);
                return(true);
            }

            result = null;
            return(false);
        }
Exemplo n.º 3
0
        public override bool TryInvoke(InvokeBinder binder, object[] args, out object result)
        {
            if (binder == null)
            {
                throw new ArgumentNullException(nameof(binder));
            }

            if (EscapedResource(args, out result))
            {
                return(true);
            }

            if (Query(args, out result))
            {
                return(true);
            }

            if (GetLastCall().IsHttpVerb())
            {
                result = HttpVerb(args);
                return(true);
            }

            if (Resource(args, out result))
            {
                return(true);
            }

            result = new MemberAccessWrapper(_httpClientWrapper, _baseUri, _uri);
            return(true);
        }
        public override bool TryInvoke(InvokeBinder binder, object[] args, out object result)
        {
            if (binder == null) throw new ArgumentNullException("binder");

            if (Resource(args, out result))
                return true;

            if (Query(args, out result))
                return true;

            if (Extensions.IsHttpVerb(GetLastCall()))
            {
                result = HttpVerb(args);
                return true;
            }

            var builder = new StringBuilder(_callLog);
            foreach (var t in args)
            {
                builder.Append("/");
                var s = t as string;
                if (s != null)
                    builder.Append("@\"").Append(s.Replace("\"", "\"\"")).Append("\"");
                else
                    builder.Append(t);
            }

            result = new MemberAccessWrapper(_httpClientWrapper, _baseUri, builder.ToString());

            return true;
        }
Exemplo n.º 5
0
        private bool Query(object[] args, out object result)
        {
            if (GetLastCall() == "Query")
            {
                if (args.Length == 0)
                {
                    throw new ArgumentException("Please provide a query");
                }

                if (args.Length != 1)
                {
                    throw new ArgumentException("Query has one argument");
                }

                if (args[0].GetType().Namespace != null)
                {
                    throw new ArgumentException("Query must be a anonymous type");
                }

                var pairs = args[0].GetType().GetProperties()
                            .Select(x => GetQueryParamValue(x, args[0])).ToArray();
                var queryString = "?" + string.Join("&", pairs);

                result = new MemberAccessWrapper(_httpClientWrapper, _baseUri, GetRelativeUri() + queryString);

                return(true);
            }

            result = null;
            return(false);
        }
Exemplo n.º 6
0
        private bool Resource(object[] args, out object result)
        {
            if (args.Length > 0)
            {
                args.ValidateResourceArgs();

                result = new MemberAccessWrapper(_httpClientWrapper, _baseUri, _uri + "/" + args[0]);
                return(true);
            }

            result = null;
            return(false);
        }
        public override bool TryGetMember(GetMemberBinder binder, out object result)
        {
            if (IsRoot && binder.Name == nameof(HttpClient))
            {
                if (!(HttpClientWrapper is HttpClientWrapper httpClientWrapper))
                {
                    throw new NotSupportedException($"{nameof(HttpClientWrapper)} doesn't support HttpClient");
                }

                result = httpClientWrapper.HttpClient;
                return(true);
            }

            result = new MemberAccessWrapper(HttpClientWrapper, BaseUri, Uri.TrimStart("/".ToCharArray()) + "/" + binder.Name, Headers);
            return(true);
        }
Exemplo n.º 8
0
        private bool EscapedResource(object[] args, out object result)
        {
            if (GetLastCall() == "Resource")
            {
                args.ValidateResourceArgs();
                if (args.Length != 1)
                {
                    throw new ArgumentException("Resource can only have one argument");
                }

                result = new MemberAccessWrapper(_httpClientWrapper, _baseUri, GetRelativeUri() + "/" + args[0]);
                return(true);
            }

            result = null;
            return(false);
        }
        public override bool TryInvoke(InvokeBinder binder, object[] args, out object result)
        {
            if (binder == null)
            {
                throw new ArgumentNullException(nameof(binder));
            }

            var command = CommandFactory.GetCommandFor(GetLastCall(), args);

            if (command != null)
            {
                result = command.Execute(args, this);
                return(true);
            }

            result = new MemberAccessWrapper(HttpClientWrapper, BaseUri, Uri, Headers);
            return(true);
        }
Exemplo n.º 10
0
        public override bool TryInvoke(InvokeBinder binder, object[] args, out object result)
        {
            if (binder == null)
            {
                throw new ArgumentNullException("binder");
            }

            if (Resource(args, out result))
            {
                return(true);
            }

            if (Query(args, out result))
            {
                return(true);
            }

            if (Extensions.IsHttpVerb(GetLastCall()))
            {
                result = HttpVerb(args);
                return(true);
            }

            var builder = new StringBuilder(_callLog);

            foreach (var t in args)
            {
                builder.Append("/");
                var s = t as string;
                if (s != null)
                {
                    builder.Append("@\"").Append(s.Replace("\"", "\"\"")).Append("\"");
                }
                else
                {
                    builder.Append(t);
                }
            }

            result = new MemberAccessWrapper(_httpClientWrapper, _baseUri, builder.ToString());

            return(true);
        }
Exemplo n.º 11
0
 public override bool TryGetMember(GetMemberBinder binder, out object result)
 {
     result = new MemberAccessWrapper(_httpClientWrapper, _baseUri, _callLog + "/" + binder.Name);
     return(true);
 }
 public StronglyTypedMemberAccessWrapper(MemberAccessWrapper memberAccessWrapper)
 {
     MemberAccessWrapper = memberAccessWrapper;
 }
Exemplo n.º 13
0
 public override bool TryGetMember(GetMemberBinder binder, out object result)
 {
     result = new MemberAccessWrapper(HttpClientWrapper, BaseUri, binder.Name, new Dictionary <string, string>());
     return(true);
 }
        private bool Query(object[] args, out object result)
        {
            if (GetLastCall() == "Query")
            {
                if (args.Length == 0)
                    throw new ArgumentException("Please provide a query");

                if (args.Length != 1)
                    throw new ArgumentException("Query has one argument");

                if (args[0].GetType().Namespace != null)
                    throw new ArgumentException("Query must be a anonymous type");

                var pairs = args[0].GetType().GetProperties()
                   .Select(x => x.Name + "=" + HttpUtility.UrlEncode(x.GetValue(args[0], null).ToString())).ToArray();
                var queryString = "?" + string.Join("&", pairs);

                result = new MemberAccessWrapper(_httpClientWrapper, _baseUri, _callLog.Replace("/Query", string.Empty) + queryString);

                return true;
            }

            result = null;
            return false;
        }
        private bool Resource(object[] args, out object result)
        {
            if (GetLastCall() == "Resource")
            {
                if (args.Length == 0)
                    throw new ArgumentException("Please provide a resource");

                if (args.Length != 1)
                    throw new ArgumentException("Resource has one argument");

                if (!args[0].GetType().IsPrimitive)
                    throw new ArgumentException("Resource must be a Primitive type");

                result = new MemberAccessWrapper(_httpClientWrapper, _baseUri, _callLog.Replace("/Resource", string.Empty) + "/" + args[0]);
                return true;
            }

            result = null;
            return false;
        }
 public override bool TryGetMember(GetMemberBinder binder, out object result)
 {
     result = new MemberAccessWrapper(HttpClientWrapper, BaseUri, Uri.TrimStart("/".ToCharArray()) + "/" + binder.Name, Headers);
     return(true);
 }
Exemplo n.º 17
0
 public override bool TryGetMember(GetMemberBinder binder, out object result)
 {
     result = new MemberAccessWrapper(_httpClientWrapper, BaseUri, binder.Name);
     return true;
 }