/// <summary>
        /// Returns formatted body of the given request
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        public virtual string GetFormattedBody(RemoteRequest req)
        {
            object body = ParamProvider.ExtractBodyParameter(req);

            if (null == body)
            {
                body = ParamProvider.ExtractBodyParameter(this);
            }

            if (null == body)
            {
                return(null);
            }

            string bodyStr = null;

            if (null != Formatter)
            {
                bodyStr = Formatter.Format(body);
            }
            else
            {
                bodyStr = string.Empty;
            }

            return(bodyStr);
        }
        /// <summary>
        /// Returns headers list for the given request
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        public virtual IEnumerable <Parameter> GetHeaders(RemoteRequest req)
        {
            IEnumerable <Parameter> headers =
                ParamProvider.ExtractHeaderParameters(req).Concat(ParamProvider.ExtractHeaderParameters(this));

            return(headers.Distinct(new ParameterNameComparer()));
        }
 /// <summary>
 /// Sends a request asynchronously.
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="req"></param>
 /// <returns></returns>
 public Task <T> SendRequestAsync <T>(RemoteRequest req) where T : RemoteResponse
 {
     return(Task.Run <T>(() =>
     {
         var response = SendRequest <T>(req);
         return response;
     }));
 }
        /// <summary>
        /// Generates URL for the given request
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        public virtual string GenerateRequestUrl(RemoteRequest req)
        {
            string url = UrlHelper.Concat(this.BaseAddress, req.EndpointAddress);

            url += "?";

            var queryParameters =
                ParamProvider.ExtractQueryParameters(req).Concat(ParamProvider.ExtractQueryParameters(this))
                .Distinct(new ParameterNameComparer());

            url += QueryHelper.GenerateQueryString(queryParameters);

            return(url);
        }
        /// <summary>
        /// Sends a request synchronously.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="req"></param>
        /// <returns></returns>
        public T SendRequest <T>(RemoteRequest req) where T : RemoteResponse
        {
            string url = GenerateRequestUrl(req);
            IEnumerable <Parameter> headers = GetHeaders(req);

            string body = GetFormattedBody(req);

            string responseStr = SendRequest(url, req.Method.Method, this.Formatter?.ContentTypeHeader, headers, body);

            if (responseStr.Length > 0)
            {
                object response = Formatter.FormatBack(responseStr, typeof(T));
                return((T)response);
            }
            else
            {
                return(default(T));
            }
        }