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);

            uniRequest.TimeOut = TimeSpan.FromSeconds(10);

            //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, fileInfo.FileName, fileInfo.ContentType);
                            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);
            uniRequest.header("user-agent", "moesifapi-csharp/" + Version);

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

            return(uniRequest);
        }
Example #2
0
        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);
        }
        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;
        }
Example #4
0
        /// <inheritdoc />
        public async Task <IHttpResponse> RequestAsync(IHttpRequest request)
        {
            if (request == null)
            {
                throw new ArgumentNullException("request");
            }

            UnirestRequest unirestRequest = factoryFunctions[request.Method](request.Url);

            if (request.Body != null)
            {
                unirestRequest = unirestRequest.body(request.Body);
            }

            HttpResponse <Stream> unirestResponse = await unirestRequest.headers(request.Headers.Build()).asBinaryAsync().ConfigureAwait(false);

            using (MemoryStream memory = new MemoryStream())
            {
                await unirestResponse.Body.CopyToAsync(memory).ConfigureAwait(false);

                unirestResponse.Body.Dispose();
                return(new HttpResponse(request, unirestResponse.Code, memory.ToArray()));
            }
        }
Example #5
0
        public HttpResponse ExecuteAsBinary(HttpRequest request)
        {
            UniHttpRequest uniRequest = ConvertRequest(request);

            return(ConvertResponse(uniRequest.asBinary()));
        }
Example #6
0
        public HttpResponse ExecuteAsString(HttpRequest request)
        {
            UniHttpRequest uniRequest = ConvertRequest(request);

            return(ConvertResponse(uniRequest.asString()));
        }