Skip to content

Commit 8dff7bb

Browse files
box-sdk-buildbox-sdk-build
and
box-sdk-build
authored
feat: add UploadWithPreflightCheck convenient method (box/box-codegen#705) (#469)
Co-authored-by: box-sdk-build <[email protected]>
1 parent f6ccb9a commit 8dff7bb

10 files changed

+299
-2
lines changed

.codegen.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{ "engineHash": "5f0371b", "specHash": "f737b7b", "version": "1.9.0" }
1+
{ "engineHash": "525e333", "specHash": "f737b7b", "version": "1.9.0" }

Box.Sdk.Gen.Tests.Integration/Test/Uploads/UploadsManagerTests.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,25 @@ public async System.Threading.Tasks.Task TestRequestCancellation() {
3737
await Assert.That.IsExceptionAsync(async() => await client.Uploads.UploadFileAsync(requestBody: new UploadFileRequestBody(attributes: new UploadFileRequestBodyAttributesField(name: fileName, parent: new UploadFileRequestBodyAttributesParentField(id: "0")), file: fileByteStream), queryParams: new UploadFileQueryParams(), headers: new UploadFileHeaders(), cancellationToken: cancellationToken));
3838
}
3939

40+
[TestMethod]
41+
public async System.Threading.Tasks.Task TestUploadFileWithPreflightCheck() {
42+
string newFileName = Utils.GetUUID();
43+
System.IO.Stream fileContentStream = Utils.GenerateByteStream(size: 1024 * 1024);
44+
await Assert.That.IsExceptionAsync(async() => await client.Uploads.UploadWithPreflightCheckAsync(requestBody: new UploadWithPreflightCheckRequestBody(attributes: new UploadWithPreflightCheckRequestBodyAttributesField(name: newFileName, size: -1, parent: new UploadWithPreflightCheckRequestBodyAttributesParentField(id: "0")), file: fileContentStream)));
45+
Files uploadFilesWithPreflight = await client.Uploads.UploadWithPreflightCheckAsync(requestBody: new UploadWithPreflightCheckRequestBody(attributes: new UploadWithPreflightCheckRequestBodyAttributesField(name: newFileName, size: 1024 * 1024, parent: new UploadWithPreflightCheckRequestBodyAttributesParentField(id: "0")), file: fileContentStream));
46+
FileFull file = NullableUtils.Unwrap(uploadFilesWithPreflight.Entries)[0];
47+
Assert.IsTrue(file.Name == newFileName);
48+
Assert.IsTrue(file.Size == 1024 * 1024);
49+
await Assert.That.IsExceptionAsync(async() => await client.Uploads.UploadWithPreflightCheckAsync(requestBody: new UploadWithPreflightCheckRequestBody(attributes: new UploadWithPreflightCheckRequestBodyAttributesField(name: newFileName, size: 1024 * 1024, parent: new UploadWithPreflightCheckRequestBodyAttributesParentField(id: "0")), file: fileContentStream)));
50+
await client.Files.DeleteFileByIdAsync(fileId: file.Id);
51+
}
52+
53+
[TestMethod]
54+
public async System.Threading.Tasks.Task TestPreflightCheck() {
55+
string newFileName = Utils.GetUUID();
56+
UploadUrl preflightCheckResult = await client.Uploads.PreflightFileUploadCheckAsync(requestBody: new PreflightFileUploadCheckRequestBody() { Name = newFileName, Size = 1024 * 1024, Parent = new PreflightFileUploadCheckRequestBodyParentField() { Id = "0" } });
57+
Assert.IsTrue(preflightCheckResult.UploadUrlField != "");
58+
}
59+
4060
}
4161
}

