Example #1
0
        /// <summary>
        /// Delete a column.
        ///
        /// It mirrors To the following Smartsheet REST API method: DELETE /column/{Id}
        ///
        /// Exceptions:
        ///   InvalidRequestException : if there is any problem with the REST API request
        ///   AuthorizationException : if there is any problem with the REST API authorization(access token)
        ///   ResourceNotFoundException : if the resource can not be found
        ///   ServiceUnavailableException : if the REST API service is not available (possibly due To rate limiting)
        ///   SmartsheetRestException : if there is any other REST API related error occurred during the operation
        ///   SmartsheetException : if there is any other error occurred during the operation
        /// </summary>
        /// <param name="id"> the ID of the column </param>
        /// <param name="sheetId"> the ID of the sheet </param>
        /// <exception cref="SmartsheetException"> the Smartsheet exception </exception>
        public virtual void DeleteColumn(long id, long sheetId)
        {
            HttpRequest request = null;

            request = CreateHttpRequest(new Uri(Smartsheet.BaseURI, "column/" + id), HttpMethod.DELETE);

            Column column = new Column();

            column.SheetId = sheetId;

            request.Entity = serializeToEntity <Column>(column);

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

            switch (response.StatusCode)
            {
            case HttpStatusCode.OK:
                this.Smartsheet.JsonSerializer.deserializeResult <object>(response.Entity.GetContent());
                break;

            default:
                HandleError(response);
                break;
            }

            this.Smartsheet.HttpClient.ReleaseConnection();
        }
        /// <summary>
        /// <para>Get a sheet as a file.</para>
        ///
        /// <para>It mirrors to the following Smartsheet REST API method:<br />
        /// GET /reports/{reportId} with "application/pdf", "application/vnd.ms-excel", or "text/csv" as Accept HTTP header</para>
        /// </summary>
        /// <param name="path">the path of the file</param>
        /// <param name="outputStream"> the output stream to which the CSV file will be written. </param>
        /// <param name="contentType"> the Accept header </param>
        /// <exception cref="System.InvalidOperationException"> if any argument is null or empty string </exception>
        /// <exception cref="InvalidRequestException"> if there is any problem with the REST API request </exception>
        /// <exception cref="AuthorizationException"> if there is any problem with  the REST API authorization (access token) </exception>
        /// <exception cref="ResourceNotFoundException"> if the resource cannot be found </exception>
        /// <exception cref="ServiceUnavailableException"> if the REST API service is not available (possibly due to rate limiting) </exception>
        /// <exception cref="SmartsheetException"> if there is any other error during the operation </exception>
        private void GetReportAsFile(string path, BinaryWriter outputStream, string contentType)
        {
            Utility.Utility.ThrowIfNull(path, outputStream, contentType);

            HttpRequest request = null;

            request = CreateHttpRequest(new Uri(this.Smartsheet.BaseURI, path), HttpMethod.GET);
            request.Headers["Accept"] = contentType;

            Api.Internal.Http.HttpResponse response = Smartsheet.HttpClient.Request(request);

            switch (response.StatusCode)
            {
            case HttpStatusCode.OK:
                try
                {
                    response.Entity.GetBinaryContent().BaseStream.CopyTo(outputStream.BaseStream);
                }
                catch (IOException e)
                {
                    throw new SmartsheetException(e);
                }
                break;

            default:
                HandleError(response);
                break;
            }

            Smartsheet.HttpClient.ReleaseConnection();
        }
        /// <summary>
        /// <para>Get a sheet as a file.</para>
        ///
        /// <para>It mirrors To the following Smartsheet REST API method:<br />
        /// GET /sheets/{sheetId} with "application/pdf", "application/vnd.ms-excel", or "text/csv" as Accept HTTP header</para>
        /// </summary>
        /// <param name="sheetId"> the Id of the sheet </param>
        /// <param name="paperSize"> the size of the PDF file </param>
        /// <param name="outputStream"> the output stream To which the CSV file will be written. </param>
        /// <param name="contentType"> the Accept header </param>
        /// <exception cref="System.InvalidOperationException"> if any argument is null or empty string </exception>
        /// <exception cref="InvalidRequestException"> if there is any problem with the REST API request </exception>
        /// <exception cref="AuthorizationException"> if there is any problem with  the REST API authorization (access token) </exception>
        /// <exception cref="ResourceNotFoundException"> if the resource cannot be found </exception>
        /// <exception cref="ServiceUnavailableException"> if the REST API service is not available (possibly due To rate limiting) </exception>
        /// <exception cref="SmartsheetException"> if there is any other error during the operation </exception>
        private void GetSheetAsFile(long sheetId, PaperSize?paperSize, BinaryWriter outputStream, string contentType)
        {
            Utils.ThrowIfNull(outputStream, contentType);

            StringBuilder path = new StringBuilder("sheets/" + sheetId);

            if (paperSize.HasValue)
            {
                path.Append("?paperSize=" + paperSize.Value);
            }

            HttpRequest request = null;

            request = CreateHttpRequest(new Uri(this.Smartsheet.BaseURI, path.ToString()), HttpMethod.GET);
            request.Headers["Accept"] = contentType;

            Api.Internal.Http.HttpResponse response = Smartsheet.HttpClient.Request(request);

            switch (response.StatusCode)
            {
            case HttpStatusCode.OK:
                try
                {
                    response.Entity.GetBinaryContent().BaseStream.CopyTo(outputStream.BaseStream);
                }
                catch (IOException e)
                {
                    throw new SmartsheetException(e);
                }
                break;

            default:
                HandleError(response);
                break;
            }

            Smartsheet.HttpClient.ReleaseConnection();
        }
        /// <summary>
        /// Called by DefaultHttpClient when a request fails to determine if we can retry the request. Calls
        /// calcBackoff to determine time in between retries.
        /// </summary>
        /// <param name="previousAttempts"></param>
        /// <param name="totalElapsedTime"></param>
        /// <param name="response"></param>
        /// <returns>true if this error code can be retried</returns>
        public bool SmartsheetShouldRetry(int previousAttempts, long totalElapsedTime, HttpResponse response)
        {
            Api.Models.Error error;
            try
            {
                error = jsonSerializer.deserialize <Api.Models.Error>(
                    response.Entity.GetContent());
            }
            catch (JsonSerializationException ex)
            {
                throw new SmartsheetException(ex);
            }
            catch (Newtonsoft.Json.JsonException ex)
            {
                throw new SmartsheetException(ex);
            }
            catch (IOException ex)
            {
                throw new SmartsheetException(ex);
            }

            switch (error.ErrorCode)
            {
            case 4001:
            case 4002:
            case 4003:
            case 4004:
                break;

            default:
                return(false);
            }

            long backoff = calcBackoff.CalcBackoffCallback(previousAttempts, totalElapsedTime, error);

            if (backoff < 0)
            {
                return(false);
            }

            logger.Info(string.Format("HttpError StatusCode={0}: Retrying in {1} milliseconds", response.StatusCode, backoff));
            Thread.Sleep(TimeSpan.FromMilliseconds(backoff));
            return(true);
        }