Skip to content

Commit 57747d5

Browse files
fix: Support status codes with no content (box/box-codegen#604) (#314)
1 parent 09a0db0 commit 57747d5

File tree

18 files changed

+68
-33
lines changed

18 files changed

+68
-33
lines changed

.codegen.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{ "engineHash": "513fdf8", "specHash": "c2c76f3", "version": "1.4.0" }
1+
{ "engineHash": "37e1577", "specHash": "c2c76f3", "version": "1.4.0" }

Box.Sdk.Gen.Tests.Integration/Test/ChunkedUploads/ChunkedUploadsManagerTests.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
using Box.Sdk.Gen.Internal;
55
using System.Linq;
66
using Microsoft.VisualStudio.TestTools.UnitTesting;
7-
using Box.Sdk.Gen.Managers;
87
using Box.Sdk.Gen;
8+
using Box.Sdk.Gen.Managers;
99
using Box.Sdk.Gen.Schemas;
1010

1111
namespace Box.Sdk.Gen.Tests.Integration {
@@ -60,8 +60,8 @@ public async System.Threading.Tasks.Task TestChunkedManualProcessById() {
6060
Assert.IsTrue(NullableUtils.Unwrap(processedSession.Id) == uploadSessionId);
6161
string sha1 = await fileHash.DigestHashAsync(encoding: "base64");
6262
string digest = string.Concat("sha=", sha1);
63-
Files committedSession = await client.ChunkedUploads.CreateFileUploadSessionCommitAsync(uploadSessionId: uploadSessionId, requestBody: new CreateFileUploadSessionCommitRequestBody(parts: parts), headers: new CreateFileUploadSessionCommitHeaders(digest: digest));
64-
Assert.IsTrue(NullableUtils.Unwrap(NullableUtils.Unwrap(committedSession.Entries)[0].Name) == fileName);
63+
Files? committedSession = await client.ChunkedUploads.CreateFileUploadSessionCommitAsync(uploadSessionId: uploadSessionId, requestBody: new CreateFileUploadSessionCommitRequestBody(parts: parts), headers: new CreateFileUploadSessionCommitHeaders(digest: digest));
64+
Assert.IsTrue(NullableUtils.Unwrap(NullableUtils.Unwrap(NullableUtils.Unwrap(committedSession).Entries)[0].Name) == fileName);
6565
await client.ChunkedUploads.DeleteFileUploadSessionByIdAsync(uploadSessionId: uploadSessionId);
6666
}
6767

@@ -114,8 +114,8 @@ public async System.Threading.Tasks.Task TestChunkedManualProcessByUrl() {
114114
Assert.IsTrue(NullableUtils.Unwrap(processedSession.Id) == uploadSessionId);
115115
string sha1 = await fileHash.DigestHashAsync(encoding: "base64");
116116
string digest = string.Concat("sha=", sha1);
117-
Files committedSession = await client.ChunkedUploads.CreateFileUploadSessionCommitByUrlAsync(url: commitUrl, requestBody: new CreateFileUploadSessionCommitByUrlRequestBody(parts: parts), headers: new CreateFileUploadSessionCommitByUrlHeaders(digest: digest));
118-
Assert.IsTrue(NullableUtils.Unwrap(NullableUtils.Unwrap(committedSession.Entries)[0].Name) == fileName);
117+
Files? committedSession = await client.ChunkedUploads.CreateFileUploadSessionCommitByUrlAsync(url: commitUrl, requestBody: new CreateFileUploadSessionCommitByUrlRequestBody(parts: parts), headers: new CreateFileUploadSessionCommitByUrlHeaders(digest: digest));
118+
Assert.IsTrue(NullableUtils.Unwrap(NullableUtils.Unwrap(NullableUtils.Unwrap(committedSession).Entries)[0].Name) == fileName);
119119
await client.ChunkedUploads.DeleteFileUploadSessionByUrlAsync(url: abortUrl);
120120
}
121121

Box.Sdk.Gen.Tests.Integration/Test/Downloads/DownloadsManagerTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ public async System.Threading.Tasks.Task TestDownloadFile() {
1919
System.IO.Stream fileContentStream = Utils.GenerateByteStreamFromBuffer(buffer: fileBuffer);
2020
Files uploadedFiles = await client.Uploads.UploadFileAsync(requestBody: new UploadFileRequestBody(attributes: new UploadFileRequestBodyAttributesField(name: newFileName, parent: new UploadFileRequestBodyAttributesParentField(id: "0")), file: fileContentStream));
2121
FileFull uploadedFile = NullableUtils.Unwrap(uploadedFiles.Entries)[0];
22-
System.IO.Stream downloadedFileContent = await client.Downloads.DownloadFileAsync(fileId: uploadedFile.Id);
23-
Assert.IsTrue(Utils.BufferEquals(buffer1: await Utils.ReadByteStreamAsync(byteStream: downloadedFileContent), buffer2: fileBuffer));
22+
System.IO.Stream? downloadedFileContent = await client.Downloads.DownloadFileAsync(fileId: uploadedFile.Id);
23+
Assert.IsTrue(Utils.BufferEquals(buffer1: await Utils.ReadByteStreamAsync(byteStream: NullableUtils.Unwrap(downloadedFileContent)), buffer2: fileBuffer));
2424
await client.Files.DeleteFileByIdAsync(fileId: uploadedFile.Id);
2525
}
2626

Box.Sdk.Gen.Tests.Integration/Test/Files/FilesManagerTests.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
using Box.Sdk.Gen.Internal;
22
using Microsoft.VisualStudio.TestTools.UnitTesting;
3+
using Box.Sdk.Gen;
34
using System;
45
using System.Collections.ObjectModel;
56
using System.Collections.Generic;
6-
using Box.Sdk.Gen;
77
using Box.Sdk.Gen.Schemas;
88
using Box.Sdk.Gen.Managers;
99

@@ -25,7 +25,8 @@ public async System.Threading.Tasks.Task TestGetFileThumbnail() {
2525
string thumbnailFileName = Utils.GetUUID();
2626
System.IO.Stream thumbnailContentStream = Utils.GenerateByteStream(size: 1024 * 1024);
2727
FileFull thumbnailFile = await UploadFileAsync(fileName: thumbnailFileName, fileStream: thumbnailContentStream);
28-
Assert.IsTrue(Utils.BufferEquals(buffer1: await Utils.ReadByteStreamAsync(byteStream: await client.Files.GetFileThumbnailByIdAsync(fileId: thumbnailFile.Id, extension: GetFileThumbnailByIdExtension.Png)), buffer2: await Utils.ReadByteStreamAsync(byteStream: thumbnailContentStream)) != true);
28+
System.IO.Stream? thumbnail = await client.Files.GetFileThumbnailByIdAsync(fileId: thumbnailFile.Id, extension: GetFileThumbnailByIdExtension.Png);
29+
Assert.IsTrue(Utils.BufferEquals(buffer1: await Utils.ReadByteStreamAsync(byteStream: NullableUtils.Unwrap(thumbnail)), buffer2: await Utils.ReadByteStreamAsync(byteStream: thumbnailContentStream)) != true);
2930
await client.Files.DeleteFileByIdAsync(fileId: thumbnailFile.Id);
3031
}
3132

Box.Sdk.Gen.Tests.Integration/Test/UserCollaborations/UserCollaborationsManagerTests.cs

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using Microsoft.VisualStudio.TestTools.UnitTesting;
22
using Box.Sdk.Gen.Internal;
3-
using System;
43
using Box.Sdk.Gen;
4+
using System;
55
using Box.Sdk.Gen.Schemas;
66
using Box.Sdk.Gen.Managers;
77

@@ -27,14 +27,33 @@ public async System.Threading.Tasks.Task TestUserCollaborations() {
2727
Assert.IsTrue(StringUtils.ToStringRepresentation(NullableUtils.Unwrap(collaborationFromApi.Status)) == "accepted");
2828
Assert.IsTrue(StringUtils.ToStringRepresentation(collaborationFromApi.Type?.Value) == "collaboration");
2929
Assert.IsTrue(collaborationFromApi.InviteEmail == null);
30-
Collaboration updatedCollaboration = await client.UserCollaborations.UpdateCollaborationByIdAsync(collaborationId: collaborationId, requestBody: new UpdateCollaborationByIdRequestBody(role: UpdateCollaborationByIdRequestBodyRoleField.Viewer));
31-
Assert.IsTrue(StringUtils.ToStringRepresentation(NullableUtils.Unwrap(updatedCollaboration.Role)) == "viewer");
30+
Collaboration? updatedCollaboration = await client.UserCollaborations.UpdateCollaborationByIdAsync(collaborationId: collaborationId, requestBody: new UpdateCollaborationByIdRequestBody(role: UpdateCollaborationByIdRequestBodyRoleField.Viewer));
31+
Assert.IsTrue(StringUtils.ToStringRepresentation(NullableUtils.Unwrap(NullableUtils.Unwrap(updatedCollaboration).Role)) == "viewer");
3232
await client.UserCollaborations.DeleteCollaborationByIdAsync(collaborationId: collaborationId);
3333
await Assert.That.IsExceptionAsync(async() => await client.UserCollaborations.GetCollaborationByIdAsync(collaborationId: collaborationId));
3434
await client.Folders.DeleteFolderByIdAsync(folderId: folder.Id);
3535
await client.Users.DeleteUserByIdAsync(userId: user.Id);
3636
}
3737

38+
[TestMethod]
39+
public async System.Threading.Tasks.Task TestConvertingUserCollaborationToOwnership() {
40+
string userName = Utils.GetUUID();
41+
string userLogin = string.Concat(Utils.GetUUID(), "@gmail.com");
42+
UserFull user = await client.Users.CreateUserAsync(requestBody: new CreateUserRequestBody(name: userName) { Login = userLogin, IsPlatformAccessOnly = true });
43+
FolderFull folder = await new CommonsManager().CreateNewFolderAsync();
44+
Collaboration collaboration = await client.UserCollaborations.CreateCollaborationAsync(requestBody: new CreateCollaborationRequestBody(item: new CreateCollaborationRequestBodyItemField() { Type = CreateCollaborationRequestBodyItemTypeField.Folder, Id = folder.Id }, accessibleBy: new CreateCollaborationRequestBodyAccessibleByField(type: CreateCollaborationRequestBodyAccessibleByTypeField.User) { Id = user.Id }, role: CreateCollaborationRequestBodyRoleField.Editor));
45+
Assert.IsTrue(StringUtils.ToStringRepresentation(NullableUtils.Unwrap(collaboration.Role)) == "editor");
46+
Collaboration? ownerCollaboration = await client.UserCollaborations.UpdateCollaborationByIdAsync(collaborationId: collaboration.Id, requestBody: new UpdateCollaborationByIdRequestBody(role: UpdateCollaborationByIdRequestBodyRoleField.Owner));
47+
Assert.IsTrue(ownerCollaboration == null);
48+
Collaborations folderCollaborations = await client.ListCollaborations.GetFolderCollaborationsAsync(folderId: folder.Id);
49+
Collaboration folderCollaboration = NullableUtils.Unwrap(folderCollaborations.Entries)[0];
50+
await client.UserCollaborations.DeleteCollaborationByIdAsync(collaborationId: folderCollaboration.Id);
51+
BoxClient userClient = client.WithAsUserHeader(userId: user.Id);
52+
await userClient.Folders.DeleteFolderByIdAsync(folderId: folder.Id);
53+
await userClient.TrashedFolders.DeleteTrashedFolderByIdAsync(folderId: folder.Id);
54+
await client.Users.DeleteUserByIdAsync(userId: user.Id);
55+
}
56+
3857
[TestMethod]
3958
public async System.Threading.Tasks.Task TestExternalUserCollaborations() {
4059
string userName = Utils.GetUUID();
@@ -48,8 +67,8 @@ public async System.Threading.Tasks.Task TestExternalUserCollaborations() {
4867
Assert.IsTrue(StringUtils.ToStringRepresentation(NullableUtils.Unwrap(collaborationFromApi.Status)) == "pending");
4968
Assert.IsTrue(StringUtils.ToStringRepresentation(collaborationFromApi.Type?.Value) == "collaboration");
5069
Assert.IsTrue(collaborationFromApi.InviteEmail == userLogin);
51-
Collaboration updatedCollaboration = await client.UserCollaborations.UpdateCollaborationByIdAsync(collaborationId: collaborationId, requestBody: new UpdateCollaborationByIdRequestBody(role: UpdateCollaborationByIdRequestBodyRoleField.Viewer));
52-
Assert.IsTrue(StringUtils.ToStringRepresentation(NullableUtils.Unwrap(updatedCollaboration.Role)) == "viewer");
70+
Collaboration? updatedCollaboration = await client.UserCollaborations.UpdateCollaborationByIdAsync(collaborationId: collaborationId, requestBody: new UpdateCollaborationByIdRequestBody(role: UpdateCollaborationByIdRequestBodyRoleField.Viewer));
71+
Assert.IsTrue(StringUtils.ToStringRepresentation(NullableUtils.Unwrap(NullableUtils.Unwrap(updatedCollaboration).Role)) == "viewer");
5372
await client.UserCollaborations.DeleteCollaborationByIdAsync(collaborationId: collaborationId);
5473
await Assert.That.IsExceptionAsync(async() => await client.UserCollaborations.GetCollaborationByIdAsync(collaborationId: collaborationId));
5574
await client.Folders.DeleteFolderByIdAsync(folderId: folder.Id);

Box.Sdk.Gen/Managers/ChunkedUploads/ChunkedUploadsManager.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -286,9 +286,12 @@ public async System.Threading.Tasks.Task<UploadParts> GetFileUploadSessionPartsA
286286
/// <param name="cancellationToken">
287287
/// Token used for request cancellation.
288288
/// </param>
289-
public async System.Threading.Tasks.Task<Files> CreateFileUploadSessionCommitByUrlAsync(string url, CreateFileUploadSessionCommitByUrlRequestBody requestBody, CreateFileUploadSessionCommitByUrlHeaders headers, System.Threading.CancellationToken? cancellationToken = null) {
289+
public async System.Threading.Tasks.Task<Files?> CreateFileUploadSessionCommitByUrlAsync(string url, CreateFileUploadSessionCommitByUrlRequestBody requestBody, CreateFileUploadSessionCommitByUrlHeaders headers, System.Threading.CancellationToken? cancellationToken = null) {
290290
Dictionary<string, string> headersMap = Utils.PrepareParams(map: DictionaryUtils.MergeDictionaries(new Dictionary<string, string?>() { { "digest", StringUtils.ToStringRepresentation(headers.Digest) }, { "if-match", StringUtils.ToStringRepresentation(headers.IfMatch) }, { "if-none-match", StringUtils.ToStringRepresentation(headers.IfNoneMatch) } }, headers.ExtraHeaders));
291291
FetchResponse response = await HttpClientAdapter.FetchAsync(new FetchOptions(url: url, networkSession: this.NetworkSession) { Method = "POST", Headers = headersMap, Data = SimpleJsonSerializer.Serialize(requestBody), ContentType = "application/json", ResponseFormat = "json", Auth = this.Auth, CancellationToken = cancellationToken }).ConfigureAwait(false);
292+
if (StringUtils.ToStringRepresentation(response.Status) == "202") {
293+
return null;
294+
}
292295
return SimpleJsonSerializer.Deserialize<Files>(response.Data);
293296
}
294297

@@ -311,9 +314,12 @@ public async System.Threading.Tasks.Task<Files> CreateFileUploadSessionCommitByU
311314
/// <param name="cancellationToken">
312315
/// Token used for request cancellation.
313316
/// </param>
314-
public async System.Threading.Tasks.Task<Files> CreateFileUploadSessionCommitAsync(string uploadSessionId, CreateFileUploadSessionCommitRequestBody requestBody, CreateFileUploadSessionCommitHeaders headers, System.Threading.CancellationToken? cancellationToken = null) {
317+
public async System.Threading.Tasks.Task<Files?> CreateFileUploadSessionCommitAsync(string uploadSessionId, CreateFileUploadSessionCommitRequestBody requestBody, CreateFileUploadSessionCommitHeaders headers, System.Threading.CancellationToken? cancellationToken = null) {
315318
Dictionary<string, string> headersMap = Utils.PrepareParams(map: DictionaryUtils.MergeDictionaries(new Dictionary<string, string?>() { { "digest", StringUtils.ToStringRepresentation(headers.Digest) }, { "if-match", StringUtils.ToStringRepresentation(headers.IfMatch) }, { "if-none-match", StringUtils.ToStringRepresentation(headers.IfNoneMatch) } }, headers.ExtraHeaders));
316319
FetchResponse response = await HttpClientAdapter.FetchAsync(new FetchOptions(url: string.Concat(this.NetworkSession.BaseUrls.UploadUrl, "/2.0/files/upload_sessions/", StringUtils.ToStringRepresentation(uploadSessionId), "/commit"), networkSession: this.NetworkSession) { Method = "POST", Headers = headersMap, Data = SimpleJsonSerializer.Serialize(requestBody), ContentType = "application/json", ResponseFormat = "json", Auth = this.Auth, CancellationToken = cancellationToken }).ConfigureAwait(false);
320+
if (StringUtils.ToStringRepresentation(response.Status) == "202") {
321+
return null;
322+
}
317323
return SimpleJsonSerializer.Deserialize<Files>(response.Data);
318324
}
319325

@@ -386,8 +392,8 @@ public async System.Threading.Tasks.Task<FileFull> UploadBigFileAsync(System.IO.
386392
}
387393
string sha1 = await fileHash.DigestHashAsync(encoding: "base64").ConfigureAwait(false);
388394
string digest = string.Concat("sha=", sha1);
389-
Files committedSession = await this.CreateFileUploadSessionCommitByUrlAsync(url: commitUrl, requestBody: new CreateFileUploadSessionCommitByUrlRequestBody(parts: parts), headers: new CreateFileUploadSessionCommitByUrlHeaders(digest: digest), cancellationToken: cancellationToken).ConfigureAwait(false);
390-
return NullableUtils.Unwrap(committedSession.Entries)[0];
395+
Files? committedSession = await this.CreateFileUploadSessionCommitByUrlAsync(url: commitUrl, requestBody: new CreateFileUploadSessionCommitByUrlRequestBody(parts: parts), headers: new CreateFileUploadSessionCommitByUrlHeaders(digest: digest), cancellationToken: cancellationToken).ConfigureAwait(false);
396+
return NullableUtils.Unwrap(NullableUtils.Unwrap(committedSession).Entries)[0];
391397
}
392398

393399
}

Box.Sdk.Gen/Managers/ChunkedUploads/IChunkedUploadsManager.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ public interface IChunkedUploadsManager {
228228
/// <param name="cancellationToken">
229229
/// Token used for request cancellation.
230230
/// </param>
231-
public System.Threading.Tasks.Task<Files> CreateFileUploadSessionCommitByUrlAsync(string url, CreateFileUploadSessionCommitByUrlRequestBody requestBody, CreateFileUploadSessionCommitByUrlHeaders headers, System.Threading.CancellationToken? cancellationToken = null) => throw new System.NotImplementedException("This method needs to be implemented by the derived class before calling it.");
231+
public System.Threading.Tasks.Task<Files?> CreateFileUploadSessionCommitByUrlAsync(string url, CreateFileUploadSessionCommitByUrlRequestBody requestBody, CreateFileUploadSessionCommitByUrlHeaders headers, System.Threading.CancellationToken? cancellationToken = null) => throw new System.NotImplementedException("This method needs to be implemented by the derived class before calling it.");
232232

233233
/// <summary>
234234
/// Close an upload session and create a file from the uploaded chunks.
@@ -249,7 +249,7 @@ public interface IChunkedUploadsManager {
249249
/// <param name="cancellationToken">
250250
/// Token used for request cancellation.
251251
/// </param>
252-
public System.Threading.Tasks.Task<Files> CreateFileUploadSessionCommitAsync(string uploadSessionId, CreateFileUploadSessionCommitRequestBody requestBody, CreateFileUploadSessionCommitHeaders headers, System.Threading.CancellationToken? cancellationToken = null) => throw new System.NotImplementedException("This method needs to be implemented by the derived class before calling it.");
252+
public System.Threading.Tasks.Task<Files?> CreateFileUploadSessionCommitAsync(string uploadSessionId, CreateFileUploadSessionCommitRequestBody requestBody, CreateFileUploadSessionCommitHeaders headers, System.Threading.CancellationToken? cancellationToken = null) => throw new System.NotImplementedException("This method needs to be implemented by the derived class before calling it.");
253253

254254
/// <summary>
255255
/// Starts the process of chunk uploading a big file. Should return a File object representing uploaded file.

Box.Sdk.Gen/Managers/Downloads/DownloadsManager.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,15 @@ public DownloadsManager(NetworkSession? networkSession = default) {
3535
/// <param name="cancellationToken">
3636
/// Token used for request cancellation.
3737
/// </param>
38-
public async System.Threading.Tasks.Task<System.IO.Stream> DownloadFileAsync(string fileId, DownloadFileQueryParams? queryParams = default, DownloadFileHeaders? headers = default, System.Threading.CancellationToken? cancellationToken = null) {
38+
public async System.Threading.Tasks.Task<System.IO.Stream?> DownloadFileAsync(string fileId, DownloadFileQueryParams? queryParams = default, DownloadFileHeaders? headers = default, System.Threading.CancellationToken? cancellationToken = null) {
3939
queryParams = queryParams ?? new DownloadFileQueryParams();
4040
headers = headers ?? new DownloadFileHeaders();
4141
Dictionary<string, string> queryParamsMap = Utils.PrepareParams(map: new Dictionary<string, string?>() { { "version", StringUtils.ToStringRepresentation(queryParams.Version) }, { "access_token", StringUtils.ToStringRepresentation(queryParams.AccessTokenField) } });
4242
Dictionary<string, string> headersMap = Utils.PrepareParams(map: DictionaryUtils.MergeDictionaries(new Dictionary<string, string?>() { { "range", StringUtils.ToStringRepresentation(headers.Range) }, { "boxapi", StringUtils.ToStringRepresentation(headers.Boxapi) } }, headers.ExtraHeaders));
4343
FetchResponse response = await HttpClientAdapter.FetchAsync(new FetchOptions(url: string.Concat(this.NetworkSession.BaseUrls.BaseUrl, "/2.0/files/", StringUtils.ToStringRepresentation(fileId), "/content"), networkSession: this.NetworkSession) { Method = "GET", Parameters = queryParamsMap, Headers = headersMap, ResponseFormat = "binary", Auth = this.Auth, CancellationToken = cancellationToken }).ConfigureAwait(false);
44+
if (StringUtils.ToStringRepresentation(response.Status) == "202") {
45+
return null;
46+
}
4447
return response.Content;
4548
}
4649

0 commit comments

Comments
 (0)