This class represents an HTTP Entity (http://www.w3.org/Protocols/rfc2616/rfc2616-sec7.html). Thread Safety: This class is not thread safe since it's mutable.
        /// <summary>
        /// Attach file.
        /// </summary>
        /// <param name="path"> the url path </param>
        /// <param name="file"> the file </param>
        /// <param name="contentType"> the content Type </param>
        /// <returns> the attachment </returns>
        /// <exception cref="FileNotFoundException"> the file not found exception </exception>
        /// <exception cref="SmartsheetException"> the Smartsheet exception </exception>
        private void AddImage(string path, string file, string contentType)
        {
            Utility.Utility.ThrowIfNull(file);

            if (contentType == null)
            {
                contentType = "application/octet-stream";
            }

            FileInfo fi = new FileInfo(file);
            HttpRequest request = CreateHttpRequest(new Uri(this.Smartsheet.BaseURI, path), HttpMethod.POST);

            request.Headers["Content-Disposition"] = "attachment; filename=\"" + fi.Name + "\"";

            HttpEntity entity = new HttpEntity();
            entity.ContentType = contentType;

            entity.Content = File.ReadAllBytes(file);
            entity.ContentLength = fi.Length;
            request.Entity = entity;

            HttpResponse response = this.Smartsheet.HttpClient.Request(request);

            switch (response.StatusCode)
            {
                case HttpStatusCode.OK:
                    break;
                default:
                    HandleError(response);
                    break;
            }

            this.Smartsheet.HttpClient.ReleaseConnection();
        }
Esempio n. 2
0
        /// <summary>
        /// Make an HTTP request and return the response.
        /// </summary>
        /// <param name="smartsheetRequest"> the Smartsheet request </param>
        /// <returns> the HTTP response </returns>
        /// <exception cref="HttpClientException"> the HTTP client exception </exception>
        public virtual HttpResponse Request(HttpRequest smartsheetRequest)
        {
            Util.ThrowIfNull(smartsheetRequest);
            if (smartsheetRequest.Uri == null)
            {
                throw new System.ArgumentException("A Request URI is required.");
            }

            int          attempt            = 0;
            HttpResponse smartsheetResponse = null;

            Stopwatch totalElapsed = new Stopwatch();

            totalElapsed.Start();

            while (true)
            {
                smartsheetResponse = new HttpResponse();

                restRequest = CreateRestRequest(smartsheetRequest);

                // Set HTTP Headers
                if (smartsheetRequest.Headers != null)
                {
                    foreach (KeyValuePair <string, string> header in smartsheetRequest.Headers)
                    {
                        restRequest.AddHeader(header.Key, header.Value);
                    }
                }

                if (smartsheetRequest.Entity != null && smartsheetRequest.Entity.GetContent() != null)
                {
                    restRequest.AddParameter(smartsheetRequest.Entity.ContentType, Util.ReadAllBytes(smartsheetRequest.Entity.GetBinaryContent()),
                                             smartsheetRequest.Entity.ContentType, ParameterType.RequestBody);
                }

                // Set the client base Url.
                httpClient.BaseUrl = new Uri(smartsheetRequest.Uri.GetLeftPart(UriPartial.Authority));

                Stopwatch timer = new Stopwatch();

                // Make the HTTP request
                timer.Start();
                restResponse = httpClient.Execute(restRequest);
                timer.Stop();

                LogRequest(restRequest, restResponse, timer.ElapsedMilliseconds);

                if (restResponse.ResponseStatus == ResponseStatus.Error)
                {
                    throw new HttpClientException("There was an issue connecting.");
                }

                // Set returned Headers
                smartsheetResponse.Headers = new Dictionary <string, string>();
                foreach (var header in restResponse.Headers)
                {
                    smartsheetResponse.Headers[header.Name] = (String)header.Value;
                }
                smartsheetResponse.StatusCode = restResponse.StatusCode;

                // Set returned entities
                if (restResponse.Content != null)
                {
                    HttpEntity entity = new HttpEntity();
                    entity.ContentType   = restResponse.ContentType;
                    entity.ContentLength = restResponse.ContentLength;

                    entity.Content            = restResponse.RawBytes;
                    smartsheetResponse.Entity = entity;
                }

                if (smartsheetResponse.StatusCode == HttpStatusCode.OK)
                {
                    break;
                }

                if (!ShouldRetry(++attempt, totalElapsed.ElapsedMilliseconds, smartsheetResponse))
                {
                    break;
                }
            }
            return(smartsheetResponse);
        }
Esempio n. 3
0
        /// <summary>
        /// Make a multipart HTTP request and return the response.
        /// </summary>
        /// <param name="smartsheetRequest"> the Smartsheet request </param>
        /// <param name="file">the full file path</param>
        /// <param name="fileType">the file type, or also called the conent type of the file</param>
        /// <param name="objectType">the object name, for example 'comment', or 'discussion'</param>
        /// <returns> the HTTP response </returns>
        /// <exception cref="HttpClientException"> the HTTP client exception </exception>
        public virtual HttpResponse Request(HttpRequest smartsheetRequest, string objectType, string file, string fileType)
        {
            Util.ThrowIfNull(smartsheetRequest);
            if (smartsheetRequest.Uri == null)
            {
                throw new System.ArgumentException("A Request URI is required.");
            }

            HttpResponse smartsheetResponse = new HttpResponse();

            restRequest = CreateRestRequest(smartsheetRequest);

            // Set HTTP Headers
            if (smartsheetRequest.Headers != null)
            {
                foreach (KeyValuePair <string, string> header in smartsheetRequest.Headers)
                {
                    restRequest.AddHeader(header.Key, header.Value);
                }
            }

            restRequest.AddFile("file", File.ReadAllBytes(file), new FileInfo(file).Name, fileType);
            if (smartsheetRequest.Entity != null && smartsheetRequest.Entity.GetContent() != null)
            {
                restRequest.AddParameter(objectType.ToLower(), System.Text.Encoding.Default.GetString(smartsheetRequest.Entity.Content),
                                         smartsheetRequest.Entity.ContentType, ParameterType.RequestBody);
            }

            restRequest.AlwaysMultipartFormData = true;

            // Set the client base Url.
            httpClient.BaseUrl = new Uri(smartsheetRequest.Uri.GetLeftPart(UriPartial.Authority));

            Stopwatch timer = new Stopwatch();

            // Make the HTTP request
            timer.Start();
            restResponse = httpClient.Execute(restRequest);
            timer.Stop();

            LogRequest(restRequest, restResponse, timer.ElapsedMilliseconds);

            if (restResponse.ResponseStatus == ResponseStatus.Error)
            {
                throw new HttpClientException("There was an issue connecting.");
            }

            // Set returned Headers
            smartsheetResponse.Headers = new Dictionary <string, string>();
            foreach (var header in restResponse.Headers)
            {
                smartsheetResponse.Headers[header.Name] = (String)header.Value;
            }
            smartsheetResponse.StatusCode = restResponse.StatusCode;

            // Set returned entities
            if (restResponse.Content != null)
            {
                HttpEntity entity = new HttpEntity();
                entity.ContentType   = restResponse.ContentType;
                entity.ContentLength = restResponse.ContentLength;

                entity.Content            = restResponse.RawBytes;
                smartsheetResponse.Entity = entity;
            }

            return(smartsheetResponse);
        }
        /// <summary>
        /// Make an HTTP request and return the response.
        /// </summary>
        /// <param name="smartsheetRequest"> the Smartsheet request </param>
        /// <returns> the HTTP response </returns>
        /// <exception cref="HttpClientException"> the HTTP client exception </exception>
        public virtual HttpResponse Request(HttpRequest smartsheetRequest)
        {
            Util.ThrowIfNull(smartsheetRequest);
            if (smartsheetRequest.Uri == null)
            {
                throw new System.ArgumentException("A Request URI is required.");
            }

            HttpResponse smartsheetResponse = new HttpResponse();

            // Create HTTP request based on the smartsheetRequest request Type
            if (HttpMethod.GET == smartsheetRequest.Method)
            {
                restRequest = new RestRequest(smartsheetRequest.Uri, Method.GET);
            }
            else if (HttpMethod.POST == smartsheetRequest.Method)
            {
                restRequest = new RestRequest(smartsheetRequest.Uri, Method.POST);
            }
            else if (HttpMethod.PUT == smartsheetRequest.Method)
            {
                restRequest = new RestRequest(smartsheetRequest.Uri, Method.PUT);
            }
            else if (HttpMethod.DELETE == smartsheetRequest.Method)
            {
                restRequest = new RestRequest(smartsheetRequest.Uri, Method.DELETE);
            }
            else
            {
                throw new System.NotSupportedException("Request method " + smartsheetRequest.Method + " is not supported!");
            }

            // Set HTTP Headers
            if (smartsheetRequest.Headers != null)
            {
                foreach (KeyValuePair <string, string> header in smartsheetRequest.Headers)
                {
                    restRequest.AddHeader(header.Key, header.Value);
                }
            }

            if (smartsheetRequest.Entity != null && smartsheetRequest.Entity.GetContent() != null)
            {
                restRequest.AddParameter("application/json", Util.ReadAllBytes(smartsheetRequest.Entity.GetBinaryContent()),
                                         ParameterType.RequestBody);
            }

            // Set the client base Url.
            httpClient.BaseUrl = new Uri(smartsheetRequest.Uri.GetLeftPart(UriPartial.Authority));

            // Make the HTTP request
            restResponse = httpClient.Execute(restRequest);

            if (restResponse.ResponseStatus == ResponseStatus.Error)
            {
                throw new HttpClientException("There was an issue connecting.");
            }

            // Set returned Headers
            smartsheetResponse.Headers = new Dictionary <string, string>();
            foreach (var header in restResponse.Headers)
            {
                smartsheetResponse.Headers[header.Name] = (String)header.Value;
            }
            smartsheetResponse.StatusCode = restResponse.StatusCode;

            // Set returned entities
            if (restResponse.Content != null)
            {
                HttpEntity entity = new HttpEntity();
                entity.ContentType   = restResponse.ContentType;
                entity.ContentLength = restResponse.ContentLength;

                entity.Content            = restResponse.RawBytes;
                smartsheetResponse.Entity = entity;
            }

            return(smartsheetResponse);
        }
        public virtual void TestRequest()
        {
            // Null Argument
            try
            {
                client.Request(null);
                Assert.Fail("Exception should have been thrown");
            }
            catch (System.ArgumentException)
            {
                // Expected
            }

            HttpRequest request = new HttpRequest();

            // No URL in request
            try
            {
                client.Request(request);

                Assert.Fail("Exception should have been thrown");
            }
            catch (System.ArgumentException)
            {
                // Expected
            }

            // Test each http method
            request.Uri    = new Uri("http://google.com");
            request.Method = HttpMethod.GET;
            client.Request(request);
            client.ReleaseConnection();


            request.Method = HttpMethod.POST;
            client.Request(request);
            client.ReleaseConnection();

            request.Method = HttpMethod.PUT;
            client.Request(request);
            client.ReleaseConnection();

            request.Method = HttpMethod.DELETE;
            client.Request(request);
            client.ReleaseConnection();



            // Test request with set headers and http entity but a null content
            Dictionary <string, string> headers = new Dictionary <string, string>();

            headers["name"] = "value";
            HttpEntity entity = new HttpEntity();

            entity.ContentType = "text/html; charset=ISO-8859-4";
            request.Entity     = entity;
            request.Headers    = headers;
            request.Method     = HttpMethod.POST;
            client.Request(request);
            client.ReleaseConnection();

            // Test request with set headers and http entity and some content
            entity.Content = Encoding.UTF8.GetBytes("Hello World!");
            request.Entity = entity;
            client.Request(request);
            client.ReleaseConnection();

            // Test IOException
            try
            {
                request.Uri = new Uri("http://bad.domain");
                client.Request(request);
                client.ReleaseConnection();
                Assert.Fail("Exception should have been thrown.");
            }
            catch (Exception)
            {
                // Expected
            }
        }
        /// <summary>
        /// Make an HTTP request and return the response.
        /// </summary>
        /// <param name="smartsheetRequest"> the Smartsheet request </param>
        /// <returns> the HTTP response </returns>
        /// <exception cref="HttpClientException"> the HTTP client exception </exception>
        public virtual HttpResponse Request(HttpRequest smartsheetRequest)
        {
            Util.ThrowIfNull(smartsheetRequest);
            if (smartsheetRequest.Uri == null)
            {
                 throw new System.ArgumentException("A Request URI is required.");
            }

            HttpResponse smartsheetResponse = new HttpResponse();

            // Create HTTP request based on the smartsheetRequest request Type
            if (HttpMethod.GET == smartsheetRequest.Method)
            {
                 restRequest = new RestRequest(smartsheetRequest.Uri, Method.GET);
            }
            else if (HttpMethod.POST == smartsheetRequest.Method)
            {
                 restRequest = new RestRequest(smartsheetRequest.Uri, Method.POST);
            }
            else if (HttpMethod.PUT == smartsheetRequest.Method)
            {
                 restRequest = new RestRequest(smartsheetRequest.Uri, Method.PUT);
            }
            else if (HttpMethod.DELETE == smartsheetRequest.Method)
            {
                 restRequest = new RestRequest(smartsheetRequest.Uri, Method.DELETE);
            }
            else
            {
                throw new System.NotSupportedException("Request method " + smartsheetRequest.Method + " is not supported!");
            }

            // Set HTTP Headers
            if (smartsheetRequest.Headers != null)
            {
                foreach (KeyValuePair<string, string> header in smartsheetRequest.Headers)
                {
                    restRequest.AddHeader(header.Key, header.Value);
                }
            }

            if (smartsheetRequest.Entity != null && smartsheetRequest.Entity.GetContent() != null)
            {
                restRequest.AddParameter("application/json", Util.ReadAllBytes(smartsheetRequest.Entity.GetBinaryContent()),
                    ParameterType.RequestBody);
            }

            // Set the client base Url.
            httpClient.BaseUrl = new Uri(smartsheetRequest.Uri.GetLeftPart(UriPartial.Authority));

            // Make the HTTP request
            restResponse = httpClient.Execute(restRequest);

            if (restResponse.ResponseStatus == ResponseStatus.Error)
            {
                throw new HttpClientException("There was an issue connecting.");
            }

            // Set returned Headers
            smartsheetResponse.Headers = new Dictionary<string, string>();
            foreach (var header in restResponse.Headers)
            {
                smartsheetResponse.Headers[header.Name] = (String)header.Value;
            }
            smartsheetResponse.StatusCode = restResponse.StatusCode;

            // Set returned entities
            if (restResponse.Content != null)
            {
                HttpEntity entity = new HttpEntity();
                entity.ContentType = restResponse.ContentType;
                entity.ContentLength = restResponse.ContentLength;

                entity.Content = restResponse.RawBytes;
                smartsheetResponse.Entity = entity;
            }

            return smartsheetResponse;
        }