/// <summary>
        /// Appends the necessary Custom Authentication credentials for making this authorized call
        /// </summary>
        /// <param name="request">The out going request to access the resource</param>        
        internal static void AppendCustomAuthParams(HttpRequest request)
        {
            // TODO: Add your custom authentication here
			// The following properties are available to use
			//     Configuration.ContentType
			//     Configuration.AppId
			//     Configuration.AppKey
			// 
			// ie. Add a header through:
			//     request.header("Key", "Value");
        }
        public HttpResponse ExecuteAsBinary(HttpRequest request)
        {
            //raise the on before request event
            raiseOnBeforeHttpRequestEvent(request);

            UniHttpRequest uniRequest = ConvertRequest(request);
            HttpResponse response = ConvertResponse(uniRequest.asBinary());

            //raise the on after response event
            raiseOnAfterHttpResponseEvent(response);
            return response;
        }
        /// <summary>
        /// Constructor to initialize the context with http request and response information
        /// </summary>
        /// <param name="request">The http request in the current context</param>
        /// <param name="response">The http response in the current context</param>
		public HttpContext(HttpRequest request, HttpResponse response)
        {
            Request = request;
            Response = response;
        }
 private void raiseOnBeforeHttpRequestEvent(HttpRequest request)
 {
     if ((null != OnBeforeHttpRequestEvent) && (null != request))
         OnBeforeHttpRequestEvent(this, request);
 }
 public Task<HttpResponse> ExecuteAsBinaryAsync(HttpRequest request)
 {
     return Task.Factory.StartNew(() => ExecuteAsString(request));
 }
        private static UniHttpRequest ConvertRequest(HttpRequest request)
        {
            var uniMethod = ConvertHttpMethod(request.HttpMethod);
            var queryUrl = request.QueryUrl;
            
            //instantiate unirest request object
            UniHttpRequest uniRequest = new UniHttpRequest(uniMethod,queryUrl);

            //set request payload
            if (request.Body != null)
            {
                uniRequest.body(request.Body);
            }
            else if (request.FormParameters != null)
            {
                if (request.FormParameters.Any(p => p.Value is Stream || p.Value is FileStreamInfo))
                {
                    //multipart
                    foreach (var kvp in request.FormParameters)
                    {
                        if (kvp.Value is FileStreamInfo){
                            var fileInfo = (FileStreamInfo) kvp.Value;
                            uniRequest.field(kvp.Key,fileInfo.FileStream);
                            continue;
                        }
                        uniRequest.field(kvp.Key,kvp.Value);
                    }
                }
                else
                {
                    //URL Encode params
                    var paramsString = string.Join("&",
                        request.FormParameters.Select(kvp =>
                        string.Format("{0}={1}", Uri.EscapeDataString(kvp.Key), Uri.EscapeDataString(kvp.Value.ToString()))));
                    uniRequest.body(paramsString);
                    uniRequest.header("Content-Type", "application/x-www-form-urlencoded");
                }
            }

            //set request headers
            Dictionary<string, Object> headers = request.Headers.ToDictionary(item=> item.Key,item=> (Object) item.Value);
            uniRequest.headers(headers);

            //Set basic auth credentials if any
            if (!string.IsNullOrWhiteSpace(request.Username))
            {
                uniRequest.basicAuth(request.Username, request.Password);
            }

            return uniRequest;
        }