-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
GetRouteMatrixOperation.cs
267 lines (232 loc) · 10.7 KB
/
GetRouteMatrixOperation.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using Azure.Core;
using Azure.Core.Pipeline;
namespace Azure.Maps.Routing.Models
{
/// <summary>
/// An <see cref="Operation{RouteMatrixResult}"/> for tracking the status of a
/// <see cref="MapsRoutingClient.GetRouteMatrix(WaitUntil, RouteMatrixOptions, CancellationToken)"/>
/// request. Its <see cref="Operation{RouteMatrixResult}.Value"/> upon successful
/// completion will be the route matrix result.
/// </summary>
public class GetRouteMatrixOperation : Operation<RouteMatrixResult>
{
/// <summary>
/// The client used to check for completion.
/// </summary>
private readonly MapsRoutingClient _client;
/// <summary>
/// The CancellationToken to use for all status checking.
/// </summary>
private readonly CancellationToken _cancellationToken;
/// <summary>
/// Whether the operation has completed.
/// </summary>
private bool _hasCompleted;
/// <summary>
/// Gets the route matrix batch result.
/// </summary>
private RouteMatrixResult _value;
private Response _rawResponse;
/// <summary>
/// The request ID used in route matrix request
/// </summary>
private string _id;
/// <summary>
/// Gets a value indicating whether the operation has completed.
/// </summary>
public override bool HasCompleted => _hasCompleted;
/// <summary>
/// Indicating whether the operation completed and
/// successfully produced a value. The <see cref="Operation{RouteMatrixResult}.Value"/>
/// property is the route matrix result.
/// </summary>
public override bool HasValue => _value != null;
/// <inheritdoc />
public override string Id => _id;
/// <summary>
/// Gets the will be the route matrix result.
/// </summary>
public override RouteMatrixResult Value => _value;
/// <inheritdoc />
public override Response GetRawResponse() => _rawResponse;
/// <inheritdoc />
public override async ValueTask<Response<RouteMatrixResult>> WaitForCompletionAsync(CancellationToken cancellationToken = default) =>
await WaitForCompletionAsync(true, cancellationToken).ConfigureAwait(false);
/// <inheritdoc />
public override async ValueTask<Response<RouteMatrixResult>> WaitForCompletionAsync(TimeSpan pollingInterval, CancellationToken cancellationToken) =>
await WaitForCompletionAsync(true, cancellationToken).ConfigureAwait(false);
/// <inheritdoc />
public override Response<RouteMatrixResult> WaitForCompletion(CancellationToken cancellationToken = default) =>
WaitForCompletionAsync(false, cancellationToken).EnsureCompleted();
/// <inheritdoc />
public override Response<RouteMatrixResult> WaitForCompletion(TimeSpan pollingInterval, CancellationToken cancellationToken) =>
WaitForCompletionAsync(false, cancellationToken).EnsureCompleted();
/// <inheritdoc />
private async ValueTask<Response<RouteMatrixResult>> WaitForCompletionAsync(bool async, CancellationToken cancellationToken = default)
{
ResponseWithHeaders<RouteGetRouteMatrixHeaders> update = async
? await _client.RestClient.GetRouteMatrixAsync(_id, cancellationToken).ConfigureAwait(false)
: _client.RestClient.GetRouteMatrix(_id, cancellationToken);
Response response = update.GetRawResponse();
// Check if the operation is no longer running
if (update.Headers.Location == null)
{
_hasCompleted = true;
using var document = JsonDocument.Parse(response.ContentStream);
_value = RouteMatrixResult.DeserializeRouteMatrixResult(document.RootElement);
return Response.FromValue(_value, response);
}
else
{
var uri = new Uri(update.Headers.Location);
var paths = uri.AbsolutePath.Split('/');
_id = paths[paths.Length - 1];
}
RouteMatrixResult result = null;
return Response.FromValue(result, response);
}
/// <summary>
/// Initializes a new <see cref="GetRouteMatrixOperation"/> instance for
/// mocking.
/// </summary>
protected GetRouteMatrixOperation()
{
}
/// <summary>
/// Initializes a new <see cref="GetRouteMatrixOperation"/> instance
/// </summary>
/// <param name="client"> The client used to check for completion. </param>
/// <param name="followUpUrl"> Follow up URL for route matrix operation. </param>
internal GetRouteMatrixOperation(MapsRoutingClient client, Uri followUpUrl)
{
Argument.AssertNotNull(client, nameof(client));
Argument.AssertNotNull(followUpUrl, nameof(followUpUrl));
try
{
var paths = followUpUrl.AbsolutePath.Split('/');
_id = paths[paths.Length - 1];
}
catch (Exception ex)
{
if (ex is FormatException || ex is UriFormatException)
{
throw new FormatException("Invalid ID", ex);
}
}
_value = null;
_rawResponse = null;
_client = client;
_cancellationToken = default;
}
/// <summary>
/// Initializes a new <see cref="GetRouteMatrixOperation"/> instance
/// </summary>
/// <param name="client"> The client used to check for completion. </param>
/// <param name="id"> An ID representing a specific operation.</param>
/// <exception cref="ArgumentNullException"> <paramref name="client"/> or <paramref name="id"/> is null. </exception>
/// <exception cref="FormatException"> <paramref name="id"/> format error. </exception>
public GetRouteMatrixOperation(MapsRoutingClient client, string id) :
this(client, id, null)
{
}
/// <summary>
/// Initializes a new <see cref="GetRouteMatrixOperation"/> instance
/// </summary>
/// <param name="client"> The client used to check for completion. </param>
/// <param name="id"> An ID representing a specific operation.</param>
/// <param name="initialResponse">
/// Either the response from initiating the operation or getting the
/// status if we're creating an operation from an existing ID.
/// </param>
/// <param name="cancellationToken">
/// Optional <see cref="CancellationToken"/> to propagate
/// notifications that the operation should be cancelled.
/// </param>
/// <exception cref="ArgumentNullException"> <paramref name="client"/> or <paramref name="id"/> is null. </exception>
internal GetRouteMatrixOperation(
MapsRoutingClient client,
string id,
Response initialResponse,
CancellationToken cancellationToken = default)
{
Argument.AssertNotNull(client, nameof(client));
Argument.AssertNotNull(id, nameof(id));
_id = id;
_value = null;
_rawResponse = initialResponse;
_client = client;
_cancellationToken = cancellationToken;
}
/// <summary>
/// Check for the latest status of the route matrix calculation operation.
/// </summary>
/// <param name="cancellationToken">
/// Optional <see cref="CancellationToken"/> to propagate
/// notifications that the operation should be cancelled.
/// </param>
/// <returns>The <see cref="Response"/> with the status update.</returns>
public override Response UpdateStatus(
CancellationToken cancellationToken = default) =>
UpdateStatusAsync(false, cancellationToken).EnsureCompleted();
/// <summary>
/// Check for the latest status of the route matrix calculation operation.
/// </summary>
/// <param name="cancellationToken">
/// Optional <see cref="CancellationToken"/> to propagate
/// notifications that the operation should be cancelled.
/// </param>
/// <returns>The <see cref="Response"/> with the status update.</returns>
public override async ValueTask<Response> UpdateStatusAsync(
CancellationToken cancellationToken = default) =>
await UpdateStatusAsync(true, cancellationToken).ConfigureAwait(false);
/// <summary>
/// Check for the latest status of the route matrix calculation operation.
/// </summary>
/// <param name="cancellationToken">
/// Optional <see cref="CancellationToken"/> to propagate
/// notifications that the operation should be cancelled.
/// </param>
/// <param name="async" />
/// <returns>The <see cref="Response"/> with the status update.</returns>
private async Task<Response> UpdateStatusAsync(bool async, CancellationToken cancellationToken)
{
// Short-circuit when already completed (which improves mocking
// scenarios that won't have a client).
if (HasCompleted)
{
return GetRawResponse();
}
// Use our original CancellationToken if the user didn't provide one
if (cancellationToken == default)
{
cancellationToken = _cancellationToken;
}
// Get the latest status
ResponseWithHeaders<RouteGetRouteMatrixHeaders> update = async
? await _client.RestClient.GetRouteMatrixAsync(_id, cancellationToken).ConfigureAwait(false)
: _client.RestClient.GetRouteMatrix(_id, cancellationToken);
// Check if the operation is no longer running
if (update.Headers.Location == null)
{
_hasCompleted = true;
}
else
{
var uri = new Uri(update.Headers.Location);
var paths = uri.AbsolutePath.Split('/');
_id = paths[paths.Length - 1];
}
// Save this update as the latest raw response indicating the state
// of the route matrix calculation operation
Response response = update.GetRawResponse();
_rawResponse = response;
return response;
}
}
}