-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
SearchAddressBatchOperation.cs
228 lines (192 loc) · 9.08 KB
/
SearchAddressBatchOperation.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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
#nullable disable
using System;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using Azure.Core;
using Azure.Core.Pipeline;
using Azure.Maps.Common;
using Azure.Maps.Search.Models;
namespace Azure.Maps.Search
{
/// <summary>
/// Search address batch long-running operation class.
/// Use this operation to get the search address batch results from server.
/// </summary>
public partial class SearchAddressBatchOperation : Operation<SearchAddressBatchResult>
{
/// <summary> Initializes a new instance of SearchAddressBatchOperation for mocking. </summary>
protected SearchAddressBatchOperation()
{
}
/// <summary>
/// Initializes a new <see cref="SearchAddressBatchOperation"/> instance
/// </summary>
/// <param name="client"> The client used to check for completion. </param>
/// <param name="followUpUrl">Follow up URL of the request. </param>
/// <exception cref="ArgumentNullException"> <paramref name="client"/> or <paramref name="followUpUrl"/> is null. </exception>
internal SearchAddressBatchOperation(MapsSearchClient 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="SearchAddressBatchOperation"/> 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>
public SearchAddressBatchOperation(MapsSearchClient client, string id) :
this(client, id, null)
{
}
/// <summary>
/// Initializes a new <see cref="SearchAddressBatchOperation"/> 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 SearchAddressBatchOperation(
MapsSearchClient 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;
}
private readonly MapsSearchClient _client;
private readonly CancellationToken _cancellationToken;
private bool _hasCompleted;
private SearchAddressBatchResult _value;
private Response _rawResponse;
private string _id;
/// <inheritdoc />
public override SearchAddressBatchResult Value => _value;
/// <inheritdoc />
public override bool HasCompleted => _hasCompleted;
/// <inheritdoc />
public override bool HasValue => _value != null;
/// <inheritdoc />
public override string Id => _id;
/// <inheritdoc />
public override Response GetRawResponse() => _rawResponse;
/// <inheritdoc />
public override Response UpdateStatus(CancellationToken cancellationToken = default) =>
UpdateStatusAsync(false, cancellationToken).EnsureCompleted();
/// <inheritdoc />
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<SearchGetSearchAddressBatchHeaders> update = async
? await _client.RestClient.GetSearchAddressBatchAsync(_id, cancellationToken).ConfigureAwait(false)
: _client.RestClient.GetSearchAddressBatch(_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;
}
/// <inheritdoc />
public override async ValueTask<Response<SearchAddressBatchResult>> WaitForCompletionAsync(CancellationToken cancellationToken = default) =>
await WaitForCompletionAsync(true, cancellationToken).ConfigureAwait(false);
/// <inheritdoc />
public override async ValueTask<Response<SearchAddressBatchResult>> WaitForCompletionAsync(TimeSpan pollingInterval, CancellationToken cancellationToken = default) =>
await this.WaitForCompletionAsync(true, cancellationToken).ConfigureAwait(false);
/// <inheritdoc />
public override Response<SearchAddressBatchResult> WaitForCompletion(CancellationToken cancellationToken = default) =>
this.WaitForCompletionAsync(false, cancellationToken).EnsureCompleted();
/// <inheritdoc />
public override Response<SearchAddressBatchResult> WaitForCompletion(TimeSpan pollingInterval, CancellationToken cancellationToken) =>
this.WaitForCompletionAsync(false, cancellationToken).EnsureCompleted();
private async ValueTask<Response<SearchAddressBatchResult>> WaitForCompletionAsync(bool async, CancellationToken cancellationToken)
{
ResponseWithHeaders<SearchGetSearchAddressBatchHeaders> update = async
? await _client.RestClient.GetSearchAddressBatchAsync(_id, cancellationToken).ConfigureAwait(false)
: _client.RestClient.GetSearchAddressBatch(_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 = SearchAddressBatchResult.DeserializeSearchAddressBatchResult(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];
}
SearchAddressBatchResult result = null;
return Response.FromValue(result, response);
}
}
}