Esempio n. 1
0
        /// <summary>
        /// Invoke the specified method
        /// </summary>
        /// <typeparam name="TBody"></typeparam>
        /// <typeparam name="TResult"></typeparam>
        /// <param name="method"></param>
        /// <param name="url"></param>
        /// <param name="contentType"></param>
        /// <param name="body"></param>
        /// <param name="query"></param>
        /// <returns></returns>
        public TResult Invoke <TBody, TResult>(string method, string url, string contentType, TBody body, params KeyValuePair <string, object>[] query)
        {
            NameValueCollection parameters = new NameValueCollection();

            try
            {
                if (query != null)
                {
                    parameters = new NameValueCollection(query);
                }

                var requestEventArgs = new RestRequestEventArgs(method, url, parameters, contentType, body);
                this.Requesting?.Invoke(this, requestEventArgs);
                if (requestEventArgs.Cancel)
                {
                    s_tracer.TraceVerbose("HTTP request cancelled");
                    return(default(TResult));
                }

                // Invoke
                WebHeaderCollection responseHeaders = null;
                var retVal = this.InvokeInternal <TBody, TResult>(requestEventArgs.Method, requestEventArgs.Url, requestEventArgs.ContentType, requestEventArgs.AdditionalHeaders, out responseHeaders, body, requestEventArgs.Query);
                this.Responded?.Invoke(this, new RestResponseEventArgs(requestEventArgs.Method, requestEventArgs.Url, requestEventArgs.Query, requestEventArgs.ContentType, retVal, 200, 0));
                return(retVal);
            }
            catch (Exception e)
            {
                s_tracer.TraceError("Error invoking HTTP: {0}", e.Message);
                this.Responded?.Invoke(this, new RestResponseEventArgs(method, url, parameters, contentType, null, 500, 0));
                throw;
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Patches the specified <paramref name="patch"/>
        /// </summary>
        public String Patch <TPatch>(string url, string contentType, String ifMatch, TPatch patch)
        {
            try
            {
                var requestEventArgs = new RestRequestEventArgs("PATCH", url, null, contentType, patch);
                this.Requesting?.Invoke(this, requestEventArgs);
                if (requestEventArgs.Cancel)
                {
                    s_tracer.TraceVerbose("HTTP request cancelled");
                    return(null);
                }

                WebHeaderCollection requestHeaders  = requestEventArgs.AdditionalHeaders ?? new WebHeaderCollection(),
                                    responseHeaders = null;
                requestHeaders[HttpRequestHeader.IfMatch] = ifMatch;

                // Invoke
                this.InvokeInternal <TPatch, Object>("PATCH", url, contentType, requestHeaders, out responseHeaders, patch, null);

                // Return the ETag of the
                return(responseHeaders["ETag"]);
            }
            catch (Exception e)
            {
                s_tracer.TraceError("Error invoking HTTP: {0}", e.Message);
                this.Responded?.Invoke(this, new RestResponseEventArgs("PATCH", url, null, contentType, null, 500, 0));
                throw;
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Perform a head operation against the specified url
        /// </summary>
        public IDictionary <string, string> Head(string resourceName, params KeyValuePair <String, Object>[] query)
        {
            NameValueCollection parameters = new NameValueCollection();

            try
            {
                if (query != null)
                {
                    parameters = new NameValueCollection(query);
                }

                var requestEventArgs = new RestRequestEventArgs("HEAD", resourceName, parameters, null, null);
                this.Requesting?.Invoke(this, requestEventArgs);
                if (requestEventArgs.Cancel)
                {
                    s_tracer.TraceVerbose("HTTP request cancelled");
                    return(null);
                }

                // Invoke
                var httpWebReq = this.CreateHttpRequest(resourceName, requestEventArgs.Query);
                httpWebReq.Method = "HEAD";

                // Get the responst
                Dictionary <String, String> retVal = new Dictionary <string, string>();
                Exception fault    = null;
                var       httpTask = httpWebReq.GetResponseAsync().ContinueWith(o =>
                {
                    if (o.IsFaulted)
                    {
                        fault = o.Exception.InnerExceptions.First();
                    }
                    else
                    {
                        this.Responding?.Invoke(this, new RestResponseEventArgs("HEAD", resourceName, parameters, null, null, 200, o.Result.ContentLength));
                        foreach (var itm in o.Result.Headers.AllKeys)
                        {
                            retVal.Add(itm, o.Result.Headers[itm]);
                        }
                    }
                }, TaskContinuationOptions.LongRunning);
                httpTask.Wait();
                if (fault != null)
                {
                    throw fault;
                }
                this.Responded?.Invoke(this, new RestResponseEventArgs("HEAD", resourceName, parameters, null, null, 200, 0));

                return(retVal);
            }
            catch (Exception e)
            {
                s_tracer.TraceError("Error invoking HTTP: {0}", e.Message);
                this.Responded?.Invoke(this, new RestResponseEventArgs("HEAD", resourceName, parameters, null, null, 500, 0));
                throw;
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Gets the specified URL in raw format
        /// </summary>
        public byte[] Get(String url)
        {
            NameValueCollection parameters = new NameValueCollection();

            try
            {
                var requestEventArgs = new RestRequestEventArgs("GET", url, null, null, null);
                this.Requesting?.Invoke(this, requestEventArgs);
                if (requestEventArgs.Cancel)
                {
                    s_tracer.TraceVerbose("HTTP request cancelled");
                    return(null);
                }

                // Invoke
                var httpWebReq = this.CreateHttpRequest(url, requestEventArgs.Query);
                httpWebReq.Method = "GET";

                // Get the responst
                byte[]    retVal           = null;
                Exception requestException = null;
                var       httpTask         = httpWebReq.GetResponseAsync().ContinueWith(o =>
                {
                    if (o.IsFaulted)
                    {
                        requestException = o.Exception.InnerExceptions.First();
                    }
                    else
                    {
                        try
                        {
                            this.Responding?.Invoke(this, new RestResponseEventArgs("GET", url, null, null, null, 200, o.Result.ContentLength));

                            byte[] buffer = new byte[2048];
                            int br        = 1;
                            using (var ms = new MemoryStream())
                                using (var httpStream = o.Result.GetResponseStream())
                                {
                                    while (br > 0)
                                    {
                                        br = httpStream.Read(buffer, 0, 2048);
                                        ms.Write(buffer, 0, br);
                                        // Raise event
                                        this.FireProgressChanged(o.Result.ContentType, ms.Length / (float)o.Result.ContentLength);
                                    }


                                    ms.Seek(0, SeekOrigin.Begin);

                                    switch (o.Result.Headers["Content-Encoding"])
                                    {
                                    case "deflate":
                                        using (var dfs = new DeflateStream(ms, CompressionMode.Decompress, leaveOpen: true))
                                            using (var oms = new MemoryStream())
                                            {
                                                dfs.CopyTo(oms);
                                                retVal = oms.ToArray();
                                            }
                                        break;

                                    case "gzip":
                                        using (var gzs = new GZipStream(ms, CompressionMode.Decompress, leaveOpen: true))
                                            using (var oms = new MemoryStream())
                                            {
                                                gzs.CopyTo(oms);
                                                retVal = oms.ToArray();
                                            }
                                        break;

                                    case "bzip2":
                                        using (var lzmas = new BZip2Stream(ms, CompressionMode.Decompress, leaveOpen: true))
                                            using (var oms = new MemoryStream())
                                            {
                                                lzmas.CopyTo(oms);
                                                retVal = oms.ToArray();
                                            }
                                        break;

                                    case "lzma":
                                        using (var lzmas = new LZipStream(ms, CompressionMode.Decompress, leaveOpen: true))
                                            using (var oms = new MemoryStream())
                                            {
                                                lzmas.CopyTo(oms);
                                                retVal = oms.ToArray();
                                            }
                                        break;

                                    default:
                                        retVal = ms.ToArray();
                                        break;
                                    }
                                }
                        }
                        catch (Exception e)
                        {
                            s_tracer.TraceError("Error downloading {0}: {1}", url, e.Message);
                        }
                    }
                }, TaskContinuationOptions.LongRunning);
                httpTask.Wait();
                if (requestException != null)
                {
                    throw requestException;
                }


                this.Responded?.Invoke(this, new RestResponseEventArgs("GET", url, null, null, null, 200, 0));

                return(retVal);
            }
            catch (WebException e)
            {
                switch (this.ValidateResponse(e.Response))
                {
                case ServiceClientErrorType.Valid:
                    return(this.Get(url));

                default:
                    throw new RestClientException <byte[]>(
                              null,
                              e,
                              e.Status,
                              e.Response);
                }
            }
            catch (Exception e)
            {
                s_tracer.TraceError("Error invoking HTTP: {0}", e.Message);
                this.Responded?.Invoke(this, new RestResponseEventArgs("GET", url, null, null, null, 500, 0));
                throw;
            }
        }