Skip to content
This repository has been archived by the owner on Jan 9, 2023. It is now read-only.

Copied ImportXls, ImportCsv, and AttachFile, and added a byte[] array… #148

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions IntegrationTestSDK/AttachmentResourcesTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Smartsheet.Api;
using Smartsheet.Api.Models;
using System.Configuration;
using System.IO;

namespace IntegrationTestSDK
{
Expand Down Expand Up @@ -31,6 +32,8 @@ public void TestAttachmentResources()

AttachNewVersion(smartsheet, sheetId, attachmentId);

AttachNewVersionBinaryBytes(smartsheet, sheetId, attachmentId);

ListAttachmentVersions(smartsheet, sheetId, attachmentId);

DeleteAttachment(smartsheet, sheetId, attachmentId);
Expand All @@ -49,6 +52,14 @@ private void AttachNewVersion(SmartsheetClient smartsheet, long sheetId, long at
smartsheet.SheetResources.AttachmentResources.VersioningResources.AttachNewVersion(sheetId, attachmentId, path, "text/plain");
}

private void AttachNewVersionBinaryBytes(SmartsheetClient smartsheet, long sheetId, long attachmentId)
{
var fI = new FileInfo(path);
var bytes = File.ReadAllBytes(path);
smartsheet.SheetResources.AttachmentResources.VersioningResources.AttachNewVersion(sheetId, attachmentId,
fI.Name, bytes, "text/plain");
}

private static void ListRowAttachments(SmartsheetClient smartsheet, long sheetId, long rowId)
{
PaginatedResult<Attachment> attachments = smartsheet.SheetResources.RowResources.AttachmentResources.ListAttachments(sheetId, rowId, null);
Expand Down
6 changes: 5 additions & 1 deletion IntegrationTestSDK/net452/integration-test-sdk-net452.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@
<Compile Include="..\WorkspaceResourcesCopyTest.cs" />
<Compile Include="..\WorkspaceResourcesTest.cs" />
</ItemGroup>
<ItemGroup />
<ItemGroup>
<PackageReference Include="Newtonsoft.Json">
<Version>12.0.2</Version>
</PackageReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
2 changes: 1 addition & 1 deletion documentation/smartsheet-csharp-sdk-docs-v2.shfbproj
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0">
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="14.0">
<PropertyGroup>
<!-- The configuration and platform will be used to determine which assemblies to include from solution and
project documentation sources -->
Expand Down
29 changes: 26 additions & 3 deletions main/Smartsheet/Api/AttachmentVersioningResources.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//
// http://www.apache.org/licenses/LICENSE-2.0
//
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand All @@ -23,7 +23,7 @@ namespace Smartsheet.Api
{
/// <summary>
/// <para>This interface provides methods to access Versioning resources that are associated to an Attachment resource.</para>
///
///
/// <para>Thread Safety: Implementation of this interface must be thread safe.</para>
/// </summary>
public interface AttachmentVersioningResources
Expand All @@ -49,6 +49,29 @@ public interface AttachmentVersioningResources
/// <exception cref="SmartsheetException"> if there is any other error during the operation </exception>
Attachment AttachNewVersion(long sheetId, long attachmentId, string file, string fileType);

/// <summary>
/// <para>Uploads a new version of a file to a Sheet or Row.
/// This operation can be performed using a simple upload or a multipart upload. For more information, see Posting an Attachment.</para>
/// <para>It mirrors to the following Smartsheet REST API method:<br />
/// POST /sheets/{sheetId}/attachments/{attachmentId}/versions</para>
/// <remarks><para>Uploading new versions is not supported for attachments on Comments or for URL attachments.</para>
/// <para>This is a resource-intensive operation and incurs 10 additional requests against the rate limit.</para></remarks>
/// </summary>
/// <param name="sheetId"> the sheet id </param>
/// <param name="attachmentId"> the attachment id </param>
/// <param name="fileName"></param>
/// <param name="binaryBytes"></param>
/// <param name="fileType"> the file type, can be null </param>
/// <returns> Attachment object for the newly created attachment </returns>
/// <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>
Attachment AttachNewVersion(long sheetId, long attachmentId,
string fileName, byte[] binaryBytes, string fileType = "application/octet-stream");

/// <summary>
/// <para>Deletes all versions of the attachment corresponding to the specified Attachment ID.
/// For attachments with multiple versions, this will effectively delete the attachment from the object that it’s attached to.</para>
Expand Down
28 changes: 25 additions & 3 deletions main/Smartsheet/Api/CommentAttachmentResources.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//
// http://www.apache.org/licenses/LICENSE-2.0
//
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand All @@ -24,7 +24,7 @@ namespace Smartsheet.Api

/// <summary>
/// <para>This interface provides methods to access Attachment resources that are associated to Comment resources.</para>
///
///
/// <para>Thread Safety: Implementation of this interface must be thread safe.</para>
/// </summary>
public interface CommentAttachmentResources
Expand All @@ -49,6 +49,28 @@ public interface CommentAttachmentResources
/// <exception cref="SmartsheetException"> if there is any other error during the operation </exception>
Attachment AttachFile(long sheetId, long commentId, string file, string fileType);

/// <summary>
/// <para>Attaches a file to the Comment.</para>
/// <para>This operation will always create a new attachment.
/// to upload a new version of the same attachment, use the Attach New Version operation.</para>
/// <para>It mirrors to the following Smartsheet REST API method:
/// POST /sheets/{sheetId}/comments/{commentId}/attachments</para>
/// </summary>
/// <param name="sheetId"> the sheetId </param>
/// <param name="commentId"> the comment Id </param>
/// <param name="fileBinary"></param>
/// <param name="fileType"> the file type, can be null </param>
/// <param name="fileName"></param>
/// <returns> the newly created Attachment </returns>
/// <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>
Attachment AttachFile(long sheetId, long commentId, string fileName,
byte[] fileBinary, string fileType);

/// <summary>
/// <para>Attaches a URL to the Comment.</para>
/// <para>It mirrors to the following Smartsheet REST API method:
Expand Down
104 changes: 79 additions & 25 deletions main/Smartsheet/Api/Internal/AbstractResources.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//
// http://www.apache.org/licenses/LICENSE-2.0
//
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand All @@ -34,7 +34,7 @@ namespace Smartsheet.Api.Internal

/// <summary>
/// This is the base class of the SmartsheetClient REST API resources.
///
///
/// Thread Safety: This class is thread safe because it is immutable and the underlying SmartsheetImpl is thread safe.
/// </summary>
public abstract class AbstractResources
Expand Down Expand Up @@ -146,7 +146,7 @@ public SmartsheetRestException getException(Api.Models.Error error)

/// <summary>
/// Represents the SmartsheetImpl.
///
///
/// It will be initialized in the constructor and will not change afterwards.
/// </summary>
protected SmartsheetImpl smartsheet;
Expand All @@ -164,12 +164,12 @@ protected internal AbstractResources(SmartsheetImpl smartsheet)

/// <summary>
/// Get a resource from SmartsheetClient REST API.
///
///
/// Parameters: - path : the relative path of the resource - objectClass : the resource object class
///
///
/// Returns: the resource (note that if there is no such resource, this method will throw ResourceNotFoundException
/// rather than returning null).
///
///
/// 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)
Expand Down Expand Up @@ -239,8 +239,8 @@ protected internal virtual T GetResource<T>(string path, Type objectClass)

/// <summary>
/// Create a resource using SmartsheetClient REST API.
///
/// Exceptions:
///
/// Exceptions:
/// IllegalArgumentException : if any argument is null, or path is an empty string
/// InvalidRequestException : if there is any problem with the REST API request
/// AuthorizationException : if there is any problem with the REST API authorization (access token)
Expand Down Expand Up @@ -290,8 +290,8 @@ protected internal virtual S CreateResource<S, T>(string path, T @object)

/// <summary>
/// Create a resource using SmartsheetClient REST API.
///
/// Exceptions:
///
/// Exceptions:
/// IllegalArgumentException : if any argument is null, or path is an empty string
/// InvalidRequestException : if there is any problem with the REST API request
/// AuthorizationException : if there is any problem with the REST API authorization (access token)
Expand Down Expand Up @@ -387,7 +387,7 @@ protected internal virtual T CreateResourceWithAttachment<T>(string path, T @obj

/// <summary>
/// Update a resource using SmartsheetClient REST API.
///
///
/// Exceptions:
/// IllegalArgumentException : if any argument is null, or path is an empty string
/// InvalidRequestException : if there is any problem with the REST API request
Expand Down Expand Up @@ -440,7 +440,7 @@ protected internal virtual T UpdateResource<T>(string path, Type objectClass, T

/// <summary>
/// List resources using SmartsheetClient REST API.
///
///
/// Exceptions:
/// IllegalArgumentException : if any argument is null, or path is an empty string
/// InvalidRequestException : if there is any problem with the REST API request
Expand Down Expand Up @@ -488,7 +488,7 @@ protected internal virtual PaginatedResult<T> ListResourcesWithWrapper<T>(string

/// <summary>
/// List resources using SmartsheetClient REST API.
///
///
/// Exceptions:
/// IllegalArgumentException : if any argument is null, or path is an empty string
/// InvalidRequestException : if there is any problem with the REST API request
Expand Down Expand Up @@ -537,7 +537,7 @@ protected internal virtual IList<T> ListResources<T>(string path, Type objectCla

/// <summary>
/// Delete a resource from SmartsheetClient REST API.
///
///
/// Exceptions:
/// IllegalArgumentException : if any argument is null, or path is an empty string
/// InvalidRequestException : if there is any problem with the REST API request
Expand Down Expand Up @@ -582,7 +582,7 @@ protected internal virtual T DeleteResource<T>(string path)

/// <summary>
/// Delete a resource from SmartsheetClient REST API.
///
///
/// Exceptions:
/// IllegalArgumentException : if any argument is null, or path is an empty string
/// InvalidRequestException : if there is any problem with the REST API request
Expand Down Expand Up @@ -626,12 +626,12 @@ protected internal virtual void DeleteResource<T>(string path, Type objectClass)

/// <summary>
/// Post an object to SmartsheetClient REST API and receive a list of objects from response.
///
///
/// Parameters: - path : the relative path of the resource collections - objectToPost : the object to post -
/// objectClassToReceive : the resource object class to receive
///
///
/// Returns: the object list
///
///
/// Exceptions:
/// IllegalArgumentException : if any argument is null, or path is an empty string
/// InvalidRequestException : if there is any problem with the REST API request
Expand Down Expand Up @@ -682,7 +682,7 @@ protected internal virtual IList<S> PostAndReceiveList<T, S>(string path, T obje

/// <summary>
/// Put an object to SmartsheetClient REST API and receive a list of objects from response.
///
///
/// Exceptions:
/// IllegalArgumentException : if any argument is null, or path is an empty string
/// InvalidRequestException : if there is any problem with the REST API request
Expand Down Expand Up @@ -734,7 +734,7 @@ protected internal virtual IList<S> PutAndReceiveList<T, S>(string path, T objec

/// <summary>
/// Post a file to an import endpoint
///
///
/// Exceptions:
/// IllegalArgumentException : if any argument is null, or path is an empty string
/// InvalidRequestException : if there is any problem with the REST API request
Expand Down Expand Up @@ -792,9 +792,63 @@ public virtual T ImportFile<T>(string path, string file, string contentType)
return (T)obj;
}

/// <summary>
/// Posts an imported file to the specified endpoint.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="path">The path.</param>
/// <param name="fileName">Name of the file.</param>
/// <param name="file">The file.</param>
/// <param name="contentType">Type of the content.</param>
/// <returns>T.</returns>
/// <exception cref="SmartsheetException"></exception>
public virtual T ImportFile<T>(string path, string fileName,
byte[] file, string contentType)
{
Utils.ThrowIfNull(path, file, contentType);
Utils.ThrowIfEmpty(path, fileName, contentType);

HttpRequest request = null;
try
{
request = CreateHttpRequest(new Uri(smartsheet.BaseURI, path), HttpMethod.POST);
}
catch (Exception e)
{
throw new SmartsheetException(e);
}

request.Headers["Content-Disposition"] = "attachment";
request.Headers["Content-Type"] = contentType;

HttpEntity entity = new HttpEntity();
entity.ContentType = contentType;
entity.Content = file;
entity.ContentLength = file.Length;
request.Entity = entity;

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

Object obj = null;
switch (response.StatusCode)
{
case HttpStatusCode.OK:
obj = smartsheet.JsonSerializer.deserializeResult<T>(
response.Entity.GetContent()).Result;
break;
default:
HandleError(response);
break;
}

smartsheet.HttpClient.ReleaseConnection();

return (T)obj;
}

/// <summary>
/// Create an HttpRequest.
///
///
/// Exceptions: Any exception shall be propagated since it's a private method.
/// </summary>
/// <param name="uri"> the URI </param>
Expand All @@ -806,7 +860,7 @@ protected internal virtual HttpRequest CreateHttpRequest(Uri uri, HttpMethod met
request.Uri = uri;
request.Method = method;

// Set authorization header
// Set authorization header
request.Headers = new Dictionary<string, string>();
request.Headers["Authorization"] = "Bearer " + smartsheet.AccessToken;

Expand All @@ -827,8 +881,8 @@ protected internal virtual HttpRequest CreateHttpRequest(Uri uri, HttpMethod met

/// <summary>
/// Handles an error HttpResponse (non-200) returned by SmartsheetClient REST API.
///
/// Exceptions:
///
/// Exceptions:
/// SmartsheetRestException : the exception corresponding to the error
/// </summary>
/// <param name="response"> the HttpResponse </param>
Expand Down
Loading