- Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathUploadSliceRequestBuilder.cs
120 lines (111 loc) · 5.38 KB
/
UploadSliceRequestBuilder.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// ------------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. See License in the project root for license information.
// ------------------------------------------------------------------------------
namespaceMicrosoft.Graph
{
usingSystem;
usingSystem.ComponentModel;
usingSystem.IO;
usingSystem.Net.Http;
usingSystem.Threading;
usingSystem.Threading.Tasks;
usingMicrosoft.Kiota.Abstractions;
usingMicrosoft.Kiota.Abstractions.Serialization;
internalclassUploadSliceRequestBuilder<T>whereT:IParsable,new()
{
privatereadonlyUploadResponseHandlerResponseHandler;
privatereadonlyIRequestAdapterRequestAdapter;
privatereadonlystringUrlTemplate;
/// <summary>
/// The beginning of the slice range to send.
/// </summary>
publiclongRangeBegin
{
get;privateset;
}
/// <summary>
/// The end of the slice range to send.
/// </summary>
publiclongRangeEnd
{
get;privateset;
}
/// <summary>
/// The length in bytes of the session.
/// </summary>
publiclongTotalSessionLength
{
get;privateset;
}
/// <summary>
/// The range length of the slice to send.
/// </summary>
publicintRangeLength=>(int)(this.RangeEnd-this.RangeBegin+1);
/// <summary>
/// Request for uploading one slice of a session
/// </summary>
/// <param name="sessionUrl">URL to upload the slice.</param>
/// <param name="requestAdapter">Client used for sending the slice.</param>
/// <param name="rangeBegin">Beginning of range of this slice</param>
/// <param name="rangeEnd">End of range of this slice</param>
/// <param name="totalSessionLength">Total session length. This MUST be consistent
/// across all slice.</param>
publicUploadSliceRequestBuilder(
stringsessionUrl,
IRequestAdapterrequestAdapter,
longrangeBegin,
longrangeEnd,
longtotalSessionLength)
{
this.UrlTemplate=sessionUrl??thrownewArgumentNullException(nameof(requestAdapter));
this.RequestAdapter=requestAdapter??thrownewArgumentNullException(nameof(requestAdapter));
this.RangeBegin=rangeBegin;
this.RangeEnd=rangeEnd;
this.TotalSessionLength=totalSessionLength;
this.ResponseHandler=newUploadResponseHandler();
}
/// <summary>
/// Uploads the slice using PUT.
/// </summary>
/// <param name="stream">Stream of data to be sent in the request. Length must be equal to the length
/// of this slice (as defined by this.RangeLength)</param>
/// <param name="cancellationToken"><see cref="CancellationToken"/> to use for cancelling requests</param>
/// <returns>The status of the upload. If UploadSession.AdditionalData.ContainsKey("successResponse")
/// is true, then the item has completed, and the value is the created item from the server.</returns>
publicasyncTask<UploadResult<T>>PutAsync(Streamstream,CancellationTokencancellationToken=default)
{
varrequestInformation=this.CreatePutRequestInformation(stream);
varnativeResponseHandler=newNativeResponseHandler();
requestInformation.SetResponseHandler(nativeResponseHandler);
awaitthis.RequestAdapter.SendNoContentAsync(requestInformation,cancellationToken:cancellationToken).ConfigureAwait(false);
returnawaitthis.ResponseHandler.HandleResponseAsync<T>(nativeResponseHandler.ValueasHttpResponseMessage).ConfigureAwait(false);
}
/// <summary>
/// Create <see cref="RequestInformation"/> instance to upload the file slice
/// <param name="stream">The <see cref="Stream"/> to upload</param>
/// </summary>
[Obsolete("Use CreatePutRequestInformation instead")]
[EditorBrowsable(EditorBrowsableState.Never)]
#pragma warning disable VSTHRD200// Use "Async" suffix for async methods
publicRequestInformationCreatePutRequestInformationAsync(Streamstream)=>CreatePutRequestInformation(stream);
#pragma warning restore VSTHRD200// Use "Async" suffix for async methods
/// <summary>
/// Create <see cref="RequestInformation"/> instance to upload the file slice
/// <param name="stream">The <see cref="Stream"/> to upload</param>
/// </summary>
publicRequestInformationCreatePutRequestInformation(Streamstream)
{
_=stream??thrownewArgumentNullException(nameof(stream));
varrequestInfo=newRequestInformation
{
HttpMethod=Method.PUT,
UrlTemplate=UrlTemplate,
};
requestInfo.SetStreamContent(stream,binaryContentType);
requestInfo.Headers.Add("Content-Range",$"bytes {this.RangeBegin}-{this.RangeEnd}/{this.TotalSessionLength}");
requestInfo.Headers.Add("Content-Length",$"{this.RangeLength}");
returnrequestInfo;
}
privateconststringbinaryContentType="application/octet-stream";
}
}