Example #1
0
        public RequestBindingContext(RequestMetadata metadata, IBindingData bindingSource)
        {
            metadata.AssertNotNull(nameof(metadata));
            bindingSource.AssertNotNull(nameof(bindingSource));

            Metadata      = metadata;
            BindingSource = bindingSource;

            Timeout = metadata.Timeout;
            Route   = metadata.Route;
        }
        public IRestRequest MapRequest(RequestMetadata metadata, IBindingData bindingSource)
        {
            metadata.AssertNotNull(nameof(metadata));
            bindingSource.AssertNotNull(nameof(bindingSource));

            var bindingContext = new RequestBindingContext(metadata, bindingSource);

            foreach (var binder in RequestBinders)
            {
                binder.Bind(bindingContext);
            }

            //TODO validate
            //bindingContext.Validate();

            if (bindingContext.FormFiles.Any())
            {
                throw new NotImplementedException();//TODO !
            }

            var request = new RestRequest()
            {
                Method  = bindingContext.Metadata.Method,
                Route   = bindingContext.Route,
                Timeout = bindingContext.Timeout
            };

            //TODO !!!
            if (bindingContext.FormFields.Any())
            {
                var content = new RequestUrlEncodedContent("utf-8"); //TODO

                foreach (var field in bindingContext.FormFields)
                {
                    content.AddField(field);
                }

                request.Content = content;
            }
            else if (bindingContext.Body != null)
            {
                request.Content = bindingContext.Body;
            }

            foreach (var header in bindingContext.Headers)
            {
                request.AddHeader(header);
            }

            foreach (var segment in bindingContext.Path)
            {
                request.AddRouteSegment(segment);
            }

            foreach (var query in bindingContext.Queries)
            {
                request.AddQuery(query);
            }

            return(request);
        }