-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
FileUploadResultOperation.cs
169 lines (148 loc) · 4.54 KB
/
FileUploadResultOperation.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
#nullable disable
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using Azure.Core;
namespace Azure.Developer.LoadTesting
{
/// <summary>
/// FileUploadResultOperation.
/// </summary>
public class FileUploadResultOperation : Operation<BinaryData>
{
private bool _completed;
private Response _response;
private BinaryData _value;
private readonly string _testId;
private readonly string _fileName;
private readonly LoadTestAdministrationClient _client;
private readonly List<string> _terminalStatus = new()
{
"VALIDATION_SUCCESS",
"VALIDATION_FAILURE",
"VALIDATION_NOT_REQUIRED"
};
/// <summary>
/// Value.
/// </summary>
public override BinaryData Value
{
get
{
if (HasCompleted && !HasValue)
{
throw new InvalidOperationException("The operation is not complete.");
}
else
{
return _value;
}
}
}
/// <summary>
/// HasValue
/// </summary>
public override bool HasValue => _completed;
/// <summary>
/// Id.
/// </summary>
public override string Id { get; }
/// <summary>
/// HasCompleted
/// </summary>
public override bool HasCompleted => _completed;
/// <summary>
/// FileUploadOperation.
/// </summary>
protected FileUploadResultOperation() { }
/// <summary>
/// FileUploadOperation.
/// </summary>
public FileUploadResultOperation(string testId, string fileName, LoadTestAdministrationClient client, Response initialResponse = null)
{
_testId = testId;
_fileName = fileName;
_client = client;
Id = $"{_testId}/{_fileName}";
_completed = false;
if (initialResponse != null)
{
_response = initialResponse;
_value = _response.Content;
GetCompletionResponse();
}
}
/// <summary>
/// GetRawResponse.
/// </summary>
public override Response GetRawResponse()
{
return _response;
}
/// <summary>
/// UpdateStatus.
/// </summary>
public override Response UpdateStatus(CancellationToken cancellationToken = default)
{
if (_completed)
{
return GetRawResponse();
}
_response = _client.GetTestFile(_testId, _fileName);
_value = _response.Content;
return GetCompletionResponse();
}
/// <summary>
/// UpdateStatusAsync.
/// </summary>
public override async ValueTask<Response> UpdateStatusAsync(CancellationToken cancellationToken = default)
{
if (_completed)
{
return GetRawResponse();
}
try
{
_response = await _client.GetTestFileAsync(_testId, _fileName).ConfigureAwait(false);
_value = _response.Content;
}
catch
{
throw new RequestFailedException(_response);
}
return GetCompletionResponse();
}
private Response GetCompletionResponse()
{
string fileValidationStatus;
JsonDocument jsonDocument;
try
{
jsonDocument = JsonDocument.Parse(_value.ToMemory());
}
catch (Exception e)
{
throw new RequestFailedException("Unable to parse JOSN " + e.Message);
}
try
{
fileValidationStatus = jsonDocument.RootElement.GetProperty("validationStatus").GetString();
}
catch
{
throw new RequestFailedException("No property validationStatus in reposne JSON: " + _value.ToString());
}
if (_terminalStatus.Contains(fileValidationStatus))
{
_completed = true;
}
return GetRawResponse();
}
}
}