Esempio n. 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>
        /// Request a token.
        /// 
        /// Exceptions: 
        ///   - IllegalArgumentException : if Url is null or empty 
        ///   - InvalidTokenRequestException : if the token request is invalid 
        ///   - InvalidOAuthClientException : if the client information is invalid 
        ///   - InvalidOAuthGrantException : if the authorization Code or refresh token is invalid or 
        ///   expired, the redirect_uri does not match, or the hash Value does not match the client secret and/or Code 
        ///   - UnsupportedOAuthGrantTypeException : if the grant Type is invalid 
        ///   - OAuthTokenException : if any other error occurred during the operation
        /// </summary>
        /// <param name="url"> the URL (with request parameters) from which the token will be requested </param>
        /// <returns> the token </returns>
        /// <exception cref="OAuthTokenException"> the o auth token exception </exception>
        /// <exception cref="JSONSerializationException"> the JSON serializer exception </exception>
        /// <exception cref="System.UriFormatException"> the URI syntax exception </exception>
        /// <exception cref="InvalidRequestException"> the invalid request exception </exception>
        private Token RequestToken(string url)
        {
            // Create the request and send it To get the response/token.
            HttpRequest request = new HttpRequest();
            request.Uri = new Uri(url);
            request.Method = HttpMethod.POST;
            request.Headers = new Dictionary<string, string>();
            request.Headers["Content-Type"] = "application/x-www-form-urlencoded";
            HttpResponse response = httpClient.Request(request);

            // Create a map of the response
            StreamReader inputStream = response.Entity.GetContent();
            IDictionary<string, object> map = jsonSerializer.DeserializeMap(inputStream);
            httpClient.ReleaseConnection();

            // Check for a error response and throw it.
            if (response.StatusCode != HttpStatusCode.OK && map.ContainsKey("error") && map["error"] != null)
            {
                string errorType = map["error"].ToString();
                string errorDescription = map["message"] == null ? "" : (string)map["message"];
                if ("invalid_request".Equals(errorType))
                {
                    throw new InvalidTokenRequestException(errorDescription);
                }
                else if ("invalid_client".Equals(errorType))
                {
                    throw new InvalidOAuthClientException(errorDescription);
                }
                else if ("invalid_grant".Equals(errorType))
                {
                    throw new InvalidOAuthGrantException(errorDescription);
                }
                else if ("unsupported_grant_type".Equals(errorType))
                {
                    throw new UnsupportedOAuthGrantTypeException(errorDescription);
                }
                else
                {
                    throw new OAuthTokenException(errorDescription);
                }
            }

            // Another error by not getting a 200 RequestResult
            else if (response.StatusCode != HttpStatusCode.OK)
            {
                throw new OAuthTokenException("Token request failed with http error code: " + response.StatusCode);
            }

            // Create a token based on the response
            Token token = new Token();
            object tempObj = map["access_token"];
            token.AccessToken = tempObj == null ? "" : (string)tempObj;
            tempObj = map["token_type"];
            token.TokenType = tempObj == null ? "" : (string)tempObj;
            tempObj = map["refresh_token"];
            token.RefreshToken = tempObj == null ? "" : (string)tempObj;

            long? expiresIn = 0L;
            try
            {
                expiresIn = Convert.ToInt64(Convert.ToString(map["expires_in"]));
            }
            catch (Exception)
            {
                expiresIn = 0L;
            }
            token.ExpiresInSeconds = expiresIn;

            return token;
        }
 /// <summary>
 /// Revoke token.
 /// </summary>
 /// <param name="token"> the  token </param>
 /// <exception cref="OAuthTokenException"> the o auth token exception </exception>
 /// <exception cref="JSONSerializationException"> the JSON serializer exception </exception>
 /// <exception cref="System.UriFormatException"> the URI syntax exception </exception>
 /// <exception cref="InvalidRequestException"> the invalid request exception </exception>
 /// <exception cref="System.InvalidOperationException"> if any other error occurred during the operation </exception>
 public virtual void RevokeToken(Token token)
 {
     // Create the request and send it To get the response/token.
     HttpRequest request = new HttpRequest();
     request.Uri = new Uri(tokenURL);
     request.Method = HttpMethod.DELETE;
     // Set authorization header
     request.Headers["Authorization"] = "Bearer " + token.AccessToken;
     HttpResponse response = httpClient.Request(request);
     // Another error by not getting a 200 RequestResult
     if (response.StatusCode != HttpStatusCode.OK)
     {
         throw new OAuthTokenException("Token request failed with http error code: " + response.StatusCode);
     }
     httpClient.ReleaseConnection();
 }