Box.Sdk.Gen/Managers/Uploads/IUploadsManager.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,5 +79,22 @@ public interface IUploadsManager {
7979
/// </param>
8080
public System.Threading.Tasks.Task<Files> UploadFileAsync(UploadFileRequestBody requestBody, UploadFileQueryParams? queryParams = default, UploadFileHeaders? headers = default, System.Threading.CancellationToken? cancellationToken = null) => throw new System.NotImplementedException("This method needs to be implemented by the derived class before calling it.");
8181

82+
/// <summary>
83+
/// Upload a file with a preflight check
84+
/// </summary>
85+
/// <param name="requestBody">
86+
///
87+
/// </param>
88+
/// <param name="queryParams">
89+
/// Query parameters of uploadFile method
90+
/// </param>
91+
/// <param name="headers">
92+
/// Headers of uploadFile method
93+
/// </param>
94+
/// <param name="cancellationToken">
95+
/// Token used for request cancellation.
96+
/// </param>
97+
public System.Threading.Tasks.Task<Files> UploadWithPreflightCheckAsync(UploadWithPreflightCheckRequestBody requestBody, UploadWithPreflightCheckQueryParams? queryParams = default, UploadWithPreflightCheckHeaders? headers = default, System.Threading.CancellationToken? cancellationToken = null) => throw new System.NotImplementedException("This method needs to be implemented by the derived class before calling it.");
98+
8299
}
83100
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using Box.Sdk.Gen;
2+
using System.Text.Json.Serialization;
3+
using System.Collections.Generic;
4+
using Box.Sdk.Gen.Internal;
5+
using System;
6+
using System.Collections.ObjectModel;
7+
using Box.Sdk.Gen.Schemas;
8+
9+
namespace Box.Sdk.Gen.Managers {
10+
public class UploadWithPreflightCheckHeaders {
11+
/// <summary>
12+
/// An optional header containing the SHA1 hash of the file to
13+
/// ensure that the file was not corrupted in transit.
14+
/// </summary>
15+
public string? ContentMd5 { get; init; }
16+
17+
/// <summary>
18+
/// Extra headers that will be included in the HTTP request.
19+
/// </summary>
20+
public Dictionary<string, string?> ExtraHeaders { get; }
21+
22+
public UploadWithPreflightCheckHeaders(Dictionary<string, string?>? extraHeaders = default) {
23+
ExtraHeaders = extraHeaders ?? new Dictionary<string, string?>() { };
24+
}
25+
}
26+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using Box.Sdk.Gen;
2+
using System.Text.Json.Serialization;
3+
using System.Collections.Generic;
4+
using Box.Sdk.Gen.Internal;
5+
using System;
6+
using System.Collections.ObjectModel;
7+
using Box.Sdk.Gen.Schemas;
8+
9+
namespace Box.Sdk.Gen.Managers {
10+
public class UploadWithPreflightCheckQueryParams {
11+
/// <summary>
12+
/// A comma-separated list of attributes to include in the
13+
/// response. This can be used to request fields that are
14+
/// not normally returned in a standard response.
15+
///
16+
/// Be aware that specifying this parameter will have the
17+
/// effect that none of the standard fields are returned in
18+
/// the response unless explicitly specified, instead only
19+
/// fields for the mini representation are returned, additional
20+
/// to the fields requested.
21+
/// </summary>
22+
public IReadOnlyList<string>? Fields { get; init; }
23+
24+
public UploadWithPreflightCheckQueryParams() {
25+
26+
}
27+
}
28+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using Box.Sdk.Gen;
2+
using System.Text.Json.Serialization;
3+
using System.Collections.Generic;
4+
using Box.Sdk.Gen.Internal;
5+
using System;
6+
using System.Collections.ObjectModel;
7+
using Box.Sdk.Gen.Schemas;
8+
9+
namespace Box.Sdk.Gen.Managers {
10+
public class UploadWithPreflightCheckRequestBody {
11+
public UploadWithPreflightCheckRequestBodyAttributesField Attributes { get; }
12+
13+
/// <summary>
14+
/// The content of the file to upload to Box.
15+
///
16+
/// <Message warning>
17+
///
18+
/// The `attributes` part of the body must come **before** the
19+
/// `file` part. Requests that do not follow this format when
20+
/// uploading the file will receive a HTTP `400` error with a
21+
/// `metadata_after_file_contents` error code.
22+
///
23+
/// </Message>
24+
/// </summary>
25+
public System.IO.Stream File { get; }
26+
27+
public string? FileFileName { get; init; }
28+
29+
public string? FileContentType { get; init; }
30+
31+
public UploadWithPreflightCheckRequestBody(UploadWithPreflightCheckRequestBodyAttributesField attributes, System.IO.Stream file) {
32+
Attributes = attributes;
33+
File = file;
34+
}
35+
}
36+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
using Box.Sdk.Gen;
2+
using System.Text.Json.Serialization;
3+
using System.Collections.Generic;
4+
using Box.Sdk.Gen.Internal;
5+
using System;
6+
using System.Collections.ObjectModel;
7+
using Box.Sdk.Gen.Schemas;
8+
9+
namespace Box.Sdk.Gen.Managers {
10+
public class UploadWithPreflightCheckRequestBodyAttributesField : ISerializable {
11+
/// <summary>
12+
/// The name of the file
13+
/// </summary>
14+
[JsonPropertyName("name")]
15+
public string Name { get; }
16+
17+
/// <summary>
18+
/// The parent folder to upload the file to
19+
/// </summary>
20+
[JsonPropertyName("parent")]
21+
public UploadWithPreflightCheckRequestBodyAttributesParentField Parent { get; }
22+
23+
/// <summary>
24+
/// Defines the time the file was originally created at.
25+
///
26+
/// If not set, the upload time will be used.
27+
/// </summary>
28+
[JsonPropertyName("content_created_at")]
29+
public System.DateTimeOffset? ContentCreatedAt { get; init; }
30+
31+
/// <summary>
32+
/// Defines the time the file was last modified at.
33+
///
34+
/// If not set, the upload time will be used.
35+
/// </summary>
36+
[JsonPropertyName("content_modified_at")]
37+
public System.DateTimeOffset? ContentModifiedAt { get; init; }
38+
39+
/// <summary>
40+
/// The size of the file in bytes
41+
/// </summary>
42+
[JsonPropertyName("size")]
43+
public int Size { get; }
44+
45+
public UploadWithPreflightCheckRequestBodyAttributesField(string name, UploadWithPreflightCheckRequestBodyAttributesParentField parent, int size) {
46+
Name = name;
47+
Parent = parent;
48+
Size = size;
49+
}
50+
internal string? RawJson { get; set; } = default;
51+
52+
void ISerializable.SetJson(string json) {
53+
RawJson = json;
54+
}
55+
56+
string? ISerializable.GetJson() {
57+
return RawJson;
58+
}
59+
60+
/// <summary>
61+
/// Returns raw json response returned from the API.
62+
/// </summary>
63+
public Dictionary<string, object?>? GetRawData() {
64+
return SimpleJsonSerializer.GetAllFields(this);
65+
}
66+
67+
}
68+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using Box.Sdk.Gen;
2+
using System.Text.Json.Serialization;
3+
using System.Collections.Generic;
4+
using Box.Sdk.Gen.Internal;
5+
using System;
6+
using System.Collections.ObjectModel;
7+
using Box.Sdk.Gen.Schemas;
8+
9+
namespace Box.Sdk.Gen.Managers {
10+
public class UploadWithPreflightCheckRequestBodyAttributesParentField : ISerializable {
11+
/// <summary>
12+
/// The id of the parent folder. Use
13+
/// `0` for the user's root folder.
14+
/// </summary>
15+
[JsonPropertyName("id")]
16+
public string Id { get; }
17+
18+
public UploadWithPreflightCheckRequestBodyAttributesParentField(string id) {
19+
Id = id;
20+
}
21+
internal string? RawJson { get; set; } = default;
22+
23+
void ISerializable.SetJson(string json) {
24+
RawJson = json;
25+
}
26+
27+
string? ISerializable.GetJson() {
28+
return RawJson;
29+
}
30+
31+
/// <summary>
32+
/// Returns raw json response returned from the API.
33+
/// </summary>
34+
public Dictionary<string, object?>? GetRawData() {
35+
return SimpleJsonSerializer.GetAllFields(this);
36+
}
37+
38+
}
39+
}

Box.Sdk.Gen/Managers/Uploads/UploadsManager.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,5 +106,33 @@ public async System.Threading.Tasks.Task<Files> UploadFileAsync(UploadFileReques
106106
return SimpleJsonSerializer.Deserialize<Files>(NullableUtils.Unwrap(response.Data));
107107
}
108108

109+
/// <summary>
110+
/// Upload a file with a preflight check
111+
/// </summary>
112+
/// <param name="requestBody">
113+
///
114+
/// </param>
115+
/// <param name="queryParams">
116+
/// Query parameters of uploadFile method
117+
/// </param>
118+
/// <param name="headers">
119+
/// Headers of uploadFile method
120+
/// </param>
121+
/// <param name="cancellationToken">
122+
/// Token used for request cancellation.
123+
/// </param>
124+
public async System.Threading.Tasks.Task<Files> UploadWithPreflightCheckAsync(UploadWithPreflightCheckRequestBody requestBody, UploadWithPreflightCheckQueryParams? queryParams = default, UploadWithPreflightCheckHeaders? headers = default, System.Threading.CancellationToken? cancellationToken = null) {
125+
queryParams = queryParams ?? new UploadWithPreflightCheckQueryParams();
126+
headers = headers ?? new UploadWithPreflightCheckHeaders();
127+
Dictionary<string, string> queryParamsMap = Utils.PrepareParams(map: new Dictionary<string, string?>() { { "fields", StringUtils.ToStringRepresentation(queryParams.Fields) } });
128+
Dictionary<string, string> headersMap = Utils.PrepareParams(map: DictionaryUtils.MergeDictionaries(new Dictionary<string, string?>() { { "content-md5", StringUtils.ToStringRepresentation(headers.ContentMd5) } }, headers.ExtraHeaders));
129+
UploadUrl preflightUploadUrl = await this.PreflightFileUploadCheckAsync(requestBody: new PreflightFileUploadCheckRequestBody() { Name = requestBody.Attributes.Name, Size = requestBody.Attributes.Size, Parent = new PreflightFileUploadCheckRequestBodyParentField() { Id = requestBody.Attributes.Parent.Id } }, headers: new PreflightFileUploadCheckHeaders(extraHeaders: headers.ExtraHeaders), cancellationToken: cancellationToken).ConfigureAwait(false);
130+
if (preflightUploadUrl.UploadUrlField == null || !(NullableUtils.Unwrap(preflightUploadUrl.UploadUrlField).Contains("http"))) {
131+
throw new BoxSdkException(message: "Unable to get preflight upload URL");
132+
}
133+
FetchResponse response = await this.NetworkSession.NetworkClient.FetchAsync(options: new FetchOptions(url: NullableUtils.Unwrap(preflightUploadUrl.UploadUrlField), method: "POST", contentType: "multipart/form-data", responseFormat: Box.Sdk.Gen.ResponseFormat.Json) { Parameters = queryParamsMap, Headers = headersMap, MultipartData = Array.AsReadOnly(new [] {new MultipartItem(partName: "attributes") { Data = SimpleJsonSerializer.Serialize(requestBody.Attributes) },new MultipartItem(partName: "file") { FileStream = requestBody.File, FileName = requestBody.FileFileName, ContentType = requestBody.FileContentType }}), Auth = this.Auth, NetworkSession = this.NetworkSession, CancellationToken = cancellationToken }).ConfigureAwait(false);
134+
return SimpleJsonSerializer.Deserialize<Files>(NullableUtils.Unwrap(response.Data));
135+
}
136+
109137
}
110138
}

docs/Uploads.md

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
- [Upload file version](#upload-file-version)
55
- [Preflight check before upload](#preflight-check-before-upload)
66
- [Upload file](#upload-file)
7+
- [Upload a file with a preflight check](#upload-a-file-with-a-preflight-check)
78

89
## Upload file version
910

@@ -56,7 +57,10 @@ This operation is performed by calling function `PreflightFileUploadCheck`.
5657
See the endpoint docs at
5758
[API Reference](https://developer.box.com/reference/options-files-content/).
5859

59-
*Currently we don't have an example for calling `PreflightFileUploadCheck` in integration tests*
60+
<!-- sample options_files_content -->
61+
```
62+
await client.Uploads.PreflightFileUploadCheckAsync(requestBody: new PreflightFileUploadCheckRequestBody() { Name = newFileName, Size = 1024 * 1024, Parent = new PreflightFileUploadCheckRequestBodyParentField() { Id = "0" } });
63+
```
6064

6165
### Arguments
6266

@@ -115,3 +119,34 @@ This function returns a value of type `Files`.
115119
Returns the new file object in a list.
116120

117121

122+
## Upload a file with a preflight check
123+
124+
Upload a file with a preflight check
125+
126+
This operation is performed by calling function `UploadWithPreflightCheck`.
127+
128+
129+
130+
```
131+
await client.Uploads.UploadWithPreflightCheckAsync(requestBody: new UploadWithPreflightCheckRequestBody(attributes: new UploadWithPreflightCheckRequestBodyAttributesField(name: newFileName, size: -1, parent: new UploadWithPreflightCheckRequestBodyAttributesParentField(id: "0")), file: fileContentStream));
132+
```
133+
134+
### Arguments
135+
136+
- requestBody `UploadWithPreflightCheckRequestBody`
137+
-
138+
- queryParams `UploadWithPreflightCheckQueryParams`
139+
- Query parameters of uploadFile method
140+
- headers `UploadWithPreflightCheckHeaders`
141+
- Headers of uploadFile method
142+
- cancellationToken `System.Threading.CancellationToken?`
143+
- Token used for request cancellation.
144+
145+
146+
### Returns
147+
148+
This function returns a value of type `Files`.
149+
150+
151+
152+

0 commit comments

Comments
 (0)